How to Check Open Ports in Ubuntu: Essential Commands and Practical Examples

1. Introduction

For many users working with Ubuntu, managing ports is a crucial aspect of maintaining system security and network performance. In particular, checking open ports is essential for preventing unauthorized access when running services or handling network communication. Additionally, port inspection helps in troubleshooting issues and optimizing network configurations.

This article provides a detailed explanation of how to check ports in Ubuntu and introduces command-based methods for doing so. Whether you are a beginner or an intermediate Ubuntu user, the information here will be highly valuable.

2. What Are Ports?

A port number acts like a “gateway” that allows a computer to send data to a specific service or process during communication. Network communication mainly uses two types of protocols: TCP and UDP. TCP is a connection-oriented protocol that establishes a connection before communication to ensure data reliability. UDP, on the other hand, is a connectionless protocol that sends data without establishing a connection, providing faster transmission.

Differences Between TCP and UDP

  • TCP (Transmission Control Protocol): A protocol designed for reliable communication that ensures data arrives correctly. It is commonly used for services such as web servers (HTTP/HTTPS) and SSH.
  • UDP (User Datagram Protocol): Unlike TCP, it does not guarantee reliable communication, but it enables low-latency data transmission. It is used for real-time services such as streaming and VoIP.

Understanding port numbers plays an essential role in managing network security and performance.

3. List of Commands to Check Ports in Ubuntu

Ubuntu provides several commands to check currently open ports. Below are some commonly used commands.

1. netstat Command

netstat is a powerful command that displays information about network connections, routing tables, and interface statistics. You can use the following command to check listening ports:

sudo netstat -lntu
  • -l: Display only listening ports
  • -n: Display IP addresses and port numbers in numeric format
  • -t: Display TCP ports
  • -u: Display UDP ports

2. ss Command

ss is widely used as a replacement for netstat and can display listening ports more efficiently. Use the following command:

ss -lntu

ss is considered superior to netstat due to its ability to quickly retrieve detailed information about network sockets.

3. lsof Command

lsof lists processes that have opened files, and it can also be used to check which processes are using network ports:

sudo lsof -i

4. nmap Command

nmap is a well-known tool used in network security for scanning open ports and identifying services. The following command scans all ports on localhost:

sudo nmap -n -PN -sT -sU -p- localhost

 

4. Opening and Closing Ports

You can strengthen the security of your Ubuntu system by opening or closing ports. Ubuntu comes with a simple firewall tool called ufw (Uncomplicated Firewall), which allows easy port management.

Opening Ports with ufw

For example, to allow HTTP traffic on port 80, use the command below:

sudo ufw allow 80

You can open the SSH port (22) in the same way:

sudo ufw allow 22

Closing Ports with ufw

To close an open port, use the following command:

sudo ufw delete allow 80

This will remove the rule that allows port 80.

5. Practical Example: Checking a Specific Port

Here is a practical example of checking a specific port. For instance, to verify whether SSH is listening on port 22, use the netstat or ss command as follows:

ss -lnt | grep :22

You can also check using lsof:

sudo lsof -i :22

The results should confirm that the SSH service is listening on port 22.

6. Common Issues and Solutions

There are a few common issues that may arise when checking or opening ports. Below are typical problems and their solutions.

Port Is Not Open

If a port does not seem to be open, first verify that the firewall settings are correct. To check the status of ufw, use the command below:

sudo ufw status

If the firewall is not blocking the port, then confirm whether the relevant service is running properly. For example, to verify SSH operation, use this command:

sudo systemctl status ssh

7. Conclusion

This article explained the essential commands and methods for checking ports in Ubuntu. Inspecting ports is vital for maintaining network security and system stability. In particular, managing listening ports accessible from the outside is crucial for minimizing vulnerabilities. Follow the steps introduced here to properly manage and secure your system ports in Ubuntu.