linux如何查看端口被谁占用(lsof 您所在的位置:网站首页 linux怎么看端口占用情况数据 linux如何查看端口被谁占用(lsof

linux如何查看端口被谁占用(lsof

2024-06-18 13:58| 来源: 网络整理| 查看: 265

1、lsof -i

lsof(list open files)是一个列出当前系统打开文件的工具。在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。所以如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等,系统在后台都为该应用程序分配了一个文件描述符,无论这个文件的本质如何,该文件描述符为应用程序与基础操作系统之间的交互提供了通用接口。因为应用程序打开文件的描述符列表提供了大量关于这个应用程序本身的信息,因此通过lsof工具能够查看这个列表对系统监测以及排错将是很有帮助的。

 详细了解lsof,可以参考:https://www.coonote.com/linux-note/linux-lsof.html

2、netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息

     netstat -tunlp 显示tcp,udp的端口和进程等相关情况

     netstat -anp 也可以显示系统端口使用情况

netstat [选项] -a或--all:显示所有连线中的Socket; -A或--:列出该网络类型连线中的相关地址; -c或--continuous:持续列出网络状态; -C或--cache:显示路由器配置的快取信息; -e或--extend:显示网络其他相关信息; -F或--fib:显示FIB; -g或--groups:显示多重广播功能群组组员名单; -h或--help:在线帮助; -i或--interfaces:显示网络界面信息表单; -l或--listening:显示监控中的服务器的Socket; -M或--masquerade:显示伪装的网络连线; -n或--numeric:直接使用ip地址,而不通过域名服务器; -N或--netlink或--symbolic:显示网络硬件外围设备的符号连接名称; -o或--timers:显示计时器; -p或--programs:显示正在使用Socket的程序识别码和程序名称; -r或--route:显示Routing Table; -s或--statistice:显示网络工作信息统计表; -t或--tcp:显示TCP传输协议的连线状况; -u或--udp:显示UDP传输协议的连线状况; -v或--verbose:显示指令执行过程; -V或--version:显示版本信息; -w或--raw:显示RAW传输协议的连线状况; -x或--unix:此参数的效果和指定"-A unix"参数相同; --ip或--inet:此参数的效果和指定"-A inet"参数相同。

方法1: lsof -i 用以显示符合条件的进程情况,lsof(list open files)是一个列出当前系统打开文件的工具。

            以root用户来执行lsof -i命令,如下:

[root@hadoop01 yum.repos.d]# lsof -i COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1873 rpc 6u IPv4 15228 0t0 UDP *:sunrpc rpcbind 1873 rpc 7u IPv4 15230 0t0 UDP *:wpages rpcbind 1873 rpc 8u IPv4 15231 0t0 TCP *:sunrpc (LISTEN) rpcbind 1873 rpc 9u IPv6 15233 0t0 UDP *:sunrpc rpcbind 1873 rpc 10u IPv6 15235 0t0 UDP *:wpages rpcbind 1873 rpc 11u IPv6 15236 0t0 TCP *:sunrpc (LISTEN) rpc.statd 2017 rpcuser 5r IPv4 15556 0t0 UDP *:921 rpc.statd 2017 rpcuser 8u IPv4 15563 0t0 UDP *:32846 rpc.statd 2017 rpcuser 9u IPv4 15567 0t0 TCP *:42335 (LISTEN) rpc.statd 2017 rpcuser 10u IPv6 15571 0t0 UDP *:41231 rpc.statd 2017 rpcuser 11u IPv6 15575 0t0 TCP *:56522 (LISTEN) cupsd 2072 root 6u IPv6 15795 0t0 TCP localhost:ipp (LISTEN) cupsd 2072 root 7u IPv4 15796 0t0 TCP localhost:ipp (LISTEN) cupsd 2072 root 9u IPv4 15811 0t0 UDP *:ipp sshd 2191 root 3u IPv4 16362 0t0 TCP *:ssh (LISTEN) sshd 2191 root 4u IPv6 16366 0t0 TCP *:ssh (LISTEN) mysqld 2379 mysql 10u IPv4 17615 0t0 TCP *:mysql (LISTEN) nginx 3953 root 11u IPv4 35803 0t0 TCP *:distinct (LISTEN) sshd 11504 root 3r IPv4 125196 0t0 TCP www.brz.com:ssh->bogon:55057 (ESTABLISHED) sshd 11504 root 7u IPv6 125274 0t0 TCP localhost:x11-ssh-offset (LISTEN) sshd 11504 root 8u IPv4 125275 0t0 TCP localhost:x11-ssh-offset (LISTEN) sshd 11716 root 3r IPv4 127456 0t0 TCP www.brz.com:ssh->bogon:55303 (ESTABLISHED) sshd 11716 root 7u IPv6 127537 0t0 TCP localhost:6011 (LISTEN) sshd 11716 root 8u IPv4 127538 0t0 TCP localhost:6011 (LISTEN) nginx 11914 nobody 11u IPv4 35803 0t0 TCP *:distinct (LISTEN)

 加端口号,查看端口被谁占用

lsof -i:端口号

例如:

注意:这里的9999端口是我自己打开的一个,前面博客提到过,如果自己没有此端口,请更换端口号

[root@hadoop01 yum.repos.d]# lsof -i:9999 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 3953 root 11u IPv4 35803 0t0 TCP *:distinct (LISTEN) nginx 11914 nobody 11u IPv4 35803 0t0 TCP *:distinct (LISTEN)

 

方法2: netstat -tunlp命令用于显示tcp,udp的端口和进程等相关情况

[root@hadoop01 yum.repos.d]# netstat -tunpl 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:9999 0.0.0.0:* LISTEN 3953/nginx tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1873/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2191/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2072/cupsd tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 11504/sshd tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 11716/sshd tcp 0 0 0.0.0.0:42335 0.0.0.0:* LISTEN 2017/rpc.statd tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2379/mysqld tcp 0 0 :::111 :::* LISTEN 1873/rpcbind tcp 0 0 :::22 :::* LISTEN 2191/sshd tcp 0 0 ::1:631 :::* LISTEN 2072/cupsd tcp 0 0 ::1:6010 :::* LISTEN 11504/sshd tcp 0 0 ::1:6011 :::* LISTEN 11716/sshd tcp 0 0 :::56522 :::* LISTEN 2017/rpc.statd udp 0 0 0.0.0.0:111 0.0.0.0:* 1873/rpcbind udp 0 0 0.0.0.0:631 0.0.0.0:* 2072/cupsd udp 0 0 0.0.0.0:776 0.0.0.0:* 1873/rpcbind udp 0 0 0.0.0.0:921 0.0.0.0:* 2017/rpc.statd udp 0 0 0.0.0.0:32846 0.0.0.0:* 2017/rpc.statd udp 0 0 :::111 :::* 1873/rpcbind udp 0 0 :::776 :::* 1873/rpcbind udp 0 0 :::41231 :::* 2017/rpc.statd

netstat -tunlp 会显示所有端口和所有对应的程序,用grep管道可以过滤出想要的关键字段,

列一下9999端口占用的程序:

注意:这里的9999端口是我自己打开的一个,前面博客提到过,如果自己没有此端口,请更换端口号

netstat -tunplp | grep 端口号

例如:

[root@hadoop01 yum.repos.d]# netstat -tunplp | grep 9999 tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 3953/nginx

使用netstat -anp进行查看

[root@hadoop01 yum.repos.d]# netstat -anp|grep 9999 tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 3953/nginx

所以这两种方法都可以查看。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有