欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

netstat 常用方法

netstat简介

  netstat是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表,实际的网络连接以及每一个网络接口设备的状态信息,netstat用于显示与IP,TCP,UDP和ICMP协议相关的统计数据,一般用于查询本机各端口的网络连接情况。

  一般用netstat -an显示所有连接的IP、端口并用数字表示。

  netstat命令的功能是显示网络连接、路由表和网络接口信息,可以让用户得知有哪些网络连接正在运作。

  使用时,如果不带参数,netstat显示活动的TCP连接

netstat常用参数

  常用的几个参数有:-a -n -p -l

  -a 显示所有socket,包括正在监听的

  -l 显示有在 Listen (监听) 的服务状态

  -n 以网络IP地址代替名称,显示网络连接情形

  -p 显示建立相关连接的程序名和PID

  -t 显示TCP协议的连接情况

  -u 显示UDP协议的连接情况

  -s 显示每个协议的统计

  -b 显示在创建每个连接或监听端口时涉及的可执行程序

  -c 每个1秒就重新显示一遍,直到用户中断

 

netstat常用场景

  1. netstat -a 列出所有连接

复制代码
[root@77-9-162-bx-core ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:privatewire     0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:4194            0.0.0.0:*               LISTEN
tcp        5      0 0.0.0.0:30211           0.0.0.0:*               LISTEN

 udp 0 0 0.0.0.0:10010 0.0.0.0:*
  udp 0 0 0.0.0.0:11916 0.0.0.0:*
  udp 0 0 0.0.0.0:30000 0.0.0.0:*
  Active UNIX domain sockets (servers and established)
  Proto RefCnt Flags Type State I-Node Path
  unix 2 [ ACC ] STREAM LISTENING 3925009694 /tmp/tmux-0/default
  unix 2 [ ACC ] STREAM LISTENING 4021720277 /tmp/tmux-1091/default
  unix 2 [ ACC ] STREAM LISTENING 3910671241 /tmp/tmux-1081/default

复制代码

  2. netstat -at 列出所有tcp连接

复制代码
root@77-9-162-bx-core ~]# netstat -at|head
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:privatewire     0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:4194            0.0.0.0:*               LISTEN
tcp        5      0 0.0.0.0:30211           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:zabbix-agent    0.0.0.0:*               LISTEN
tcp        0      0 localhost:cslistener    0.0.0.0:*               LISTEN
tcp        0      0 localhost:10248         0.0.0.0:*               LISTEN
tcp        1      0 0.0.0.0:31080           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:30089           0.0.0.0:*               LISTEN
复制代码

     3. netstat -au 列出所有udp连接

复制代码
[root@77-9-162-bx-core ~]# netstat -au|head
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 0.0.0.0:krb524          0.0.0.0:*
udp        0      0 0.0.0.0:upnotifyp       0.0.0.0:*
udp        0      0 0.0.0.0:n1-fwp          0.0.0.0:*
udp        0      0 0.0.0.0:n1-rmgmt        0.0.0.0:*
udp        0      0 0.0.0.0:asc-slmd        0.0.0.0:*
udp        0      0 0.0.0.0:privatewire     0.0.0.0:*
udp        0      0 0.0.0.0:55511           0.0.0.0:*
udp        0      0 0.0.0.0:55512           0.0.0.0:*
复制代码

  4. netstat -nl|grep port  查询端口号是否占用

[root@77-9-162-bx-core ~]# netstat -ln|grep 8090
tcp        0      0 0.0.0.0:8090            0.0.0.0:*               LISTEN

  5. netstat -nlp|grep port 查询占用端口号的程序名或pid

[root@77-9-162-bx-core ~]# netstat -lnp|grep 8090
tcp        0      0 0.0.0.0:8090            0.0.0.0:*               LISTEN      8904/vintage

  6. netstat -apn|grep server 查询程序占用端口

# netstat -apn|grep server
Active Internet connections (servers and established)
tcp        0      0 0.0.0.0:8100            0.0.0.0:*               LISTEN      17557/server

 

连接状态

LISTEN  监听来自远方的TCP端口的连接请求

SYN-SENT  在发送连接请求后等待匹配的连接请求

SYN-RECEIVED  在收到和发送一个连接请求后等待对方对连接请求的确认

ESTABLISHED  代表一个打开的连接

TIME-WAIT  等待足够的时间以确保远程TCP接收到连接中断请求的确认

CLOSED  没有任何连接状态

CLOSED-WAIT  等待从本地用户发来的连接中断请求


来自 https://www.cnblogs.com/shining5/p/8074080.html

普通分类: