1. 使用 lsof 命令
$ lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dhclient 832 root 6u IPv4 11631 0t0 UDP *:bootpc
sshd 14081 root 3u IPv4 66795 0t0 TCP *:33899 (LISTEN)
sshd 14081 root 4u IPv6 66804 0t0 TCP *:33899 (LISTEN)
docker-pr 15728 root 4u IPv6 166965 0t0 TCP *:8002 (LISTEN)
docker-pr 15867 root 4u IPv6 166314 0t0 TCP *:http-alt (LISTEN)
docker-pr 15878 root 4u IPv6 166340 0t0 TCP *:17051 (LISTEN)
...
$ lsof -i:9876
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 32184 root 58u IPv6 831163 0t0 TCP *:9876 (LISTEN)
java 32184 root 60u IPv6 832262 0t0 TCP member.blackpai.com:9876->member.blackpai.com:59928 (ESTABLISHED)
...
2. 使用 netstat 命令
$ netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 18423/mongod
tcp 0 0 0.0.0.0:33899 0.0.0.0:* LISTEN 14081/sshd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18005/nginx: master
tcp6 0 0 :::33899 :::* LISTEN 14081/sshd
...
$ netstat -tunlp | grep 9876
tcp6 0 0 :::9876 :::* LISTEN 32184/java