The netstat and the lsof are the two main commands to check the tcp/udp listening ports in Linux.
Netstat Installation:
CentOS and Debian:
yum install net-tools
apt install net-tools
netstat --listen or netstat -l
To show established tcp connections :
netstat -vatn
To show only UDP connections:
netstat -vaun
If you are looking for a specific service listens to which port :
netstat -ntpl | grep ssh
Filter with specific port number :
netstat -ltnp | grep -w ':80'
l- tells netstat to only show listening sockets.
t – tells it to display tcp connections.
n – instructs it show numerical addresses.
p – enables showing of the process ID and the process name.
grep -w – shows matching of exact string (:22).
lsof is another command used to list all open ports and files in your system. Installation for CentOS and Debian:
yum install lsof
apt install lsof
To display all open Ports:
lsof -i
To display all open files :
lsof
To display all tcp listen:
lsof -iTCP -sTCP:LISTEN
To show a specific port :
lsof -i :80
To show only IPV4 addresses in MacOS:
netstat -f inet -rn