The nc utility, which is also called netcat, comes in very handy when you want to test TCP/IP servers and clients. netcat 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 -l, --listen (Listen for connections) Listen for connections rather than connecting to a remote machine
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.
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 -v and -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 (192.168.1.10):
# nc -lu 5555
on client machine:
# echo "PING" >/dev/udp/192.168.1.10/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.
If you need to test if a port can be opened on a CentOS/RHEL system, above procedure can be used. If the above is successful, then it might require your application to be tuned properly to bind to an IP and port and listen to the connection on that port.