53

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.

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

  • 12
    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 '12 at 18:21
19

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

  • This should be the correct answer. Netstat isn't installed by default – José Dec 21 '18 at 14:04
8

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.

  • Yes, and run it via sudo. – Martijn Heemels Feb 7 '12 at 23:09
  • 1
    My friend taught me to remember this command with a flower name netstat -tulpen Flag e gives additional information. – Deele Dec 15 '12 at 14:37
  • And now, in 2018, my fresh Debian box doesn't have netstat but does have lsof. :) – Jason Nov 11 '18 at 20:58
8

I'm a big fan on netstat -ntlp and lsof -i, both mentioned already.

A new(er) command to me is ss.

The invocation is like:

ss -l

It's good to have options, in terms of commands and flags.

  • 1
    options are especially good since the mongo docker container (and presumably many others) had ss but not netstat or lsof – Foon Nov 14 '16 at 1:23
  • I started using this as an interview question. Probably asked 40 different people. So far not one has mentioned ss. I ask it like the OP, then if they answer, I say, "ok, I take away X, now how can you do it." Until the run out of ideas. – dmourati Jun 29 '17 at 4:14
6

You can do:

netstat -an | egrep 'Proto|LISTEN'

or simply:

netstat -anl

which will give you all listening sockets on the system.

  • 2
    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 '12 at 22:31
0

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.

  • 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 '12 at 19:10
  • 8
    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 '12 at 22:55
  • The only line of code that the above article link has is sudo nmap -T4 -A -v 192.168.1.1/24 everything else is go do this - go do that without any details... Its like reading the back cover of a book - lots of words without meat. – KingsInnerSoul Apr 1 '16 at 14:37
0

Listening ports are not the same as ports open from the outside. You need to consider the firewall. If you try a program like nmap from another computer then you will be able to see the open ports not blocked by firewall.

0

I prefer to use instead:

netstat -antp 
lsof -i 
netstat -lptu
netstat -tulpn

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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