實驗:路由連接與轉發
1、五臺機器,其中三臺充當路由器,剩餘兩臺作爲主機
VMnet1 192.168.41.0
VMnet4 192.168.141.0
VMnet5 172.26.41.0
VMnet6 10.41.41.0
主機1 centos6 :192.168.41.171
適配器3:VMnet1 192.168.41.0
路由:
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.41.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1004 0 0 eth2
0.0.0.0 192.168.41.161 0.0.0.0 UG 0 0 0 eth2
路由器1 centos6 :192.168.41.161 172.26.41.161
適配器:VMnet1 192.168.41.0
適配器5:VMnet5 172.26.41.0
路由:
Destination Gateway Genmask Flags Metric Ref Use Iface
172.26.41.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.41.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
0.0.0.0 172.26.41.162 0.0.0.0 UG 0 0 0 eth1
路由器2 centos7 :172.26.41.162 10.41.41.162
適配器5:VMnet1 172.26.41.0
適配器6:VMnet5 10.41.41.0
路由:
Destination Gateway Genmask Flags Metric Ref Use Iface
10.41.41.0 0.0.0.0 255.255.255.0 U 102 0 0 ens38
172.26.41.0 0.0.0.0 255.255.255.0 U 101 0 0 ens37
192.168.41.0 172.26.41.161 255.255.255.0 UG 0 0 0 ens37
192.168.141.0 10.41.41.163 255.255.255.0 UG 0 0 0 ens38
路由器3 centos7 :10.41.41.163 192.168.141.163
適配器6:VMnet5 172.26.41.0
適配器4:VMnet4 192.168.141.0
路由:
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.41.41.162 0.0.0.0 UG 0 0 0 ens37
10.41.41.0 0.0.0.0 255.255.255.0 U 101 0 0 ens37
192.168.141.0 0.0.0.0 255.255.255.0 U 102 0 0 ens38
主機2 centos7 :192.168.141.164
適配器4:VMnet4 192.168.141.0
路由:
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.141.163 0.0.0.0 UG 0 0 0 ens38
169.254.0.0 0.0.0.0 255.255.0.0 U 1004 0 0 ens38
192.168.141.0 0.0.0.0 255.255.255.0 U 0 0 0 ens38
2、實驗2 6臺機器實現路由轉發
1、虛擬網卡規劃:
使用到的虛擬網卡全部爲僅主機,對應地址和設置爲
VMnet1 192.168.41.0/24
VMnet3 10.41.141.0/24
VMnet4 192.168.141.0/24
VMnet5 172.26.41.0/24
VMnet6 10.41.41.0/24
2、實驗機器6臺
1、3、5號機器爲centos6
2、4、6號機器爲centos7
IP地址分配與配置(所有的網卡配置都不配置網卡的網關):
機器1: 網卡1:VMnet1 IP:192.168.41.221
機器2: 網卡1:VMnet1 IP:192.168.41.222
網卡2:VMnet3 IP:10.41.141.222
機器3: 網卡1:VMnet3 IP:10.41.141.223
網卡2:VMnet4 IP:192.168.141.223
機器4、 網卡1:VMnet4 IP:192.168.141.224
網卡2:VMnet5 IP:172.26.41.224
機器5、 網卡1:VMnet5 IP:172.26.41.225
網卡2:VMnet6 IP:10.41.41.225
機器6、 網卡1:VMnet6 IP:10.41.41.226
3、主機配置修改
所有機器統一修改設置:
所有的主機關閉selinux:
直接編輯修改配置文件/etc/selinux/config,修改爲
SELINUX=disabled
或者直接命令修改
sed -i.bak 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config;setenforce 0;getenforce
所有的主機的iptables:
Centos 6
#service iptables stop;chkconfig iptables off;service iptables status
顯示如下爲成功
iptables: Firewall is not running.
Centos 7
#systemctl stop firewalld.service ;systemctl disable firewalld.service ;systemctl status firewalld.service|grep Active
顯示如下爲成功
Active: inactive (dead)
開啓路由轉發功能:注意/proc/sys/net/ipv4/ip_forward的值,實驗過程中可能會變化,
測試路由時必須確認是否爲1
echo 1 > /proc/sys/net/ipv4/ip_forward
cat /proc/sys/net/ipv4/ip_forward
配置信息檢查:
centos7檢查的方法:
cat /proc/sys/net/ipv4/ip_forward;echo -e "\n";systemctl status firewalld;echo -e;getenforce
擴展:
下面的顯示出的結果顯示顏色 結果分別爲1 、dead 、Disabled三個紅色部分
echo 1 > /proc/sys/net/ipv4/ip_forward;cat /proc/sys/net/ipv4/ip_forward| grep 1;echo -e "\n";systemctl status firewalld|grep dead;echo -e;getenforce|grep -i 'disabled'
centos6檢查的方法:
cat /proc/sys/net/ipv4/ip_forward;echo -e "\n";service iptables status;echo -e;getenforce
擴展:
下面的顯示出的結果顯示顏色 結果分別爲1 、not running 、Disabled三個紅色部分
需要先設置grep別名,設置了的直接執行下面命令即可,否則不顯示顏色
設置別名:alias grep='grep --color=auto'
echo 1 > /proc/sys/net/ipv4/ip_forward;cat /proc/sys/net/ipv4/ip_forward|grep 1;echo -e "\n";service iptables status|grep 'not running';echo -e;getenforce|grep -i 'disabled'
4、使用到的相關命令使用方法
ip route命令:
關於路由的相關設置:ip route
查看路由信息:ip route show
添加一條路由
# ip route add 10.10.10.0/24 via 172.16.0.254 dev eth0
刪除一條路由
# ip route del 10.10.10.0/24
添加默認路由
# ip route add default via 172.16.0.254 dev eth0
刪除默認路由
# ip route del default via 172.16.0.254 dev eth0
route命令:
查看路由:route -n
添加路由:route add -net 192.168.146.0/24 gw 192.168.192.200
刪除路由:route del -net 192.168.192.0/24 gw 192.168.166.200
添加默認路由:route add default gw 192.168.192.200
刪除默認路由:route del default gw 192.168.192.200
5、修改路由表
機器1:
網卡1:VMnet1 IP:192.168.41.221 網卡1名稱:eth1
因爲沒有配置網卡的網關,所以需要制定默認路由,機器1可以設置默認路由(如果設置了默認網關,
不需要再配置默認路由)
添加默認路由:
route add default gw 192.168.41.222
結果如下:
[root@centos6 ~]# route add default gw 192.168.41.222
[root@centos6 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.41.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth1
0.0.0.0 192.168.41.222 0.0.0.0 UG 0 0 0 eth1
機器2: 網卡1:VMnet1 IP:192.168.41.222 網卡1名稱:ens33
網卡2:VMnet3 IP:10.41.141.222 網卡2名稱:ens37
因爲機器2和機器1同一個網段,它們不需要設置路由,因此機器2只需要設置一個默認路由即可
配置轉發
echo 1 > /proc/sys/net/ipv4/ip_forward;cat /proc/sys/net/ipv4/ip_forward
配置路由:
ip route add default via 10.41.141.223 dev ens37
結果如下:
[root@centos7ys ~]# ip route add default via 10.41.141.223 dev ens37
[root@centos7ys ~]# ip route show
default via 10.41.141.223 dev ens37
10.41.141.0/24 dev ens37 proto kernel scope link src 10.41.141.222 metric 101
192.168.41.0/24 dev ens33 proto kernel scope link src 192.168.41.222 metric 100
機器3: 網卡1:VMnet3 IP:10.41.141.223 網卡1名稱:eth2
網卡2:VMnet4 IP:192.168.141.223 網卡2名稱:eth1
因爲機器3屬於中間的路由器,需要向兩個方向轉發路由,因此需要配置多條路由
配置轉發
echo 1 > /proc/sys/net/ipv4/ip_forward;cat /proc/sys/net/ipv4/ip_forward
配置路由:
route add -net 172.26.41.0/24 gw 192.168.141.224
route add -net 192.168.41.0/24 gw 10.41.141.222
* route add -net 172.26.41.0/24 gw 192.168.141.224
擴展:
可以合併上述其中的兩條記錄爲一條,即將 192.168.141.224設置爲默認路由
route add default gw 192.168.141.224
route add -net 192.168.41.0/24 gw 10.41.141.222
結果如下:
[root@centos6 ~]# route add -net 172.26.41.0/24 gw 192.168.141.224
[root@centos6 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.26.41.0 192.168.141.224 255.255.255.0 UG 0 0 0 eth1
192.168.141.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
10.41.141.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
[root@centos6 ~]#
[root@centos6 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.26.41.0 192.168.141.224 255.255.255.0 UG 0 0 0 eth1
192.168.141.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.41.0 10.41.141.222 255.255.255.0 UG 0 0 0 eth2
10.41.141.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
[root@centos6 ~]# route add -net 10.41.41.0/24 gw 192.168.141.224
[root@centos6 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.26.41.0 192.168.141.224 255.255.255.0 UG 0 0 0 eth1
10.41.41.0 192.168.141.224 255.255.255.0 UG 0 0 0 eth1
192.168.141.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.41.0 10.41.141.222 255.255.255.0 UG 0 0 0 eth2
10.41.141.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
機器4、 網卡1:VMnet4 IP:192.168.141.224 網卡1名稱:ens33
網卡2:VMnet5 IP:172.26.41.224 網卡2名稱:ens37
因爲機器4屬於中間的路由器,需要向兩個方向轉發路由,因此需要配置多條路由
配置轉發
echo 1 > /proc/sys/net/ipv4/ip_forward;cat /proc/sys/net/ipv4/ip_forward
配置路由:
ip route add 10.41.141.0/24 via 192.168.141.223
ip route add 10.41.41.0/24 via 172.26.41.225
* ip route add 192.168.41.0/24 via 192.168.141.223
擴展:
可以合併上述其中的兩條記錄爲一條,即將 192.168.141.223設置爲默認路由
ip route add default via 192.168.141.223
ip route add 10.41.41.0/24 via 172.26.41.225
結果如下:
[root@centos7ys ~]# ip route add 10.41.141.0/24 via 192.168.141.223
[root@centos7ys ~]# ip route add 10.41.41.0/24 via 172.26.41.225
[root@centos7ys ~]# ip route show
10.41.41.0/24 via 172.26.41.225 dev ens37
10.41.141.0/24 via 192.168.141.223 dev ens33
172.26.41.0/24 dev ens37 proto kernel scope link src 172.26.41.224 metric 101
192.168.41.0/24 via 192.168.141.223 dev ens33
192.168.141.0/24 dev ens33 proto kernel scope link src 192.168.141.224 metric 100
機器5、 網卡1:VMnet5 IP:172.26.41.225 網卡1名稱:eth1
網卡2:VMnet6 IP:10.41.41.225 網卡2名稱:eth2
因爲機器5和機器6同一個網段,它們之間不需要設置路由,因此機器5只需要設置一個默認路由即可
配置轉發
echo 1 > /proc/sys/net/ipv4/ip_forward;cat /proc/sys/net/ipv4/ip_forward
配置路由:
route add default gw 172.26.41.224
結果如下:
[root@centos6 ~]# route add default gw 172.26.41.224
[root@centos6 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.26.41.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
10.41.41.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth2
0.0.0.0 172.26.41.224 0.0.0.0 UG 0 0 0 eth1
機器6、 網卡1:VMnet6 IP:10.41.41.226 網卡1名稱:ens33
因爲沒有配置網卡的網關,所以需要制定默認路由,機器6可以設置默認路由(如果設置了默認網關,不需要再配置默認路由)
添加默認路由:
ip route add default via 10.41.41.225
結果如下:
[root@centos7ys ~]# ip route add default via 10.41.41.225
[root@centos7ys ~]# ip route show
default via 10.41.41.225 dev ens33
10.41.41.0/24 dev ens33 proto kernel scope link src 10.41.41.226 metric 100
6、最終結果:
從1到6
ping結果:
[root@centos6 ~]# ping 10.41.41.226 -c 6
PING 10.41.41.226 (10.41.41.226) 56(84) bytes of data.
64 bytes from 10.41.41.226: icmp_seq=1 ttl=60 time=2.53 ms
64 bytes from 10.41.41.226: icmp_seq=2 ttl=60 time=3.17 ms
64 bytes from 10.41.41.226: icmp_seq=3 ttl=60 time=3.21 ms
64 bytes from 10.41.41.226: icmp_seq=4 ttl=60 time=3.43 ms
64 bytes from 10.41.41.226: icmp_seq=5 ttl=60 time=3.39 ms
64 bytes from 10.41.41.226: icmp_seq=6 ttl=60 time=6.93 ms
--- 10.41.41.226 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5017ms
rtt min/avg/max/mdev = 2.536/3.782/6.939/1.443 ms
traceroute結果:
[root@centos6 ~]# traceroute 10.41.41.226
traceroute to 10.41.41.226 (10.41.41.226), 30 hops max, 60 byte packets
1 192.168.41.222 (192.168.41.222) 0.224 ms 0.177 ms 0.144 ms
2 10.41.141.223 (10.41.141.223) 0.403 ms 0.861 ms 0.816 ms
3 192.168.141.224 (192.168.141.224) 0.613 ms 0.598 ms 0.532 ms
4 172.26.41.225 (172.26.41.225) 1.166 ms 1.615 ms 1.583 ms
5 10.41.41.226 (10.41.41.226) 2.507 ms !X 2.480 ms !X 2.426 ms !X
從6到1
ping結果
[root@centos7ys ~]# ping 192.168.41.221 -c 6
PING 192.168.41.221 (192.168.41.221) 56(84) bytes of data.
64 bytes from 192.168.41.221: icmp_seq=1 ttl=60 time=2.24 ms
64 bytes from 192.168.41.221: icmp_seq=2 ttl=60 time=6.62 ms
64 bytes from 192.168.41.221: icmp_seq=3 ttl=60 time=3.16 ms
64 bytes from 192.168.41.221: icmp_seq=4 ttl=60 time=3.20 ms
64 bytes from 192.168.41.221: icmp_seq=5 ttl=60 time=5.68 ms
64 bytes from 192.168.41.221: icmp_seq=6 ttl=60 time=4.55 ms
--- 192.168.41.221 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5012ms
rtt min/avg/max/mdev = 2.240/4.244/6.626/1.534 ms
traceroute結果:
[root@centos7ys ~]# traceroute 192.168.41.221
traceroute to 192.168.41.221 (192.168.41.221), 30 hops max, 60 byte packets
1 gateway (10.41.41.225) 0.157 ms 0.091 ms 0.042 ms
2 172.26.41.224 (172.26.41.224) 0.241 ms 0.210 ms 0.187 ms
3 192.168.141.223 (192.168.141.223) 0.544 ms 0.506 ms 0.467 ms
4 10.41.141.222 (10.41.141.222) 0.523 ms 0.474 ms 0.406 ms
5 192.168.41.221 (192.168.41.221) 0.508 ms !X 0.611 ms !X 0.557 ms !X