linux 查看端口号

在使用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       892     root    3u  IPv4  13149      0t0  TCP *:ssh (LISTEN)
sshd       892     root    4u  IPv6  13151      0t0  TCP *:ssh (LISTEN)
postgres  1070 postgres    3u  IPv6  11215      0t0  TCP localhost:postgresql (LISTEN)
postgres  1070 postgres    6u  IPv4  11216      0t0  TCP localhost:postgresql (LISTEN)
postgres  1070 postgres   10u  IPv6  11224      0t0  UDP localhost:49869->localhost:49869 
postgres  1127 postgres   10u  IPv6  11224      0t0  UDP localhost:49869->localhost:49869
memcached 1171 memcache   26u  IPv4  14401      0t0  TCP localhost:11211 (LISTEN)
memcached 1171 memcache   27u  IPv4  14402      0t0  UDP localhost:11211 
rsync     1183     root    4u  IPv4  14403      0t0  TCP localhost:rsync (LISTEN)
sshd      8583     root    3u  IPv4  20140      0t0  TCP 192.168.131.150:ssh->192.168.131.1:58475 (ESTABLISHED)
sshd      8632    shang    3u  IPv4  20140      0t0  TCP 192.168.131.150:ssh->192.168.131.1:58475 (ESTABLISHED)
swift-con 8801     root    4u  IPv4  24806      0t0  TCP *:6011 (LISTEN)
swift-con 8802     root    4u  IPv4  24799      0t0  TCP *:6021 (LISTEN)
swift-con 8803     root    4u  IPv4  24790      0t0  TCP *:6031 (LISTEN)

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

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

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

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

参考网址:

http://my.oschina.net/u/193184/blog/146885
原文地址:https://www.cnblogs.com/pingandezhufu/p/5446210.html