Netstat命令显示各种与网络相关的信息,例如网络连接,路由表,接口统计信息,伪装连接,多播成员身份等,
在本文中,让我们回顾10个实用的unix netstat命令 例子。
1.列出所有端口(侦听和非侦听端口)
使用netstat -a列出所有端口
# netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN udp 0 0 *:bootpc *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0 unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket
使用netstat -at列出所有tcp端口
# netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN
使用netstat -au列出所有udp端口
# netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:bootpc *:* udp 0 0 *:49119 *:* udp 0 0 *:mdns *:*
2.处于监听状态的列表套接字
使用netstat -l仅列出侦听端口
# netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:ipp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN udp 0 0 *:49119 *:*
使用netstat -lt仅列出正在侦听的TCP端口
# netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN
使用netstat -lu仅列出侦听的UDP端口
# netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:49119 *:* udp 0 0 *:mdns *:*
使用netstat -lx仅列出侦听的UNIX端口
# netstat -lx Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6294 private/maildrop unix 2 [ ACC ] STREAM LISTENING 6203 public/cleanup unix 2 [ ACC ] STREAM LISTENING 6302 private/ifmail unix 2 [ ACC ] STREAM LISTENING 6306 private/bsmtp
3.显示每个协议的统计信息
使用netstat -s显示所有端口的统计信息
# netstat -s Ip: 11150 total packets received 1 with invalid addresses 0 forwarded 0 incoming packets discarded 11149 incoming packets delivered 11635 requests sent out Icmp: 0 ICMP messages received 0 input ICMP message failed. Tcp: 582 active connections openings 2 failed connection attempts 25 connection resets received Udp: 1183 packets received 4 packets to unknown port received. .....
使用netstat -st(或)-su显示TCP(或)UDP端口的统计信息
# netstat -st # netstat -su
4.使用netstat -p在netstat输出中显示PID和程序名称
netstat -p选项可以与任何其他netstat选项结合使用。这将添加“PID/Program 名称”到netstat输出。这在调试以确定特定端口上正在运行的程序时非常有用。
# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 1 0 ramesh-laptop.loc:47212 192.168.185.75:www CLOSE_WAIT 2109/firefox tcp 0 0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox
5.唐’在netstat输出中解析主机,端口和用户名
当你不穿’如果要显示主机,端口或用户的名称,请使用netstat -n选项。这将以数字显示,而不是解析主机名,端口名,用户名。
由于netstat不执行任何查找,因此这也加快了输出速度。
# netstat -an
如果你不这样做’如果只想解决这三个项目(端口,主机或用户)中的任何一项,请使用以下命令。
# netsat -a --numeric-ports # netsat -a --numeric-hosts # netsat -a --numeric-users
6.连续打印netstat信息
netstat将每隔几秒钟连续打印一次信息。
# netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:52564 101.11.169.230:www CLOSING tcp 0 0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:42367 101.101.34.101:www CLOSING ^C
7.查找系统中不支持的地址族
netstat --verbose
最后,您将获得类似的内容。
netstat: no support for `AF IPX' 上 this system. netstat: no support for `AF AX25' 上 this system. netstat: no support for `AF X25' 上 this system. netstat: no support for `AF NETROM' 上 this system.
8.使用netstat -r显示内核路由信息
# netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 eth2 link-local * 255.255.0.0 U 0 0 0 eth2 default 192.168.1.1 0.0.0.0 UG 0 0 0 eth2
注意: 使用netstat -rn以数字格式显示路由,而无需解析主机名。
9.找出程序在哪个端口上运行
# netstat -ap | grep ssh (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 1 0 dev-db:ssh 101.174.100.22:39213 CLOSE_WAIT - tcp 1 0 dev-db:ssh 101.174.100.22:57643 CLOSE_WAIT -
找出哪个进程正在使用特定端口:
# netstat -an | grep ':80'
10.显示网络接口列表
# 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 1500 0 0 0 0 0 0 0 0 0 BMU eth2 1500 0 26196 0 0 0 26883 6 0 0 BMRU lo 16436 0 4 0 0 0 4 0 0 0 LRU
使用netstat -ie在接口上显示扩展信息(类似于ifconfig):
# netstat -ie Kernel Interface table eth0 链接 encap:Ethernet HWaddr 00:10:40:11:11:11 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX 通过tes:0 (0.0 B) TX 通过tes:0 (0.0 B) Memory:f6ae0000-f6b00000
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
我最喜欢的netstat组合是sudo netstat -pnutl
它需要sudo来查看所有应用程序名称,但是没有它就可以工作,但是显示的信息较少