14Linux之网络管理

14Linux之网络管理

14 网络管理

14.1 网卡名字

  • ens33命名格式

    en:ethernet  
    wl:wlan		 		# 无线局域网
    ww:wwan		 		# 无线广域网
        
    名称类型:
    o<index>	 		# 集成设备的设备索引号
    s<slot>		 		# 扩展槽对的索引号
    x<MAC>		 		# 基于MAC地址的命名
    p<bus>s<slot>		# 基于总线及槽的拓扑结构进行命名
    
  • 配置回归传统命名方式

    # 修改网卡配置文件名称
    [root@ccc ~]# cd /etc/sysconfig/network-scripts/
    [root@ccc network-scripts]# mv ifcfg-ens33 ifcfg-eth0
    
    # 修改网卡配置文件设备名称
    [root@ccc network-scripts]# sed -i "s@ens33@eth0@g" ifcfg-eth0   
    
    # GRUB添加kernel参数
    [root@ccc network-scripts]# vim /etc/sysconfig/grub 
    [root@ccc network-scripts]# tail -2 /etc/sysconfig/grub | head -1
    GRUB_CMDLINE_LINUX="spectre_v2=retpoline rhgb quiet 'net.ifnames=0 biosdevname=0'"
    
    # 加载到引导分区
    [root@ccc network-scripts]# grub2-mkconfig -o /boot/grub2/grub.cfg 
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-3.10.0-1127.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1127.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-d756bc9b1bf84350b002ec5d9e37360d
    Found initrd image: /boot/initramfs-0-rescue-d756bc9b1bf84350b002ec5d9e37360d.img
    done
    
    # 重启系统以生效
    [root@ccc ~]# reboot
    

14.2 基本网络配置

14.2.1 查看网卡信息

  • 查看网卡信息

    # 查看当前系统所连接的所有网卡
    [root@ccc ~]# lspci | grep -i eth
    02:01.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
    
    # 确认网线已经连接好
    [root@ccc ~]# ethtool eth0
    Settings for eth0:
    	Supported ports: [ TP ]
    	Supported link modes:   10baseT/Half 10baseT/Full 
    	                        100baseT/Half 100baseT/Full 
    	                        1000baseT/Full 
    	Supported pause frame use: No
    	Supports auto-negotiation: Yes
    	Supported FEC modes: Not reported
    	Advertised link modes:  10baseT/Half 10baseT/Full 
    	                        100baseT/Half 100baseT/Full 
    	                        1000baseT/Full 
    	Advertised pause frame use: No
    	Advertised auto-negotiation: Yes
    	Advertised FEC modes: Not reported
    	Speed: 1000Mb/s
    	Duplex: Full
    	Port: Twisted Pair
    	PHYAD: 0
    	Transceiver: internal
    	Auto-negotiation: on
    	MDI-X: off (auto)
    	Supports Wake-on: d
    	Wake-on: d
    	Current message level: 0x00000007 (7)
    			       drv probe link
    	Link detected: yes
    [root@ccc ~]# mii-tool eth0
    eth0: negotiated 1000baseT-FD flow-control, link ok
    # link ok指网卡能够被识别,并且接了有效网线
    

