阿里云CentOS7 搭建 VPN

一.  目标思路

1. 用阿里云ECS美国服务器, 在CentOS 7系统上,架设VPN服务

2. 用PC/iPhone6+ 连接使用VPN服务(阿里云美国服务器)

3. 用VPN服务访问国外网络,如Google,Facebook

二. 架设VPN

CentOS 7 系统版本

uname -a
Linux mimvp_usa 3.10.0-327.18.2.el7.x86_64 #1 SMP Thu May 12 11:03:55 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

 1. 安装 ppp pptpd iptables

ppp 数据链路层协议

pptpd VPN服务类型之一

iptables 防火墙,用来消息转发

sudo yum -y install ppp pptpd iptables

安装后版本信息

Package ppp-2.4.5-33.el7.x86_64 already installed and latest version
Package pptpd-1.4.0-2.el7.x86_64 already installed and latest version
Package iptables-1.4.21-16.el7.x86_64 already installed and latest version

此处,AWS EC2 默认不支持 pptpd 源,提示错误: No package pptpd available

解决办法:

方法一: 下载rpm包直接安装(推荐)

1
2
3
4
5
# 针对EL6.x版本:
wget -c http: //poptop .sourceforge.net /yum/stable/packages/pptpd-1 .4.0-1.el6.x86_64.rpm
# 针对EL7.x版本:
wget -c http: //dl .fedoraproject.org /pub/epel/7/x86_64/p/pptpd-1 .4.0-2.el7.x86_64.rpm
rpm -ivh pptpd-1.4.0-1.el6.x86_64.rpm   # 安装显示安装进度--install--verbose--hash

 

方法二: 需要添加新的源

1
2
3
4
5
6
7
8
9
10
yum repolist   # 查看yum源列表
 
# 只针对EL7版本:
yum localinstall http: //dl .fedoraproject.org /pub/epel/7/x86_64/e/epel-release-7-5 .noarch.rpm -y
 
yum makecache   # 将服务器上的软件包信息下载到本地缓存, 以提高搜索和安装软件的速度
yum repolist   # 可以再次查看新加入的列表
yum  install pptpd -y   # 再次执行安装pptpd
 
# 可用 yum-config-manager --disable <repoid> 删除源

 

2. 配置 ppp DNS信息

a. 编辑  options.pptpd 配置文件

sudo vim /etc/ppp/options.pptpd

b. 查找 ms-dns

#ms-dns 10.0.0.1
#ms-dns 10.0.0.2

c. 添加两行 ms-dns,配置后结果如下:

#ms-dns 10.0.0.1
#ms-dns 10.0.0.2
ms-dns 8.8.8.8
ms-dns 8.8.4.4

上面使用的是Google发布的Public DNS,也可以修改为OpenDNS的IP,即
ms-dns 208.67.222.222 
ms-dns 208.67.220.220

 

3. 配置 ppp VPN账号和密码

a. 编辑 chap-secrets 配置文件

sudo vim /etc/ppp/chap-secrets

b.设置 VPN账号 + 服务类型 + VPN密码 + IP

若IP为*则代表所有IP都可以使用该账号密码,配置后结果如下:
# Secrets for authentication using CHAP
# client    server    secret            IP addresses

   vpnuser  pptpd    Passwd@2018    *

本例中账号为 vpnuser ; 密码为 Passwd@2018

 4. 配置 pptpd

a.编辑 options.pptpd 配置文件

sudo vim /etc/pptpd.conf

b. 查找 localip,删去掉 localip 和 remoteip 前的 #,修改后如下:

# (Recommended)
localip 192.168.12.1
remoteip 192.168.12.234-238,192.168.12.245

# or
#localip 192.168.0.234-238,192.168.0.245
#remoteip 192.168.1.234-238,192.168.1.245

 

5. 配置内核支持转发

a. 编辑 sysctl.conf 配置文件

sudo vim /etc/sysctl.conf

b. 在末尾添加一行

net.ipv4.ip_forward=1

c. 配置后结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
 
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce=2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv4.conf.lo.arp_announce=2
 
net.ipv4.ip_forward=1

 d. 保存,退出,重新加载内核配置项

sudo sysctl -p

运行结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# sudo sysctl -p
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.ip_forward = 1

 

6. 配置 iptables

CentOS 7 默认不支持 service iptables status  和  /etc/init.d/iptables  status

