在百度搜索了很多关于CentOS7搭建VPN的教程,但是有很多走不完全套的,因此浪费了很多时间,在这里我把查到能走通的教程整理了一遍,分享给大家。

更新组件

整个安装过程不用经过编译,组件都是在yum中下载,首先我们更新一下所有组件:

yum update -y

这里有个地方需要注意的是:有些主机的硬件并不支持最新内核,因此在不确定的情况下就不要升级内核了,用以下的命令:

yum –exclude=kernel* update -y

更新需要较长时间,请耐心等待。

非常重要的小插曲

撰写这篇文章并且共享出来,主要是为了能和大家互相学习和交流,但是正在看这篇文章的不乏一些现在就有自己的服务器或VPS,急着马上搭建好一台自己的VPN服务器的同学,如果你是这类同学,请不用心急,因为我制作好了自动运行脚本,脚本的内容和文章内容是一样的,把脚本下载到服务器,运行并配置自己的账号密码、ip和客户端ip即可。

脚本下载地址:https://github.com/BoizZ/PPTP-L2TP-IPSec-VPN-auto-installation-script-for-CentOS-7

运行脚本:sh vpn-script-for-centos7.sh

好,那么希望继续学习的同学请往下看。

安装epel源

为什么要安装epel源呢?是因为必要组件xl2tpd在基础的yum源里面是没有的。

yum install epel-release -y

安装依赖组件

安装完epel源以后就可以直接安装依赖组件了。

yum install -y openswan ppp pptpd xl2tpd wget

修改配置文件

需要等待所有依赖组件安装完成才能执行以下步骤(小标题括号内是文件路径)。