14.2.2 ifconfig命令

  • ifconfig命令

    # 查看网卡信息
    ifconfig -a			# 查看所有网卡信息
    ifconfig eth0		# 查看单个的网卡信息
    
    # 临时设置IP等
    ifconfig eth0 192.168.1.122 netmask 255.255.255.0		# 设定临时IP和掩码(重启失效)
    ifconfig eth0 192.168.1.122/24							# 临时设定IP地址
    ifconfig eth0:1 192.168.0.2 netmask 255.255.255.0		# 配置子接口
        
    # 删除
    ifconfig eth0:0 down				# 删除
    ifconfig eth0:1 del 192.168.0.2		# 删除,不加掩码
        
    # 开启与关闭
    ifconfig eth0 down|up# 不加载网卡配置文件
    ifdown eth0 |ifup eth0# 加载网卡配置文件
    
    # 设置网卡最大传输单元
    ifconfig eth0 mtu 1500
    
    # 开启关闭模式(了解)
    ifconfig eth0 promisc			# 开启繁杂模式
    ifconfig eth0 -promisc			# 关闭繁杂模式
    
    ifconfig eth0 multicast			# 开启多播
    ifconfig eth0 -multicast		# 关闭多播
    
    ifconfig eth0 allmulti			# 开启
    ifconfig eth0 -allmulti			# 关闭
    
    # 删除、添加ipv6地址
    ifconfig eth0 add 3ffe:3240:800:1005::2/64
    
  • ifconfig命令结果解释

    [root@ccc ~]# ifconfig eth0
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        # 从flags可知该接口已启用,支持广播、组播
        # mtu:1500(最大输出单元1500字节)
        # UP:表示接口已启用
        # BROADCAST:表示主机支持广播
        # RUNNING:表示接口在工作中
        # MULTICAST:表示主机支持多播
          inet 192.168.29.55  netmask 255.255.255.0  broadcast 192.168.29.255
            # ipv4地址			子网掩码				广播地址
          inet6 fe80::20c:29ff:fec0:5db3  prefixlen 64  scopeid 0x20<link>
            # ipv6地址					 掩码长度	作用域,link表示仅该接口有效
          ether 00:0c:29:c0:5d:b3  txqueuelen 1000  (Ethernet)
            # 网卡接口的MAC地址		输出队列长度		接口类型为Ethernet
          RX packets 1706  bytes 156657 (152.9 KiB)
        	# 表示开机后此接口累计接收的报文个数,总字节数
          RX errors 0  dropped 0  overruns 0  frame 0
        	# 表示开机后此接口累计接收的报文错误数,丢弃数,溢出数(速度过快而丢失的数据包数),冲突的帧数
          TX packets 1249  bytes 121636 (118.7 KiB)
        	# 表示开机后此接口累计发送的报文数,总字节数
          TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        	# 表示开机后此接口累计发送的报文错误数,丢弃数,溢出数(速度过快而丢失的数据包数)
            # carrier 载荷数(发生carrier错误而丢失的数据包数)
            # collisions 冲突数
    
  • 补充

    # 1、全双工与半双工(目前网卡一般采用全双工模式)
    全双工(Full-Duplex Transmissions)指交换机在发数据的同时也能接收数据,两者同步进行
    全双工延迟小、冲突少、速度快
    半双工指同一时间只有一个动作发生
    
    # CRC
    CRC即循环冗余校验码(Cyclic Redundancy Check)是数据通信领域常用的查错校验码
    特征:信息字段和校验字段的长度可以任意选定。接收设备执行类似的算法,以保证数据传输的正确性和完整性
        
    # 网卡工作原理
    网卡发包:
        1、ip包+14字节的mac头-->数据帧frame
        2、frame拷贝到网卡芯片内的缓冲区,由网卡处理
        3、网卡芯片为frame添加头部同步信息和CRC校验称为可发送的packet,发送该packet
    网卡收包:
        1、网卡包packet到达网卡,网卡先检查packet的CRC校验,保证其完整和正确性,去掉头得frame
        2、网卡将frame拷贝到网卡内部的FIFO缓冲区
        3、网卡驱动程序产生硬件中断,把frame从网卡拷贝到内存,剩下交给内核
        
    网卡丢包:
        1、内核通常要快速的将网络数据包拷贝到系统内存
        2、网卡上接收的网络数据包的缓存大小固定,相比系统内存小得多
        12其一被延迟都会造成网卡FIFO缓存溢出
        进入的数据包占满了网卡的缓存,后续的包只能被丢弃,是ifconfig中overrun的来源
    
  • 解决丢包问题

    # 丢包排查
    网卡工作在数据链路层,数据链路层会做一些校验,封装成帧。
    可以查看校验是否出错,确定传输是否有问题
    其次可以从软件方面,查看是否因为缓冲区太小而丢包
    
    # 1 检查硬件情况
    # 1.1 查看工作模式是否正常
    [root@ccc ~]# ethtool eth0 | egrep -i 'speed|duplex'
    	Speed: 1000Mb/s
    	Duplex: Full
    # 1.2 查看CRC校验是否正常
    [root@ccc ~]# ethtool -S eth0 | grep crc
         rx_crc_errors: 0
    # Speed、Duplex、CRC都没问题,基本排除物理层面的干扰
    
    # 2、通过ifconfig可以看到overruns是否一致增大,如果一直增大
    [root@ccc ~]# for i in `seq 1 100`; do ifconfig eth0 | grep RX | grep overruns; sleep 1;done
            RX errors 0  dropped 0  overruns 0  frame 0
            RX errors 0  dropped 0  overruns 0  frame 0
    
    # 3、调整网卡缓冲区
    [root@ccc ~]# ethtool -g eth0
    Ring parameters for eth0:
    Pre-set maximums:							# 最大可以设置的值
    RX:		4096
    RX Mini:	0
    RX Jumbo:	0
    TX:		4096
    Current hardware settings:					# 当前设置的值
    RX:		256
    RX Mini:	0
    RX Jumbo:	0
    TX:		256
    [root@ccc ~]# ethtool -G eth0 rx 2048		# 调大
    [root@ccc ~]# ethtool -G eth0 tx 2048		# 调大
    [root@ccc ~]# ethtool -g eth0
    Ring parameters for eth0:
    Pre-set maximums:
    RX:		4096
    RX Mini:	0
    RX Jumbo:	0
    TX:		4096
    Current hardware settings:
    RX:		2048
    RX Mini:	0
    RX Jumbo:	0
    TX:		2048
    
  • ethtool网卡降速

    [root@ccc ~]# ethtool -s eth0 speed 100 duplex full
    [root@ccc ~]# ethtool -s eth0 speed 100 duplex full autoneg off
    # 关闭自适应才能成功
    [root@ccc ~]# ethtool eth0		# 查看
    
    想要永久配置需将上述ethtool设置写入配置文件/etc/rc.local
    必须加x权限
    [root@ccc ~]# chmod +x /etc/rc.d/rc.local
    

