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 '15 at 3:48

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.

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

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
  • 2
    I had to specify the port with -p or it didn't work: nc -l -p 1234 – Leahkim May 23 '17 at 13:33
  • nc -l 12345 worked in ubuntu 16.04 – yuen shi Oct 8 '18 at 5:13
  • For those with /bin/nc --> nc.traditional (e.g. debian), use the -p option, as @Leahkim advised, above. – ankostis May 18 at 11:06

Listen using netcat.

# nc -l 5555

Check using ss

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.