Linux网络故障排查命令(ifconfig、ping、telnet、netstat、lsof、nc、curl、tcpdump)

ifconfig

该命令用来查看当前系统的网卡和IP地址信息

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ ifconfig
enp1s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether b4:a9:fc:80:03:03  txqueuelen 1000  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (本地环回)
        RX packets 15831  bytes 3201620 (3.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15831  bytes 3201620 (3.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.109  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::c1dc:2b2b:b4d4:d4bd  prefixlen 64  scopeid 0x20<link>
        ether 4c:eb:bd:0c:f8:f5  txqueuelen 1000  (以太网)
        RX packets 4486  bytes 2191259 (2.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2383  bytes 643251 (643.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我们来解释一下上面的输出信息:
以上显示了
本机当前激活的网卡信息:如lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
每个激活的网卡的IPv4、IPv6地址:如inet 127.0.0.1 、inet6 fe80::c1dc:2b2b:b4d4:d4bd
子网掩码:如netmask 255.0.0.0
广播地址:如broadcast 192.168.1.255

-s,显示网卡信息的精简列表

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
enp1s0    1500        0      0      0 0             0      0      0      0 BMU
lo       65536   113980      0      0 0        113980      0      0      0 LRU
wlp2s0    1500    10199      0      0 0          4491      0      0      0 BMRU

-a、up、down

ifconfig只会显示激活的网卡信息,-a选项可以显示所有网卡信息。

ifconfig Iface up	# 激活名为Iface的网卡
ifconfig Iface down	# 禁用名为Iface的网卡

将IP地址绑定到某个网卡,以及解绑操作

# 将指定ip地址绑定到某个网卡上
ifconfig 网卡名 add IP地址
# 从某个网卡解绑指定的IP地址
ifconfig 网卡名 del IP地址

ping

ping命令一般用于侦测本机到目标主机的网络是否畅通。

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ ping 120.55.94.78
PING 120.55.94.78 (120.55.94.78) 56(84) bytes of data.
64 比特,来自 120.55.94.78: icmp_seq=1 ttl=55 时间=9.87 毫秒
64 比特,来自 120.55.94.78: icmp_seq=2 ttl=55 时间=6.61 毫秒
64 比特,来自 120.55.94.78: icmp_seq=3 ttl=55 时间=6.93 毫秒
64 比特,来自 120.55.94.78: icmp_seq=4 ttl=55 时间=10.4 毫秒
64 比特,来自 120.55.94.78: icmp_seq=5 ttl=55 时间=6.67 毫秒
64 比特,来自 120.55.94.78: icmp_seq=6 ttl=55 时间=6.96 毫秒
64 比特,来自 120.55.94.78: icmp_seq=7 ttl=55 时间=6.97 毫秒
64 比特,来自 120.55.94.78: icmp_seq=8 ttl=55 时间=6.96 毫秒
64 比特,来自 120.55.94.78: icmp_seq=9 ttl=55 时间=6.94 毫秒
64 比特,来自 120.55.94.78: icmp_seq=10 ttl=55 时间=6.93 毫秒
64 比特,来自 120.55.94.78: icmp_seq=11 ttl=55 时间=6.98 毫秒
64 比特,来自 120.55.94.78: icmp_seq=12 ttl=55 时间=6.92 毫秒
64 比特,来自 120.55.94.78: icmp_seq=13 ttl=55 时间=6.59 毫秒
64 比特,来自 120.55.94.78: icmp_seq=14 ttl=55 时间=6.89 毫秒
64 比特,来自 120.55.94.78: icmp_seq=15 ttl=55 时间=6.97 毫秒
64 比特,来自 120.55.94.78: icmp_seq=16 ttl=55 时间=8.31 毫秒
64 比特,来自 120.55.94.78: icmp_seq=17 ttl=55 时间=7.80 毫秒
64 比特,来自 120.55.94.78: icmp_seq=18 ttl=55 时间=6.95 毫秒
64 比特,来自 120.55.94.78: icmp_seq=19 ttl=55 时间=10.3 毫秒
64 比特,来自 120.55.94.78: icmp_seq=20 ttl=55 时间=10.2 毫秒
64 比特,来自 120.55.94.78: icmp_seq=21 ttl=55 时间=9.88 毫秒
^C
--- 120.55.94.78 ping 统计 ---
已发送 21 个包, 已接收 21 个包, 0% 包丢失, 耗时 20028 毫秒
rtt min/avg/max/mdev = 6.587/7.762/10.386/1.374 ms

上面输出显示了目标网络可达。在linux上,如果目标网络不可达,则使用ping命令会一直发送而无输出结果,如:

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ ping 120.55.94.79
PING 120.55.94.79 (120.55.94.79) 56(84) bytes of data.
^C
--- 120.55.94.79 ping 统计 ---
已发送 5 个包, 已接收 0 个包, 100% 包丢失, 耗时 4080 毫秒

ping的目标也可以是个域名:

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ ping https://www.baidu.com/
ping: https://www.baidu.com/: 未知的名称或服务
dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ ping www.baidu.com
PING www.a.shifen.com (36.152.44.95) 56(84) bytes of data.
64 比特,来自 36.152.44.95 (36.152.44.95): icmp_seq=1 ttl=56 时间=32.5 毫秒
64 比特,来自 36.152.44.95 (36.152.44.95): icmp_seq=2 ttl=56 时间=11.9 毫秒
64 比特,来自 36.152.44.95 (36.152.44.95): icmp_seq=3 ttl=56 时间=10.4 毫秒
^C
--- www.a.shifen.com ping 统计 ---
已发送 3 个包, 已接收 3 个包, 0% 包丢失, 耗时 2003 毫秒
rtt min/avg/max/mdev = 10.444/18.269/32.466/10.056 ms

ping命令是通过发送ICMP数据包实现的。

telnet

如果一个服务程序对外开启了监听,我们可以使用telnet ip port的方式连接上去:
Web服务器默认的端口号是80,所以我们可以连接成功

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ telnet www.baidu.com 80
Trying 36.152.44.95...
Connected to www.a.shifen.com.
Escape character is '^]'.

也就是说我们可以通过telnet命令检测指定IP地址和端口号的监听服务是否存在
为了检测一个服务是否正常对外服务,一般先用ping检测是否能到达该IP地址,然后再用telnet检测是否能连接上去。

netstat

该命令经常用于查看网络连接状态
下面是它的一些参数:

-a:显示所有选项
-t:仅显示tcp相关选项
-u:仅显示udp相关选项
-n:不显示别名
-l:仅列出处于监听状态下的服务
-p:显示建立相关链接的程序名
-r:显示路由信息、路由表
-e:显示扩展信息,如uid
-s:按各个协议进行统计
-c:每隔一段事件执行netstat

对于一些有别名的IP地址和端口号,如127.0.0.1的别名是localhost、80的端口会被显示成http,加上n之后就只会显示数字了:
在这里插入图片描述

lsof

lsof(list opened filedescriptor):列出已经打开的文件描述符
一般来说,lsof的输出较多,可以使用grep命令来过滤如:

lsof -i | grep xxx

在这里插入图片描述
也可以使用-p来过滤出指定pid的进程

lsof -p pid

nc

nc即netcat命令,常用语模拟一个服务器程序被其他客户端连接,或者模拟一个客户端连接其他服务器,连接之后就可以进行数据收发。默认使用TCP,加上-u选项使用的就是UDP

模拟一个服务器程序和客户端程序进行通信

使用-l,在某个ip地址和端口上开启一个监听服务,以便让其他客户端连接,为了显示详细信息,可以加上-v

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ nc -v -l 0.0.0.0 6000
Listening on 0.0.0.0 6000

启动了一个监听服务器,监听IP地址为0.0.0.0,这样机器就可以被外网访问了,监听端口为6000
模拟客户端程序不需要加-l,直接写上IP地址和端口号即可:

在这里插入图片描述

发送文件

这里接受文件方是服务器,发送文件方是客户端。
服务端,接受命令

nc -l IP地址 端口号 > 接受后存储的文件名

客户端,发送命令

nc IP地址 端口号 < 发送的文件名

使用的时候先开服务端的监听再开客户端。

curl

用法:

curl 某个url
curl http://www.baidu.com

在这里插入图片描述
这样的结果就是将目标网页的内容输出到shell窗口上,当然也可以保存:

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ curl http://www.baidu.com > ./Desktop/testBaidu.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  58073      0 --:--:-- --:--:-- --:--:-- 58073

HTTP常用的请求方式是GET和POST,我们可以使用-X选项显式指定方式,不指定的时候默认是GET方式:

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ curl -X GET http://www.baidu.com/index.php?s=9
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

dyy@dyy-Lenovo-ThinkBook-14-IIL:~$ curl -X POST -d 'somepostdata' 'https://www.somesite.com/api/vl/chat'
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>openresty/1.13.6.1</center>
</body>
</html>

tcpdump

参数

tcpdump是一个linux上的抓包工具,在sudo权限下使用
常用命令选项如下:
1、-i:指定要捕获的目标网卡名字,网卡名可以使用ifconfig获得,如果要抓取所有网卡上的包,使用any关键字:

## 抓取网卡lo上的包
tcpdump -i lo
## 抓取所有网卡上的包
tcpdump -i any

2、-X:以ASCII和十六进制形式输出捕获的数据包内容,减去链路层的包头信息;
-XX:以ASCII和十六进制形式输出捕获的数据包内容,包括链路层的包头信息;
3、-n:不要将IP地址显示成别名
-nn:不要将IP地址和端口显示成别名
4、-S:以绝对值显示包的ISN(包序列号),默认以上一个包的偏移量显示
5、-vv-vvv:显示详细抓包数据
6、-w:将抓取的包的原始信息,写入文件中

tcpdump -i any -w filename

7、-r:从保存的文件包中读取数据包信息

tcpdump命令还支持各种数据包过滤的表达式:

## 仅仅显示经过8888端口的数据包,包括tcp和udp的8888
tcpdump -i any 'port 8888'
## 仅仅显示经过tcp 8888 渡口的数据包
tcpdump -i any 'tp port 8888'
## 仅仅显示源端口是tcp 8888的数据包
tcpdump -i any 'tcp src port 8888'
## 仅仅显示源端口tcp8888或者目标端口是udp 8888的数据包
tcpdump -i any 'tcp src port 8888 or udp dst port 8888'

连接一个正常的监听端口

创建一个服务器监听程序:

nc -v -l 127.0.0.1 12345

在另外一个窗口,对12345端口进行抓包

 tcpdump -i any 'port 12345' -XSX -nn -vv

在另外一个窗口创建一个客户端发送数据:

nc -v 127.0.0.1 12345

在这里插入图片描述
在没有发送数据之前,有三个包:
在这里插入图片描述
就是TCP连接的三次握手数据包。三次握手的过程:
客户端先向服务器发送一个SYN;
服务器应答一个SYN+ACK,应答的序列号递增1
客户端应答一个ACK。
我们可以关注一下上面的
Flags:
S -> S. ->.
seq 和 ack:
1737383646
->
2670654383, 1737383647
->
1737383647, 2670654384
显然是正确的

猜你喜欢

转载自blog.csdn.net/qq_42604176/article/details/121063032