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.
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.
- Using
`nc`
CommandOne 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 portFor 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`
. - Using
`telnet`
CommandAnother 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 portFor 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.`
. - Using
`nmap`
CommandThe
`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 hostnameTo 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:
1 2 3 4 5 6 7 8 9 10 11 12 | #!/usr/bin/env bash HOST=192.168.10.100 #remote host PORT=22 # Port to check nc -z ${HOST} ${PORT} if [ $? -eq 0 ] then echo "Port is open" else echo "Port is closed" fi |
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.
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.
- Using
`lsof`
CommandThe
`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 :portFor 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.
- Using
`ss`
CommandThe
`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 portFor 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.