ping命令用于测试两台主机之间是否可以通信,一般情况下会使用ping www.baidu.com来测试网络连通性,如果不指定发送包的个数默认是一直发送数据包,可以使用Ctrl+C停止。网络联通时就会收到回复,只要出现的不是以下信息就要根据具体的情况进行排错。
ping原理:ping命令使用的是ICMP协议,ICMP协议中目的主机必须返回ICMP应答消息给源主机,也就是说如果目的主机收到了消息,那么会发回一个应答消息,此时便可以认为两台主机之间可以通信。
- root@ubuntu:~# ping www.baidu.com
- PING www.a.shifen.com (36.152.44.96) 56(84) bytes of data.
- 64 bytes from 36.152.44.96 (36.152.44.96): icmp_seq=1 ttl=128 time=13.8 ms
- 64 bytes from 36.152.44.96 (36.152.44.96): icmp_seq=2 ttl=128 time=20.2 ms
- 64 bytes from 36.152.44.96 (36.152.44.96): icmp_seq=3 ttl=128 time=23.7 ms
- 64 bytes from 36.152.44.96 (36.152.44.96): icmp_seq=4 ttl=128 time=21.8 ms
- ^C
- --- www.a.shifen.com ping statistics ---
- 4 packets transmitted, 4 received, 0% packet loss, time 3007ms
- rtt min/avg/max/mdev = 13.797/19.878/23.738/3.725 ms
-
ifconfig命令用于查看网卡配置和网络状态相关信息。ifconfig命令是net-tools工具包下的一个命令,如果发现没有该命令需要先进行安装:
- root@ubuntu:~# ifconfig
- Command 'ifconfig' not found, but can be installed with:
- apt install net-tools
- root@ubuntu:~# sudo apt-get install net-tools
- Reading package lists... Done
- Building dependency tree... Done
- Reading state information... Done
- ......
- ......
- Unpacking net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
- Setting up net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
- Processing triggers for man-db (2.10.2-1) ...
- root@ubuntu:~#
-
安装完成之后就可以使用ifconfig命令了,可以看到有两个网卡,一个网卡是enp0s5,另一个网卡是lo,lo网卡是本地回环口,也就是127.0.0.1,在enp0s5网卡里面可以看到IPv4地址:inet、子网掩码:netmask、MAC地址:ether、接收数据包的相关信息:RX、发送数据包的相关信息:TX
- root@ubuntu:~# ifconfig
- enp0s5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
- inet 10.211.55.5 netmask 255.255.255.0 broadcast 10.211.55.255
- inet6 fe80::21c:42ff:fece:f29b prefixlen 64 scopeid 0x20<link>
- inet6 fdb2:2c26:f4e4:0:21c:42ff:fece:f29b prefixlen 64 scopeid 0x0<global>
- ether 00:1c:42:ce:f2:9b txqueuelen 1000 (Ethernet)
- RX packets 131844 bytes 193152579 (193.1 MB)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 48018 bytes 2733699 (2.7 MB)
- 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 (Local Loopback)
- RX packets 634 bytes 53200 (53.2 KB)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 634 bytes 53200 (53.2 KB)
- TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-
ifconfig命令还可以用于用于开始或者关闭指定的网卡,格式为ifconfig 网卡名 [up/down]。
- root@ubuntu:~# ifconfig enp0s5 down
- root@ubuntu:~# ifconfig enp0s5 up
-
除此以外还可以使用ifconfig命令配置IP地址,格式为ifconfig 网卡名 IP地址 子网掩码。
traceroute命令用来检测主机与目标主机之间经过的网关的数量,一般用于网络故障排查,使用命令之前如果,没有则需要先使用sudo apt-get install traceroute进行安装。
每一个序号表示经过一个网关,如果出现*号可能是防火墙禁用了ICMP的返回信息。
- root@ubuntu:~# traceroute www.baidu.com
- traceroute to www.baidu.com (36.152.44.96), 30 hops max, 60 byte packets
- 1 * * *
- 2 * * *
- 3 * * *
- 4 * * *
- 5 * * *
- 6 * * *
- 7 * * *
- 8 * * *
- 9 * * *
- 10 * * *
- 11 * * *
- 12 * * *
- 13 * * *
- 14 * * *
- 15 * * *
- 16 * * *
- 17 * * *
- 18 * * *
- 19 * * *
- 20 * * *
- 21 * * *
- 22 * * *
- 23 * * *
- 24 * * *
- 25 * * *
- 26 * * *
- 27 * * *
- 28 * * *
- 29 * * *
- 30 * * *
-
netstat命令用于显示网络相关的各种信息,一般使用netstat -ano和netstat -nt命令。
netstat -ano命令查看所有的网络连接情况,netstat -nt命令用于查看当前的网络连接情况:
wget是Linux中的一个下载文件的工具。使用格式为wget [可选参数] URL地址
可选参数:
-c断点续传
-P下载到指定目录
相对于网卡配置文件的修改的繁琐,使用nmtui配置网络是一个不错的选择。
第一个选项用于配置网卡相关信息
第二个选项用于控制网络的开关
第三个选项用于修改主机名