Linux - How to Check if a Port Is Open
One common thing that people come across when working on Linux systems ( or any other OS is how to tell if a port is open and listening.
To tell if a port is open on linux you can use the following command:
sudo netstat -tunlp
The above command should give you everything you need. It shows TCP and UDP ports that are listening. It will also show the associated PID for each port so you can find out which process is listening on that port. Here is a quick explanation of the options that we used:
|-n||Numeric, no host or port lookup|
|-p||Show PID for associated process|
Check out our netstat cheat sheet here for more info: Netstat Cheat Sheet.
Keep in mind that you may or may not have netstat installed on your system. If you don’t you can either install it or use an alternative command ( see below ).
The ss command is a modern replacement for netstat. Depending on your distro you may have either netstat, ss, neither, or both. It can be run with similar parameters to what you would use with netstat.
sudo ss -tunlp
Use lsof to Check If a Port is Open on Linux
Another tool that you can use is lsof. This stands for list open files. If you want to find all listening ports you can use this command:
sudo lsof -i -P -n | grep LISTEN
If you want to see what is listening on a specific port ( 8080 for example ) you can use the same command but with these options.
sudo lsof -i:8080
The above command will tell you what is listening on port 8080.
The fuser command can be used to tell you what is using a given file. It can also be used to find out what is listening on a given port. To find out what is using port 8080, you can use the following:
Nmap is an excellent tool for network scanning and network exploration.
You can use nmap to scan for open ports like this:
sudo nmap -sTU -O 192.168.0.5
This is assuming your IP is 192.168.0.5.