Introduction: A port in Linux is nothing but a logical connection place. The TCP/IP use port for communication across the LAN, WAN, and Internet. Typically, a server program such as Apache (httpd) listens on TCP port 80 or 443. A client program such as a web browser connects to TCP port 80 to request web page. You can find preassigned port numbers in /etc/services files with the help of cat command or grep command/egrep command linux command. For example:
$ cat /etc/services
$ grep -w 80/tcp /etc/services
$ grep -w 443/tcp /etc/services
$ egrep -w '(80|22|443)/tcp' /etc/services
Linux find out which port is open using the command line
The procedure to list open ports in Linux is as follows:
- Open the terminal application
- Use command netstat -tulpn to open ports
- Another option is to run ss -tulpn to open ports on modern Linux distros
Let us see all commands in details.
Linux find port open command
One can use the netstat command or ss command find out which process is listing upon a port:
$ sudo netstat -tulpn
$ sudo netstat -tulpn | more
$ sudo netstat -tulpn | grep ':port'
$ sudo netstat -tulpn | grep ':22'
$ sudo ss -tulpn
$ sudo ss -tulpn | more
$ sudo ss -tulpn | grep ':port'
$ sudo ss -tulpn | grep ':22'
- -t – List all TCP ports on Linux
- -u – List all UDP ports on Linux
- -l – Show only listening sockets/ports
- -p – Show process names (PID) using sockets/ports in Linux
- -n – Do not try to resolve service names (speed up option) using /etc/resolv.conf file
How to find out and list of all open TCP/UDP ports in Linux
Other commands that you can use are as follows.
fuser command examples to find out which port is open
One can list the processes PID that opened tcp port 22, enter:
$ sudo fuser 22/tcp
Let us see process name associated with PID # 1462, execute:
$ sudo ls -l /proc/1462/exe
lrwxrwxrwx 1 root root 0 Nov 2 11:30 /proc/1462/exe -> /usr/sbin/sshd
/usr/sbin/sshd has opened tcp port 22. But, what is sshd? Let us find out:
$ whatis sshd
sshd (8) - OpenSSH SSH daemon
How to use lsof command to display open port on Linux
$ sudo lsof -i :portNumber
$ sudo lsof -i tcp:portNumber
$ sudo lsof -i udp:portNumber
$ sudo lsof -i :22
$ sudo lsof -i :22 | grep LISTEN
You just learned how to list open ports on a Linux based system. For more info see man pages.