How to check if port is in use
To check the listening ports and applications on Linux:
- Open a terminal application i.e. shell prompt.
- Run any one of the following command on Linux to see open ports:
- For the latest version of Linux use the ss command. For example,
ss -tulwnp
Let us see commands and its output in details.
Option #1: lsof command
The syntax is:
Option #2: netstat or ss command
You can check the listening ports and applications with netstat as follows.
Linux netstat syntax
Prerequisite
By default, netstat
command may not be installed on your system. Hence, use the apk command on Alpine Linux, dnf command/yum command on RHEL & co, apt command/apt-get command on Debian, Ubuntu & co, zypper command on SUSE/OpenSUSE, pacman command on Arch Linux to install the netstat
.
Run the netstat command along with grep command to filter out port in LISTEN state:
OR filter out specific TCP port such as 443:
$ netstat -tulpn | grep ':443'
Where netstat command options are:
- -t : Select all TCP ports
- -u : Select all UDP ports
- -l : Show listening server sockets (open TCP and UDP ports in listing state)
- -p : Display PID/Program name for sockets. In other words, this option tells who opened the TCP or UDP port. For example, on my system, Nginx opened TCP port 80/443, so I will /usr/sbin/nginx or its PID.
- -n : Don’t resolve name (avoid dns lookup, this speed up the netstat on busy Linux/Unix servers)
The netstat command deprecated for some time on Linux. Therefore, you need to use the ss command as follows:
Where, ss command options are as follows:
- -t : Show only TCP sockets on Linux
- -u : Display only UDP sockets on Linux
- -l : Show listening sockets. For example, TCP port 22 is opened by SSHD server.
- -p : List process name that opened sockets
- -n : Don’t resolve service names i.e. don’t use DNS
Related: Linux Find Out Which Process Is Listening Upon a Port
FreeBSD/macOS (OS X) netstat syntax
The syntax is as follows:
You can use the sockstat command on macOS or FreeBSD to display open TCP or UDP ports too. For example:
{vivek@freebsd13-server:~}$ sudo sockstat -4 -6 -l
Outputs from my FreeBSD server version 13.xx:
USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS
root master 1723 13 tcp4 127.0.0.1:25 *:*
root master 1723 14 tcp4 192.168.2.20:25 *:*
root sshd 1627 3 tcp6 *:22 *:*
root sshd 1627 4 tcp4 *:22 *:*
ntpd ntpd 1615 20 udp6 *:123 *:*
ntpd ntpd 1615 21 udp4 *:123 *:*
ntpd ntpd 1615 22 udp4 192.168.2.20:123 *:*
ntpd ntpd 1615 23 udp6 ::1:123 *:*
ntpd ntpd 1615 24 udp6 fe80::1%lo0:123 *:*
ntpd ntpd 1615 25 udp4 127.0.0.1:123 *:*
ntpd ntpd 1615 26 udp4 172.16.0.5:123 *:*
root syslogd 1085 6 udp6 *:514 *:*
root syslogd 1085 7 udp4 *:514 *:*
? ? ? ? udp4 *:17890 *:*
? ? ? ? udp6 *:17890 *:*
Option #3: nmap command
The syntax is:
You can combine TCP/UDP scan in a single command:
$ sudo nmap -sTU -O 192.168.2.13
Testing if a port is open from a bash script
One can use the “/dev/tcp/{HostName}_OR_{IPAddrress}>/{port}
” syntax to check if a TCP port is open on a Linux or Unix machine when using Bash. In other words, the following is Bash specific feature. Let us see if TCP port 22 is open on localhost and 192.168.2.20:
Now we can build some logic as follows:
What if I’m not using Bash…
Try the nc command as follows:
The updated Bash script:
Using Perl to check if a TCP port is open in Linux or Unix
Here is a Perl script to check if TCP port 22 for OpenSSH is open with a 5-second timeout using IO::Socket::INET:
Python example to check if a TCP port is open in Linux or Unix
Try thise simple code that uses low level socket networking feature. For example:
Conclusion
This page explained command to determining if a port is in use on Linux or Unix-like server. For more information see the nmap command and lsof command page online here or by typing the man command as follows:
$ man lsof$ man ss$ man netstat$ man nmap$ man 5 services$ man nc