..

linux上网络问题排查工具

在linux上有非常多的工具,能在让你在排查问题多额时候获取有用的信息,这篇文件将会介绍一些network相关的troubleshot技巧。

ifconfig

ifconfig是linux上设置网卡信息的命令,他们启动网卡,禁止网卡,查看网卡的ip地址个mac地址等等.

# 查看所有网卡的信息
ifconfig
# 查看eth0网卡的信息
ifconfig eth0
# 设置 eth0 网卡的ip地址和子网掩码
ifconfig eth0 192.168.50.5 netmask 255.255.255.0
# 启用eth0网卡
ifup eth0
# 禁止eh0网卡
ifdown eth0
# 设置eth0网卡的最大通讯包大小
ifconfig eth0 mtu xxx

ping

ping是最常用的检查两个机器是否网络连通的方式,

ping 192.168.1.1
ping baidu.com

ping是基于ICMP协议,因为可以设置ICMP不响应,所以有时候这个方式不那么管用。 往/etc/sysctl.conf中加入net.ipv4.icmp_echo_ignore_all=1然后sysctl -p刷新就能禁止ping返回了,ping命令能够指定发送包的次数:

# 发送五次
ping -c 5 baidu.com

traceroute

traceroute是检查网卡中数据包的ip路由情况的

hellojukay@local:~/data/Code/github/blog$ traceroute baidu.com
traceroute to baidu.com (220.181.57.216), 30 hops max, 60 byte packets
 1  Hiwifi.lan (192.168.199.1)  0.393 ms  0.451 ms  0.515 ms
 2  192.168.1.1 (192.168.1.1)  1.730 ms  1.729 ms  1.740 ms
 3  222.131.232.1 (222.131.232.1)  31.347 ms  31.431 ms  31.564 ms
 4  61.51.246.93 (61.51.246.93)  4.611 ms  4.618 ms  4.662 ms
 5  61.51.169.117 (61.51.169.117)  30.587 ms  30.601 ms 61.51.169.85 (61.51.169.85)  30.295 ms
 6  202.96.12.33 (202.96.12.33)  30.314 ms  28.500 ms  27.731 ms
 7  219.158.4.154 (219.158.4.154)  28.739 ms 219.158.3.70 (219.158.3.70)  28.018 ms  28.102 ms
 8  219.158.44.110 (219.158.44.110)  27.593 ms  26.365 ms  28.378 ms
 9  202.97.57.121 (202.97.57.121)  33.824 ms 202.97.88.253 (202.97.88.253)  8.011 ms 202.97.88.241 (202.97.88.241)  5.855 ms
10  * 36.110.244.18 (36.110.244.18)  5.428 ms *
11  * * 218.30.112.125 (218.30.112.125)  5.261 ms
12  * * *
13  220.181.17.22 (220.181.17.22)  5.733 ms * 220.181.182.30 (220.181.182.30)  6.286 ms
14  * * *
15  * * *
16  * * *
17  * * *
18  * * *
19  * * *
20  * * *
21  * * *
22  * * *
23  * * *
24  * * *
25  * * *
26  * * *
27  * * *
28  * * *
29  * * *
30  * * *

netstat

netstat命令能够展示机器上网络链接信息,路由表信息

查看路由表信息

hellojukay@local:~/data/Code/github/blog$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         Hiwifi.lan      0.0.0.0         UG        0 0          0 enp30s0
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.199.0   0.0.0.0         255.255.255.0   U         0 0          0 enp30s0

查看tcp链接信息

hellojukay@local:~/data/Code/github/blog$ netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 local.lan:59736         185.225.12.35:26510     TIME_WAIT  
tcp        0      0 local.lan:38490         111.206.23.145:https    ESTABLISHED
tcp        0      0 local.lan:59712         185.225.12.35:26510     TIME_WAIT  
tcp        0      0 local.lan:59704         185.225.12.35:26510     TIME_WAIT  
tcp        0      0 local.lan:59780         185.225.12.35:26510     TIME_WAIT  
tcp        0      0 localhost:socks         localhost:51926         TIME_WAIT  
tcp        0      0 local.lan:59772         185.225.12.35:26510     TIME_WAIT  
tcp        0      0 local.lan:39646         192.0.73.2:https        ESTABLISHED
tcp      391      0 localhost:51998         localhost:socks         CLOSE_WAIT 
tcp        0      0 local.lan:51000         a104-78-89-18.dep:https ESTABLISHED
tcp        0      0 localhost:socks         localhost:51958         TIME_WAIT 

