In Linux, a port is a numbered network connection that allows a device to communicate with other devices over the internet or a local network. It is important to ensure that the desired ports are open and accessible to ensure the smooth functioning of network services. There are various ways to check if a port is open in Linux, and in this article, we will discuss five of them.

Advertisement

In this tutorial, we will discuss 3 methods (`nc`, `nmap`, and `telnet`) to check the listening (open) port on a remote host. Also, discuss 2 (`lsof` and `ss`) commands to check if a port is listening on the local machine.

Check Open Port on Remote Host

First, check if a post is open and listening on the remote host.

  1. Using `nc` Command

    One way to check if a port is open is to use the `nc` (Netcat) utility. The nc command allows you to send data to a port and see if a response is received. To check if a port is open, use the following syntax:

    ## Syntax 
    nc -vz hostname port
    

    For example, to check if port 22 is open on the host example.com, you can use the following command:

    nc -vz 192.168.1.100 22 
    

    If the port is open, you will see the message `Connection to hostname port [tcp/ssh] succeeded!`. If the port is closed, you will see the message `Connection to hostname port [tcp/ssh] failed: Connection refused`.

  2. Using `telnet` Command

    Another way to check if a port is open is to use the `telnet` utility. The telnet command allows you to connect to a port on a remote host and see if a connection is established. To check if a port is open, use the following syntax:

    ## Syntax 
    telnet hostname port
    

    For example, to check if port 80 is open on the host www.example.com, you can use the following command:

    telnet www.example.com 80 
    

    If the port is open, you will see a blank screen. To exit, press `CTRL + ]` and then type quit. If the port is closed, you will see the message `Connected to hostname. Escape character is '^]'. Connection closed by foreign host.`.

  3. Using `nmap` Command

    The `nmap` command is a utility that performs network scanning and probing. It can be used to check if a port is open by performing a port scan on the target host. To check if a port is open, use the following syntax:

    ## Syntax 
    nmap -p port hostname
    

    To check if port 80 is open on the host `www.example.com` using the `nmap` command, you can use the following syntax:

    nmap -p 80 www.example.com 
    

    If the port is open, you will see a line in the output indicating that the port is `open`. If the port is closed, you will see a line indicating that the port is `closed`.

Shell Script to Check Port Status

Shell scripts are very useful for task automation. You can create a bash script that checks if a port is open on the local or remote host. An example script is written below:

Here HOST is the hostname or IP address of the remote or local host system. The PORT contains the port number to be checked. As you can read above, `nc` is the command line utility that can connect to the host on any port and return the status. The “$?” is a system environment variable that contains the exit status of the last command.

You can also modify the script as per your requirements. For example, we can pass the HOST and PORT values as command line parameters as shown below screenshot.

Check Script To Check Port Is Open
Check Script To Check Port Is Open

The above results show that on host 192.168.10.101 port 80 is open for our system but port 81 is closed.

Check Listening Port on LocalHost

Many times we need to check if any port is listening on our local machine. In that case, you can use the following commands. Lets, check if a port is listening on the local host.

  1. Using `lsof` Command

    The `lsof` command is a utility that displays information about open files. It can be used to check if a port is open by looking for the port in the list of open files. To check if a port is open, use the following syntax:

    ## Syntax 
    lsof -i :port
    

    For example, to check if port 80 is open, you can use the following command:

    lsof -i :80 
    

    If the port is open, you will see a line with the port number and the name of the process using the port. If the port is closed, you will not see any output.

  2. Using `ss` Command

    The `ss` command is a utility that displays network socket information. It can be used to check if a port is open by looking for the port in the list of open sockets. To check if a port is open, use the following syntax:

    ## Syntax 
    ss -lnp | grep port
    

    For example, to check if port 80 is open, you can use the following command:

    ss -lnp | grep 80 
    

    If the port is open, you will see a line with the port number and the status LISTEN. If the port is closed, you will not see any output.

Note that you may need to use `sudo` to run these commands, depending on your system configuration.

Conclusion:

In conclusion, checking if a port is open in Linux is an essential task for maintaining the smooth functioning of network services. The five methods discussed in this article provide different ways to check the availability of a port in Linux. These methods include using the `telnet` command, the `netstat` command, the `lsof` command, the `nmap` command, and the `ss` command. Whether you are a beginner or an experienced Linux user, these methods will help you troubleshoot any issues related to port availability on your system.

Share.
Leave A Reply

Exit mobile version