Introduction: I have created a bash function that is able to check whether a port is available and increments it by 1 if false until a certain maximum port number. E.g., if port 500 is unavailable then the availability of 501 will be checked until 550.

Aim: In order to test this bash function I need to create a range of ports that are in LISTEN state.

Attempts: On Windows it is possible to create a LISTEN port using these PowerShell commands:

PS C:\Users\u> netstat -nat | grep 1234
PS C:\Users\u> $listener = [System.Net.Sockets.TcpListener]1234
PS C:\Users\u> $listener.Start();
PS C:\Users\u> netstat -nat | grep 1234
TCP               LISTENING       InHost
PS C:\Users\u> $listener.Stop();
PS C:\Users\u> netstat -nat | grep 1234
PS C:\Users\u>

Based on this I was trying to think about a command that could do the same on CentOS, but I do not know why and I started to Google without finding a solution that solves this issue.

Expected answer: I will accept and upvote the answer that contains a command that is able to create a LISTEN port and once the command has been run the port should stay in LISTEN state, i.e.:

[user@host ~]$ ss -nat | grep 500
LISTEN     0      128                       *:500                       *:*
  • On a side note: it's possible to find the listening ports without going through all this dance. On Linux: netstat -an --tcp | awk '/LISTEN/ {sub(".*:", "", $4); print $4}' | sort -nu. On *BSD: netstat -an -f inet -p tcp | awk '/LISTEN/ {sub(".*\\.", "", $4); print $4}' | sort -nu.
    – lcd047
    Jul 8, 2015 at 3:48

6 Answers 6


you can create a port listener using Netcat .

root@ubuntu:~# nc -l 5000

you can also check if port is open or not using netstat command .

root@vm-ubuntu:~# netstat -tulpen | grep nc
tcp        0      0   *               LISTEN      0          710327      17533/nc

you can also check with nc :

Netcat Server listener :

nc -l localhost 5000

Netcat Client :

root@vm-ubuntu:~# nc -v localhost 5000
Connection to localhost 5000 port [tcp/*] succeeded!

if port is not open

root@vm-ubuntu:~# nc -v localhost 5000
nc: connect to localhost port 5000 (tcp) failed: Connection refused
  • 13
    I had to specify the port with -p or it didn't work: nc -l -p 1234
    – Leahkim
    May 23, 2017 at 13:33
  • nc -l 12345 worked in ubuntu 16.04
    – yuen shi
    Oct 8, 2018 at 5:13
  • For those with /bin/nc --> nc.traditional (e.g. debian), use the -p option, as @Leahkim advised, above.
    – ankostis
    May 18, 2019 at 11:06

You could use nc -l as a method to do what you are looking for. Some implementations of nc have a -L option which allows the connections to persist.

If you only need them for a little while you could open this command in a for loop and have a bunch of ports opened that way.

If you need these opened longer you can use one of the super servers to create a daemon.

  • 4
    Instead of -L I would suggest: -k, --keep-open accept multiple connections in listen mode Mar 22, 2019 at 9:44

Listen using netcat.

# nc -l 5555

Check using ss

# ss -nat|grep 5555
LISTEN     0      1                         *:5555                     *:*

Note that Debian's netcat package has a different implementation where (at least) you need to provide the port via the -p option and the -k option doesn't work. You might run into this issue using Docker.

sudo apt install -y netcat

nc -l -p 1337

You may consider using openbsd-netcat instead where the -k option works.


Below python code is helpful to open a dummy port

'''    Simple socket server using threads
import socket
import sys
HOST = ''   # Symbolic name, meaning all available interfaces
PORT = 5500 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
print 'Socket bind complete'
#Start listening on socket
print 'Socket now listening'
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])

Save the file and run it with python command as shown below

~]# python portlistener.py
Socket created
Socket bind complete
Socket now listening

Then you can verify from required machine.


Below command was immediately exiting without listening on port on RHEL 6 nc -l localhost 5000

For me below command worked where netcat started listening on port. nc -lk localhost 5000

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .