Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
What are you doing that you are afraid of running out of ports? –  MDMarra Jun 25 '10 at 0:26
add comment

8 Answers 8

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        
share|improve this answer
add comment

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>"

share|improve this answer
1  
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
add comment
netstat -an | grep ESTABLISHED | wc -l

will give you the number of open ports, 32 in my case.

cat /proc/sys/net/ipv4/ip_local_port_range

Will return something like:

32768 61000

which means, 61000 - 32768 - $OPENPORTS = AvailablePorts

On my box, thats:

61000-32768-32 = 28200 available port numbers.

share|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
    
Just saw "ss" below, thats awesome, didn't know about it.. much better! Use the ip_local_port_range to determine what your linux is configured to allow, but that shows you what you are currently using in a much more accessible format! –  Grizly May 8 '13 at 6:17
add comment

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.

share|improve this answer
add comment

Try

# lsof -n -i -P 

For completeness sake :)

share|improve this answer
add comment

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

share|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
add comment

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

share|improve this answer
    
not really, it will only scan ports for 127.0.0.1, not for any wan IP –  Lucas Kauffman Jul 21 '12 at 20:33
add comment

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
share|improve this answer
add comment

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.