howtouselinux

3 Ways to Open a port in Linux

Table of Contents

 

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.

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

  • use nc command to open a port in Linux: nc -4 -l 1234.
  • nc –listen –source-port 1234 -4.
  • use nc command to open a port in Ubuntu linux: nc -lk 1234.
  • use python code to open a port in Linux.

 

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

The best Linux command to open a port is using nc command. Open the terminal and type nc –listen –source-port  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 it.

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
[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:

6 ways to Check a remote port is open in Linux

Thanks a lot for your support.

Linux Troubleshooting Guide:

Linux Learning Guide:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press