How to Check Open Ports on Linux: Simple Commands
To check open ports on Linux, use the
ss -tuln or netstat -tuln commands to list listening TCP and UDP ports. Alternatively, lsof -i shows open network connections with process details.Syntax
Here are common commands to check open ports on Linux:
ss -tuln: Shows listening TCP and UDP ports with numeric addresses.netstat -tuln: Lists listening TCP and UDP ports (may require installation).lsof -i: Lists open internet sockets with process info.
bash
ss -tuln netstat -tuln lsof -i
Example
This example uses ss -tuln to show open ports and their states.
bash
ss -tuln
Output
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 0.0.0.0:68 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
Common Pitfalls
Common mistakes when checking open ports include:
- Running
netstatwithout root privileges may show incomplete info. - Using
sswithout-ncan show hostnames instead of numeric ports, which is slower. - Not installing
net-toolspackage ifnetstatis missing.
bash
netstat -tuln # Wrong: netstat -tul (missing -n shows names, slower) # Right: netstat -tuln
Quick Reference
| Command | Description |
|---|---|
| ss -tuln | Show listening TCP/UDP ports with numeric addresses |
| netstat -tuln | List listening TCP/UDP ports (may need net-tools) |
| lsof -i | List open internet sockets with process info |
| sudo lsof -i :80 | Check which process uses port 80 |
| ss -tunap | Show all TCP/UDP ports with process info (requires root) |
Key Takeaways
Use
ss -tuln for a fast, modern way to list open ports.Install
net-tools if netstat is not available.Add
-n to avoid slow hostname resolution.Use
lsof -i to see which processes use network ports.Run commands with
sudo for complete information.