CentOS 7 支持 systemctl status  iptables.service

因此,需要安装 iptables.service ,见:CentOS 7 安装 iptables 防火墙

sudo yum update iptables
sudo yum -y install iptables iptables-services

 a. iptables 使用方法

查看iptables运行状态

sudo systemctl status  iptables.service

启动iptables

sudo systemctl start  iptables.service

查看 iptables 过滤规则

sudo iptables -L -n

清空防火墙配置

1
2
3
4
sudo iptables -P INPUT ACCEPT         # 改成 ACCEPT 标示接收一切请求
sudo iptables -F                      # 清空默认所有规则
sudo iptables -X                      # 清空自定义所有规则
sudo iptables -Z                      # 计数器置0

配置规则

1
2
3
4
sudo iptables -A INPUT -i lo -j ACCEPT                                  # 允许127.0.0.1访问本地服务
sudo iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT           # 允许访问外部服务
sudo iptables -A INPUT -p icmp -m icmp --icmp- type 8 -j ACCEPT          # 允许 ping
sudo iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT    # 开启 ssh 端口

若需要配置其他端口,比如 80 3306 8080 参考 ssh 端口配置方式

 

b. 配置 iptables 转发策略(暂时不做,看看能否上网,再决定)

由于阿里云是双网卡,内网eth0 + 外网eth1,所以这块特别容易误写为eth0

/sbin/iptables -t nat -A POSTROUTING -s 192.168.12.0/24 -o eth1 -j SNAT --to-source eth1-ip地址

/sbin/iptables -t nat -A POSTROUTING -s 10.175.249.0/24 -o eth0 -j SNAT --to-source eth0-ip地址

/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

c. 设置VPN端口策略

iptables -A INPUT -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 1723-j ACCEPT

iptables -A INPUT -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 53-j ACCEPTiptables -A INPUT -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 47-j ACCEPT
iptables -A INPUT -p gre -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT----------------------------------------------
pptpd服务器连接上网解决办法
iptables -Fiptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.12.0/24 -o eth0 -j MASQUERADEiptables -I FORWARD -p tcp --syn -i ppp+ -j TCPMSS --set-mss 1396

----------------------------------------------------

ifconfig

ppp0      Link encap:Point-to-Point Protocol

          inet addr:192.168.0.1  P-t-P:192.168.0.235  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1396  Metric:1
          RX packets:14295 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12560 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:1429603 (1.4 MB)  TX bytes:5145792 (5.1 MB)

d.保存 iptables 选项,并重启 iptables

sudo service iptables save

sudo systemctl restart  iptables.service

e.如果想让iptables自动加载以上模块,iptables启动时默认是不加载模块的,这样就会导致连接时报619无法验证密码的错误.
vi /etc/sysconfig/iptables-config 

在IPTABLES_MODULES=""字段中添加以上模块。

IPTABLES_MODULES="ip_nat_pptp ip_nat_proto_gre"

重启iptables即可

7. 重启 iptables, pptpd

1
2
3
4
5
sudo service iptables restart       # 重新启动 iptables      systemctl restart  iptables.service
sudo service pptpd restart          # 重新启动 pptpd         systemctl restart  pptpd.service
sudo chkconfig iptables on          # 开机启动 iptables      systemctl enable iptables.service
sudo chkconfig pptpd on             # 开机启动 pptpd         systemctl enable pptpd.service
sudo iptables -P INPUT DROP         # 加载防火墙策略

 

8. 查看开机启动 iptables, pptpd

systemctl list-unit-files | grep enabled

crond.service                          enabled 
cups.service                           enabled 

iptables.service                       enabled
ntpd.service                           enabled 
pptpd.service                          enabled  
cups.socket                            enabled 
rpcbind.socket                         enabled 
default.target                         enabled 

 

9. 测试连接VPN,访问Google

Ubuntu 配置VPN

aliyun-centos7-set-up-vpn-02

用VPN访问Google Play

aliyun-centos7-set-up-vpn-03

访问米扑代理(http://proxy.mimvp.com/check.php),查看连接VPN后的网络地址:

aliyun-centos7-set-up-vpn-04

47.88.76.10  美国  阿里巴巴

至此,搭建VPN,成功!

猜你喜欢

转载自blog.csdn.net/magerguo/article/details/80090939
今日推荐