通過二層路由實現不同網段的虛擬機器的上網(包括排錯)
阿新 • • 發佈:2018-12-16
1.在真機上(使真機作為第二層路由器,虛擬機器通過真機做地址轉換從而可以上網)
[[email protected] ~]# ping www.baidu.com # 保證真機可以上網,ping通就代表可以上網
[[email protected] ~]# systemctl start firewalld # 因為libvirt服務開啟影響了火牆的開啟
^C
[[email protected] ~]# systemctl stop libvirtd.service
[[email protected] ~]# systemctl start firewalld
[ [email protected] ~]# systemctl start libvirtd.service
# 檢視路由功能是否開啟
[[email protected] ~]# firewall-cmd --list-all # 檢視到防火牆masquerade 策略關閉,即路由功能沒開啟
[[email protected] ~]# firewall-cmd --add-masquerade # 開啟路由功能
success
[[email protected] ~]# firewall-cmd --list-all
[[email protected] ~]# sysctl -a | grep ip_forward # 核心路由必須開啟,1表示開啟
net.ipv4.ip_forward = 1
注: 作為路由器 (1)核心路由功能必須開啟 (2)防火牆策略必須開啟
2.配置第一層路由器
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# vim ifcfg-eth0
1 DEVICE=eth0
2 ONBOOT=yes
3 BOOTPROTO=none
4 IPADDR0=172.25.254.142
5 PREFIX0=24
6 NAME=eth0
7 GATEWAY0=172.25.254.78 # 新增真機ip作為閘道器
[ [email protected] network-scripts]# systemctl restart network
[[email protected] network-scripts]# route -n
[[email protected] network-scripts]# ping 183.232.231.173 # ping 不通,可能是因為核心路由功能沒開啟
[[email protected] network-scripts]# sysctl -a | grep ip_forward # 檢視,果然是沒開啟,0代表沒開啟
net.ipv4.ip_forward = 0
[[email protected] network-scripts]# vim /etc/sysctl.conf # 開啟核心路由功能
net.ipv4.ip_forward = 1
[[email protected] network-scripts]# sysctl -p # 檢視是否更改成功
net.ipv4.ip_forward = 1
[[email protected] network-scripts]# ping 183.232.231.173 # 此時就可以ping 通了,即保證路由器可以上網,183.232.231.173是百度的域名解析地址
[[email protected] network-scripts]# firewall-cmd --add-masquerade # 開啟路由功能,幫助客戶端上網
success
[[email protected] network-scripts]# firewall-cmd --list-all
3.配置客戶端
[[email protected] Desktop]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# vim ifcfg-eth0
1 DEVICE=eth0
2 BOOTPROTO=none
3 ONBOOT=yes
4 IPADDR0=192.168.0.242
5 NETMASK=255.255.255.0
6 GATEWAY0=192.168.0.142
[[email protected] network-scripts]# systemctl restart network
[[email protected] network-scripts# route -n # 新增閘道器,保證可以和不同網段的主機通訊
[[email protected] network-scripts]# ping 183.232.231.188 # 可以ping通baidu,成功上網
[[email protected] network-scripts]# traceroute 183.232.231.173 # 追蹤路由,第一個是通過第一層路由通訊到第二層路由,再通過第二層路由訪問百度
# 在虛擬機器上測試上網,成功開啟網址,說明可以上網
4.排錯:
(1)如果網路配置都ok的話,但是ping不通路由器,一定是物理網絡卡壞了,刪除網絡卡,重新新增網絡卡
(2)檢視路由器的核心路由功能是否開啟,確保全部開啟
(3)檢視路由器的火牆策略是否開啟
(4)檢視客戶端的閘道器是否設定正確