Linux find out which port is open using the command line

I am a new Linux system user. How do I find out and list of all open ports in Linux operating system? Can you tell me Linux command to find out which port is open at the bash shell?

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 list well know port numbers command

Linux find out which port is open using the command line

The procedure to list open ports in Linux is as follows:

  1. Open the terminal application
  2. Use command netstat -tulpn to open ports
  3. 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'

OR
$ sudo ss -tulpn
$ sudo ss -tulpn | more
$ sudo ss -tulpn | grep ':port'
$ sudo ss -tulpn | grep ':22'

Click to enlarge image

Where,
  • -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
Sample outputs:

22/tcp:               1462

Let us see process name associated with PID # 1462, execute:
$ sudo ls -l /proc/1462/exe
Outputs:

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
Example outputs:

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

Click to enlarge file

Conclusion

You just learned how to list open ports on a Linux based system. For more info see man pages.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum