How to check if port is open or in use on Linux or Unix?

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

Output
netstat - tulpen output

# netstat -tulpen | grep LISTEN

Above command will only show listening ports in LINUX like operating system.

Output
check only listening ports

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

Output
Output check port with lsof command

Check which applications were having Listening sockets open

lsof -n -P | grep LISTEN

verify listening ports with lsof

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

Output
show TCP ports nmap

Show listening UDP ports connections from the network.

# nmap -sU -O localhost

nmap check UDP ports

Show all listening TCP and UDP ports connections from the network.

# nmap -n -PN -sT -sU -p- localhost

nmap check TCP and UDP ports

Conclusion

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.

Resources

nmap documentation

Add Comment