最近有個小項(xiàng)目,評估某公司的windows環(huán)境,因此需要搭建一個測試環(huán)境,為方便測試,將一臺裝有CentOS 7的VM配置為測試環(huán)境的NAT網(wǎng)關(guān)。
實(shí)驗(yàn)環(huán)境
- 測試環(huán)境網(wǎng)絡(luò):192.168.200.0/24
- 物理機(jī)所在網(wǎng)絡(luò):192.168.1.0/24
- CentOS 7網(wǎng)絡(luò)配置:
ens33:192.168.1.254/24
ens36: 192.168.200.254/24
配置過程
開啟IPv4路由轉(zhuǎn)發(fā)功能
/etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
以上配置文件修改后,重啟后生效,若不想重啟,可執(zhí)行如下命令
sysctl -w net.ipv4.ip_forward=1
Centos 7 上面的ens33接口用于連接物理網(wǎng)絡(luò)并為測試網(wǎng)絡(luò)提供NAT服務(wù),ens36屬于測試網(wǎng)絡(luò)網(wǎng)段,作為測試網(wǎng)絡(luò)段內(nèi)主機(jī)的網(wǎng)關(guān),將兩快網(wǎng)卡分別添加進(jìn)external和internal zone中并重新載入生效
firewall-cmd --zone=external --add-interface=ens33 --permanent
firewall-cmd --zone=internal --add-interface=ens36 --permanent
firewall-cmd --complete-reload
查看并確認(rèn)對應(yīng)的網(wǎng)卡在相應(yīng)的zone中
external (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:
internal (active)
target: default
icmp-block-inversion: no
interfaces: ens36
sources:
services: dhcpv6-client mdns samba-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
在external zone開啟masquerade為配置NAT做準(zhǔn)備
firewall-cmd --zone=external --add-masquerade --permanent
配置NAT規(guī)則
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o ens33 -j MASQUERADE -s 192.168.200.0/24
# 最后重新載入生效
firewall-cmd --complete-reload
以上配置完成后,位于192.168.200.0/24網(wǎng)段的機(jī)器將能夠通過配置的NAT網(wǎng)關(guān)進(jìn)行Internet訪問,需要注意的一點(diǎn)是,位于網(wǎng)關(guān)背后的機(jī)器的默認(rèn)網(wǎng)關(guān),務(wù)必設(shè)置為NAT server的內(nèi)網(wǎng)接口的IP地址
由于我這臺NAT server還會作為PXE的相關(guān)服務(wù)器,因此如下服務(wù)在internal zone放行,如果看官有相應(yīng)需求,可作為參考
firewall-cmd --permanent --zone=internal --add-service=dhcp
firewall-cmd --permanent --zone=internal --add-service=tftp
firewall-cmd --permanent --zone=internal --add-service=dns
firewall-cmd --permanent --zone=internal --add-service=http
firewall-cmd --permanent --zone=internal --add-service=nfs
firewall-cmd --permanent --zone=internal --add-service=ssh