Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need a Linux command to list all free open ports for use in an application

lsof -i TCP| fgrep LISTEN

Does not seen to be helping as the Ports it lists are not necessarily free for use. How do I list free open ports not in use?

share|improve this question

migrated from stackoverflow.com Jan 8 '13 at 11:29

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 36 down vote accepted
netstat -lntu

as replied by @askmish will give you list of services running on your system on tcp and udp ports where

  • -l = only services which are listening on some port
  • -n = show port number, don't try to resolve the service name
  • -t = tcp ports
  • -u = udp ports
  • -p = name of the program

You don't need the 'p' parameter as you're only interested in getting which ports are free and not which program is running on it.

This only shows which ports on your system are used up, though. This doesn't tell you the status of your network e.g. if you're behind NAT and you want some services to be accessible from outside. Or if the firewall is blocking the port for outside visitors. In that case, nmap comes to the rescue. WARNING: Use nmap only on networks which are under your control. Also, there are firewall rules which can block nmap pings, you'll have to fiddle around with options to get correct results.

share|improve this answer

This command will list open network ports and the processes that own them:

netstat -lnptu

you can thereafter filter the results to your exact specs.

You could also use nmap for more granular results about ports.

share|improve this answer
    
The -p flag requires root privileges for some processes, so it would be sudo netstat -lnptu –  klaus se Oct 30 at 1:17

All opened ports including response traffic:

netstat -tuwanp | awk '{print $4}' | sort | uniq -c | wc -l
share|improve this answer

Your Answer

 
discard

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.