文章目录

一、Netstat

Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字。

监听端口(列出所有端口,查看服务是否运行)

$ netstat -lntp         # 查看系统的监听端口(最常用)

二、Netstat 常用命令

$ netstat -a            # 列出所有连接
$ netstat -at # 列出 TCP协议的连接
$ netstat -au # 列出 UDP 协议的连接
$ netstat -ant # 禁用反向域名解析,加快查询速度(列出所有 TCP 协议的连接,禁用域名解析技术)
$ sudo netstat -nlpt # 获取进程名、进程号
$ sudo netstat -ltpe # 获取进程名、进程号以及用户 ID
$ netstat -s # netstat 可以打印出网络统计数据,包括某个协议下的收发包数量。
$ netstat -rn # 显示内核路由信息
$ netstat -i # 打印网络接口
$ netstat -ct # 持续输出
$ netstat -g # 输出 IPv4 和 IPv6 的多播组信息。
2.1 Linux 查看程序端口使用情况
netstat –apn                //查看所有的进程和端口使用情况
netstat -ntlp //查看当前所有tcp端口·
netstat -ntulp |grep 80 //查看所有80端口使用情况, kill -9 {PID}
netstat -an | grep 3306 //查看所有3306端口使用情况·


# 打印 active 状态的连接,使用watch 命令监视 active 状态的连接
$ netstat -atnp | grep ESTA
$ watch -d -n0 "netstat -atnp | grep ESTA"

# 查看服务是否在运行(指定服务名)
$ sudo netstat -aple | grep ntp
udp 0 0 enlightened.local:ntp *:* root 17430 1789/ntpd
udp 0 0 localhost:ntp *:* root 17429 1789/ntpd
udp 0 0 *:ntp *:* root 17422 1789/ntpd
udp6 0 0 fe80::216:36ff:fef8:ntp [::]:* root 17432 1789/ntpd
udp6 0 0 ip6-localhost:ntp [::]:* root 17431 1789/ntpd
udp6 0 0 [::]:ntp [::]:* root 17423 1789/ntpd
2.2 Linux 查看程序端口占用情况
ps -aux | grep tomcat      # 查看特定进程和端口使用情况
ps -aux | grep java # `ps -aux | grep pid`同效

netstat -nat | grep -iw "8080" | wc -l # 查看8080端口被占用的数量
netstat -nat | grep -iw "8080" # 当前8080端口被哪些进程占用
lsof -i:8080 # 查看8080端口PID、FD、USER等信息

关于杀死该进程请点击:

三、Netstat 命令详情

命令:​​netstat -a​​​,​​-t​​​列出 TCP 协议,​​-u​​ 列出 UDP 协议

$ netstat -a          # 列出所有连接
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 enlightened:domain *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 enlightened.local:54750 li240-5.members.li:http ESTABLISHED
tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN
udp 0 0 enlightened:domain *:*
udp 0 0 *:bootpc *:*
udp6 0 0 [::]:54952 [::]:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 12403 @/tmp/dbus-IDgfj3UGXX
unix 2 [ ACC ] STREAM LISTENING 40202 @/dbus-vfs-daemon/socket-6nUC6CCx

$ netstat -at # 列出 TCP协议的连接
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 enlightened:domain *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp 0 0 enlightened.local:37892 ABTS-North-Static-:http ESTABLISHED
.....

$ netstat -au # 列出 UDP 协议的连接
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:34660 *:*
udp 0 0 enlightened:domain *:*
udp 0 0 *:ntp *:*
udp6 0 0 fe80::216:36ff:fef8:ntp [::]:*
udp6 0 0 ip6-localhost:ntp [::]:*
udp6 0 0 [::]:ntp [::]:*


$ netstat -ant # 列出所有 TCP 协议的连接,没有使用域名解析技术
Active Internet connections (servers and established)

​-l​​​ : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protocol)与资料的程序)
​​​-p​​​ : 显示进程标识符和程序名称,每一个套接字/端口都属于一个程序。
​​​-n​​: 不进行DNS轮询,显示IP(可以加速操作)

$ sudo netstat -nlpt   # 获取进程名、进程号以及用户 ID
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1144/dnsmasq
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 661/cupsd
tcp6 0 0 ::1:631 :::* LISTEN 661/cupsd


$ sudo netstat -ltpe # 同时查看进程名、进程号和用户名。
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 enlightened:domain *:* LISTEN root 11090 1144/dnsmasq
tcp 0 0 localhost:ipp *:* LISTEN root 9755 661/cupsd
tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN root 9754 661/cupsd


$ netstat -s # netstat 可以打印出网络统计数据,包括某个协议下的收发包数量。
Ip:
32797 total packets received
0 forwarded
0 incoming packets discarded
32795 incoming packets delivered
29115 requests sent out
60 outgoing packets dropped
Icmp:
125 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:
destination unreachable: 125
125 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 125
... OUTPUT TRUNCATED ...


$ netstat -rn # 显示内核路由信息
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

$ 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 31611 0 0 0 27503 0 0 0 BMRU
lo 65536 0 2913 0 0 0 2913 0 0 0 LRU

$ netstat -ie/ifconfig
Kernel Interface table
eth0 Link encap:Ethernet HWaddr 00:16:36:f8:b2:64
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::216:36ff:fef8:b264/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:31682 errors:0 dropped:0 overruns:0 frame:0
TX packets:27573 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:29637117 (29.6 MB) TX bytes:4590583 (4.5 MB)
Interrupt:18 Memory:da000000-da020000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:2921 errors:0 dropped:0 overruns:0 frame:0
TX packets:2921 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:305297 (305.2 KB) TX bytes:305297 (305.2 KB)



$ netstat -ct # 持续输出
$ netstat -g # 输出 IPv4 和 IPv6 的多播组信息。