准備工作,檢查環境
1、先看看你的主機是否支持pptp,返回結果為yes就表示通過。
modprobe ppp-compress-18 && echo yes
2、檢查是否開啟了TUN,有的虛擬機主機需要開啟,返回結果為cat: /dev/net/tun: File descriptor in bad state。就表示通過。
cat /dev/net/tun
正式開始安裝配置
1、安裝yum源,centos官方源已經去除xl2tpd,如果使用的官方鏡像安裝的服務器,則需要自己安裝yum源。但是使用的阿里雲或者騰訊雲公共鏡像安裝的服務器可以不用。
yum install -y epel-release
2、安裝xl2tpd
yum install -y xl2tpd libreswan lsof
3、配置xl2tpd
vim /etc/xl2tpd/xl2tpd.conf
[global]
[lns default]
ip range = 192.168.1.128-192.168.1.254
local ip = 192.168.1.99
require chap = yes
refuse pap = yes
require authentication = yes
name = LinuxVPNserver
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
4、配置ppp文件,注意其中被注釋的選項,沒有被注釋的話,啟動會報錯,或者會導致連不上
vim /etc/ppp/options.xl2tpd
ipcp-accept-local
ipcp-accept-remote
ms-dns 8.8.8.8
name xl2tpd
#noccp
auth
#crtscts
idle 1800
mtu 1410
mru 1410
nodefaultroute
debug
#lock
proxyarp
connect-delay 5000
refuse-pap
refuse-mschap
require-mschap-v2
persist
logfile /var/log/xl2tpd.log
5、配置ipsec文件
1) vim /etc/ipsec.conf
config setup
protostack=netkey
nat_traversal=yes
interfaces="%defaultroute" virtual_private=%v4:192.168.0.0/16,%v4:10.0.0.0/8,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v4:!10.254.253.0/24
include /etc/ipsec.d/*.conf
2) vim /etc/ipsec.d/l2tp-ipsec.conf
conn l2tp-psk
authby=secret
pfs=no
auto=add
rekey=no
type=transport
left=47.75.104.65
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
rightsubnet=vhost:%priv,%no
6、設置用戶名密碼
vim /etc/ppp/chap-secrets
# client server secret IP addresses
userName * “123456” *
7、設置共享密鑰
vim /etc/ipsec.d/default.secrets
: PSK "YourPSK"
8、防火牆設置
1) yum install iptables-services#下載iptables服務
2) vim /etc/sysconfig/iptables
*nat
:PREROUTING ACCEPT [2:104]
:INPUT ACCEPT [2:104]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -m policy --dir out --pol none -j MASQUERADE
-A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
COMMIT
*filter
:INPUT ACCEPT [84:7150]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [49:10368]
-A INPUT -m policy --dir in --pol ipsec -j ACCEPT
-A INPUT -p udp -m policy --dir in --pol ipsec -m udp --dport 1701 -j ACCEPT
-A INPUT -p udp -m udp --dport 500 -j ACCEPT
-A INPUT -p udp -m udp --dport 4500 -j ACCEPT
-A FORWARD -m policy --dir in --pol ipsec -j ACCEPT
-A FORWARD -i ppp+ -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
3) service iptables restart
4) iptables -S #查看iptables規則
9、修改內核參數
1) vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.lo.accept_redirects = 0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.lo.send_redirects = 0
2) sysctl -p#使配置生效
10、重啟檢查服務
1) service ipsec restart#重啟ipsec服務
service ipsec status #查看ipsec服務狀態
systemctl enable ipsec #確定沒有問題后,將服務設置成開機啟動
2) ipsec verify##檢查ipsec
[root@iZj6c7krv4f28m7t3p2f2tZ ~]# ipsec verify
Verifying installed system and configuration files
Version check and ipsec on-path [OK]
Libreswan 3.25 (netkey) on 3.10.0-957.5.1.el7.x86_64
Checking for IPsec support in kernel [OK]
NETKEY: Testing XFRM related proc values
ICMP default/send_redirects [OK]
ICMP default/accept_redirects [OK]
XFRM larval drop [OK]
Pluto ipsec.conf syntax [OK]
Two or more interfaces found, checking IP forwarding[OK]
Checking rp_filter [OK]
Checking that pluto is running [OK]
Pluto listening for IKE on udp 500 [OK]
Pluto listening for IKE/NAT-T on udp 4500 [OK]
Pluto ipsec.secret syntax [OBSOLETE]
003 WARNING: using a weak secret (PSK)
Checking 'ip' command [OK]
Checking 'iptables' command [OK]
Checking 'prelink' command does not interfere with FIPS[OK]
Checking for obsolete ipsec.conf options [OBSOLETE KEYWORD]
Warning: ignored obsolete keyword 'nat_traversal'
3) service xl2tpd restart#重啟xl2tpd服務
service xl2tpd status #查看xl2tpd服務狀態
systemctl enable xl2tpd #確定沒有問題后,將服務設置成開機啟動
11、服務器在阿里雲或者騰訊雲之類的平台,需要配置1701的端口開放,協議是udp
12、連接VPN。注意,如果是Mac系統,需要在/etc/ppp目錄下新建options文件,否則會連不上:
sudo vim /etc/ppp/options
在options文件中輸入:
plugin L2TP.ppp
l2tpnoipsec