14.2.3 ping命令

  • ping

    ping 目标IP地址  # ctrl+c结束,测试两台主机网络是否相通
    ping -c 次数 目标IP地址
    
    # 在自己机器上执行,禁止别人ping自己
    [root@ccc ~]# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
    

14.2.4 NetworkManager服务

  • 作用:是redhat6自带的检测网络、自动连接网络的图形化工具。

  • NetworkManager服务会干扰网络配置,如DNS经常会被刷掉,因此常关闭。

  • 网卡相关的配置文件/etc/sysconfig/network-scripts/ifcfg-ethX接口的网络配置文件

    # 打开网卡配置文件,完成静态IP配置
    # 修改后重启网络服务systemctl restart network
    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=none							# dhcp动态获取IP
    										# none根据其他选项决定动态或静态
    										# static手动指定IP
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    NAME=eth0
    UUID=39e06fd4-3081-4cfe-8937-65fd357b6727
    DEVICE=eth0								# 网卡名字
    ONBOOT=yes								# yes激活状态no禁用
    IPADDR=192.168.29.55					# IP地址
    NETMASK=255.255.255.0					# 子网掩码
    NS1=8.8.8.8								# DNS服务器
    PREFIX=24
    GATEWAY=192.168.29.1					# 默认网关
    

14.2.5 DNS配置文件

  • DNS解析文件/etc/resolv.conf

    [root@ccc ~]# cat /etc/resolv.conf			# 设置DNS指向,最多三个
    # Generated by NetworkManager
    nameserver 8.8.8.8							# 对应网卡配置文件中的配置项DNS
    
  • 本地名称解析文件(优先于DNS)/etc/hosts

    dns优先级
    浏览器dns缓存-->本地系统DNS缓存-->本地计算机HOSTS文件-->ISP DNS缓存-->递归/迭代查询
    

14.2.6 永久设置主机名

  • hostnamectl

  • /etc/hostname

    [root@ccc ~]# hostnamectl set-hostname zzz
    [root@ccc ~]# hostname
    zzz
    [root@ccc ~]# vim /etc/hostname 
    

14.2.7 端口与服务的对应关系

  • 对应关系

    [root@ccc ~]# grep '^ftp | ^ssh' /etc/services 
    

14.3 路由route

14.3.1 交换与路由

  • 交换

    指同网络访问,两台交换机连接在同一个交换机上,配置同网段的不同IP就可以直接通讯。

  • 一台主机能被当成路由器的必要条件

    1、至少有两块网卡分别连接两个不同的网段

    2、开启路由转发功能

    ​ echo 1 > /proc/sys/net/ipv4/ip_forward

    3、在该linux主机上添加正确的路由规则/策略

    ​ route

    4、其他主机若想要上述linux主机帮自己转发数据包,必须将自己的gw指定成上述linux主机的ip地址

