正向代理

环境:

需要准备两台虚拟机

需要每台有两个网卡 内网网卡和外网网卡

 

 

 

 

内网网段 172.16.1.0/24  网卡 ens37

外网网段 10.0.0.0/24    网卡   ens33

可以访问外网的  内网网络IP地址 172.16.1.72

可以访问外网的  外网网络IP地址 10.0.0.72

不可以访问外网的  内网网络IP地址 172.16.1.73

不可以访问外网的  外网网络IP地址 10.0.0.73

 

 

 第一步 在可以访问外网的服务器上 开启IP转发

1.开启IP转发

1
2
3
4
[root@ localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@ localhost ~]# sysctl -p   //让配置生效
net.ipv4.ip_forward = 1

2. 开启内网转发 (先开启firewalld)  

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@ localhost ~]# systemctl status firewalld  //查看状态
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@ localhost ~]# systemctl start firewalld // 开启防火墙
 
[root@ localhost ~]# firewall-cmd --add-masquerade --permanent
success
[root@ localhost ~]# firewall-cmd --permanent --direct --passthrough ipv4 -t net -I POSTROUTING -o ens37 -j MASQUERADE -s 172.16.1.72/24   //ens37 可访问外网的 内网网卡  172.16.1.72 可访问外网的 内网IP地址
Success
[root@ localhost ~]# firewall-cmd --reload  //生效
success  

第二步 在不可以访问外网的服务器上操作 

1.停掉访问外网的网卡  注:此操作后 xshell不能连接了 需要到虚拟机上进行下一步操作

1
[root@ localhost ~]# ifdown ens33

2.更改内网服务器的网卡 (添加网关)2.1 2.2 选一个就好

2.1 临时修改 

1
[root@ localhost ~]# route add default gw 172.16.1.72 dev ens37

2.2 永久修改 注:172.16.1.42 为可访问外网的虚拟机的 内网IP地址

1
[root@ localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens37

 

 

 

 

 

3.启动的时候 为了防止ens33也启动 

1
2
[root@ localhost ~]# systemctl restart network
[root@ localhost ~]# ifdown ens33 //需要再次关闭ens33  

第三步:测试ens33 没有开启的时候还可不可以ping

1.

 

 2. 用xshell测试 ,可以用另一台主机ssh远程登录进去

 

posted @ 2020-02-12 20:05  相磊  阅读(142)  评论(0编辑  收藏  举报