点滴记忆---观雨亭
CENTOS7开启NAT【抄】
2022-1-16 eehello

抄自:https://my.oschina.net/golang/blog/3007610

https://pealog.com/457.html


# 开启 NAT 转发
firewall-cmd --permanent --zone=public --add-masquerade

# 开放 DNS 使用的 53 端口,UDP
# 必须,否则其他机器无法进行域名解析
firewall-cmd --zone=public --add-port=80/tcp --permanent

# 检查是否允许 NAT 转发
firewall-cmd --query-masquerade
# 禁止防火墙 NAT 转发
firewall-cmd --remove-masquerade


端口转发:

# 将80端口的流量转发至8080
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080

# 将80端口的流量转发至192.168.0.1
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.0.1

# 将80端口的流量转发至192.168.0.1的8080端口
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.0.1:toport=8080



第二种方法(CentOS7中就不存在/etc/sysctl.conf文件,未继续验证):

开启ip_forward转发 ,在/etc/sysctl.conf配置文件尾部添加

    net.ipv4.ip_forward=1

然后让其生效

    sysctl -p

执行firewalld命令进行转发:

    firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o enoxxxxxx -j MASQUERADE -s 192.168.1.0/24

注意enoxxxxxx对应外网网口名称。
重启防火墙

    systemctl restart firewalld.service

设置外网服务器的IP地址为192.168.1.1,然后,在内网服务器中对配置文件/etc/sysconfig/network-scripts/ifcfg-enxxxxxx进行修改,设置网卡的网关为外网服务器的IP地址:

    ONBOOT=yes
    IPADDR=192.168.1.2
    PREFIX=24
    GATEWAY=192.168.1.1
    DNS1=119.29.29.29

重启内网服务器的网络服务,即可访问外网了。

    systemctl restart network.service

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容