netstat命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。
如果你的计算机有时候接收到的数据报导致出错数据或故障,你不必感到奇怪,TCP/IP可以容许这些类型的错误,并能够自动重发数据报。但如果累计的出错情况数目占到所接收的IP数据报相当大的百分比,或者它的数目正迅速增加,那么你就应该使用netstat查一查为什么会出现这些情况了。
二.常见格式:
-a 显示所有选项,默认不显示LISTEN相关
-t 仅显示tcp相关选项
-u 仅显示udp相关选项
-n (numeric)拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服務状态
-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e (extend)显示扩展信息,例如uid等
-s (statistice)按各个协议进行统计
-c (continuous)每隔一个固定时间,执行该netstat命令。
三.使用实例:
列出所有端口 netstat -a
# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp localhost: *:* LISTEN
udp *:bootpc *:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix [ ACC ] STREAM LISTENING /tmp/.X11-unix/X0
unix [ ACC ] STREAM LISTENING //run/acpid.socket
列出所有 tcp 端口 netstat -at
# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp localhost: *:* LISTEN
tcp localhost:ipp *:* LISTEN
tcp *:smtp *:* LISTEN
tcp6 localhost:ipp [::]:* LISTEN
列出所有 udp 端口 netstat -au
# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp *:bootpc *:*
udp *: *:*
udp *:mdns *:*
只显示监听端口 netstat -l
# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp localhost:ipp *:* LISTEN
tcp6 localhost:ipp [::]:* LISTEN
udp *: *:*
只列出所有监听 tcp 端口 netstat -lt
# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp localhost: *:* LISTEN
tcp *:smtp *:* LISTEN
tcp6 localhost:ipp [::]:* LISTEN
只列出所有监听 udp 端口 netstat -lu
# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp *: *:*
udp *:mdns *:*
只列出所有监听 UNIX 端口 netstat -lx
# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix [ ACC ] STREAM LISTENING /maildrop
unix [ ACC ] STREAM LISTENING /cleanup
unix [ ACC ] STREAM LISTENING /ifmail
unix [ ACC ] STREAM LISTENING /bsmtp
显示所有端口的统计信息 netstat -s
# netstat -s
Ip:
total packets received
with invalid addresses
forwarded
incoming packets discarded
incoming packets delivered
requests sent
Icmp:
ICMP messages received
input ICMP message failed.
Tcp:
active connections openings
failed connection attempts
connection resets received
Udp:
packets received
packets to unknown port received.
.....
显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
# netstat -st
# netstat -su
netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp ramesh-laptop.loc: .:www CLOSE_WAIT /firefox
tcp ramesh-laptop.loc: lax:www ESTABLISHED /firefox
当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。
同样可以加速输出,因为不用进行比对查询。
# netstat -an
如果只是不想让这三个名称中的一个被显示,使用以下命令
# netsat -a --numeric-ports
# netsat -a --numeric-hosts
# netsat -a --numeric-users
netstat 将每隔一秒输出网络信息。
# netstat -c
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp ramesh-laptop.loc: ---.ama:www ESTABLISHED
tcp ramesh-laptop.loc: .:www CLOSING
tcp ramesh-laptop.loc: server----:www ESTABLISHED
tcp ramesh-laptop.loc: .:www CLOSING
^C
netstat --verbose
在输出的末尾,会有如下的信息
netstat: no support `AF IPXnetstat: no support `AF AX25netstat: no support `AF X25netstat: no support `AF NETROM
# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
. * . U eth3
link-local * . U eth3
. . UG eth3
注意: 使用 netstat -rn 显示数字格式,不查询主机名称。
并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
# netstat -ap | grep ssh
tcp dev-db:ssh .: CLOSE_WAIT -
tcp dev-db:ssh .: CLOSE_WAIT -
找出运行在指定端口的进程
# netstat -an | grep ':80'
# netstat -i
Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 BMU
eth3 BMRU
lo LRU
显示详细信息,像是 ifconfig 使用 netstat -ie:
# netstat -ie
Kernel Interface table
eth0 Link encap:Ethernet HWaddr :::::
UP BROADCAST MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: ( B) TX bytes: ( B)
Memory:f6ae0000-f6b00000
查看连接某服务端口最多的的IP地址
wss8848@ubuntu:~$ netstat -nat | grep |awk |awk -F: |sort|uniq -c|sort -nr|head -
.
.
.
.
.
.
.
.
.
.
TCP各种状态列表
wss8848@ubuntu:~$ netstat -nat |awk
established)
Foreign
LISTEN
TIME_WAIT
ESTABLISHED
TIME_WAIT
SYN_SENT
先把状态全都取出来,然后使用uniq -c统计,之后再进行排序。
wss8848@ubuntu:~$ netstat -nat |awk |sort|uniq -c
ESTABLISHED
FIN_WAIT1
Foreign
LAST_ACK
LISTEN
SYN_SENT
TIME_WAIT
established)
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。