Introduction
Knowing how to check if a port is in use is an essential skill for any system administrator, network engineer, or developer working with Ubuntu Linux. Ports are endpoints between two connections, and if a port is already in use, it could prevent applications from starting or communicating properly. This tutorial will guide you through several methods to determine if a port is currently in use on an Ubuntu system.
Using netstat Command
netstat
(network statistics) is a command-line tool that displays network connections, routing tables, and a number of network interface statistics. It is used for finding problems in the network and to determine the amount of traffic on the network as a performance measurement.
sudo netstat -tuln | grep '<port>'
Replace <port> with the port number you want to check. If the port is in use, you’ll see an output similar to the following:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
Using ss Command
The ss
command is a utility to investigate sockets. It’s a more modern and faster alternative to netstat
. To check if a port is in use:
sudo ss -tuln | grep '<port>'
Output:
LISTEN 0 128 *:80 *:*
Checking a Specific Service with systemctl
If you know the service name you suspect is using the port, you can check its status using systemctl
:
sudo systemctl status <service-name>
If the service is active and running, it might be using the designated port.
Using lsof Command
The lsof
command stands for ‘List Open Files’. It is used to list all the open files and the processes that opened them. This includes network files, which will tell us if a port is in use:
sudo lsof -i :<port>
If there is a process using the port, you will see an output showing the details of the process.
Using nmap Command
The nmap
(network mapper) tool is used for network discovery and security auditing. You can also use it to check if a port is open:
sudo nmap -sTU -O localhost -p <port>
Replace <port> with the port number you’re interested in. This command will run TCP and UDP scans on your local machine.
Writing a Bash Script to Check Port Usage
You may also automate port checking with a simple Bash script. The following is an example script that uses netstat
to check if a port is in use:
#!/bin/bash
port=$1
if sudo netstat -tuln | grep ":$port" > /dev/null; then
echo "Port $port is in use."
else
echo "Port $port is not in use."
fi
Advanced: Monitoring Ports in Real-time
If you want to monitor port usage in real-time, you can use the watch
command with either netstat
, ss
, or lsof
.
watch 'sudo lsof -i :<port>'
This will run the lsof
command every 2 seconds by default, and update the display with the current information about the specified port.
Conclusion
In this tutorial, we went through multiple methods to check if a port is currently in use on an Ubuntu machine. From using basic tools like netstat
and ss
to advanced real-time monitoring with watch
, these methods provide a solid foundation for troubleshooting network and service issues related to port usage.