同理查看udp信息的命令是netstat -u,列出所有本地监听的tcp端口:

hellojukay@local:~$ netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:netbios-ssn     0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:socks         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:microsoft-ds    0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:netbios-ssn        [::]:*                  LISTEN     
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN     
tcp6       0      0 [::]:microsoft-ds       [::]:*                  LISTEN   s

这里只是展示了网络信息,如果还需要展示对应的进程信息,要查看更多的信息,往往需要使用sudo:

hellojukay@local:~$ sudo netstat -ltp
[sudo] hellojukay 的密码:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:netbios-ssn     0.0.0.0:*               LISTEN      3487/smbd           
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN      1895/cupsd          
tcp        0      0 localhost:socks         0.0.0.0:*               LISTEN      6228/shadowsocks-de 
tcp        0      0 0.0.0.0:microsoft-ds    0.0.0.0:*               LISTEN      3487/smbd           
tcp6       0      0 [::]:netbios-ssn        [::]:*                  LISTEN      3487/smbd           
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN      1895/cupsd          
tcp6       0      0 [::]:microsoft-ds       [::]:*                  LISTEN      3487/smbd  

如果需要持续观察网络信息,可以使用-c命令,指定刷新的时间比如:

# 每隔5秒钟展示一次tcp监听的信息
sudo netstat -tl -c 5

展示网卡信息

sudo netstat -i

dig

dig命令是用来查看DNS解析情况的命令,举例子:我们查看当前网络是怎么解析www.baidu.com

hellojukay@local:~$ dig www.baidu.com

; <<>> DiG 9.11.3-1-Debian <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52721
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		746	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	9	IN	A	61.135.169.125
www.a.shifen.com.	9	IN	A	61.135.169.121

;; Query time: 0 msec
;; SERVER: 192.168.199.1#53(192.168.199.1)
;; WHEN: Sat Dec 08 19:00:53 CST 2018
;; MSG SIZE  rcvd: 104

nslookup

nslookup也是用来解析DNS的命令:

hellojukay@local:~$ nslookup www.baidu.com
Server:		192.168.199.1
Address:	192.168.199.1#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 61.135.169.125
Name:	www.a.shifen.com
Address: 61.135.169.121

host

host命令是用来查询DNS信息

hellojukay@local:~$ host baidu.com
baidu.com has address 220.181.57.216
baidu.com has address 123.125.115.110
baidu.com mail is handled by 20 mx50.baidu.com.
baidu.com mail is handled by 20 jpmx.baidu.com.
baidu.com mail is handled by 20 mx1.baidu.com.
baidu.com mail is handled by 15 mx.n.shifen.com.
baidu.com mail is handled by 10 mx.maillb.baidu.com.

route

route命令是用来查看本地路由表的,当然也,它也能修改路由表信息

# 查看路由表
hellojukay@local:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         Hiwifi.lan      0.0.0.0         UG    100    0        0 enp30s0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.199.0   0.0.0.0         255.255.255.0   U     100    0        0 enp30s0

添加一条路由信息

route add -net 10.10.10.0/24 gw 192.168.0.1

删除一条路由信息

# route del -net 10.10.10.0/24 gw 192.168.0.1

添加默认网关

route add default gw 192.168.0.1

nc

nc是非常的hacks的网络工具,能够绑定本地端口,也能链接远程端口,并发送指定数据,我也经常使用nc ip port的方式来检查是否能够链接服务器的某个端口

将本地的文件发送到远程服务器的80端口

nc 192.168.1.199.177 80 < data.txt

本地本地端口,将接受的的数据重定向到本地文件

nc -l 8080 > data.txt

所有通过nc命令是可以传送文件的。

telnet

有时候我也是用telnet来检查网络是否可以链接

curl

curl http://ip:port -v

虽然不是http服务,但是我们也可以通过返回的错误信息,查看是否网络可以链接,

lsof

lsof也就是list stystem opened files,列出系统中打开的所有文件,因为在linux上一切都是文件,打开tcp端口也是打开文件,所以我要看程序打开了那些文件可以这样:

sudo lsof -p 7978

查看22端口被谁占用了

hellojukay@local:~$ sudo lsof -i:22
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    24816 root    3u  IPv4 593637      0t0  TCP *:ssh (LISTEN)
sshd    24816 root    4u  IPv6 593639      0t0  TCP *:ssh (LISTEN)
hellojukay@local:~$