Testing connectivity using netcat on RHEL/CentOS

In this post I am going to show you how to test connectivity to a server on specific ports, essentially port scanning. We will perform these tests using a tool called netcat, which is a versatile networking utility for debugging and investigating the network.

Although the title states that this is for RHEL/CentOS, the same syntax applies for Ubuntu as I have tested it. I am sure it works for other versions of Linux, I just haven’t tested them so cannot confirm.

Before using netct, you must install it. On RHEL/CentOS, install it using yum:

yum install nc

The basic netcat command follows this syntax:

netcat [options] host port

This command will initiate a TCP connaction to the host and port stated in the command. Please note that netcat always initiates a TCP connection by default unless you specifically use the -u option which specifies UDP. Another thing to note is that connections made via netcat are unencrypted.

So using the above, I can test whether a web server is listening on port 80 by running the following command:

[roo@vs12app001:~]# nc -vz 172.16.0.33 80
Connection to 172.16.0.33 80 port [tcp/http] succeeded!
[roo@vs12app001:~]#

You will notice that used options -vz which are defined below:

-v Have nc give more verbose output.
-z Specifies that nc should just scan for listening daemons, without sending any data to them.

If the connection failed, meaning that port 80 is not open then you would see a time out error per the below:

[roo@vs12app001:~]# nc -vz 172.16.0.33 80
nc: connect to 172.16.0.33 port 80 (tcp) failed: Connection timed out
[roo@vs12app001:~]#

If the port was open but a firewall blocked it then you would see a connection refused error:

[roo@vs12app001:~]# nc -vz 172.16.0.33 80
nc: connect to 172.16.0.33 port 80 (tcp) failed: Connection refused
[roo@vs12app001:~]#

To test connectivity over a range of ports use the following syntax:

netcat host startport-endport

The command below shows this in practice, where I test connectivity to 172.16.0.31 on port rage 8080-8085:

[roo@vs12app001:~]# nc -vz 172.16.0.33 8080-8085
Connection to 172.16.0.33 8080 port [tcp/http-alt] succeeded!
Connection to 172.16.0.33 8081 port [tcp/tproxy] succeeded!
nc: connect to 172.16.0.33 port 8082 (tcp) failed: Connection refused
nc: connect to 172.16.0.33 port 8083 (tcp) failed: Connection refused
nc: connect to 172.16.0.33 port 8084 (tcp) failed: Connection refused
nc: connect to 172.16.0.33 port 8085 (tcp) failed: Connection refused
[roo@vs12app001:~]#

There are many more examples of how netcat can be used but the ones above are how I utilize this tool the most. See the references below for more information.

References:
Testing Network Services with netcat
How To Use Netcat to Establish and Test TCP and UDP Connections on a VPS
8 Practical Linux Netcat NC Command Examples
Linux Command Man Page for netcat

Configure a static IP Address in RHEL 6/Centos 6

20151224013419This is a quick post to show how to set a static IP in RHEL6 or CentOS 6. The process is fairly straight-forward however you need to be aware of the different files that need to be changed in order for it to work properly. So, to set a static IP in RHEL6 or CentOS 6 follow the steps below.

The Steps

1. The first step is to edit the interface that you want to set the static IP on. In my example I want to change the IP on eth0 from 172.16.0.21 to 10.10.10.50. This not only means that the IP address will change but the default gateway will also. So to proceed I will edit the interface config file for eth0, which is ifcfg-eth0 using vi, per the below:

[root@web01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

2. What you see in this file can vary. The basic requirements for networking are listed below, change them to match your desired networking information:

DEVICE=eth0
BOOTPROTO=STATIC
IPADDR=10.10.10.50
NETMASK=255.255.255.0
GATEWAY=10.10.10.1
ONBOOT=yes

We can see from the above that I have configured the static IP of 10.10.10.50. You must also ensure the correct subnet mask and gateway for your network are configured. All the other parameters must match the above.

3. I tend to remove all superfluous information from the interface file apart from what is listed in step 2 above. If you’re wondering what I mean by this, see the example below:

DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=A5:BC:CB:48:F2:05
TYPE=Ethernet
BOOTPROTO=static
NAME="System eth0"
UUID=5cb07fe0-0cd0-7ddc-45c1-d6fbb43fa6c05
IPADDR=172.16.0.21
NETMASK=255.255.255.0

You do not need NM_CONTROLLED, HWADDR, TYPE, NAME & UUID for the interface to work. In some cases, having the HWADDR parameter present could prevent the interface from working, such as when you clone a VM and it has the HWADDR or mac address of the old server in the interface file. As as personal best practice I remove all of this unneeded gumph and keep the file as clean and as simple as possible. Another major point to highlight is that in the above config the GATEWAY parameter is not present, whereas in my config in step 2 it is present.

4. The next step is to check the /etc/sysconfig/network file. As before, edit it in vi:

[root@web01 ~]# vi /etc/sysconfig/network

I see the following:

NETWORKING=yes
HOSTNAME=web01

So there is nothing to change for me. However, this is where the GATEWAY parameter can also be configured. If it is not stated in the interface config file, per the example interface config file in step 3 you should see the settings below in the /etc/sysconfig/network file:

NETWORKING=yes
HOSTNAME=web01
GATEWAY=172.16.0.1

If your interface config file doesn’t contain the GATEWAY parameter, then you will need add it here to reflect your new default gateway. So using my example it would be changed to:

NETWORKING=yes
HOSTNAME=web01
GATEWAY=10.10.10.1

5. The next step is to update the /etc/hosts file. As before, edit it in vi:

[root@web01 ~]# vi /etc/hosts

You should see a line with the server’s old static IP followed by the hostname, per the below:

127.0.0.1   localhost
172.16.0.21     web01

Change the line to reflect the new IP, in my case 10.10.10.50:

127.0.0.1   localhost
10.10.10.50     web01

Exit and save the changes to the file.

6. To commit the changes made in the network and hosts files above in steps 4 & 5 respectively you can either reboot the server or restart the network services. If it’s a production system and you want to avoid any downtime restart the network services by running:

[root@web01 ~]# service network restart

After restarting the network services the operating system will use the new IP.

References:
How To Configure Static IP On CentOS 6
How to Configure a Static IP Address in Red Hat, CentOS and Fedora
Setup static IP in redhat 6