get a process list by listen port

netstat -ntlp | grep -w 80 | awk '{print $7}' | cut -d/ -f1

By: ncaio
2009-05-20 20:29:56

These Might Interest You

  • another formatting/oneliner for lsof User - Process - Port Show Sample Output

    alias oports="echo 'User: Command: Port:'; echo '----------------------------' ; lsof -i 4 -P -n | grep -i 'listen' | awk '{print \$3, \$1, \$9}' | sed 's/ [a-z0-9\.\*]*:/ /' | sort -k 3 -n |xargs printf '%-10s %-10s %-10s\n' | uniq"
    wickedcpj · 2011-08-02 04:54:25 0
  • Show TCP Listen ports sorted by number (bugs: IPV6 addresses not supported) Show Sample Output

    netstat -tlpn | sort -t: -k2 -n
    hute37 · 2014-07-22 14:08:01 0
  • 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

    echo foo | ncat [ip address] [port]
    dragonauta · 2012-10-26 10:53:47 0
  • Run netstat as root (via sudo) to get the ID of the process listening on the desired socket. Use awk to 1) match the entry that is the listening socket, 2) matching the exact port (bounded by leading colon and end of column), 3) remove the trailing slash and process name from the last column, and finally 4) use the system(…) command to call kill to terminate the process. Two direct commands, netstat & awk, and one forked call to kill. This does kill the specific port instead of any port that starts with 50. I consider this to be safer.

    sudo netstat -plnt | awk '($4 ~ /:50$/){sub(/\/.*/, "", $7); system("sudo kill " $7)}'
    drscriptt · 2018-06-07 17:01:49 0

What Others Think

whatslistening() { netstat -ntulp | pcregrep Proto\|:$1\\b } whatslistening 80
MRobvious · 472 weeks and 5 days ago
lsof|awk '/*:ht/'
sil · 472 weeks and 4 days ago
I'd prefer the one from sil, ncaio will also find 80x and 80xx.
tcfusion · 384 weeks and 5 days ago
yes, you're right ! I applied the change in command. Thanks
ncaio · 384 weeks and 5 days ago

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? 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.


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: