搭建PPTP协议VPN 上外网
##最下面有一键部署脚本
环境:一台能上外网的服务器(这里我用的是一台香港的轻量级服务器 centos7)
安装PPTP
yum install epel-release -y #安装epel源
yum install pptpd -y #安装pptpd程序包,不过还会安装一些依赖包
#开启内核转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf && sysctl -p
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
1 #查看是否开启成功,1
就代表开启
编辑配置文件
1.编辑出配置文件 /etc/pptpd.conf
vim /etc/pptpd.conf
... ...
# TAG: connections
# Limits the number of client connections that may be accepted.
#
# If pptpd is allocating IP addresses (e.g. delegate is not
# used) then the number of connections is also limited by the
# remoteip option. The default is 100.
connections 100 #去掉这行的注释,这里指定的最大并发多少,建议少点好,设置为10。
... ...
#配置文件的最先面配置localip 和remoteip 配置文件中也给出了例子
# IP for each simultaneous client.
#
# (Recommended)
localip 192.168.0.1 #这里写你的服务器的ip(公网ip)
remoteip 192.168.0.234-238,192.168.0.245 #这里随便,就是非配给连接过来服务器的ip
# or
#localip 192.168.0.234-238,192.168.0.245
#remoteip 192.168.1.234-238,192.168.1.245
#上面两个localip | remoteip在你的配置文件都有例子,推荐选择第一个去掉注释然后localip为虚拟网关第二个remoteip为DHCP分配地址,你可以更改为其他私网网段
2.总配置文件就算配置完毕了,接下来配置pptp的连接协议
vim /etc/ppp/options.pptpd
... ...
# Network and Routing
# If pppd is acting as a server for Microsoft Windows clients, this
# option allows pppd to supply one or two DNS (Domain Name Server)
# addresses to the clients. The first instance of this option
# specifies the primary DNS address; the second instance (if given)
# specifies the secondary DNS address.
#ms-dns 10.0.0.1 #去掉注释然后改为你定义的DNS推荐使用8.8.8.8毕竟主机在国外
#注! 配置一个ms-dns 就行,配置多个也无所谓
#ms-dns 10.0.0.2 #同样去掉注释推荐8.8.4.4
#好了第二个配置文件就算配置完毕了!()
3.接下来就是配置连接pptp的用户和密码了
vim /etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# client server secret IP addresses
zhanghao * 123456 *
#这里zhanghao:为用户名,123456为密码,按照这个格式写就行
###以上就是需要修改的配置了
# systemctl enable pptpd --now 启动pptp并设置为开机自启动
# ss -nutlp | grep 1723 #查看服务端口
#此时就可以连接vpn了但是需要上网的话还差一步就是设置nat转发
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.1.0/24 -j MASQUERADE
#使用nat进行转发其中eth0 192.168.1.0/24分别为网卡名和IP地址网段这个按照你当时配置
##我这里的网卡为eth0,可以通过ip addr show 、 ifconfig 等查看自己的网卡名,192.168.1.0为刚刚在主配置文件里定义的remote ip的那个网段,比如remoteip 192.168.1.234-238,192.168.1.245 之前配置文件定义的是这个,就写这个ip的网段192.168.1.0
#所有配置完成后就可以连接vpn上Youbube了,这里还有一个问题就是,连接之后上不了国内的网站,以后有时间在研究吧…
连接步骤如下图
#一键部署脚本
curl -s www.zhanghaobk.com:81/deploy_pptp_vpn.sh -o deploy_pptp_vpn.sh && bash deploy_pptp_vpn.sh