To list open ports in Linux, you can use several commands depending on your needs. Here are some of the most common methods:
1. Using netstat
netstat
is a versatile tool for monitoring network connections.
bashCopy codesudo netstat -tuln
-t
: Show TCP ports.-u
: Show UDP ports.-l
: Show listening ports.-n
: Show numerical addresses instead of resolving hostnames.
2. Using ss
ss
is a modern replacement for netstat
, providing similar functionality.
bashCopy codesudo ss -tuln
- The options have the same meaning as in
netstat
.
3. Using lsof
lsof
lists open files, including network sockets.
bashCopy codesudo lsof -i -P -n | grep LISTEN
-i
: Selects all network files.-P
: Prevents conversion of port numbers to service names.-n
: Prevents conversion of IP addresses to hostnames.grep LISTEN
: Filters the output to show only listening ports.
4. Using nmap
nmap
is a powerful network scanning tool that can also be used to list open ports.
bashCopy codesudo nmap -sT -O localhost
-sT
: Performs a TCP connect scan.-O
: Enables OS detection.
5. Using iptables
(for firewall rules)
To see open ports managed by iptables
, use:
bashCopy codesudo iptables -L -n -v
6. Using ss
(for established connections)
If you want to see established connections, use:
bashCopy codess -s
Each of these commands will give you insights into which ports are open and what services are listening on them. Choose the one that best fits your needs.