Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking to bind to a port for SSH tunnelling via a simple shell script (likely: BASH), and I'd like a more ... "sane" manner of testing whether or not the port is already bound to another process, and I'd rather not like to grok (read: grep) the output of netstat.

Is there some command or utility that makes doing this sane? I either have terrible google-fu and a terrible vocabulary, or most people don't try to iterate over ports programmatically via a shell, but instead via a programming language.

share|improve this question
A bit less heavy than netstat, though still same concept: fuser <port>/tcp. – Max Alginin Nov 5 '09 at 1:50
up vote 5 down vote accepted

you could use lsof:

*-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]*

lsof -i 4TCP@
share|improve this answer
You were a few seconds ahead of me :) – Alex Nov 5 '09 at 2:59
While lsof works, looping through every process and it's open ports is slow. netstat -tan will give you the list of open ports. – David Nov 5 '09 at 3:42

Another option would be lsof. You can do something like:

lsof -i tcp:<port> -s tcp:listen
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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