How do I check which ports are opened or listening under Linux or Unix like operating system?
It is very important to check which ports are open, listening on your system network interfaces. By verifying port state you can troubleshoot a service or know whether a port is already in use by an application installed on your server. It is also very useful in determining an intrusion in your system by checking these ports.
1.) Check ports with the netstat command
For Linux – You can check all TCP or UDP port with netstat as follows.
# netstat -tulpen
# netstat -tulpen | grep LISTEN
Above command will only show listening ports in LINUX like operating system.
For Mac OS X and FreeBSD – Show only TCP connections with netstat command
# netstat -anp tcp
For Mac OS X and FreeBSD – Show only UDP connections
# netstat -anp udp
use pipe with grep LISTEN to show only listening ports for example
# netstat -anp tcp | grep LISTEN
2.) Check port with lsof command
lsof -i -P -n
Check which applications were having Listening sockets open
lsof -n -P | grep LISTEN
3.) Check port with nmap Network Mapped command
nmap can be used to detect the open port on your system. It has a lot of other uses, but in this article, we will focus on how to use nmap to scan ports
Show listening TCP ports connections from the network.
# nmap -sT -O localhost
Show listening UDP ports connections from the network.
# nmap -sU -O localhost
Show all listening TCP and UDP ports connections from the network.
# nmap -n -PN -sT -sU -p- localhost
How to check port status in your system is essential for every system administrator and Linux user. By using these commands you can know a lot about the status of ports on your system.
If you have any questions? Leave a comment below, or you can contact me using the contact us page.