Linux查看进程、端口占用相关命令

1、netstat 命令

1.1、查看端口进程

1.1.1 -nlp

netstat -nlp | grep 端口号

eg:

netstat -nlp | grep 3001

查询结果:

1.1.1 -anp

netstat -anp  | grep 端口号

eg:

netstat -anp  | grep 3001

查询结果:

1.2、netstat 参数解析

usage: netstat [-veenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vnNcaeol] [<Socket> ...]
       netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]

        -r, --route                display routing table            #显示路由表
        -I, --interfaces=<Iface>   display interface table for <Iface>  #显示网络接口表
        -i, --interfaces           display interface table  #显示网络接口表
        -g, --groups               display multicast group memberships      #显示多播组成员
        -s, --statistics           display networking statistics (like SNMP)    #显示网络统计数据,如IP/ICMP/SNMP/..各协议统计。
        -M, --masquerade           display masqueraded connections     #显示伪装的连接

        -v, --verbose              be verbose   #详细信息
        -n, --numeric              don't resolve names     #不做名字解析,不加此参数,80端口会显示成http,127.0.0.1显示成localhost,uid为0显示成root等等   
        --numeric-hosts            don't resolve host names     #不做主机名解析
        --numeric-ports            don't resolve port names     #不做端口名解析
        --numeric-users            don't resolve user names     #不做用户名解析
        -N, --symbolic             resolve hardware names   
        -e, --extend               display other/more information   #显示更多信息,用户名,inode
        -p, --programs             display PID/Program name for sockets     #显示pid和程序名字
        -c, --continuous           continuous listing   #持续的列出相关信息

        -l, --listening            display listening server sockets     #显示处于监听状态的套接字
        -a, --all, --listening     display all sockets (default: connected)     #显示所有的套接字
        -o, --timers               display timers       #显示计时器
        -F, --fib                  display Forwarding Information Base (default)       #使用-rF查看路由表时,显示转发信息
        -C, --cache                display routing cache instead of FIB     #使用-rC查看路由表时,显示详细的路由缓存
        -T, --notrim               stop trimming long addresses     #停止修剪长地址
        -t  			#显示tcp链接
        -Z, --context              display SELinux security context for sockets     #显示套接字的SELINUX上下文
		-u 			#显示udp链接
		-x 			#显示unix套接字 

  <Iface>: Name of interface to monitor/list.
  <Socket>={-t|--tcp} {-u|--udp} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
  <AF>=Use '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25) 

2、lsof 命令

2.1、查看端口进程

lsof -i:端口号

eg:

lsof -i:3001

查询结果:

2.2、lsof 参数解析

lsof语法格式是: lsof [options] filename
常用的参数列表:

lsof filename 显示打开指定文件的所有进程
lsof -a 表示两个参数都必须满足时才显示结果
lsof -c string 显示COMMAND列中包含指定字符的进程所有打开的文件
lsof -u username 显示所属user进程打开的文件
lsof -g gid 显示归属gid的进程情况
lsof +d /DIR/ 显示目录下被进程打开的文件
lsof +D /DIR/ 同上,但是会搜索目录下的所有目录,时间相对较长
lsof -d FD 显示指定文件描述符的进程
lsof -n 不将IP转换为hostname,缺省是不加上-n参数
lsof -i 用以显示符合条件的进程情况
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
46 –> IPv4 or IPv6
protocol –> TCP or UDP
hostname> Internet host name
hostaddr –> IPv4地址
service> /etc/service中的 service name (可以不只一个)
port –> 端口号 (可以不只一个)

3、查看端口占用进程

3.1、ps 命令

3.1.1 查看端口占用进程

  如果想详细查看这个进程,ID具体是哪一个进程,可以通过ps命令查看进程的详细信息。

ps -ef	|  grep 24809

eg:

ps -ef	|  grep 24809

查询结果:

3.1.1 ps 参数解析

使用方式:ps [options] [–help]
常用参数:

-A 显示所有进程(等价于-e)(utility)
-a 显示一个终端的所有进程,除了会话引线
-N 忽略选择。
-d 显示所有进程,但省略所有的会话引线(utility)
-x 显示没有控制终端的进程,同时显示各个命令的具体路径。dx不可合用。(utility)
-p pid 进程使用cpu的时间
-u uid or username 选择有效的用户id或者是用户名
-g gid or groupname 显示组的所有进程。
U username 显示该用户下的所有进程,且显示各个命令的详细路径。如:ps U zhang;(utility)
-f 全部列出,通常和其他选项联用。如:ps -fa or ps -fx and so on.
-l 长格式(有F,wchan,C 等字段)
-j 作业格式
-o 用户自定义格式。
v 以虚拟存储器格式显示
s 以信号格式显示
-m 显示所有的线程
-H 显示进程的层次(和其它的命令合用,如:ps -Ha)(utility)
e 命令之后显示环境(如:ps -d e; ps -a e)(utility)
h 不显示第一行

3.2、ll [option] 命令查看进程

通过 ll [option] 可以查看、可以快速定位到进程的目录。

ll /proc/进程ID/cwd

eg:

ll /proc/24809/cwd

查询结果:

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值