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

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

時間:2018-02-13 13:30:38      閱讀:275      評論:0      收藏:0      [點我收藏+]html

 

標籤:修改   roo   forward   驗證   ipv4   外網   routing   nat   external   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:22ssh

iptables實現:
注意:若是不能成功須要清空iptables規則,從新添加
命令:tcp

iptables -F

A機:spa

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

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

端口轉發:htm

A:blog

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

firewall-cmd實現:

A:
一、啓用IP轉發

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 ~]#

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

標籤:修改   roo   forward   驗證   ipv4   外網   routing   nat   external   

原文地址:http://blog.51cto.com/m51cto/2071495

(0)

(0)

   

舉報

相關文章
相關標籤/搜索