ipsec.conf配置文件(/etc/ipsec.conf

# /etc/ipsec.conf - Libreswan IPsec configuration file
# This file:  /etc/ipsec.conf
#
# Enable when using this configuration file with openswan instead of libreswan
#version 2
#
# Manual:     ipsec.conf.5
# basic configuration
config setup
    # NAT-TRAVERSAL support,see README.NAT-Traversal
    nat_traversal=yes
    # exclude networks used on server side by adding %v4:!a.b.c.0/24
    virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12
    # OE is Now off by default. Uncomment and change to on,to enable.
    oe=off
    # which IPsec stack to use. auto will try netkey,then klips then mast
    protostack=netkey
    force_keepalive=yes
    keep_alive=1800
conn L2TP-PSK-NAT
    rightsubnet=vhost:%priv
    also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
    authby=secret
    pfs=no
    auto=add
    keyingtries=3
    rekey=no
    ikelifetime=8h
    keylife=1h
    type=transport
    left=$serverip
    leftid=$serverip
    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any
    dpddelay=40
    dpdtimeout=130
    dpdaction=clear
# For example connections,see your distribution's documentation directory,# or the documentation which Could be located at
#  /usr/share/docs/libreswan-3.*/ or look at https://www.libreswan.org/
#
# There is also a lot of information in the manual page,"man ipsec.conf"
# You may put your configuration (.conf) file in the "/etc/ipsec.d/" directory
# by uncommenting this line
#include /etc/ipsec.d/*.conf

设置预共享密钥配置文件(/etc/ipsec.secrets

#include /etc/ipsec.d/*.secrets
$serverip username PSK password

注解:第二行中username为登录名,password为登录密码

pptpd.conf配置文件(/etc/pptpd.conf)

#ppp /usr/sbin/pppd
option /etc/ppp/options.pptpd
#debug
# stimeout 10
#noipparam
logwtmp
#vrf test
#bcrelay eth1
#delegate
#connections 100
localip 10.0.1.2
remoteip 10.0.1.200-254

xl2tpd.conf配置文件(/etc/xl2tpd/xl2tpd.conf)

;
; This is a minimal sample xl2tpd configuration file for use
; with L2TP over IPsec.
;
; The idea is to provide an L2TP daemon to which remote Windows L2TP/IPsec
; clients connect. In this example,the internal (protected) network
; is 192.168.1.0/24.  A special IP range within this network is reserved
; for the remote clients: 192.168.1.128/25
; (i.e. 192.168.1.128 ... 192.168.1.254)
;
; The listen-addr parameter can be used if you want to bind the L2TP daemon
; to a specific IP address instead of to all interfaces. For instance,; you Could bind it to the interface of the internal LAN (e.g. 192.168.1.98
; in the example below). Yet another IP address (local ip,e.g. 192.168.1.99)
; will be used by xl2tpd as its address on pppx interfaces.
[global]
; ipsec saref = yes
listen-addr = 104.171.165.91
auth file = /etc/ppp/chap-secrets
port = 1701
[lns default]
ip range = 10.0.1.100-10.0.1.254
local ip = 10.0.1.1
refuse chap = yes
refuse pap = yes
require authentication = yes
name = L2TPVPN
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

options.pptpd配置文件(/etc/ppp/options.pptpd)

# Authentication
name pptpd
#chapms-strip-domain
# Encryption
# BSD licensed ppp-2.4.2 upstream with MPPE only,kernel module ppp_mppe.o
# {{{
refuse-pap
refuse-chap
refuse-mschap
# Require the peer to authenticate itself using MS-CHAPv2 [Microsoft
# Challenge Handshake Authentication Protocol,Version 2] authentication.
require-mschap-v2
# Require MPPE 128-bit encryption
# (note that MPPE requires the use of MSCHAP-V2 during authentication)
require-mppe-128
# }}}
# OpenSSL licensed ppp-2.4.1 fork with MPPE only,kernel module mppe.o
# {{{
#-chap
#-chapms
# Require the peer to authenticate itself using MS-CHAPv2 [Microsoft
# Challenge Handshake Authentication Protocol,Version 2] authentication.
#+chapms-v2
# Require MPPE encryption
# (note that MPPE requires the use of MSCHAP-V2 during authentication)
#mppe-40    # enable either 40-bit or 128-bit,not both
#mppe-128
#mppe-stateless
# }}}
ms-dns 8.8.4.4
ms-dns 8.8.8.8
#ms-wins 10.0.0.3
#ms-wins 10.0.0.4
proxyarp
#10.8.0.100
# Logging
#debug
#dump
lock
nobsdcomp 
novj
novjccomp
nologfd

options.xl2tpd配置文件(/etc/ppp/options.xl2tpd)

rm -f /etc/ppp/options.xl2tpd
cat >>/etc/ppp/options.xl2tpd<<EOF
#require-pap
#require-chap
#require-mschap
ipcp-accept-local
ipcp-accept-remote
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
mtu 1400
noccp
connect-delay 5000
# To allow authentication against a Windows domain EXAMPLE,and require the
# user to be in a group "VPN Users". Requires the samba-winbind package
# require-mschap-v2
# plugin winbind.so
# ntlm_auth-helper '/usr/bin/ntlm_auth --helper-protocol=ntlm-server-1 --require-membership-of="EXAMPLE\VPN Users"'
# You need to join the domain on the server,for example using samba:
# http://rootmanager.com/ubuntu-ipsec-l2tp-windows-domain-auth/setting-up-openswan-xl2tpd-with-native-windows-clients-lucid.html

创建chap-secrets配置文件,即用户列表及密码(/etc/ppp/chap-secrets)

# Secrets for authentication using CHAP
# client     server     secret               IP addresses
username          pptpd     password               *
username          l2tpd     password               *

注解:第三第四行中username为登录名,password为登录密码

系统配置

允许IP转发

sysctl -w net.ipv4.ip_forward=1
sysctl -w net.ipv4.conf.all.rp_filter=0
sysctl -w net.ipv4.conf.default.rp_filter=0
sysctl -w net.ipv4.conf.$eth.rp_filter=0
sysctl -w net.ipv4.conf.all.send_redirects=0
sysctl -w net.ipv4.conf.default.send_redirects=0
sysctl -w net.ipv4.conf.all.accept_redirects=0
sysctl -w net.ipv4.conf.default.accept_redirects=0

注解:以上均是命令,复制上去运行即可
也可以修改配置文件(/etc/sysctl.conf):

net.ipv4.ip_forward = 1
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.$eth.rp_filter = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0

允许防火墙端口

创建文件/usr/lib/firewalld/services/pptpd.xml并修改:

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>pptpd</short>
  <description>PPTP</description>
  <port protocol="tcp" port="1723"/>
</service>

创建文件/usr/lib/firewalld/services/l2tpd.xml并修改:

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>l2tpd</short>
  <description>L2TP IPSec</description>
  <port protocol="udp" port="500"/>
  <port protocol="udp" port="4500"/>
  <port protocol="udp" port="1701"/>
</service>

初始化并重启防火墙:

firewall-cmd --reload
firewall-cmd --permanent --add-service=pptpd
firewall-cmd --permanent --add-service=l2tpd
firewall-cmd --permanent --add-service=ipsec
firewall-cmd --permanent --add-masquerade
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -p tcp -i ppp+ -j TCPMSS --syn --set-mss 1356
firewall-cmd --reload

这里是由于CentOS7自带firewall,并且不预装iptables,因此自己也不多此一举去安装了,因为效果都是一样的。

启动并设置开机自启动服务

systemctl enable pptpd ipsec xl2tpd
systemctl restart pptpd ipsec xl2tpd

大功告成

最后一步,并且是最重要的一步,当然是连接一下自己刚建好的VPN服务器啦!

现在估计你和我有同样的喜悦,当然如果中间出现什么问题的话,交流区在下方,很乐意大家踊跃参与!

在CentOS 7中搭建PPTP、L2TP、IPSec服务的更多相关文章

  1. 如何将Android设备连接到PPTP VPN

    我使用ubuntu’sguide创建了一个PPTPVPN服务器.现在我有:我现在的问题是我的Android手机无法连接到我创建的pptp.当我尝试输入用户名和密码然后点击连接,我唯一的消息是连接…

  2. CentOS 8.2服务器上安装最新版Node.js的方法

    这篇文章主要介绍了CentOS 8.2服务器上安装最新版Node.js的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. centos+php+coreseek+sphinx+mysql之一coreseek安装篇

    这篇文章主要介绍了centos+php+coreseek+sphinx+mysql之一coreseek安装篇的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下

  4. Centos7 中 Node.js安装简单方法

    这篇文章主要介绍了Centos7 中 Node.js安装简单方法,非常不错具有一定的参考借鉴价值,需要的朋友可以参考下

  5. nodejs脚本centos开机启动实操方法

    在本篇文章里小编给大家整理的是关于nodejs脚本centos开机启动实操方法,有兴趣的朋友们参考下。

  6. Centos PHP 扩展Xchche的安装教程

    这篇文章主要介绍了Centos PHP 扩展Xchche的安装教程的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下

  7. Linux CentOS系统下安装node.js与express的方法

    这篇文章主要给大家介绍了在Linux CentOS系统下安装node.js与express的方法,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。

  8. CentOS上安装Node.js和mongodb笔记

    这篇文章主要介绍了CentOS上安装Node.js和mongodb笔记,本文讲解了Python安装、Node.js安装、npm安装、mongodb驱动安装、mongodb数据库操作测试代码等内容,需要的朋友可以参考下

  9. centos7上编译安装php7以php-fpm方式连接apache

    这篇文章主要介绍了centos7上编译安装php7以php-fpm方式连接apache的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

  10. CentOS 7.9服务器Java部署环境配置的过程详解

    这篇文章主要介绍了CentOS 7.9服务器Java部署环境配置,主要包括ftp服务器搭建过程、jdk安装方法以及mysql安装过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

随机推荐

  1. 在airgapped(离线)CentOS 6系统上安装yum软件包

    我有一个CentOS6系统,出于安全考虑,它已经被空气泄漏.它可能从未连接到互联网,如果有,它很长时间没有更新.我想将所有.rpm软件包放在一个驱动器上,这样它们就可以脱机安装而无需查询互联网.但是,我在测试VM上遇到的问题是,即使指定了本地路径,yum仍然会挂起并尝试从在线存储库进行更新.另外,有没有办法使用yum-utils/yumdownloader轻松获取该包的所有依赖项和所有依赖项?目前

  2. centos – 命名在日志旋转后停止记录到rsyslog

    CentOS6.2,绑定9.7.3,rsyslog4.6.2我最近设置了一个服务器,我注意到在日志轮换后,named已停止记录到/var/log/messages.我认为这很奇怪,因为所有日志记录都是通过rsyslog进行的,并且named不会直接写入日志文件.这更奇怪,因为我在更新区域文件后命名了HUPed,但它仍然没有记录.在我停止并重新启动命名后,记录恢复.这里发生了什么?

  3. centos – 显示错误的磁盘大小

    对于其中一个磁盘,Df-h在我的服务器上显示错误的空白区域:Cpanel表明它只有34GB免费,但还有更多.几分钟前,我删除了超过80GB的日志文件.所以,我确信它完全错了.fdisk-l/dev/sda2也显示错误:如果没有格式化,我该怎么做才能解决这个问题?并且打开文件描述符就是它需要使用才能做到这一点.所以…使用“lsof”并查找已删除的文件.重新启动写入日志文件的服务,你很可能会看到空间可用.

  4. 如何在centos 6.9上安装docker-ce 17?

    我目前正在尝试在centOS6.9服务器上安装docker-ce17,但是,当运行yuminstalldocker-ce时,我收到以下错误:如果我用跳过的标志运行它我仍然得到相同的消息,有没有人知道这方面的方法?

  5. centos – 闲置工作站的异常负载平均值

    我有一个新的工作站,具有不寻常的高负载平均值.机器规格是:>至强cpu>256GB的RAM>4x512GBSSD连接到LSI2108RAID控制器我从livecd安装了CentOS6.564位,配置了分区,网络,用户/组,并安装了一些软件,如开发工具和MATLAB.在启动几分钟后,工作站负载平均值的值介于0.5到0.9之间.但它没有做任何事情.因此我无法理解为什么负载平均值如此之高.你能帮我诊断一下这个问题吗?

  6. centos – Cryptsetup luks – 检查内核是否支持aes-xts-plain64密码

    我在CentOS5上使用cryptsetupluks加密加密了一堆硬盘.一切都很好,直到我将系统升级到CentOS6.现在我再也无法安装磁盘了.使用我的关键短语装载:我收到此错误:在/var/log/messages中:有关如何装载的任何想法?找到解决方案问题是驱动器使用大约512个字符长的交互式关键短语加密.出于某种原因,CentOS6中的新内核模块在由旧版本创建时无法正确读取512个字符的加密密钥.似乎只会影响内核或cryptsetup的不同版本,因为在同一系统上创建和打开时,512字符的密钥将起作用

  7. centos – 大量ssh登录尝试

    22个我今天登录CentOS盒找到以下内容这是过去3天内的11次登录尝试.WTF?请注意,这是我从我的提供商处获得的全新IP,该盒子是全新的.我还没有发布任何关于此框的内容.为什么我会进行如此大量的登录尝试?是某种IP/端口扫描?基本上有4名匪徒,其中2名来自中国,1名来自香港,1名来自Verizon.这只发生在SSH上.HTTP上没有问题.我应该将罪魁祸首子网路由吗?你们有什么建议?

  8. centos – kswap使用100%的CPU,即使有100GB的RAM也可用

    >Linux内核是否应该足够智能,只需从内存中清除旧缓存页而不是启动kswap?

  9. centos – Azure将VM从A2 / 3调整为DS2 v2

    我正在尝试调整前一段时间创建的几个AzureVM,从基本的A3和标准A3到标准的DS2v2.我似乎没有能力调整到这个大小的VM.必须从头开始重建服务器会有点痛苦.如果它有所不同我在VM中运行CentOS,每个都有一个带有应用程序和操作系统的磁盘.任何人都可以告诉我是否可以在不删除磁盘的情况下删除VM,创建新VM然后将磁盘附加到新VM?

  10. centos – 广泛使用RAM时服务器计算速度减慢

    我在非常具体的情况下遇到服务器速度下降的问题.事实是:>1)我使用计算应用WRF>2)我使用双XeonE5-2620v3和128GBRAM(NUMA架构–可能与问题有关!

返回
顶部