How to check Open Ports in Linux
Today, we will show you how you can check opened ports in your Linux System. There are certain reasons to keep a track of open ports on the server and block the access to some port which might be a security threat. We will use netstat command to list all the open ports which is a part of package net-tools.
- Root Access to the Linux System
- net-tools package installed
List all the open ports.
- Check and confirm if netstat is available in your system. Some of the distributions come with net-tools installed.
If net-tools is installed then this command should show the particular version of net-tools and netstat installed. If not then it will show command not found.
- If the system is missing net-tools then you can install net-tools package using following commands respective to your distribution.
sudo yum install net-tools -y
sudo apt install net-tools -y
- User netstat command to find the list of all the open and listening ports on your Linux system.
It will show something like below image with ports and particular service using that port.
Lets discuss the flags used with the netstat command below.
-t : Show only TCP sockets on Linux
-u : Display only UDP sockets on Linux
-l : Show listening sockets. For example, TCP port 22 is opened by SSHD server.
-p : List process name that opened sockets
-n : Don’t resolve service names i.e. don’t use DNS
List particular open port.
Using the following command you can check if Nginx port 80 is listening on the server.
netstat -tulpn | grep 80
If a port is listening then you will find output like the below image.
Tip – You can close the port by shutting down the particular service which is using it. For demonstration purpose, we will stop nginx service and port 80 will be automatically closed.
nginx service stopped and port 80 no more listening
nginx service started and port 80 again started listening