14.3.2 Linux处理数据包的过程

  • 向外界主机发送数据时,在他从网卡流入后需要对他做路由决策,根据其目标决定是流入本机的用户空间还是在内核空间就直接转发给其他主机

    # 1、是流入本机用户空间的数据
    数据会从内核空间流入用户空间(被应用程序接受并处理)
    # 2、不是流入本机用户空间的数据,只经过本机把数据包转发给其他主机
    需开启Linux主机的路由转发功能
    # 临时开启方式一
    echo 1 > /proc/sys/net/ipv4/ip_forward
    # 临时开启方式二
    sysctl -w net.ipv4.ip_forward=1
    # 永久开启改配置文件
    echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/ip_forward.conf
    

14.3.3 网关/路由

  • 主机路由:掩码位32位,Destination精确到某一台主机

    通常主机路由是直接指明到某台具体主机怎么走,主机路由也就是所谓的静态路由

  • 网络路由:掩码位小于32位,Destination精确到某一网段的主机

    网络路由指明到某类网络怎么走

  • 默认路由:掩码为0

    不走主机路由和网络路由的全都走默认路由

    操作系统设置的默认路由一般也称网关

    # 1、在Linux中,路由条目的优先级确定方式是先匹配掩码位长度,越长优先级越高
    # 2、路由条目掩码长度相同时,比较节点之间的管理距离(如metric),短的优先
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.29.1    0.0.0.0         UG    100    0        0 eth0
    192.168.29.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
    

14.3.4 route命令

  • route命令由于显示和管理路由表

  • route add 增加路由条目,route del 减少路由条目

  • route -n 显示路由表信息,-n选项表示不解析主机名

    route [add/del] [-host/-net/default] [address[/mask]] [netmask] [gw] [dev]
    
    add/del		# 增加或删除路由条目
    -net		# 增肌或删除的是一条网络路由
    -host		# 增加或删除的是一条主机路由
    default		# 增加或删除的是一条默认路由
    netmask		# 明确使用netmask关键字指定掩码,也可直接在地址上用cidr格式的掩码,即IP/MASK
    gw			# 指定下一跳的地址(下一跳的地址必须是能达到的,且一般是与本网段直连的接口)
    dev			# 强制将路由条目关联到指定的接口上(一般内核会自动判断路由条目应该关联到那个网络接口)
    
    # route命令添加的都是临时生效的
    
  • 添加和删除默认路由

    [root@ccc ~]# route add default gw 192.168.29.1
    [root@ccc ~]# route del default 
    [root@ccc ~]# route del default gw 192.168.29.1
    # 如果有多条默认路由,再加上gw可唯一删除指定条目
    # 默认路由的destination和gemask都是0.0.0.0因此可用default代替
    
  • 添加和删除网络路由

    [root@ccc ~]# route add -net 192.168.29.11/24 gw 192.168.29.55
    [root@ccc ~]# route add -net 192.168.29.11 netmask 255.255.255.255 gw 192.168.29.55
    [root@ccc ~]# route del -net 192.168.29.11/24
    [root@ccc ~]# route del -net 192.168.29.11 netmask 255.255.255.255 gw 192.168.29.55
    
  • 添加和删除主机路由

    [root@ccc ~]# route add -host 192.168.29.11/32 gw 192.168.29.55
    [root@ccc ~]# route add -host 192.168.29.11/32
    

14.3.5 配置永久路由

  • 创建配置文件

    /etc/sysconfig/network-scripts/route-ethX

    要从哪个接口出去X就是几

  • 配置文件内容

    DEST via nexthop

    # 默认路由
    default via 192.168.100.1
    # 网段路由
    192.168.10.0/24 via 192.168.100.1
    # 主机路由
    192.168.100.52/32 via 192.168.100.33 dev eth1
    

