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

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

環境以下:vim

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規則,從新添加
命令:
ssh

iptables -F

A機:tcp

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機:spa

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

端口轉發:3d

A:code

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和網關
blog

firewall-cmd實現:ip

A:
一、啓用IP轉發
get

vim /etc/sysctl.conf net.ipv4.ip_forward = 1

sysctl -p #命令生效

二、修改網卡的zone

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

三、設置IP地址假裝

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

四、設置NAT規則

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

五、重載Firewall使配置生效

firewall-cmd --reload 

六、端口映射

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

七、驗證:

root@aiker:/mnt/c/Users/aikera# ssh -p 1122 root@192.168.0.173 root@192.168.0.173's password: #輸入192.168.100.3的密碼 [root@aiker03 ~]#
相關文章
相關標籤/搜索