How to check if a port is in use in a Linux system? Some time for trouble shooting purpose, you need to know if a port is used by application. Following list of commands can help you do that.
1. lsof (list of open files)
example:
* -P (not port transferring)
-n (no IP number transferring)
$lsof -i -P -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
kdeconnec 1992 user1 20u IPv6 50361 0t0 UDP *:1716
kdeconnec 1992 user1 21u IPv6 50362 0t0 TCP *:1716 (LISTEN)
chrome 2498 user1 168u IPv4 54132 0t0 UDP 224.0.0.251:5353
chrome 2570 user1 23u IPv4 66844 0t0 TCP 192.168.1.18:55922->24.75.64.81:443 (CLOSE_WAIT)
chrome 2570 user1 40u IPv4 54135 0t0 TCP 192.168.1.18:35542->172.53.63.188:5228 (ESTABLISHED)
check specific port, eg 22
$ sudo lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1155 root 3u IPv4 288 0t0 TCP *:ssh (LISTEN)
2. netstat or ss
* -t tcp protocol
-u udp protocol
-p program info or process ID
-l show listen socket
-n no IP number transferring
$ss -tupln
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 0.0.0.0:631 0.0.0.0:*
udp UNCONN 0 0 224.0.0.251:5353 0.0.0.0:* users:(("chrome",pid=2498,fd=168))
$netstat -tupln|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 116/sshd