Sling Academy
Home/DevOps/Ubuntu: How to Check if a Port is Currently in Use

Ubuntu: How to Check if a Port is Currently in Use

Last updated: January 29, 2024

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.

Next Article: Ubuntu: How to open/close a port in firewall (UFW)

Previous Article: Ubuntu: How to set up and configure firewall (UFW)

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide