1. 程式人生 > >iptables和firewall-cmd實現nat轉發配置

iptables和firewall-cmd實現nat轉發配置

-a ade route toa prot fire 修改網卡 rec config

環境如下:

A機器兩塊網卡eth0(192.168.0.173)、eth1(192.168.100.1),eth0可以上外網,eth1僅僅是內部網絡,B機器只有eth1(192.168.100.3),和A機器eth1可以通信互聯。
需求讓B機器可以連接外網,端口轉發,通過A:1122連接B:22

iptables實現:
註意:如果不能成功需要清空iptables規則,重新添加
命令:

iptables -F

A機:

ifconfig eth1 192.168.100.1/24 #臨時設置IP
echo "1">/proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE 

B機:


ifconfig eth1 192.168.100.3/24  #臨時設置ip
route add default  gw 192.168.100.1 #設置網關

端口轉發:

A:

echo "1">/proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -d 192.168.0.173 -p tcp --dport 1122 -j DNAT --to 192.168.100.3:22
iptables -t nat -A POSTROUTING -s 192.168.100.3 -j SNAT --to 192.168.0.173

B:
設置主機的IP和網關

firewall-cmd實現:

A:
1、啟用IP轉發

vim /etc/sysctl.conf 

net.ipv4.ip_forward = 1

sysctl -p #命令生效

2、修改網卡的zone

firewall-cmd --permanent --zone=external --change-interface=eth0 
firewall-cmd --permanent --zone=internal --change-interface=eth1

3、設置IP地址偽裝

firewall-cmd --zone=external --add-masquerade --permanent

4、設置NAT規則

firewall-cmd --permanent --direct --passthrough ipv4 -t nat POSTROUTING -o eth0 -j MASQUERADE -s 192.168.100.0/24

5、重載Firewall使配置生效

firewall-cmd --reload 

6、端口映射

firewall-cmd --zone=external --add-forward-port=port=1122:proto=tcp:toport=22:toaddr=192.168.100.3 --permanent
firewall-cmd --reload 

7、驗證:

root@aiker:/mnt/c/Users/aikera# ssh -p 1122 [email protected]
[email protected]‘s password:       #輸入192.168.100.3的密碼
[root@aiker03 ~]#

iptables和firewall-cmd實現nat轉發配置