后台开发中netstat命令使用方法

后台开发,netstat命令总是绕不过,不仅工作中经常用过,面试也是考的多。netstat命令,对应的选项比较多,功能比较强大。netstat 常用来查看,后台服务进程的相关状态。
netstat 可以用来打印网络连接、路由表、连接的数据统计。下面我们来学习一下。
打印所有连接使用 -a,列出所有连接。
[root@vm-16-9-centos ~]# netstat -aactive internet connections (servers and established)proto recv-q send-q local address foreign address state tcp 0 0 0.0.0.0:ssh 0.0.0.0:* listen tcp 0 0 vm-16-9-centos:49326 169.254.0.55:lsi-bobcat establishedtcp 0 52 vm-16-9-centos:ssh 119.137.1.7:20029 establishedtcp 0 0 vm-16-9-centos:ssh 113.110.224.255:63626 establishedtcp6 0 0 [::]:mysql [::]:* listen udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 vm-16-9-centos:ntp 0.0.0.0:* udp 0 0 vm-16-9-centos:ntp 0.0.0.0:* udp 0 0 0.0.0.0:38064 0.0.0.0:* udp6 0 0 vm-16-9-centos:ntp [::]:* udp6 0 0 vm-16-9-centos:ntp [::]:*打印 tcp 或 udp 连接使用 -t 选项列出 tcp 协议的连接。
[root@vm-16-9-centos ~]# netstat -tactive internet connections (w/o servers)proto recv-q send-q local address foreign address state tcp 0 0 vm-16-9-centos:49326 169.254.0.55:lsi-bobcat establishedtcp 0 52 vm-16-9-centos:ssh 119.137.1.7:20029 establishedtcp 0 0 vm-16-9-centos:ssh 113.110.224.255:63626 establishedtcp 0 0 vm-16-9-centos:ssh vps-d395feee.vps.:39128 established使用 -u 选项列出 udp 协议的连接。
[root@vm-16-9-centos ~]# netstat -uactive internet connections (w/o servers)proto recv-q send-q local address foreign address state禁用反向域名解析使用 -n 选项禁用域名解析功能,这样可以加快查找速度。
[root@vm-16-9-centos ~]# netstat -nactive internet connections (w/o servers)proto recv-q send-q local address foreign address state tcp 0 0 172.17.16.9:49326 169.254.0.55:5574 establishedtcp 0 52 172.17.16.9:22 119.137.1.7:20029 establishedtcp 0 0 172.17.16.9:22 113.110.224.255:63626 establishedtcp 0 0 172.17.16.9:52236 169.254.0.4:80 time_wait显示对应的程序识别码和程序名称使用 -p 选项列出正在监听的套接字。
[root@vm-16-9-centos ~]# netstat -pactive internet connections (w/o servers)proto recv-q send-q local address foreign address state pid/program name tcp 0 0 vm-16-9-centos:49326 169.254.0.55:lsi-bobcat established 1400/ydservice tcp 0 52 vm-16-9-centos:ssh 119.137.1.7:20029 established 22131/sshd: root@pt tcp 0 0 vm-16-9-centos:ssh host-188-14-121-2:55529 time_wait - tcp 0 0 vm-16-9-centos:ssh 113.110.224.255:63626 established 32201/sshd: root@pt打印监听中的连接使用 -l 选项列出正在监听的套接字。
[root@vm-16-9-centos ~]# netstat -lactive internet connections (only servers)proto recv-q send-q local address foreign address state tcp 0 0 0.0.0.0:ssh 0.0.0.0:* listen tcp6 0 0 [::]:mysql [::]:* listen udp 0 0 0.0.0.0:bootpc 0.0.0.0:*打印连接进程信息使用 -p 选项查看进程信息。
[root@vm-16-9-centos ~]# netstat -pactive internet connections (w/o servers)proto recv-q send-q local address foreign address state pid/program name tcp 0 0 vm-16-9-centos:49326 169.254.0.55:lsi-bobcat established 1400/ydservice tcp 0 0 vm-16-9-centos:48680 169.254.0.55:webcache time_wait - tcp 0 52 vm-16-9-centos:ssh 119.137.1.7:20029 established 22131/sshd: root@pt tcp 0 0 vm-16-9-centos:ssh 113.110.224.255:63626 established 32201/sshd: root@pt tcp 0 0 vm-16-9-centos:ssh host-188-14-121-2:56025 established 10384/sshd: unknown打印网络工作信息统计表使用 -s 选项查看进程信息(只展示部分结果)。
[root@vm-16-9-centos ~]# netstat -sip: 7502855 total packets received 0 forwarded 0 incoming packets discarded 7502837 incoming packets delivered 7624338 requests sent out 40 dropped because of missing routeicmp: 938052 icmp messages received 147 input icmp message failed. icmp input histogram: destination unreachable: 411 timeout in transit: 8 redirects: 2 echo requests: 937623 echo replies: 7 timestamp request: 1 946258 icmp messages sent 0 icmp messages failed icmp output histogram: destination unreachable: 8634 echo replies: 937623 timestamp replies: 1打印网络接口使用 -i 选项查看进程信息。
[root@vm-16-9-centos ~]# netstat -ikernel interface tableiface mtu rx-ok rx-err rx-drp rx-ovr tx-ok tx-err tx-drp tx-ovr flgeth0 1500 7825094 0 0 0 7712988 0 0 0 bmrulo 65536 10230 0 0 0 10230 0 0 0 lru持续输出信息使用 -c 选项查看进程信息。
[root@vm-16-9-centos ~]# netstat -cactive internet connections (w/o servers)proto recv-q send-q local address foreign address state tcp 0 0 vm-16-9-centos:49326 169.254.0.55:lsi-bobcat establishedtcp 0 52 vm-16-9-centos:ssh 119.137.1.7:20029 establishedtcp 0 0 vm-16-9-centos:ssh vps-d395feee.vps.:39442 establishedtcp 0 0 vm-16-9-centos:ssh 113.110.224.255:63626 establishedtcp 0 0 vm-16-9-centos:52660 169.254.0.4:http time_wait tcp 0 0 vm-16-9-centos:ssh b3d278e1.virtua.c:35113 time_wait tcp 0 0 vm-16-9-centos:ssh 58.230.147.230:56693 time_wait active unix domain sockets (w/o servers)proto refcnt flags type state i-node pathunix 3 [ ] dgram 7444 /run/systemd/notify根据进程pid查端口利用 grep 命令,通过进程 pid 查出监听端口。
[root@vm-16-9-centos test]# netstat -nap|grep 12178tcp6 0 0 :::8888 :::* listen 12178/gin根据端口查进程利用 grep 命令,通过监听端口查出进程 pid 。
[root@vm-16-9-centos test]# netstat -nap|grep 8888tcp6 0 0 :::8888 :::* listen 12178/gin统计网络socket状态借助 awk 命令,统计socket状态对应的个数。
[test]# netstat -n | awk '/^tcp/ {++s[$nf]} end {for(a in s) print a, s[a]}' established 5time_wait 2

在pcb板上镀金和镀银有区别吗
Synaptics推出电容式触摸屏解决方案
5G会让物联网成为新的风口吗
Keithley2450数字源表
Microchip推出全新的MPLAB REAL ICE功耗监测模块
后台开发中netstat命令使用方法
堪比影院级别的回音壁你见过吗?索尼HT-X9000F回音壁了解一下
定时器/计数器方式1的应用编写程序
详谈触摸式调光台灯的智能化控制技术
2023 IOTE展|泰凌微电子最新产品和技术等您来探索!打卡有礼!
2017全球最佳性能手机,华为荣耀V9是唯一国产手机
谈谈XR关键技术及VR/AR/MR/XR关系
基于3nm支持的A17处理器值得期待吗
无线电波的接收原理
LBB315PA-020位移传感器使用方法
C语言编程10大规范
“柔性时代”来临:柔性可穿戴传感器正向产业化方向发展
七夕礼物怎么选择?推荐这几款运动蓝牙耳机
电动汽车成为新能源的主流的趋势明显
2018年Q2德国线上市场手机品牌排名:华为、荣耀手机突破三星独霸局面