Linux find out which port is open using the command line

last updated in Categories , , , , ,

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'

Linux find out which port is open using bash command line
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

How to Find Out List of All Open Ports in Linux Using Command
Click to enlarge file

Conclusion

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.