查詢端口是否被占用:
1.netstat -anp |grep 端口号
以5000端口為例:
#netstat -anp |grep 5000
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 28629/python
看監控狀态為LISTEN表示已經被占用,最後一列顯示5000端口被服務python占用,檢視具體端口号,隻要有如上這一行就表示被占用了。
2.netstat -nultp(此處不用加端口号)
該指令是檢視目前所有已經使用的端口情況,如下:
netstat -nultp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
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:5000 0.0.0.0:* LISTEN 28629/python
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:631 :::* LISTEN -
tcp6 0 0 ::1:25 :::* LISTEN -
tcp6 0 0 ::1:6010 :::* LISTEN -
udp 0 0 192.168.122.1:53 0.0.0.0:* -
udp 0 0 0.0.0.0:67 0.0.0.0:* -
udp 0 0 0.0.0.0:49621 0.0.0.0:* -
udp 0 0 0.0.0.0:5353 0.0.0.0:* -
由上也可以看到5000端口被服務python占用,程序号為28629。