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?
netstat -an | grep ESTABLISHED | wc -l
will give you the number of open ports, 32 in my case.
Will return something like:
which means, 61000 - 32768 - $OPENPORTS = AvailablePorts
On my box, thats:
61000-32768-32 = 28200 available port numbers.
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
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>"
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