内网服务器实现共享上网

1.准备工作

主机 IP地址 内网地址 备注
node1 10.0.0.10 172.16.1.10 管理机
node2 10.0.0.11 172.16.1.11 内网机
##两台服务器都必须添加内网地址(172.16.1.0/24)

##两台主机必须做hosts解析
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
node1 10.0.0.10 
node2 10.0.0.11
#管理机使用scp -rp把hosts发送给内网机
scp -rp /etc/hosts root@10.0.0.11:/etc/hosts

##管理机发送秘钥给内网机
#生成秘钥
[root@node1 ~]$ ssh-keygen
#推送到别的节点
ssh-copy-id root@10.0.0.11
#免秘钥连接
ssh root@node2

##免秘钥连接成功后关闭内网机的eth0的网卡
ifdown eth0 关闭eth0网络接口
ifup eth0 打开eth0网络接口
systemctl restart network

2.在批量服务器node1上执行

第一个历程:内网172.16.1.0网段内网通过10.0.0.11进行转发
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to 10.0.0.11

第二个历程:管理机器上打开转发
echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
sysctl -p

3.在内网机器上,更改其网关为 10.0.0.11

vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY=10.0.0.11

最后重启网络服务即可
systemctl restart network

最后的现象:(从11跳转到外网)
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=21.2 ms
From gateway (10.0.0.11) icmp_seq=2 Redirect Host(New nexthop: 10.0.0.2 (10.0.0.2))
From gateway (10.0.0.11): icmp_seq=2 Redirect Host(New nexthop: 10.0.0.2 (10.0.0.2))
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=26.1 ms

你可能感兴趣的:(网络,网关,局域网,iptables)