之前曾经介绍过SoftEthern VPN的搭建,最近换了新的VPS,需要重新搭建VPN,由于除了iOS以外的其他平台都可以用ShadowSocks的梯子,就想搭建一个最简单的无需安装第三方App的VPN给iOS使用。想到系统自带的VPN可以连接L2TP over IPSec
,就决定搭一个L2TP的VPN。
由于新VPS装的是CentOS 6,所以CentOS 7风格的命令就写在注释里了。
安装
先安装openswan
和xl2tpd
:
yum install openswan xl2tpd
如果没有ppp
也要安装。
配置IPSec
在/etc/ipsec.d
中新建一个vpn.conf
文件,内容如下:
1 | conn L2TP-PSK-NAT |
其中left
的值改为VPS的公网IP。
再新建一个vpn.secrets
文件,里面写一行:
1 | YOUR_PUBLIC_IP_ADDRESS %any: PSK "YOUR_PRE_SHARED_KEY" |
前面还是公网IP,后面引号里面是自己设置的预共享密钥。
更改系统参数
编辑/etc/sysctl.conf
文件,修改或添加成以下配置:
1 | net.ipv4.ip_forward = 1 |
用sysctl -p
命令使更改生效。
然后用以下脚本将/proc/sys/net/ipv4/conf
下配置的值都改为0
:
1 | for each in /proc/sys/net/ipv4/conf/* |
启动IPSec
启动IPSec并加入开机启动:
service ipsec start #systemctl start ipsec
chkconfig ipsec on #systemctl enable ipsec
然后用ipsec verify
检查一下是否配置正确,正常如下:
Version check and ipsec on-path [OK]
Libreswan 3.15 (netkey) on 4.12.9-1.el6.elrepo.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]
Hardware random device [N/A]
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 [OK]
Checking 'ip' command [OK]
Checking 'iptables' command [OK]
Checking 'prelink' command does not interfere with FIPSChecking for obsolete ipsec.conf options [OK]
Opportunistic Encryption [DISABLED]
如果有异常请检查之前的配置。
配置xl2tpd
编辑/etc/xl2tpd/xl2tpd.conf
配置如下:
1 | [global] |
其实主要注意监听地址和几个yes
就行了,其他基本不用动。
然后编辑/etc/ppp/options.xl2tpd
配置如下:
1 | ipcp-accept-local |
在/etc/ppp/chap-secrets
里添加帐号密码:
1 | # client server secret IP addresses |
按照对应格式填上帐号密码即可。
启动xl2tpd
并加入开机启动:
service xl2tpd start #systemctl start xl2tpd
chkconfig xl2tpd on #systemctl enable xl2tpd
最后在本地设备上填上地址、预共享密钥、用户名、密码就可以连接了。
-
本文作者:
Shintaku
本文链接:
https://www.shintaku.xyz/posts/l2tp/
版权声明:
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Shintaku's Blog 。