22

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?

1
  • 2
    What are you doing that you are afraid of running out of ports?
    – MDMarra
    Jun 25, 2010 at 0:26

9 Answers 9

33

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

4
  • the -a46 didn't work. Any help?
    – erotsppa
    Jun 27, 2010 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, 2010 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, 2010 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, 2013 at 6:17
8

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

2
  • 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, 2010 at 16:20
  • Aaah, you are right. I have removed the IPV4 reference in my answer.
    – Alex
    Jun 25, 2010 at 17:14
1

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.

1

Try

# lsof -n -i -P 

For completeness sake :)

0

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

4
  • 1
    netstat --inet will help the most. Jun 25, 2010 at 0:32
  • I meant -? missing character. Jun 25, 2010 at 0:35
  • or read the manpage
    – MDMarra
    Jun 25, 2010 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, 2010 at 0:51
0

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

2
  • 1
    not really, it will only scan ports for 127.0.0.1, not for any wan IP Jul 21, 2012 at 20:33
  • That will only give listening ports bound to localhost, not all open ports on the system. May 24, 2021 at 14:49
0

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
0

use the following command on terminal to check all ports

netstat -lntu

To see a specific ports status use the following command

netstat -an | grep ':6060'

replace 6060 with your specific port number.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .