如何查找哪个进程在占用端口

原文

  • netstat: 展示网络连接,路由表,网络接口数据。
  • fuser: 标识占用文件或端口的进程。
  • lsof: 展示被打开的文件以及占用文件的进程。
  • /proc/$pid/:
    Note: 普通用户只能查询到当前用户运行的进程,建议以root用户执行。
netstat

$ netstat -tulpn
部分结果:

1
2
3
4
5
6
7
8
9
10
11
bejond@bejond-HP:~$ netstat -tulpn
(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:35421 0.0.0.0:* LISTEN 12779/java
tcp 0 0 127.0.0.1:19645 0.0.0.0:* LISTEN 1277/python
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6942 0.0.0.0:* LISTEN 12779/java
tcp 0 0 0.0.0.0:34303 0.0.0.0:* LISTEN 12779/java
tcp 0 0 0.0.0.0:8643 0.0.0.0:* LISTEN 16031/java

可以使用grep过滤:
$ netstat -tulpn | grep 8643

1
2
3
4
bejond@bejond-HP:~$ netstat -tulpn | grep 8643
(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:8643 0.0.0.0:* LISTEN 16031/java

TCP端口8643正在执行java程序,进程号为16031,可以通过$ ls -l /proc/16031/exe验证。

1
2
bejond@bejond-HP:~$ ls -l /proc/16031/exe
lrwxrwxrwx 1 bejond bejond 0 1213 17:52 /proc/16031/exe -> /home/bejond/tools/java-tools/jdk1.8.0_121/bin/java

可以通过$ man java$ whatis java了解java是什么程序。

fuser

查找哪个进程占用8643端口:
$ fuser 8643/tcp

1
2
bejond@bejond-HP:~$ fuser 8643/tcp
8643/tcp: 16031

如果要查找哪里的程序创建进程16031及其工作组:
$ ls -l /proc/16031/cwd

1
2
bejond@bejond-HP:~$ ls -l /proc/16031/cwd
lrwxrwxrwx 1 bejond bejond 0 1213 19:08 /proc/16031/cwd -> /home/bejond/code/server/wildfly-10.0.0.Final_v5/bin

或者直接输入$ pwdx 16031

1
2
bejond@bejond-HP:~$ pwdx 16031
16031: /home/bejond/code/server/wildfly-10.0.0.Final_v5/bin

如何查找进程的拥有者:
$ ps aux
部分结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bejond@bejond-HP:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 154936 6832 ? Ss 15:28 0:01 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 15:28 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 15:28 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S< 15:28 0:00 [mm_percpu_wq]
root 7 0.0 0.0 0 0 ? S 15:28 0:01 [ksoftirqd/0]
root 8 0.0 0.0 0 0 ? S 15:28 0:10 [rcu_sched]
root 9 0.0 0.0 0 0 ? S 15:28 0:00 [rcu_bh]
root 10 0.0 0.0 0 0 ? S 15:28 0:00 [migration/0]
root 11 0.0 0.0 0 0 ? S 15:28 0:00 [watchdog/0]
root 12 0.0 0.0 0 0 ? S 15:28 0:00 [cpuhp/0]
root 13 0.0 0.0 0 0 ? S 15:28 0:00 [cpuhp/1]
root 14 0.0 0.0 0 0 ? S 15:28 0:00 [watchdog/1]

使用grep过滤:
$ ps aux | grep 16031

1
2
3
bejond@bejond-HP:~$ ps aux | grep 16031
bejond 16031 2.5 11.4 4537864 920976 pts/6 Sl+ 17:52 2:40 /home/bejond/tools/java-tools/jdk1.8.0_121/bin/java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n -Dorg.jboss.boot.log.file=/home/bejond/code/server/wildfly-10.0.0.Final_v5/standalone/log/server.log -Dlogging.configuration=file:/home/bejond/code/server/wildfly-10.0.0.Final_v5/standalone/configuration/logging.properties -jar /home/bejond/code/server/wildfly-10.0.0.Final_v5/jboss-modules.jar -mp /home/bejond/code/server/wildfly-10.0.0.Final_v5/modules org.jboss.as.standalone -Djboss.home.dir=/home/bejond/code/server/wildfly-10.0.0.Final_v5 -Djboss.server.base.dir=/home/bejond/code/server/wildfly-10.0.0.Final_v5/standalone
bejond 17481 0.0 0.0 16112 1080 pts/7 S+ 19:36 0:00 grep --color=auto 16031

尝试执行如下命令:
$ ps -eo pid,user,group,args,etime,lstart | grep '16031'

1
2
3
bejond@bejond-HP:~$ ps -eo pid,user,group,args,etime,lstart | grep '16031'
16031 bejond bejond /home/bejond/tools/java-too 01:47:04 Wed Dec 13 17:52:52 2017
17510 bejond bejond grep --color=auto 16031 00:00 Wed Dec 13 19:39:56 2017

想要看到更多信息?另一个方式是$ cat /proc/16031/environ:

1
2
bejond@bejond-HP:~$ cat /proc/16031/environ
LESSOPEN=| /usr/bin/lesspipe %sUSER=bejondLANGUAGE=en_USLC_TIME=zh_CN.UTF-8XDG_SEAT=seat0SSH_AGENT_PID=1211XDG_SESSION_TYPE=x11SHLVL=1QT4_IM_MODULE=fcitxHOME=/home/bejondDESKTOP_SESSION=xubuntuJRE_HOME=/home/bejond/tools/java-tools/jdk1.8.0_45/jreSMARTGITHG_MAX_HEAP_SIZE=4096mXDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0LC_MONETARY=zh_CN.UTF-8DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/busANT_OPTS=-Xmx4g -XX:MaxPermSize=2gJBOSS_HOME=/home/bejond/code/server/wildfly-10.0.0.Final_v5COLORTERM=truecolorGLADE_MODULE_PATH=:MANDATORY_PATH=/usr/share/gconf/xubuntu.mandatory.pathQT_QPA_PLATFORMTHEME=gtk2LOGNAME=bejondGTK_IM_MODULE=fcitxWINDOWID=75510244_=./standalone.shDEFAULTS_PATH=/usr/share/gconf/xubuntu.default.pathGTK_OVERLAY_SCROLLING=0XDG_SESSION_ID=c1CLUTTER_BACKEND=x11TERM=xterm-256colorRBENV_SHELL=bashPATH=/home/bejond/anaconda3/bin:/home/bejond/anaconda2/bin:/home/bejond/.rbenv/plugins/ruby-build/bin:/home/bejond/.rbenv/shims:/home/bejond/.rbenv/bin:/home/bejond/bin:/home/bejond/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/bejond/tools/java-tools/jdk1.8.0_121/bin:/home/bejond/tools/java-tools/apache-ant-1.9.5/bin:/home/bejond/tools/rar:/home/bejond/tools:/home/bejond/tools/java-tools/jdk1.8.0_45/jre/bin:/home/bejond/tools/Sublime_Text_2:/home/bejond/tools/smartgithg-4_6_3/bin:/home/bejond/tools/eclipse:/home/bejond/tools/FileZilla3/bin:/home/bejond/tools/navicat110_mysql_en:/home/bejond/tools/git-tools:/home/bejond/tools/giteye:/home/bejond/tools/scripts:/home/bejond/tools/jd:/home/bejond/tools/idea-IU-139.659.2/bin:/home/bejond/tools/apache-cxf-3.1.11/bin:/home/bejond/tools/apache-maven-3.5.0/bin:/home/bejond/tools/java-tools/jdk1.8.0_121/bin:/home/bejond/tools/java-tools/jdk1.8.0_45/jre/binGDM_LANG=en_USGLADE_PIXMAP_PATH=:ANT_HOME=/home/bejond/tools/java-tools/apache-ant-1.9.5SESSION_MANAGER=local/bejond-HP:@/tmp/.ICE-unix/1236,unix/bejond-HP:/tmp/.ICE-unix/1236XDG_MENU_PREFIX=xfce-LC_ADDRESS=zh_CN.UTF-8XDG_RUNTIME_DIR=/run/user/1000XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0DISPLAY=:0.0LANG=en_US.UTF-8XDG_CURRENT_DESKTOP=XFCELC_TELEPHONE=zh_CN.UTF-8LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:XDG_SESSION_DESKTOP=xubuntuXMODIFIERS=@im=fcitxXAUTHORITY=/home/bejond/.XauthoritySSH_AUTH_SOCK=/run/user/1000/keyring/sshGLADE_CATALOG_PATH=:XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/bejondLC_NAME=zh_CN.UTF-8SHELL=/bin/bashQT_ACCESSIBILITY=1GDMSESSION=xubuntuLESSCLOSE=/usr/bin/lesspipe %s %sLC_MEASUREMENT=zh_CN.UTF-8CLASSPATH=.:/home/bejond/tools/java-tools/jdk1.8.0_121/lib:/home/bejond/tools/java-tools/jdk1.8.0_121/lib:.:/home/bejond/tools/java-tools/jdk1.8.0_121/lib:/home/bejond/tools/java-tools/jdk1.8.0_121/jre/libLC_IDENTIFICATION=zh_CN.UTF-8XDG_VTNR=7QT_IM_MODULE=fcitxJAVA_HOME=/home/bejond/tools/java-tools/jdk1.8.0_121PWD=/home/bejond/code/server/wildfly-10.0.0.Final_v5/binCLUTTER_IM_MODULE=ximXDG_DATA_DIRS=/usr/share/xubuntu:/usr/share/xfce4:/usr/local/share:/usr/share:/var/lib/snapd/desktop:/var/lib/snapd/desktop:/usr/shareXDG_CONFIG_DIRS=/etc/xdg/xdg-xubuntu:/etc/xdg:/etc/xdgLC_NUMERIC=zh_CN.UTF-8LC_PAPER=zh_CN.UTF-8VTE_VERSION=4804
lsof

可以用如下几种方式,第一种即可。
$ lsof -i :8643
$ lsof -i tcp:8643
$ lsof -i :8643 | grep LISTEN
结果:

1
2
3
bejond@bejond-HP:~$ lsof -i :8643
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 16031 bejond 382u IPv4 2396178 0t0 TCP *:8643 (LISTEN)
附:
  • 如果知道占用端口进程是java进程,可以使用jps [-v]命令查看,但是没法通过端口查询。
  • 如果想杀掉进程,可以使用$ kill -9 16031,当然还有根据程序名关闭$ killall java,可以关闭所有java进程,不推荐这么用,风险极大🌚。