linux 查看端口号

2023-05-26,,

在使用Linux系统的过程中,有时候会遇到端口被占用而导致服务无法启动的情况。比如HTTP使用80端口,但当启动Apache时,却发现此端口正在使用。

这种情况大多数是由于软件冲突、或者默认端口设置不正确导致的,此时需要查看究竟哪个进程占用了端口,来决定进一步的处理方法。

1.查看端口占用情况的命令:lsof -i

root@ubuntuServer0:/home/shang/bin# lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd root 3u IPv4 0t0 TCP *:ssh (LISTEN)
sshd root 4u IPv6 0t0 TCP *:ssh (LISTEN)
postgres postgres 3u IPv6 0t0 TCP localhost:postgresql (LISTEN)
postgres postgres 6u IPv4 0t0 TCP localhost:postgresql (LISTEN)
postgres postgres 10u IPv6 0t0 UDP localhost:->localhost:
postgres postgres 10u IPv6 0t0 UDP localhost:->localhost:
memcached memcache 26u IPv4 0t0 TCP localhost: (LISTEN)
memcached memcache 27u IPv4 0t0 UDP localhost:
rsync root 4u IPv4 0t0 TCP localhost:rsync (LISTEN)
sshd root 3u IPv4 0t0 TCP 192.168.131.150:ssh->192.168.131.1: (ESTABLISHED)
sshd shang 3u IPv4 0t0 TCP 192.168.131.150:ssh->192.168.131.1: (ESTABLISHED)
swift-con root 4u IPv4 0t0 TCP *: (LISTEN)
swift-con root 4u IPv4 0t0 TCP *: (LISTEN)
swift-con root 4u IPv4 0t0 TCP *: (LISTEN)

这里返回了Linux当前所有打开端口的占用情况。第一段是进程,最后一列是侦听的协议、侦听的IP与端口号、状态。如果端口号是已知的常用服务(如80、21等),则会直接显示协议名称,如http、ftp、ssh等。

2.查看某一端口的占用情况: lsof -i:端口号

root@ubuntuServer0:/home/shang/bin# lsof -i:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
swift-pro root 4u IPv4 0t0 TCP *:http-alt (LISTEN)
swift-pro root 4u IPv4 0t0 TCP *:http-alt (LISTEN)
swift-pro root 4u IPv4 0t0 TCP *:http-alt (LISTEN)

3.结束占用端口的进程:killall 进程名

参考网址:

http://my.oschina.net/u/193184/blog/146885

linux 查看端口号的相关教程结束。

《linux 查看端口号.doc》

下载本文的Word格式文档,以方便收藏与打印。