The nc utility, which is also called
netcat, comes in very handy when you want to test TCP/IP servers and clients.<code>netcat</code> is a computer networking utility for reading from and writing to network connections using TCP or UDP. netcat can be installed using the below command, if not already installed.
# yum -y install nmap-ncat
Open a port using nc
A port can be opened on CentOS/RHEL by using
netcat for testing purpose. From the man page of nc command.
# man nc
The -l option tells
netcat to act as a server, which means that netcat will start listening for connections at the given port number. Lets open a port 5555 on the server with above command option:
# nc -l 5555
Once the port is open with above command, this can be actually tested on the same system or other system with telnet command.
# telnet localhost [port number]
# telnet server-ip [port-number]
So, in our example, lets try to telnet to the port 5555 from a remote machine.
# telnet 192.168.1.10 5555 Trying 192.168.1.10... Connected to 192.168.1.10. Escape character is '^]'. ^] telnet> q Connection closed.
netcat opens and binds to a single ip and once the connection is closed from the client side nc is also killed.
Testing UDP port connectivity
By default, nc uses the TCP protocol. However, if you execute nc with the -u flag, then nc will use the UDP protocol. Finally, the
-vv options tell netcat to generate verbose output, which can come in handy when you want to troubleshoot network connections.
To test a UDP port use the below method.
On server machine (18.104.22.168):
# nc -lu 5555
On client machine:
# echo "PING" >/dev/udp/22.214.171.124/5555
The bash shell sends UDP packets when you redirect data to the special device
/dev/udp/host/port as shown above. The server machine would receive whatever data is sent to it using the echo command from the client machine as shown in the screenshot below.