博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iptables和firewall-cmd实现nat转发配置
阅读量:6996 次
发布时间:2019-06-27

本文共 1464 字,大约阅读时间需要 4 分钟。

环境如下:

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_forwardiptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE

B机:

ifconfig eth1 192.168.100.3/24  #临时设置iproute add default  gw 192.168.100.1 #设置网关

端口转发:

A:

echo "1">/proc/sys/net/ipv4/ip_forwardiptables -t nat -A PREROUTING -d 192.168.0.173 -p tcp --dport 1122 -j DNAT --to 192.168.100.3:22iptables -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 root@192.168.0.173root@192.168.0.173's password:       #输入192.168.100.3的密码[root@aiker03 ~]#

转载于:https://blog.51cto.com/m51cto/2071495

你可能感兴趣的文章