linux 网络学习问题命令总结

  1. ifconfig:首先查看网络ip,通过ifconfig可以查看到enp0s3,enp0s8这个网卡
[root@localhost vagrant]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::a00:27ff:fede:e0e  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:de:0e:0e  txqueuelen 1000  (Ethernet)
        RX packets 5025  bytes 1868795 (1.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4170  bytes 397055 (387.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.106  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe3f:af4e  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:3f:af:4e  txqueuelen 1000  (Ethernet)
        RX packets 161  bytes 19372 (18.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17  bytes 1306 (1.2 KiB)
        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 0  (Local Loopback)
        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
  1. ping:ping使用了ICMP回送请求和回送应答报文。ping工具发出去的数据包没有通过tcp/udp协议,但是要经过ip协议。ping命令计算的时间是数据包的往返总时间。
[ci@localhost commonlib]$ ping  www.baidu.com
PING www.a.shifen.com (119.75.217.26) 56(84) bytes of data.
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=1 ttl=53 time=2.67 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=2 ttl=53 time=2.65 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=3 ttl=53 time=2.61 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=4 ttl=53 time=2.63 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=5 ttl=53 time=2.68 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=6 ttl=53 time=2.62 ms
  1. traceroute: traceroute路由跟踪,首先它发送一份TTL字段为1的IP数据包给目的主机,处理这个数据包的第一个路由器将TTL值减1,然后丢弃该数据报,并给源主机发送一个ICMP报文(“超时”信息,这个报文包含了路由器的IP地址,这样就得到了第一个路由器的地址),然后traceroute发送一个TTL为2的数据报来得到第二个路由器的IP地址,继续这个过程,直至这个数据报到达目的主机。
[ci@localhost commonlib]$  traceroute www.baidu.com
traceroute to www.baidu.com (119.75.217.26), 30 hops max, 60 byte packets
 1  10.151.30.253 (10.151.30.253)  2.560 ms  2.928 ms  3.293 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *

以星号表示的。出现这样的情况,可能是防火墙封掉了ICMP的返回信息,所以我们得不到什么相关的数据包返回数据。

  1. mtr:报告模式展示实时数据,结合ping和traceroute
[root@localhost vagrant]# mtr 140.143.234.183
localhost.localdomain (0.0.0.0)                                                                                                                                                            Thu Jan 17 11:42:47 2019
Keys:  Help   Display mode   Restart statistics   Order of fields   quit
                                                                                                                                                                           Packets               Pings
 Host                                                                                                                                                                    Loss%   Snt   Last   Avg  Best  Wrst StDev
 1. 10.0.2.2                                                                                                                                                              0.0%    47    0.3   0.2   0.0   0.6   0.0
 2. 172.18.162.3                                                                                                                                                          0.0%    47    2.7   4.9   1.6  58.6   9.0
 3. ???
 4. ???
 5. 223x223x224x6.omn.ne.jp                                                                                                                                               2.1%    47    9.1   6.9   3.6  39.5   5.5
 6. 103.216.40.18                                                                                                                                                         0.0%    47   11.4   9.4   3.7  80.6  12.6
 7. 10.196.5.233                                                                                                                                                          0.0%    47    8.7   7.6   4.3  27.2   4.0
 8. 10.200.150.222                                                                                                                                                       38.3%    47   27.3  25.3  11.5 156.3  26.1
 9. 10.200.150.206                                                                                                                                                        0.0%    47   23.0  29.0   7.2 103.7  19.0
10. 100.120.219.235                                                                                                                                                       0.0%    46   32.2  10.5   5.5  60.6   9.0
11. 140.143.234.183                                                                                                                                                       0.0%    46    5.9   9.4   4.0  94.6  14.9
  1. netstat -rn ,route -n 查看网卡配置
[root@localhost vagrant]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG        0 0          0 enp0s3
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 enp0s8
  • Destination为目标地址
  • Gateway 网关地址
  • Genmask 子网掩码

简单说下子网掩码计算规则,还有一种10.0.2.0/24的方式表示,相当于24为子网掩码,在ip4中一共32位,所以相当于255.255.255.0,前24为1,后8位位0,此时看到上边的路由规则:

  • 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s8 表示ip段为192.168.1.0~192.168.1.255的目标地址,会采用0.0.0.0,0.0.0.0表示本`络。规则就是做与运算,可以看出192.168.1.*^255.255.255.0=192.168.1.0,正好匹配。
  • 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 enp0s3 表示其他未匹配到的ip都会走10.0.2.2网关出口,如上计算任何ip都会^ 0.0.0.0 都是 0.0.0.0 ,所以此规则为默认规则。
    子网掩码计算可以通过地址查看https://www.sojson.com/convert/subnetmask.html,如下图:
    5397939-bd4de881a3afe139.png
    image.png

可以明显看出,运算规则ip与子网掩码进行与运算,得到匹配网络地址。

  1. ip route show:展示路由配置规则
[root@localhost vagrant]# ip route show
default via 10.0.2.2 dev enp0s3 proto dhcp metric 100 
192.168.1.0/24 dev enp0s8 proto kernel scope link src 192.168.1.106 metric 101 

default via 10.0.2.2 dev enp0s3 proto dhcp metric 100清楚点表示默认的路由规则就是10.0.2.2

  1. nslookup :查看dns地址
[ci@localhost ~]$ nslookup www.baidu.com
//dns地址
Server:     10.151.30.200
Address:    10.151.30.200#53

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

  1. arping -I enp0s8 10.151.30.108 查看对方网卡是否有回复
  1. vim /etc/resolv.conf 配置dns

  2. /etc/sysconfig/network/network-scripts/ifcfg-eth* 配置服务器ip的配置文件

猜你喜欢

转载自blog.csdn.net/weixin_34344677/article/details/86899270