環境如下:
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實作:
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
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 [email protected]
[email protected]'s password: #輸入192.168.100.3的密碼
[root@aiker03 ~]#