How to find the number of open ports in linux? I want to see if I am running out of ports. Also, how do I see the limit of my OS?

link|improve this question

6% accept rate
What are you doing that you are afraid of running out of ports? – MDMarra Jun 25 '10 at 0:26
feedback

7 Answers

As others have mentioned, netstat is the tool to use to determine what ports are in use currently. As to the limits, the number of ports available are a 16bit unsigned integer which gives you the range 0-65535. The ports that are available for applications to bind to are the reserved privileged/root ports (0-1024) plus whatever is not covered by your ephemeral port range.

You can view your ephemeral ports by running cat /proc/sys/net/ipv4/ip_local_port_range.

To modify that persistently, you would have to add/modify "net.ipv4.ip_local_port_range" in the /etc/sysctl.conf file, or interactively with sysctl -n net.ipv4.ip_local_port_range="<start_port> <end_port>"

link|improve this answer
nit picking, but it's not exactly a ipv4 limit. It's a tcp/udp limit. and those run independently of ipv4. (ex. ipv6 doesn't do anything for transport layer) – Joel K Jun 25 '10 at 16:20
Aaah, you are right. I have removed the IPV4 reference in my answer. – Alex Jun 25 '10 at 17:14
feedback

On modern linux, use the ss (socket stats) utility.

$ ss -s
Total: 10160 (kernel 10262)
TCP:   10349 (estab 8886, closed 408, orphaned 0, synrecv 0, timewait 393/0), ports 3147

Transport Total     IP        IPv6
*         10262     -         -        
RAW       0         0         0        
UDP       5         5         0        
TCP       9941      9941      0        
INET      9946      9946      0        
FRAG      0         0         0        
link|improve this answer
feedback
netstat -a46 | grep ESTABLISHED | wc -l

compared to

cat /proc/sys/net/ipv4/ip_local_port_range
link|improve this answer
the -a46 didn't work. Any help? – erotsppa Jun 27 '10 at 16:08
what distro you running? (that works on ubuntu server 10.04 LTS). Of course, if you don't have ipv6 installed, then just use netstat -a. – Grizly Jun 27 '10 at 23:15
Tested on my CentOS box, seems it hangs if you don't use "-n" to stop name resolution. (netstat -an | grep ESTABLISHED | wc -l) – Grizly Jun 28 '10 at 0:12
feedback

netstat will allow you to see what ports are open, do "netstat -" to see what fits your needs best.

link|improve this answer
1  
netstat --inet will help the most. – Paul Tomblin Jun 25 '10 at 0:32
I meant -? missing character. – jer.salamon Jun 25 '10 at 0:35
or read the manpage – MDMarra Jun 25 '10 at 0:50
Also include --inet6 (short for both: -4 -6), to get IPv6 sockets and ip-agnostic sockets (the latter being the default on dual stack hosts, see rfc 3493 section 3.7). – Tobu Jun 25 '10 at 0:51
feedback

Personally I prefer nmap. You can find the state of all ports by issuing nmap -P 1-65535 target. Most distributions should have this package available via their package manager.

link|improve this answer
feedback

'nmap localhost' will give you all your open ports and services running on them.

link|improve this answer
feedback

netstat -tulnp

The arguments to the netstat program are listed below:

*

  t - Show TCP
*

  u - Show UDP
*

  l - Show only listening processes (netstat can show both listening and all established connections, i.e. as a client too)
*

  n - Do not resolve network IP address names or port numbers
*

  p - Show the process name that is listening on the port
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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