最近在linux服务器上部署应用,总是出现端口被占用的情况,有时关闭软件,kill进程也会出现端口占用。
有时候关闭软件后,后台进程死掉,也会导致端口被占用。
命令:netstat -nultp
#netstat -nultp #查看端口使用情况
#netstat -nultp | grep 22 #只查看端口22的使用情况
linux-3v74:~ # netstat -nultp
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:22 0.0.0.0:* LISTEN 1656/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1782/master
tcp 0 0 :::22 :::* LISTEN 1656/sshd
tcp 0 0 ::1:25 :::* LISTEN 1782/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 1001/wickedd-dhcp4
linux-3v74:~ # netstat -nultp | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1656/sshd
tcp 0 0 :::22 :::* LISTEN 1656/sshd
命令: netstat -anp
#netstat -anp #查看端口使用情况
#netstat -anp | grep 22 #只查看端口22的使用情况
linux-3v74:~ # netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1656/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1782/master
tcp 0 0 10.169.42.82:22 10.169.42.154:54363 ESTABLISHED 1861/sshd: root
tcp 0 0 10.169.42.82:22 10.169.42.154:54364 ESTABLISHED 1876/sshd: root@not
tcp 0 148 10.169.42.82:22 10.169.42.86:58230 ESTABLISHED 1964/1
tcp 0 0 :::22 :::* LISTEN 1656/sshd
tcp 0 0 ::1:25 :::* LISTEN 1782/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 1001/wickedd-dhcp4
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ] DGRAM 15124 1/systemd /run/systemd/journal/syslog
unix 2 [ ACC ] STREAM LISTENING 27625 1826/gnome-session- @/tmp/.ICE-unix/1826
unix 2 [ ACC ] SEQPACKET LISTENING 15128 1/systemd /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 15132 1/systemd /run/lvm/lvmetad.socket
......
linux-3v74:~ # netstat -anp | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1656/sshd
tcp 0 0 10.169.42.82:22 10.169.42.154:54363 ESTABLISHED 1861/sshd: root
tcp 0 0 10.169.42.82:22 10.169.42.154:54364 ESTABLISHED 1876/sshd: root@not
tcp 0 52 10.169.42.82:22 10.169.42.86:58230 ESTABLISHED 1964/1
tcp 0 0 :::22 :::* LISTEN 1656/sshd
unix 2 [ ACC ] STREAM LISTENING 22820 984/nscd /var/run/nscd/socket
unix 2 [ ACC ] STREAM LISTENING 22337 1/systemd /var/run/pcscd/pcscd.comm
unix 2 [ ACC ] STREAM LISTENING 22340 1/systemd /run/dbus/system_bus_socket
unix 2 [ ] DGRAM 27467 1822/systemd /run/user/484/systemd/notify
unix 2 [ ACC ] STREAM LISTENING 27469 1822/systemd /run/user/484/systemd/private
unix 2 [ ACC ] STREAM LISTENING 27322 1742/gdm-simple-sla @/tmp/dbus-6GUvF5JW
unix 2 [ ACC ] STREAM LISTENING 22343 1/systemd @ISCSIADM_ABSTRACT_NAMESPACE
unix 22 [ ] DGRAM 10944 1/systemd /run/systemd/journal/dev-log
unix 3 [ ] STREAM CONNECTED 22784 1/systemd /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 22782 981/dbus-daemon
unix 3 [ ] STREAM CONNECTED 27522 1836/at-spi-bus-lau
unix 3 [ ] STREAM CONNECTED 22862 981/dbus-daemon
unix 3 [ ] STREAM CONNECTED 22864 981/dbus-daemon /run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 27440 1822/systemd
unix 2 [ ] DGRAM 22799 984/nscd
unix 3 [ ] STREAM CONNECTED 22449 974/VGAuthService
unix 3 [ ] STREAM CONNECTED 22356 1/systemd
unix 3 [ ] STREAM CONNECTED 22450 1/systemd /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 22863 981/dbus-daemon
unix 3 [ ] STREAM CONNECTED 23622 1057/wickedd-nanny
unix 2 [ ] DGRAM 27455 1822/systemd
unix 3 [ ] STREAM CONNECTED 26622 1782/master
linux-3v74:~ #
命令:lsof -i :22
#如果用lsof -i命令查不到端口, 安装一下就ok了 yum install lsof -y
Tip : lsof -i 需要 root 用户的权限来执行
linux-3v74:~ # lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1656 root 3u IPv4 25129 0t0 TCP *:ssh (LISTEN)
sshd 1656 root 4u IPv6 25135 0t0 TCP *:ssh (LISTEN)
sshd 1861 root 4u IPv4 27727 0t0 TCP 10.169.42.82:ssh->CN-W-GRSTV63.INT.vertivco.com:54363 (ESTABLISHED)
sshd 1876 root 4u IPv4 27827 0t0 TCP 10.169.42.82:ssh->CN-W-GRSTV63.INT.vertivco.com:54364 (ESTABLISHED)
sshd 1964 root 4u IPv4 30076 0t0 TCP 10.169.42.82:ssh->DESKTOP-47MK0MU.INT.vertivco.com:58230 (ESTABLISHED)
在查到端口占用的进程后,如果你要杀掉对应的进程可以使用 kill 命令:
kill -9 PID