netstat -tlpn | sort -t: -k2 -n

List Listen Port by numbers

Show TCP Listen ports sorted by number (bugs: IPV6 addresses not supported)
Sample Output
tcp        0      0 10.150.2.178:1098           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:1099           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:1100           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:1101           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:3873           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:4444           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:4445           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:4446           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:4447           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:4448           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 10.150.2.178:4457           0.0.0.0:*                   LISTEN      453/java            
tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      1410/postmaster     
tcp        0      0 127.0.0.1:5433              0.0.0.0:*                   LISTEN      1023/postmaster     
tcp        0      0 0.0.0.0:5801                0.0.0.0:*                   LISTEN      6118/Xvnc           

4
By: hute37
2014-07-22 14:08:01

These Might Interest You

  • you can use a pair of commands to test firewalls. 1st launch this command at destination machine ncat -l [-u] [port] | cat then use this command at source machine to test remote port echo foo | ncat [-u] [ip address] [port] First command will listen at specified port. It will listen TCP. If you use -u option will listen UDP. Second command will send "foo" through ncat and will reach defined IP and port. Show Sample Output


    -1
    echo foo | ncat [ip address] [port]
    dragonauta · 2012-10-26 10:53:47 0
  • swap out "80" for your port of interest. Can use port number or named ports e.g. "http"


    1
    lsof -iTCP:80 -sTCP:LISTEN
    indexsmithy · 2018-04-07 16:20:25 0

  • 2
    netstat -ntlp | grep -w 80 | awk '{print $7}' | cut -d/ -f1
    ncaio · 2009-05-20 20:29:56 4
  • Doesn't list connections to other ports than port 80. Not f.e. 8080 or 8091. Excludes programs listening for connections, like your favorite webserver.


    0
    watch "netstat -plan | grep -v LISTEN | grep \":80 \" | awk {'print \$5'} | cut -d: -f 1 | uniq -c | sort -nk 1"
    simonsimcity · 2013-09-05 13:50:00 0
  • Simple way to test if a port is available to the public. Run this command on the "server" and run a `telnet host-ip port-number` on the client. Test by sending strings to the server, which will be displayed in the server terminal.


    0
    nc -l <port-number>
    cybertoast · 2013-03-20 15:25:57 1
  • "sort_csn" is a function to sort a comma separated list of numbers. Define the the function with this: sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; } Use the function like this: sort_csn 443,22,80,8200,1533,21,1723,1352,25 21,22,25,80,443,1352,1533,1723,8200 One example where this is useful is when port scanning with nmap and getting a list of open ports in random order. If you use Nessus, you may need to create a scan policy for that set of specific ports and it is clearer to read with the port numbers in ascending order (left to right). Caveat: no spaces in the comma separated list (just number1,number2,number3,etc). A variation of this to sort a comma separated list of strings: sort_css () { echo "${1}" | sed -e "s/,/\n/g"| sort -u | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; } usage: sort_css apples,pears,grapes,melons,oranges apples,grapes,melons,oranges,pears Show Sample Output


    0
    sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }
    mpb · 2011-01-26 15:18:08 2

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: