首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iptables和firewall-cmd实现nat转发配置

iptables和firewall-cmd实现nat转发配置

作者头像
拓荒者
发布2019-03-15 16:52:29
2.2K0
发布2019-03-15 16:52:29
举报

iptables和firewall-cmd实现nat转发配置

环境如下:

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 root@192.168.0.173
root@192.168.0.173‘s password:       #输入192.168.100.3的密码
[root@aiker03 ~]#
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iptables和firewall-cmd实现nat转发配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档