Skip to Content

3 Ways to Open a port in Linux

How to open a port on Linux is a common question when testing a firewall-related problem.

In this article, we will use nc command and Python code to open a TCP port manually on Linux and check if this port is working or not.

Methods to open a port in Linux

The following Linux commands can be used to open a port.

  • use nc command to open a port in Linux: nc -4 -l 1234 (Redhat 7)  nc -l -p 1234 -4 (Redhat 8)
  • use nc command to open a port in Ubuntu Linux: nc -lk 1234
  • use python code to open a port in Linux.

 

Related: 3 ways to check open ports in Linux

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

The easiest way to open a port in Linux is using nc command. Open the terminal and type nc -l -p port number. The port will be opening on our Linux system. Nc command is delivered as part of nmap-ncat rpm in Linux. We can use yum or dnf to install this package.

In the below example we can open port 1234.

  • [root@centos-7 ~]# nc -4 -l 1234
  • [root@centos-8 ~]# nc -l -p 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 -l -p 1234 -4

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

[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]))"

how to check tcp port status in Linux

You can use the netstat command to check the status of a TCP port in Linux. For example, if you want to check the status of TCP port 80, you would type:

netstat -tlpn | grep :80

understanding TCP connection status in netstat command

The LISTEN state in netstat means that the system is listening for incoming connections on that port. The TIME_WAIT state in netstat means that the system is waiting for a certain amount of time before closing the connection.

The SYN_SENT status in netstat means that the system is trying to establish a connection with a remote host. The CLOSE_WAIT status in netstat means that the system is waiting for the remote host to close the connection.

What is the nc command in linux?

The nc command in linux is a networking utility that can be used to create and manage TCP and UDP connections. It can be used to send and receive data, as well as to listen for incoming connections.

What are the different options for the nc command?

The nc command has a number of different options that can be used to create and manage TCP and UDP connections.

These options include the -l option, which is used to listen for incoming connections, the -p option, which is used to specify a port number, the -s option, which is used to specify a source address, the -d option, which is used to specify a destination address, and the -v option, which is used to print verbose output.

Related Post:

Troubleshoot Network Slow Problems In Linux

Thanks a lot for your support.

Linux Troubleshooting Guide:

Linux Learning Guide:

Da xie

Friday 1st of December 2023

cool. The nc command works for me. Thanks.