14.3.6 测试

  • 环境

    交换机 IP 交换机 IP
    交换机1 1.1.1.0 交换机2 2.2.2.0
    交换机3 3.3.3.0 交换机4 4.4.4.0
    主机名 网卡 IP 主机 网卡 IP
    主机1 eth0 1.1.1.6 主机2 eth0 1.1.1.2
    eth1 2.2.2.2
    主机3 eth0 2.2.2.3 主机4 eth0 3.3.3.4
    eth1 3.3.3.3 eth1 4.4.4.4
  • 主机1ping主机2网卡1:1.1.1.6------------->1.1.1.2

    =============================主机1==============================
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway    Genmask      Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0    255.255.255.0  U    100   0    0 eth0
    [root@ccc ~]# ping 1.1.1.2
    PING 1.1.1.2 (1.1.1.2) 56(84) bytes of data.
    64 bytes from 1.1.1.2: icmp_seq=1 ttl=64 time=1.05 ms
    ^C
    --- 1.1.1.2 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2003ms
    rtt min/avg/max/mdev = 0.326/0.805/1.052/0.338 ms
    
  • 主机1ping主机2网卡2:1.1.1.6------------->2.2.2.2

    =============================主机1==============================
    [root@ccc ~]# ping -c 2 2.2.2.2
    connect: 网络不可达
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway    Genmask       Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0    255.255.255.0  U    100    0    0  eth0
    [root@ccc ~]# route add -net 2.2.2.0/24 dev eth0
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask      Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0     255.255.255.0   U     100   0  0  eth0
    2.2.2.0      0.0.0.0     255.255.255.0   U     0     0  0  eth0
    [root@ccc ~]# ping -c 2 2.2.2.2
    PING 2.2.2.2 (2.2.2.2) 56(84) bytes of data.
    64 bytes from 2.2.2.2: icmp_seq=1 ttl=64 time=0.552 ms
    64 bytes from 2.2.2.2: icmp_seq=2 ttl=64 time=0.354 ms
    --- 2.2.2.2 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1000ms
    rtt min/avg/max/mdev = 0.354/0.453/0.552/0.099 ms
    
  • 主机1ping主机3网卡1:1.1.1.6------------->2.2.2.3

    =============================主机1==============================
    [root@ccc ~]# ping -c 2 2.2.2.3
    PING 2.2.2.3 (2.2.2.3) 56(84) bytes of data.
    From 1.1.1.6 icmp_seq=1 Destination Host Unreachable
    From 1.1.1.6 icmp_seq=2 Destination Host Unreachable
    --- 2.2.2.3 ping statistics ---
    2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1001ms pipe 2
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway   Genmask      Flags Metric Ref   Use Iface
    1.1.1.0      0.0.0.0   255.255.255.0  U     100    0    0  eth0
    2.2.2.0      0.0.0.0   255.255.255.0  U     0      0    0  eth0
    [root@ccc ~]# route add -net 2.2.2.0/24 gw 1.1.1.2 dev eth0
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask      Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0     255.255.255.0   U   100   0     0 eth0
    2.2.2.0      1.1.1.2     255.255.255.0   UG  0     0     0 eth0
    2.2.2.0      0.0.0.0     255.255.255.0   U   0     0     0 eth0
    =============================主机2==============================
    [root@ccc ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
    =============================主机3==============================
    [root@ccc ~]# route add -net 1.1.1.0/24 gw 2.2.2.2 dev eth0
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway   Genmask      Flags Metric Ref  Use Iface
    1.1.1.0      2.2.2.2   255.255.255.0  UG   0     0     0  eth0
    2.2.2.0      0.0.0.0   255.255.255.0  U    100   0     0  eth0
    3.3.3.0      0.0.0.0   255.255.255.0  U    101   0     0  eth1
    =============================主机1==============================
    [root@ccc ~]# ping -c 2 2.2.2.3
    PING 2.2.2.3 (2.2.2.3) 56(84) bytes of data.
    64 bytes from 2.2.2.3: icmp_seq=1 ttl=63 time=1.60 ms
    64 bytes from 2.2.2.3: icmp_seq=2 ttl=63 time=0.657 ms
    --- 2.2.2.3 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1000ms
    rtt min/avg/max/mdev = 0.657/1.129/1.602/0.473 ms
    
  • 主机1ping主机3网卡2:1.1.1.6------------->3.3.3.3

    =============================主机1==============================
    [root@ccc ~]# ping -c 2 3.3.3.3
    connect: 网络不可达
    [root@ccc ~]# route add -net 3.3.3.0/24 gw 1.1.1.2 dev eth0
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway    Genmask      Flags Metric Ref  Use Iface
    1.1.1.0      0.0.0.0    255.255.255.0   U   100   0     0  eth0
    2.2.2.0      1.1.1.2    255.255.255.0   UG  0     0     0  eth0
    2.2.2.0      0.0.0.0    255.255.255.0   U   0     0     0  eth0
    3.3.3.0      1.1.1.2    255.255.255.0   UG  0     0     0  eth0
    =============================主机2==============================
    [root@ccc ~]# route add -net 3.3.3.0/24 dev eth1
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination Gateway     Genmask      Flags Metric Ref  Use Iface
    1.1.1.0     0.0.0.0     255.255.255.0   U   100    0     0 eth0
    2.2.2.0     0.0.0.0     255.255.255.0   U   101    0     0 eth1
    3.3.3.0     0.0.0.0     255.255.255.0   U    0      0    0 eth1
    =============================主机3==============================
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway    Genmask      Flags Metric Ref  Use Iface
    1.1.1.0      2.2.2.2    255.255.255.0  UG   0      0    0  eth0
    2.2.2.0      0.0.0.0    255.255.255.0  U    100    0    0  eth0
    3.3.3.0      0.0.0.0    255.255.255.0  U    101    0    0  eth1
    =============================主机1==============================
    [root@ccc ~]# ping -c 2 3.3.3.3
    PING 3.3.3.3 (3.3.3.3) 56(84) bytes of data.
    64 bytes from 3.3.3.3: icmp_seq=1 ttl=63 time=1.44 ms
    64 bytes from 3.3.3.3: icmp_seq=2 ttl=63 time=0.797 ms
    --- 3.3.3.3 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1002ms
    rtt min/avg/max/mdev = 0.797/1.121/1.446/0.326 ms
    
  • 主机1ping主机4网卡1:1.1.1.6------------->3.3.3.4

    =============================主机1==============================
    [root@ccc ~]# ping -c 2 3.3.3.4
    PING 3.3.3.4 (3.3.3.4) 56(84) bytes of data.
    From 1.1.1.2 icmp_seq=1 Destination Host Unreachable
    From 1.1.1.2 icmp_seq=2 Destination Host Unreachable
    --- 3.3.3.4 ping statistics ---
    2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1000ms
    pipe 2
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask      Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0     255.255.255.0   U    100   0    0  eth0
    2.2.2.0      1.1.1.2     255.255.255.0   UG   0     0    0  eth0
    2.2.2.0      0.0.0.0     255.255.255.0   U    0     0    0  eth0
    3.3.3.0      1.1.1.2     255.255.255.0   UG   0     0    0  eth0
    =============================主机2==============================
    [root@ccc ~]# route add -net 3.3.3.0/24 gw 2.2.2.3 dev eth1
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask     Flags Metric Ref  Use Iface
    1.1.1.0      0.0.0.0    255.255.255.0  U   100   0     0   eth0
    2.2.2.0      0.0.0.0    255.255.255.0  U   101   0     0   eth1
    3.3.3.0      2.2.2.3    255.255.255.0  UG  0     0     0   eth1
    3.3.3.0      0.0.0.0    255.255.255.0  U   0     0     0   eth1
    =============================主机3==============================
    [root@ccc ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
    =============================主机4==============================
    [root@ccc ~]# route add -net 1.1.1.0/24 gw 3.3.3.3 dev eth0
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway    Genmask     Flags Metric Ref  Use  Iface
    1.1.1.0      3.3.3.3    255.255.255.0  UG   0     0     0  eth0
    3.3.3.0      0.0.0.0    255.255.255.0  U    100   0     0  eth0
    4.4.4.0      0.0.0.0    255.255.255.0  U    101   0     0  eth1
    =============================主机1==============================
    [root@ccc ~]# ping -c 2 3.3.3.4
    PING 3.3.3.4 (3.3.3.4) 56(84) bytes of data.
    64 bytes from 3.3.3.4: icmp_seq=1 ttl=62 time=1.71 ms
    64 bytes from 3.3.3.4: icmp_seq=2 ttl=62 time=1.16 ms
    
    --- 3.3.3.4 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1002ms
    rtt min/avg/max/mdev = 1.162/1.440/1.718/0.278 ms
    
  • 主机1ping主机4网卡2:1.1.1.6------------->4.4.4.4

    =============================主机1==============================
    [root@ccc ~]# route add -net 4.4.4.0/24 gw 1.1.1.2 dev eth0
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway    Genmask      Flags Metric Ref  Use Iface
    1.1.1.0      0.0.0.0    255.255.255.0   U    100   0    0  eth0
    2.2.2.0      1.1.1.2    255.255.255.0   UG   0     0    0  eth0
    2.2.2.0      0.0.0.0    255.255.255.0   U    0     0    0  eth0
    3.3.3.0      1.1.1.2    255.255.255.0   UG   0     0    0  eth0
    4.4.4.0      1.1.1.2    255.255.255.0   UG   0     0    0  eth0
    =============================主机2==============================
    [root@ccc ~]# route add -net 4.4.4.0/24 gw 2.2.2.3 dev eth1
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask      Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0     255.255.255.0   U    100   0    0 eth0
    2.2.2.0      0.0.0.0     255.255.255.0   U    101   0    0 eth1
    3.3.3.0      2.2.2.3     255.255.255.0   UG   0     0    0 eth1
    3.3.3.0      0.0.0.0     255.255.255.0   U    0     0    0 eth1
    4.4.4.0      2.2.2.3     255.255.255.0   UG   0     0    0 eth1
    =============================主机3==============================
    [root@ccc ~]# route add -net 4.4.4.0/24 dev eth1
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask     Flags Metric Ref Use Iface
    1.1.1.0      2.2.2.2     255.255.255.0  UG   0     0    0 eth0
    2.2.2.0      0.0.0.0     255.255.255.0  U    100   0    0 eth0
    3.3.3.0      0.0.0.0     255.255.255.0  U    101   0    0 eth1
    4.4.4.0      0.0.0.0     255.255.255.0  U    0     0    0 eth1
    =============================主机4==============================
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination   Gateway     Genmask     Flags Metric Ref Use Iface
    1.1.1.0       3.3.3.3     255.255.255.0  UG   0    0    0 eth0
    3.3.3.0       0.0.0.0     255.255.255.0  U    100  0    0 eth0
    4.4.4.0       0.0.0.0     255.255.255.0  U    101  0    0 eth1
    =============================主机1==============================
    [root@ccc ~]# ping -c 2 4.4.4.4
    PING 4.4.4.4 (4.4.4.4) 56(84) bytes of data.
    64 bytes from 4.4.4.4: icmp_seq=1 ttl=62 time=1.77 ms
    64 bytes from 4.4.4.4: icmp_seq=2 ttl=62 time=0.972 ms
    --- 4.4.4.4 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1002ms
    rtt min/avg/max/mdev = 0.972/1.375/1.778/0.403 ms
    
  • 优化

    =============================主机1==============================
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask      Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0     255.255.255.0   U    100   0    0 eth0
    2.2.2.0      1.1.1.2     255.255.255.0   UG   0     0    0 eth0
    2.2.2.0      0.0.0.0     255.255.255.0   U    0     0    0 eth0
    3.3.3.0      1.1.1.2     255.255.255.0   UG   0     0    0 eth0
    4.4.4.0      1.1.1.2     255.255.255.0   UG   0     0    0 eth0
    [root@ccc ~]# route del -net 2.2.2.0/24 dev eth0
    [root@ccc ~]# route del -net 2.2.2.0/24 dev eth0
    [root@ccc ~]# route del -net 3.3.3.0/24 dev eth0
    [root@ccc ~]# route del -net 4.4.4.0/24 dev eth0
    [root@ccc ~]# route add default gw 1.1.1.2 dev eth0
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway      Genmask     Flags Metric Ref Use Iface
    0.0.0.0      1.1.1.2      0.0.0.0       UG   0     0     0 eth0
    1.1.1.0      0.0.0.0      255.255.255.0 U    100   0     0 eth0
    =============================主机2==============================
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway      Genmask     Flags Metric Ref Use Iface
    1.1.1.0      0.0.0.0      255.255.255.0  U    100   0    0 eth0
    2.2.2.0      0.0.0.0      255.255.255.0  U    101   0    0 eth1
    3.3.3.0      2.2.2.3      255.255.255.0  UG   0     0    0 eth1
    3.3.3.0      0.0.0.0      255.255.255.0  U    0     0    0 eth1
    4.4.4.0      2.2.2.3      255.255.255.0  UG   0     0    0 eth1
    [root@ccc ~]# route del -net 3.3.3.0/24 dev eth1
    [root@ccc ~]# route del -net 3.3.3.0/24 dev eth1
    [root@ccc ~]# route del -net 4.4.4.0/24 dev eth1
    [root@ccc ~]# route add -net default gw 2.2.2.3 dev eth1
    [root@ccc ~]# route -n
    Kernel IP routing table
    Destination  Gateway     Genmask      Flags Metric Ref Use Iface
    0.0.0.0      2.2.2.3     0.0.0.0        UG   0      0   0  eth1
    1.1.1.0      0.0.0.0     255.255.255.0  U    100    0   0  eth0
    2.2.2.0      0.0.0.0     255.255.255.0  U    101    0   0  eth1
    
原文地址:https://www.cnblogs.com/caojiaxin/p/14004846.html