What is the command to display a list of open ports on a Debian server?

I tried netstat -a | egrep 'Proto|LISTEN' but I would like something more specific that actually lists the port number.

link|improve this question

50% accept rate
feedback

6 Answers

up vote -7 down vote accepted

What about using

 nmap -p1-65535 localhost
link|improve this answer
6  
NO! Unless you're doing a security review of some kind, the asker implies local access to the machine (and further implies root ownership). This would miss services that are bound to specific addresses. Also, nmap with no flags by default only scans the 1,000 most common TCP ports, thus missing many possible open ports. – Jeff Ferland Feb 6 at 22:13
Ok, I get it that you all do not like this answer. And in normal cases I might delete it, but it IS an accepted answer and further negative voting really does not help anybody. The point has been made. Thanks. – mdpc Feb 9 at 17:52
feedback
 netstat -pln

-l will list listening ports, -p will also display the process, -n will show port numbers instead of names. Add -t to only show TCP ports.

link|improve this answer
3  
For the -p to work properly, you need to run this as root, so sudo netstat -tpln, otherwise the process column will not be particularly useful, unless you're the user whose process is listening on a given port. – cjc Feb 6 at 18:21
feedback

lsof -i -P

Check the man page for lsof as there is no shortage of options. -P lists the port number rather than the name taken from /etc/services Run as root, though, this will provide you with a list of all active network connections and their status (listening, established, etc).

link|improve this answer
feedback

You can do:

netstat -an | egrep 'Proto|LISTEN'

or simply:

netstat -anl

which will give you all listening sockets on the system.

link|improve this answer
This would be the best solution (where "best" is defined as "works on the broadest range of systems" (BSD, Linux, AIX, Solaris, I believe HP-UX)) – voretaq7 Feb 6 at 22:31
Except that the question is tagged as "linux" only. – djangofan Feb 7 at 0:07
feedback

What almost everybody wants (TCP and UDP) is netstat -tunlp.

I use it every day, maybe every hour. The 'lsof' hack is more portable (works on Solaris too), but on Debian it's not an essential package, you have to install it.

link|improve this answer
Yes, and run it via sudo. – Martijn Heemels Feb 7 at 23:09
feedback

TechRepulic has a decent article that you can find here. It has some similar commands as you listed above but also a few variations. I would also highly recommend using nmap to do a port scan of the computer in question so you can see from an external perspective what ports are open and listening.

link|improve this answer
Could you please tell me why this was down voted? As I simply provided a link with a lot of the solutions above which were approved along with a different perspective of doing an external scan as well. Thanks. – Eric Feb 6 at 19:10
I didn't downvote, but on serverfault like most stack-exchange we generally expect you to to put the answer here, and not just a link to somewhere else. Links go away over time, but we want content on SF to still be valuable when the links die. – Zoredache Feb 6 at 22:55
feedback

Your Answer

 
or
required, but never shown

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