Linux port ranges information

Linux and TCP/IP ports range from 0 to 65535. They are categorized in following ways:

1. System / Well-Known Ports (0 – 1023)

They are reserved for core or system wide network services. For example, SSH on port 22, HTTP on port 80, HTTPS on port 443, DNS on port 53, SMTP on port 25,etc

2. Registered Ports (1024 – 49151)

These ports are registered by software corporations for their applications. For example, Microsoft SQL Server uses port 1433, MySQL uses port 3306, and PostgreSQL uses port 5432.

3. Dynamic / Private Ports (49152 – 65535)

These ports are not assigned or registered to any specific application. They are sometimes used by the OS as temporary (ephermal) client-side ports when making outgoing network connections.

How to check which ports are free on your server?

There is no single command that outputs a static list of 'unused' ports. Instead use these commands:

Check active listening ports via ss or netstat

$ sudo ss -tuln

# or 

$ sudo netstat -tuln

Check which process is using a specific port (example, port 8080)

sudo lsof -i :8080

If you see no output of this command, that means the port is not in use and is completely free.

Is it possible for multiple processes to listen on the same port?

Yes, but by default Operating System blocks it and throws error. There are some conditions on which sharing same port for two processes depends:

  1. Different IP Addresses: A socket is bound to a combination of an IP address and a port, not just the port alone. So, process A can listen on 127.0.0.1:8080 (localhost), while at the same time, process B listens on 192.168.1.50:8080 (LAN IP). However, if a process binds to 0.0.0.0:8080, which means all IPv4 addresses, then it will claim every local IP on that port and will prevent other processes from binding to a specific IP on port 8080.

  2. Differnt Transport Protocols (TCP, UDP): TCP and UDP have completely separate port pools. One process can listen on TCP port 53, while a different process listens on UDP port 53 and this will be without any conflict.

  3. Parent and Child Processes (Forking): A master process (example, Nginx) opens a socket on port 80, then spawns multiple worker processes. All of these spawned processes then inherit that open socket and listens on port 80 simultaneously handling incoming traffic. If you see multiple PIDs in sudo lsof command for the same port, this is completely normal when a process uses this Parent and Child model, or master-worker model. Servers like Nginx, Apache bind to the port once as a parent process and then clone themselves into multiple worker processes to handle incoming traffic simultaneously.