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