linux 路由配置及其網絡組配置實例
§·路由配置
網絡簡單拓撲圖如下:
※·實例要求:主機A可以ping同主機B即可。
※·實驗步驟及其要點:
1.主機A設置IP地址與默認網關;
2.配置路由器1兩接口的IP地址,并開啟主機的路由功能,添加172.14.16.0路由條目;
3.配置路由器1兩接口的IP地址,并開啟主機的路由功能,添加172.14.14.0路由條目;
4.主機B設置IP地址與默認網關
※·設備配置:
步驟一 :主機A配置說明:
[root@love681 ~]# ip addr add 172.14.14.2/24 dev eth0 #接口上配置A主機的地址為:172.14.14.2 [root@love681 ~]# ip route add default via 172.14.14.1 dev eth0 #配置默認網關為:172.14.14.1 [root@love681 ~]# ip addr show #查看其配置的IP地址 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:21:e5:b5 brd ff:ff:ff:ff:ff:ff inet 172.14.14.2/24 scope global eth0 inet6 fe80::20c:29ff:fe21:e5b5/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:21:e5:bf brd ff:ff:ff:ff:ff:ff inet6 fe80::20c:29ff:fe21:e5bf/64 scope link valid_lft forever preferred_lft forever 4: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN link/ether 96:f3:ff:51:3e:19 brd ff:ff:ff:ff:ff:ff [root@love681 ~]# ip route show #查看配置的路由條目 172.14.14.0/24 dev eth0 proto kernel scope link src 172.14.14.2 default via 172.14.14.1 dev eth0 [root@love681 ~]# iptables -F #測試環境中取消防火墻的規則 [root@love681 ~]#
步驟二 :路由器1配置說明
[root@centos68 /]# ip addr add 172.14.14.1/24 dev eth1 #配置接口eth1的地址172.14.14.1 [root@centos68 /]# ip addr add 172.14.15.1/24 dev eth0 #配置接口eth0的地址172.14.15.1 [root@centos68 /]# ip route add 172.14.16.0/24 via 172.14.15.2 dev eth0 #配置到到 172.14.16.0網段的路由,自己直連的網段不需要添加到自己的靜態路由表 [root@centos68 /]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:42:59:60 brd ff:ff:ff:ff:ff:ff inet 10.1.16.61/16 brd 10.1.255.255 scope global eth0 inet 172.14.15.1/24 scope global eth0 inet6 fe80::20c:29ff:fe42:5960/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:42:59:6a brd ff:ff:ff:ff:ff:ff inet 172.14.14.1/24 scope global eth1 inet6 fe80::20c:29ff:fe42:596a/64 scope link valid_lft forever preferred_lft forever [root@centos68 /]# ip route show #查看路由器1的路由條目 172.14.16.0/24 via 172.14.15.2 dev eth0 172.14.15.0/24 dev eth0 proto kernel scope link src 172.14.15.1 172.14.14.0/24 dev eth1 proto kernel scope link src 172.14.14.1 10.1.0.0/16 dev eth0 proto kernel scope link src 10.1.16.61 169.254.0.0/16 dev eth0 scope link metric 1002 default via 10.1.0.1 dev eth0 proto static [root@centos68 /]# echo 1 > /proc/sys/net/ipv4/ip_forward #開啟主機路由功能 [root@centos68 /]#
步驟三:路由器2配置說明
[root@Centos7 ~]# ip addr add 172.14.15.2/24 dev eth1 #配置路由器2的eth1接口的IP地址 [root@Centos7 ~]# ip addr add 172.14.16.1/24 dev eth2 #配置路由器2的eth2接口的IP地址 [root@Centos7 ~]# ip route add 172.14.14.0/24 via 172.14.15.1 dev eth1 #添加到達172.14.1.0的網絡靜態路由,直連網段不需要添加 [root@Centos7 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward #開啟主機路由功能 [root@Centos7 ~]# ip addr show 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:90:ef:7d brd ff:ff:ff:ff:ff:ff inet 10.1.16.70/16 brd 10.1.255.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe90:ef7d/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:90:ef:87 brd ff:ff:ff:ff:ff:ff inet 172.14.15.2/24 scope global eth1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe90:ef87/64 scope link valid_lft forever preferred_lft forever 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:90:ef:91 brd ff:ff:ff:ff:ff:ff inet 172.14.16.1/24 scope global eth2 valid_lft forever preferred_lft forever [root@Centos7 ~]# ip route show #查看本機靜態路由表 default via 10.1.0.1 dev eno16777736 proto static metric 100 10.1.0.0/16 dev eno16777736 proto kernel scope link src 10.1.16.70 metric 100 172.14.14.0/24 via 172.14.15.1 dev eth1 172.14.15.0/24 dev eth1 proto kernel scope link src 172.14.15.2 172.14.16.0/24 dev eth2 proto kernel scope link src 172.14.16.1 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 [root@Centos7 ~]#
步驟四:主機B配置說明
[root@love721 ~]# ip addr add 172.14.16.2/24 dev eno16777736 #配置主機IP地址 [root@love721 ~]# ip route add default via 172.14.16.1 dev eno16777736 #配置主機默認路由 [root@love721 ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:04:cb:7e brd ff:ff:ff:ff:ff:ff inet 172.14.16.2/24 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe04:cb7e/64 scope link valid_lft forever preferred_lft forever [root@love721 ~]# ip route show default via 172.14.16.1 dev eno16777736 172.14.16.0/24 dev eno16777736 proto kernel scope link src 172.14.16.2 [root@love721 ~]# iptables -F [root@love721 ~]#
※·測試主機A到主機B的連通性
[root@love681 ~]# ping 172.14.16.2 #可以正常的ping通主機B PING 172.14.16.2 (172.14.16.2) 56(84) bytes of data. 64 bytes from 172.14.16.2: icmp_seq=1 ttl=62 time=2.71 ms 64 bytes from 172.14.16.2: icmp_seq=2 ttl=62 time=1.97 ms 64 bytes from 172.14.16.2: icmp_seq=3 ttl=62 time=2.50 ms 64 bytes from 172.14.16.2: icmp_seq=4 ttl=62 time=1.54 ms 64 bytes from 172.14.16.2: icmp_seq=5 ttl=62 time=0.631 ms 64 bytes from 172.14.16.2: icmp_seq=6 ttl=62 time=0.848 ms --- 172.14.16.2 ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 5069ms rtt min/avg/max/mdev = 0.631/1.702/2.712/0.780 ms [root@love681 ~]# traceroute 172.14.16.2 #使用路由追蹤到達主機B的需要經過的路由 traceroute to 172.14.16.2 (172.14.16.2), 30 hops max, 60 byte packets 1 172.14.14.1 (172.14.14.1) 0.441 ms 3.419 ms 3.355 ms #經過路由器1 2 172.14.15.2 (172.14.15.2) 3.981 ms 4.134 ms 4.191 ms #經過路由器2 3 172.14.15.2 (172.14.15.2) 4.279 ms !X 4.470 ms !X 4.482 ms !X #到達主機B [root@love681 ~]#
§· 網絡組實現
※·網絡組介紹:
·網絡組:是將多個網卡聚合在一起方法,從而實現冗錯和提高吞吐量
·網絡組不同于舊版中bonding技術,提供更好的性能和擴展性
·網絡組由內核驅動和teamd守護進程實現.
·多種方式runner
broadcast
roundrobin
activebackup
loadbalance
lacp(implements the 802.3ad Link Aggregation Control Protocol)
※·注意事項:
·啟動網絡組接口不會自動啟動網絡組中的port接口
·啟動網絡組接口中的port接口不會自動啟動網絡組接口
·禁用網絡組接口會自動禁用網絡組中的port接口
·沒有port接口的網絡組接口可以啟動靜態IP連接
·啟用DHCP連接時,沒有port接口的網絡組會等待port接口的加入
※·網絡組的配置:
[root@Centos7 ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:90:ef:7d brd ff:ff:ff:ff:ff:ff inet 10.1.16.70/16 brd 10.1.255.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe90:ef7d/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:90:ef:87 brd ff:ff:ff:ff:ff:ff inet6 fe80::20c:29ff:fe90:ef87/64 scope link valid_lft forever preferred_lft forever 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:90:ef:91 brd ff:ff:ff:ff:ff:ff 6: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN link/ether 52:54:00:55:9d:3d brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 7: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500 link/ether 52:54:00:55:9d:3d brd ff:ff:ff:ff:ff:ff [root@Centos7 ~]# nmcli connection add type team con-name team_00 ifname team_00 config '{"name":"loadbalance"}' #添加一個team_00的網絡組,網絡組的模式為loadbalance Connection 'team_00' (073bf1f6-8b9e-4bbe-8341-96faeebc3be3) successfully added. [root@Centos7 ~]# nmcli connection modify team_00 ipv4.addresses 172.14.14.100/24 #配置team_00的ipv4地址為:172.14.14.100 [root@Centos7 ~]# nmcli connection modify team_00 ipv4.method manual #配置team_00的ip地址為靜態地址 [root@Centos7 ~]# nmcli connection add type team-slave con-name team00_eth1 ifname eth1 master team_00 #把eth1接口加入到team_00,并把端口新建一個配置文件為:team00_eth1 Connection 'team00_eth1' (57d9b226-0a8a-4136-82fd-e1e241ae0f4a) successfully added. [root@Centos7 ~]# nmcli connection add type team-slave con-name team00_eth2 ifname eth2 master team_00 #把eth2接口加入到team_00,并把端口新建一個配置文件為:team00_eth2 Connection 'team00_eth2' (5b8947e9-782c-44a6-ae0c-8e964d2ef076) successfully added. [root@Centos7 ~]# nmcli connection show #查看添加的配置文件 NAME UUID TYPE DEVICE team00_eth2 5b8947e9-782c-44a6-ae0c-8e964d2ef076 802-3-ethernet -- team_00 073bf1f6-8b9e-4bbe-8341-96faeebc3be3 team team_00 eth1 16375bd9-73c1-447f-bb76-a21b5d23d6a3 802-3-ethernet eth1 virbr0-nic d9876d68-76d6-4744-bc1d-5d950f41834c generic virbr0-nic virbr0 0e013aae-2fc9-4e46-83a1-a25f00335e1d bridge virbr0 eno16777736 5a5f89ce-e54a-4f22-b843-3d438b093fbb 802-3-ethernet eno16777736 eth2 3a73717e-65ab-93e8-b518-24f5af32dc0d 802-3-ethernet eth2 team00_eth1 57d9b226-0a8a-4136-82fd-e1e241ae0f4a 802-3-ethernet -- [root@Centos7 ~]# nmcli connection up team_00 #開啟網絡組 team_00 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13) [root@Centos7 ~]# nmcli connection up team00_eth1 #開啟端口:team00_eth1 eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/14) [root@Centos7 ~]# nmcli connection up team00_eth2 #開啟端口:team00_eth2 eth2 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/15) [root@Centos7 ~]# nmcli connection show #查看以上操作生效情況 NAME UUID TYPE DEVICE team00_eth2 5b8947e9-782c-44a6-ae0c-8e964d2ef076 802-3-ethernet eth2 team_00 073bf1f6-8b9e-4bbe-8341-96faeebc3be3 team team_00 virbr0-nic d9876d68-76d6-4744-bc1d-5d950f41834c generic virbr0-nic virbr0 0e013aae-2fc9-4e46-83a1-a25f00335e1d bridge virbr0 eno16777736 5a5f89ce-e54a-4f22-b843-3d438b093fbb 802-3-ethernet eno16777736 eth2 3a73717e-65ab-93e8-b518-24f5af32dc0d 802-3-ethernet -- team00_eth1 57d9b226-0a8a-4136-82fd-e1e241ae0f4a 802-3-ethernet eth1 [root@Centos7 ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.1.16.70 netmask 255.255.0.0 broadcast 10.1.255.255 inet6 fe80::20c:29ff:fe90:ef7d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:90:ef:7d txqueuelen 1000 (Ethernet) RX packets 139768 bytes 16052186 (15.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6473 bytes 1466854 (1.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:90:ef:87 txqueuelen 1000 (Ethernet) RX packets 128192 bytes 10403568 (9.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 89 bytes 11498 (11.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:90:ef:87 txqueuelen 1000 (Ethernet) RX packets 128311 bytes 10419999 (9.9 MiB) RX errors 0 dropped 91600 overruns 0 frame 0 TX packets 89 bytes 12365 (12.0 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 764 bytes 64000 (62.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 764 bytes 64000 (62.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 team_00: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.14.14.100 netmask 255.255.255.0 broadcast 172.14.14.255 inet6 fe80::20c:29ff:fe90:ef87 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:90:ef:87 txqueuelen 0 (Ethernet) RX packets 286 bytes 16228 (15.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 14 bytes 1746 (1.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:55:9d:3d txqueuelen 0 (Ethernet) 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 [root@Centos7 ~]# teamdctl team_00 state #查看team_00的狀態情況 setup: runner: roundrobin #網絡組模式 ports: eth1 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up #網卡eth1 鏈路情況 down count: 0 eth2 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up #網卡eth2 鏈路情況 down count: 0
※·網絡組的配置測試:
·在測試主機上使用:ping 172.14.14.100 -t -l 60000 (不停的ping 使用大的數據包)
C:\Users\hasee>ping 172.14.14.100 -t -l 60000 #測試ping情況 正在 Ping 172.14.14.100 具有 60000 字節的數據: 來自 172.14.14.100 的回復: 字節=60000 時間=1ms TTL=64 來自 172.14.14.100 的回復: 字節=60000 時間=3ms TTL=64 來自 172.14.14.100 的回復: 字節=60000 時間=6ms TTL=64 請求超時。 #由于測試down了eth2接口 來自 172.14.14.100 的回復: 字節=60000 時間=5ms TTL=64 來自 172.14.14.100 的回復: 字節=60000 時間=3ms TTL=64 來自 172.14.14.100 的回復: 字節=60000 時間=3ms TTL=64 172.14.14.100 的 Ping 統計信息: 數據包: 已發送 = 62,已接收 = 48,丟失 = 14 (22% 丟失), 往返行程的估計時間(以毫秒為單位): 最短 = 1ms,最長 = 7ms,平均 = 3ms C:\Users\hasee>
·down eth2后,有幾個數據包的丟失,實驗測試完成
[root@Centos7 ~]# teamdctl team_00 state #down eth2后,查看team_00情況 setup: runner: roundrobin ports: eth1 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up down count: 1 eth2 link watches: link summary: down instance[link_watch_0]: name: ethtool link: down down count: 1 [root@Centos7 ~]#
小結:網絡組實現的功能與bonding效果差不多,也可以實現網絡線路的備份。
原創文章,作者:linux_root,如若轉載,請注明出處:http://www.www58058.com/43830