Hostwinds Blog
Search results for:
Ports are like doorways that let apps and programs on your Linux system send and receive data. If you want to see which ports are in use, which ones are open, or just figure out what's going on with your network, you've come to the right place.
In this guide, we'll walk through multiple ways to check ports in Linux using simple commands. By the end, you'll feel more comfortable working with ports and knowing what's happening on your system.
Before jumping into the commands, let's take a moment to understand what ports are:
With that out of the way, let's explore how to check what's happening with your ports.
Linux has a number of tools to help you check open or listening ports. Below, we'll walk through five common methods and explain what each one does.
The netstat command gives you a detailed view of network connections and port usage.
Open a terminal and type:
netstat -tuln
Here's what each option means:
What You'll See:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:68 0.0.0.0:*
Here's how to read this:
Quick Note: netstat is part of the net-tools package, which may not come pre-installed on newer Linux versions. Install it using:
sudo apt install net-tools # On Debian/Ubuntu
sudo yum install net-tools # On RHEL/CentOS
ss is a newer, faster alternative to netstat. It gives similar information but works better on modern systems.
Run the following command:
ss -tuln
The options are the same as netstat, so you should be able to jump right in.
What You'll See:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
udp UNCONN 0 0 0.0.0.0:68 0.0.0.0:*
This output is similar to netstat, but ss tends to be faster, especially if you're dealing with a lot of connections.
lsof (List Open Files) is a handy tool for seeing which files or network connections are being used by processes.
To see which ports are open and what's using them, run:
sudo lsof -i -P -n
What You'll See:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1234 root 3u IPv4 12345 0t0 TCP *:22 (LISTEN)
dhclient 5678 root 6u IPv4 54321 0t0 UDP *:68
If you're troubleshooting and need to find out which process is using a specific port, lsof is your go-to tool.
nmap (Network Mapper) is ideal for scanning networks and checking which ports are open.
To scan all ports, run:
sudo nmap -sT -p- localhost
What You'll See:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp open https
Why nmap is Useful:
nmap is great if you want a complete view of all open ports on your system or need to scan another machine.
Netcat is a simple but powerful tool for checking if a specific port is open.
To check if port 22 is open, type:
nc -zv localhost 22
What You'll See:
Connection to localhost 22 port [tcp/ssh] succeeded!
Why It's Useful:
If you just need to quickly check whether a specific port is open, nc does the job without much fuss.
When you check ports, here's what the results usually mean:
When checking ports, you may come across the following scenarios:
Checking ports is just the first step. Here are a few ways to tidy things up:
Stop Unnecessary programs:
If a program is running on a port you don't need, turn it off:
sudo systemctl stop <service_name>
sudo systemctl disable <service_name>
Use a Firewall:
Limit access to ports using a tool like ufw (Uncomplicated Firewall):
sudo ufw allow 22 # Allow SSH
sudo ufw deny 80 # Block HTTP
Regularly Monitor Ports:
Make it a habit to check open ports now and then, especially if you're running a server.
Checking ports in Linux doesn't have to be complicated. Whether you use netstat, ss, lsof, nmap, or nc, each tool gives you a slightly different view of what's going on. Pick the one that works best for your needs and don't be afraid to explore. The more you practice, the easier it gets!
Written by Hostwinds Team / January 14, 2025