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 on Ubuntu Server 14.04

20151229113855At home I use Ubuntu Server 14.04 and have never had to set  static IP as I would configure a static IP/mac address mapping in my router’s DHCP settings so it would always get the same address . That was until last week when I had to look up the exact steps on Google. Needless to say,  the process is straight-forward, here are the steps to configure a static IP on Ubuntu Server 14.04:

The Steps

1. The first thing to mention is that the interface config file in Ubuntu looks a little weird if you are used to working with only RHEL/CentOS. By default Ubuntu Server enables DHCP on a network interface. To configure the interface you have to edit the /etc/network/interfaces file. Below I am opening it with vi editor:

admin@web01:~$ sudo vi /etc/network/interfaces file

You will see something similar to the below:

This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

2. As you can see from the above eth0 is set to use DHCP and not a static IP. I want to set a static IP with the following info:

IP address10.10.10.50
Subnet Mask255.255.255.0
Default Gateway10.10.10.1
DNS Server 110.10.10.1
DNS Server 28.8.8.8

To achieve this, change iface eth0 inet dhcp to iface eth0 inet static. Then add the network parameters per the below:

This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 10.10.10.50
netmask 255.255.255.0
gateway 10.10.10.1
dns-nameservers 10.10.10.1 8.8.8.8

Save the changes and exit.

3. The next step is to update the /etc/hosts file by opening it with vi or any other editor of your choice:

admin@web01:~$ sudo vi /etc/hosts

You should see the lines below:

127.0.0.1   localhost
127.0.1.1   web01

Add a new line under the existing lines containing the static IP and relevant hostname(s):

127.0.0.1   localhost
127.0.1.1   web01
10.10.10.50     web01

Save the changes and exit the file.

4. To commit the changes made in the steps above you can either reboot the server or restart the network services. Restart the network services by running:

admin@web01:~$ sudo ifdown eth0 & sudo ifup eth0

Please note that in previous version of Ubuntu server you could run service networking restart to restart services. This no longer works in Ubuntu Server 14.04. You need to shut down the interface and then bring it back up per step 4 above.

After restarting the network services the operating system and all applications will recognise and use the new IP.

Please also note that restarting the network services will force an update to the /etc/resolve.conf file. It will automatically add the DNS servers we configured in step 2 above to the file. We can confirm this by reading the file:

admin@web01:~$ cat /etc/resolv.conf

The output is:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.10.10.1
nameserver 8.8.8.8

That’s it! Your Ubuntu Server is now running using a static IP.

References:
Linux Basics – Set A Static IP On Ubuntu
Official Ubuntu Documentation > Network Configuration > Interfaces