How to Find and Close Open Listening Ports in Linux

When securing an Ubuntu server or a desktop Linux environment, one of the most fundamental tasks is understanding exactly which applications are actively communicating with the network. An “open port” essentially acts as a doorway into your operating system. While some ports are necessary (such as port 80 for a web server or port 22 for SSH access), unused or unknown listening ports present a massive security vulnerability that hackers routinely exploit.

Instead of relying blindly on a firewall to block incoming traffic, it is far safer to identify the underlying application holding the port open and terminate the service entirely. In modern Linux distributions, the standard tool for discovering this information is ss (socket statistics), which has entirely replaced the older, deprecated netstat utility.

How to Identify Listening Ports

To view a comprehensive list of all active TCP and UDP ports currently listening for incoming connections, you need to open your terminal and run a specific command with elevated privileges.

  1. Open your terminal application or connect to your server via SSH.
  2. Type the following command and press Enter: sudo ss -tulpn
  3. You will be prompted to enter your administrator (sudo) password. Type it in and press Enter.

The output will display a structured table. Here is what the command flags actually mean:

  • -t: Displays TCP sockets.
  • -u: Displays UDP sockets.
  • -l: Shows only listening sockets (ports actively waiting for a connection).
  • -p: Shows the process ID (PID) and the name of the program using the socket. (This requires sudo).
  • -n: Displays numeric addresses and port numbers instead of resolving them to names.

Understanding the Command Output

When you look at the output generated by the ss command, you need to focus on two specific columns: Local Address:Port and Process.

For example, if you see an entry that looks like 0.0.0.0:22, it means a service is listening on port 22 across all available network interfaces. If you look at the corresponding Process column, you might see something like users:(("sshd",pid=1054,fd=3)). This confirms that the SSH daemon (sshd) is responsible for the open port, and its process ID is 1054.

If you see a service listening on 127.0.0.1:3306, it means a MySQL database is running, but it is only accessible locally (localhost), meaning it is not exposed to the public internet.

How to Close an Open Port

If you discover an unfamiliar or unnecessary port, you cannot simply “close” the port itself; you must stop the application that is keeping it open. There are two primary methods for doing this.

Method 1: Stopping the Systemd Service

If the application holding the port open is a standard background service installed via the package manager (like Apache, Nginx, or Docker), you should stop and disable the service permanently using systemctl.

  1. Once you have identified the service name (e.g., apache2), run the following command to stop it immediately: sudo systemctl stop apache2
  2. To prevent the service from automatically restarting the next time you reboot the server, run: sudo systemctl disable apache2
  3. Verify the port is closed by running sudo ss -tulpn again.

Method 2: Killing the Process Directly

If the port is being held open by a rogue script, a temporary executable, or an application that does not have a formal systemd service file, you will need to forcefully terminate the process using its Process ID (PID).

  1. Locate the pid number in the output of the ss command (e.g., pid=4092).
  2. Run the kill command followed by the PID: sudo kill 4092
  3. If the application refuses to close gracefully, you can force it to terminate immediately by adding the -9 flag: sudo kill -9 4092

Best Practices for Network Security

Forcefully killing processes is a highly effective troubleshooting method, but it is not a complete security strategy. After you have identified and disabled unnecessary listening ports, you should configure a firewall like UFW (Uncomplicated Firewall) to block unexpected incoming traffic by default. Maintaining a minimal attack surface by running only the exact software you need is the most reliable way to secure a Linux environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.