How to check Open Ports in Linux

Category : Advanced

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.

Requirements

  • Root Access to the Linux System
  • net-tools package installed

Procedure

List all the open ports.
  1. Check and confirm if netstat is available in your system. Some of the distributions come with net-tools installed.
    netstat --version
    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.
  2. If the system is missing net-tools then you can install net-tools package using following commands respective to your distribution.
    CentOS
    sudo yum install net-tools -y
    Debian/Ubuntu
    sudo apt install net-tools -y
  3. User netstat command to find the list of all the open and listening ports on your Linux system.
    netstat -tulpn
    It will show something like below image with ports and particular service using that port.
    netstat port listLets 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.
check particular port

 

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 stopped

nginx service started and port 80 again started listening
nginx service started