CentOS7搭建VPN

Posted by Clear Blog on November 25, 2018

环境准备脚本

  • ubuntu环境: $ wget https://raw.githubusercontent.com/tomoncle/pptp-vpn-server/master/pptp-vpn-server-ubuntu14.04.sh

  • centos环境: $ wget https://raw.githubusercontent.com/tomoncle/pptp-vpn-server/master/pptp-vpn-server-centos7.sh

运行

  • 1.授权:$ chmod +x pptp-vpn-server-*.sh
  • 2.运行:$ ./pptp-vpn-server-*.sh, 执行过程需要你显示的指定你连接的用户名,密码.

参数:username: vpn连接的用户名

参数:password: vpn连接的密码

client连接

打开客户端,使用pptp协议连接, 填入你的服务器地址,用户名和密码即可使用.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /bin/bash

#########
# os: centos7
# tomoncle


install_pptp() { 
  sudo yum install -y ppp
  wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  sudo rpm -ivh epel-release-latest-7.noarch.rpm
  sudo yum repolist
  sudo yum -y update
  sudo yum install -y pptpd
}

config_kernel_IP_forwarding() {
  echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  sudo sysctl -p
}

config_pptp() {
  sudo sed -i 's/#localip 192.168.0.1/localip 192.168.0.1/g' /etc/pptpd.conf
  sudo sed -i 's/#remoteip 192.168.0.234-238,192.168.0.245/remoteip 192.168.0.234-238,192.168.0.245/g' /etc/pptpd.conf 
  sudo sed -i 's/#ms-dns 10.0.0.1/ms-dns 8.8.8.8/g' /etc/ppp/options.pptpd
  sudo sed -i 's/#ms-dns 10.0.0.2/ms-dns 8.8.4.4/g' /etc/ppp/options.pptpd
  sudo echo "$username  pptpd  \"$password\"  *" >> /etc/ppp/chap-secrets
}

iptables_config() {
  sudo yum -y install iptables
  sudo firewall-cmd --permanent --add-masquerade
  sudo firewall-cmd --permanent --add-port=47/tcp
  sudo firewall-cmd --permanent --add-port=1723/tcp
  sudo firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPT
  sudo firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
  sudo firewall-cmd --reload
  sudo systemctl enable pptpd
}


# start shell.
install_pptp

read -p "Please enter the VPN connection username:" username
read -p "Please enter the VPN connection password:" password

config_kernel_IP_forwarding
iptables_config
config_pptp
sudo systemctl restart pptpd

echo -e "\npptp vpn service config success!!!"