3 Ways to Open a port in Linux

Updated: Sep 26

How to open a port on Linux is a common question when testing a firewall related problem. Today we will use nc command to open a tcp port manually on Linux and check if this port is working or not.




Use nc or ncat to open a port on Redhat/Centos Linux

Nc or ncat is delivered as part of nmap-ncat rpm in RHEL/CentOS which we can install using yum or dnf. Use --listen with --port to open a port using nc command. In the below example we open port 1234

[root@centos-7 ~]# nc -4 -l 1234

[root@centos-8 ~]# nc --listen --source-port 1234


Open another terminal of this server and check port status

[root@centos-8 ~]# netstat -ntlp | grep 1234

tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 28983/nc

tcp6 0 0 :::1234 :::* LISTEN 28983/nc


As we see port 1234 is listening for both IPv4 and IPv6. To only use IPv4 use -4 with the above command.




[root@centos-8 ~]# nc --listen --source-port 1234 -4

Next on another terminal, you can check the port status for port 1234

Check the port status

[root@centos-8 ~]# netstat -ntlp | grep 1234

tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 29329/nc


Use nc to open a port on Ubuntu Linux

We can use this command to open the port.

# nc -lk port number


Use Python code to open a Port on Linux

python -c "import socket ; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.bind(('0.0.0.0', 1013)); s.listen(1);  conn, addr = s.accept(); print('Connected with ' + addr[0] + ':' + str(addr[1]))"


Related Post:

Linux: 5 ways to Check a remote port is open

Thanks a lot for your support.

Linux Troubleshooting Guide:



Linux Learning Guide:



328 views