Get a list of Open Ports in Linux
Solution 1:
netstat -lntu
as replied by @askmish will give you list of services running on your system on tcp and udp ports where
-
-l
= only services which are listening on some port -
-n
= show port number, don't try to resolve the service name -
-t
= tcp ports -
-u
= udp ports -
-p
= name of the program
You don't need the 'p' parameter as you're only interested in getting which ports are free and not which program is running on it.
This only shows which ports on your system are used up, though. This doesn't tell you the status of your network e.g. if you're behind NAT and you want some services to be accessible from outside. Or if the firewall is blocking the port for outside visitors. In that case, nmap comes to the rescue. WARNING: Use nmap only on networks which are under your control. Also, there are firewall rules which can block nmap pings, you'll have to fiddle around with options to get correct results.
Solution 2:
Since net-tools
is deprecated, you can use the ss
command instead of netstat
if netstat
is not present on your machine:
ss -lntu
should work similarly to
netstat -lntu
according to the built-in help:
-n, --numeric don't resolve service names
-l, --listening display listening sockets
-t, --tcp display only TCP sockets
-u, --udp display only UDP sockets
-p, --processes show process using socket
If you wish to see the processes using each port, and they belong to different users, you need to run
sudo ss -lntup
Solution 3:
This command will list open network ports and the processes that own them:
netstat -lnptu
you can thereafter filter the results to your exact specs.
You could also use nmap
for more granular results about ports.
Solution 4:
All opened ports including response traffic:
netstat -tuwanp 2>/dev/null | awk '{print $4}' | sort | uniq -c | wc -l
Solution 5:
My take on the original question was that he was asking about the unused ports, not the ports currently connected to services. If this is the case, there's no specific way to list them, other than to listed the used ports and assume the others are unused.
One additional point to keep in mind: as a user, you'll not be able to open a port less than 1024 (you'll need root permissions for that).