你的位置:首页 > Linux系统
很多时候我们线上服务器并没有那么多的公网IP可用,有些机器也不需要连接公网,在有需要的时候可以用一台有公网IP的内网机器做路由来连接公网,在CentOS7下实现非常简单。
一、假设A机器有公网IP地址可以连接公网, 需要在A机器开启转发
vi /etc/sysctl.conf 添加: net.ipv4.ip_forward = 1
或者直接
echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
执行sysctl -p使之生效
[root@localhost ~]# sysctl -p net.ipv4.ip_forward = 1
二、A机器公网网卡
2.1、
执行 firewall-cmd --permanent --zone=external --change-interface=ens32(注意,此处改为你的公网网卡名称,此例公网网卡:ens32,可以用ifconfig或ip addr查看)
[root@localhost ~]# firewall-cmd --permanent --zone=external --change-interface=ens32 The interface is under control of NetworkManager, setting zone to 'external'. success
2.2、
执行 firewall-cmd --zone=external --list-all
[root@localhost ~]# firewall-cmd --zone=external --list-all external (active) target: default icmp-block-inversion: no interfaces: ens32 sources: services: ssh ports: protocols: masquerade: yes forward-ports: source-ports: icmp-blocks: rich rules:
三、A机器内网网卡
3.1、
执行 firewall-cmd --permanent --zone=internal --change-interface=ens33(注意,此处改为你的内网网卡名称,此例内网网卡:ens33,可以用ifconfig或ip addr查看)
[root@localhost ~]# firewall-cmd --permanent --zone=internal --change-interface=ens33 The interface is under control of NetworkManager, setting zone to 'internal'. success
3.2、
执行 firewall-cmd --zone=internal --list-all
[root@localhost ~]# firewall-cmd --zone=internal --list-all internal (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh mdns samba-client dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
3.3、
执行 firewall-cmd --complete-reload 使之生效
[root@localhost ~]# firewall-cmd --complete-reload success
四、其他内网机器把网关设置为A机器的内网IP地址即可上网,如下:
cat /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=d29d1206-27f9-4070-8537-54f0389347c5 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.1.34 NETMASK=255.255.255.0 GATEWAY=192.168.1.26 DNS1=114.114.114.114 DNS2=223.5.5.5 IPV6_PRIVACY=no
重新启动网络服务
systemctl restart network
测试内网192.168.1.34已经可以正常连接公网,是不是很简单。

- 发表评论
- 查看评论
【暂无评论!】发表评论: