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?
9 Answers
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
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.
-
-
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.– GrizlyJun 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)– GrizlyJun 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!– GrizlyMay 8, 2013 at 6:17
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>"
-
1nit 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 KJun 25, 2010 at 16:20
-
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.
netstat will allow you to see what ports are open, do "netstat -" to see what fits your needs best.
-
1
-
-
-
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).– TobuJun 25, 2010 at 0:51
'nmap localhost' will give you all your open ports and services running on them.
-
1not 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
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
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.