之前在机房部署了PPTP的V**环境,后面发现有的同事使用的mac本不能连接PPTP,原因是IOS10.0系统以后就不支持PPTP的V**了,于是打算将V**更换L2TP类型的。 L2TP是一种工业标准的Internet隧道协议,功能大致和PPTP协议类似,比如同样可以对网络数据流进行加密。不过也有不同之处: 1)PPTP要求网络为IP网络,L2TP要求面向数据包的点对点连接; 2)PPTP使用单一隧道,L2TP使用多隧道; 3)L2TP提供包头压缩、隧道验证,而PPTP不支持。 4)L2TP的可应用性更为广泛,很多路由不支持PPTP穿透
废话不多说了,以下是在Centos7.2下部署L2TP环境的操作记录: 下载安装脚本https://files.cnblogs.com/files/think8848/StackScript.zip(百度云盘下载地址https://pan.baidu.com/s/1bA0FjK 提取密码:x7nn)
代码语言:javascript复制[root@linux-node2 ~]# wget -O StackScript.zip https://files.cnblogs.com/files/think8848/StackScript.zip
[root@linux-node2 ~]# unzip StackScript.zip
[root@linux-node2 ~]# chmod 755 StackScript
[root@linux-node2 ~]# /bin/bash -x StackScript
脚本执行中报错: ...... sh /tmp/vpn.sh Error: Network interface 'eth0' is not available.
原因:我这里的L2TP部署机的外网网卡是em1,而不是脚本中指定的eth0,因此需要将/tmp/vpn.sh脚本文件中的eth0替换成em1 具体解决操作如下: 先查看下StackScript脚本内容
代码语言:javascript复制[root@linux-node2 ~]# cat StackScript
#!/bin/bash
# <UDF name="VPN_IPSEC_PSK" Label="IPsec Pre-Shared Key" />
# <UDF name="VPN_USER" Label="VPN Username" />
# <UDF name="VPN_PASSWORD" Label="VPN Password" />
if [ -f /etc/apt/sources.list ]; then
url=vpnsetup
apt-get -y update
apt-get -y install wget
elif [ -f /etc/yum.conf ]; then
url=vpnsetup-centos
yum -y install wget
else
echo "Your distribution is not supported by this StackScript"
exit 1
fi
wget "https://git.io/$url" -O /tmp/vpn.sh && sh /tmp/vpn.sh && rm -f /tmp/vpn.sh
# Fix xl2tpd on CentOS 7 for Linode VMs, because kernel module
# l2tp_ppp is not available in the default Linode kernel
if grep -qs "release 7" /etc/redhat-release; then
if [ -f /usr/lib/systemd/system/xl2tpd.service ]; then
sed -i '/ExecStartPre/s/^/#/' /usr/lib/systemd/system/xl2tpd.service
systemctl daemon-reload
systemctl restart xl2tpd
fi
fi
由于/tmp/vpn.sh脚本(可以预览下脚本里的配置内容)中定义的一些信息跟我的部署机上的信息不一致,所以需要做一些修改:eth0替换成em1;eth替换成em;192.168.42替换成192.168.1;将192.168.43替换成192.168.1;192.168.42.1替换成本机的内网ip:192.168.1.17(这里包含了防火墙规则设置)
代码语言:javascript复制[root@linux-node2 ~]# sed -i 's/eth0/em1/g' /tmp/vpn.sh
[root@linux-node2 ~]# sed -i 's/eth/em/g' /tmp/vpn.sh //将外网网卡名称有eth0改为em1,以及防火墙规则设置
[root@linux-node2 ~]# sed -i 's/192.168.42/192.168.1/g' /tmp/vpn.sh
[root@linux-node2 ~]# sed -i 's/192.168.43/192.168.1/g' /tmp/vpn.sh
[root@linux-node2 ~]# sed -i 's/192.168.42.1/192.168.1.17/g' /tmp/vpn.sh
接着将StackScript脚本中剩余的操作单独放在一个新脚本里执行:
代码语言:javascript复制[root@linux-node2 ~]# vim l2tp.sh
#/bin/bash
sh /tmp/vpn.sh && rm -f /tmp/vpn.sh
# Fix xl2tpd on CentOS 7 for Linode VMs, because kernel module
# l2tp_ppp is not available in the default Linode kernel
if grep -qs "release 7" /etc/redhat-release; then
if [ -f /usr/lib/systemd/system/xl2tpd.service ]; then
sed -i '/ExecStartPre/s/^/#/' /usr/lib/systemd/system/xl2tpd.service
systemctl daemon-reload
systemctl restart xl2tpd
fi
fi
[root@linux-node2 ~]# chmod 755 l2tp.sh
[root@linux-node2 ~]# /bin/bash -x l2tp.sh
...........
...........
IPsec VPN server is now ready for use!
Connect to your new VPN with these details:
Server IP: 113.110.186.117
IPsec PSK: 4K4PJvu33hhqh6U5
Username: vpnuser
Password: 3YiPfMmLCZxfwvJV
Write these down. You'll need them to connect!
Important notes: https://git.io/vpnnotes
Setup VPN clients: https://git.io/vpnclients
================================================
rm -f /tmp/vpn.sh
grep -qs 'release 7' /etc/redhat-release
'[' -f /usr/lib/systemd/system/xl2tpd.service ']'
sed -i '/ExecStartPre/s/^/#/' /usr/lib/systemd/system/xl2tpd.service
systemctl daemon-reload
systemctl restart xl2tpd
上面的PSK、用户名和密码都是从Linode上拔下来的,这些信息可以自己修改: a)修改PSK为huanqiuwangshibo
代码语言:javascript复制[root@linux-node2 ~]# vim /etc/ipsec.secrets
%any %any : PSK "huanqiuwangshibo"
b)修改用户名和密码为wangshibo和wangshibo@123
代码语言:javascript复制[root@linux-node2 ~]# vim /etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# client server secret IP addresses
"wangshibo" l2tpd "wangshibo@123" *
然后重启IPsec和xl2tpd服务
代码语言:javascript复制[root@linux-node2 ~]# systemctl restart ipsec xl2tpd
最后可以检查下ipsec状态,如下没有FAILED就说明状态正常
代码语言:javascript复制[root@linux-node2 ~]# ipsec verify
Verifying installed system and configuration files
Version check and ipsec on-path [OK]
Libreswan 3.19 (netkey) on 3.10.0-327.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 [ENABLED]
/proc/sys/net/ipv4/conf/brqd340f735-5a/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/docker0/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/em2/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/em3/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/em4/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tap42a276b5-a7/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tap7a36096c-bb/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tap8314f726-c7/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tap8b0bfb5c-fc/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tap9e0ad654-eb/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tapab9a2526-d3/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tapc6a6623c-1f/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tapda472e40-78/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tapdc7ebaa9-38/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/tun0/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/veth17da48b/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/veth446262b/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/virbr0/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/virbr0-nic/rp_filter [ENABLED]
rp_filter is not fully aware of IPsec and should be disabled
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 FIPS [OK]
Checking for obsolete ipsec.conf options [OK]
ipsec verify: encountered 39 errors - see 'man ipsec_verify' for help
按照上面操作完成后,基本即ok了,就可以在客户端连接L2TP类型的V**了 也可以查看下/etc/ipsec.conf、/etc/ipsec.secrets、/etc/ppp/chap-secrets、/etc/xl2tpd/xl2tpd.conf这几个文件配置,确保配置信息无误。
客户端连接L2TP/IPsec的操作记录 (1)先说下windows客户机连接L2TP/IPsec的操作记录,这里温馨提示下,请按照此教程配置客户端。
这里以win8/win10为例: 1)右键单击系桌面右下角的无线/网络图标。 2)选择打开网络与共享中心。 3)单击设置新的连接或网络。 4) 选择连接到工作区,然后单击 下一步。 5)单击使用我的Internet连接 (V**)。 6)在Internet地址字段中输入你的 V** 服务器 IP。 7)在目标名称 字段中输入任意内容。单击创建。 8)返回网络与共享中心。单击左侧的更改适配器设置。 9)右键单击新创建的V**连接,并选择属性。 10)单击安全 选项卡,从 V** 类型下拉菜单中选择 "使用 IPsec 的第 2 层隧道协议 (L2TP/IPSec)"。 11)单击允许使用这些协议。确保选中 "质询握手身份验证协议 (CHAP)" 复选框。 12)单击高级设置 按钮。 13)单击使用预共享密钥作身份验证并在密钥字段中输入你的 V** IPsec PSK。 14)单击确定 关闭 高级设置。 15)单击确定 保存 V** 连接的详细信息。
特别注意的是: 在首次连接之前一定要修改一次注册表(否则会报错:无法建立计算机与 V**服务器之间的网络连接,因为远程服务器未响应.....)。具体操作如下: 1)确保win10客户机的"控制面板"->"管理工具"->"组件服务"->"服务"->"IPsec Policy Agent"是正在运行的状态. 2)修改注册表。按键ctrl r,在"运行"里输入"regedit"打开注册表 a)依次打开:HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesRasmanParameters,然后在右边栏里确认有AllowL2TPWeakCrypto,并且右键AllowL2TPWeakCrypto->"修改"->"数值数据"为1(如果不存在AllowL2TPWeakCrypto,就新建它) b)依次打开:HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesPolicyAgent ,然后在右边栏里新建QWORD(64位),并命名为"AssumeUDPEncapsulationContextOnSendRule",并且修改它的"数值数据"为2 3)依次打开客户机的"C盘"->"Windows"->"System32"-"cmd.exe",右击cmd.exe,以管理员身份运行。然后在命令窗口里输入:
代码语言:javascript复制REG ADD HKLMSYSTEMCurrentControlSetServicesPolicyAgent /v AssumeUDPEncapsulationContextOnSendRule /t REG_DWORD /d 0x2 /f
4)最后,一定要重启客户机 5)然后再次点击新建的L2TL的V**,就能成功连接了。如下:
连接成功后能正常上网,并且查看本机外网ip已经变成L2TP服务器的外网ip了
如果客户端后续连接失败(报错:L2TP连接尝试失败,因为安全层在初始化与远程计算机协商时遇到一个处理错误) 可以重启服务端的ipsec和xl2tpd服务,然后再尝试在客户端连接 [root@linux-node2 ~]# systemctl restart ipsec xl2tpd
(2)接着说下linux客户机连接L2TP/IPsec的操作记录(这里以centos6为例)
安装基本工具
代码语言:javascript复制[root@dev ~]# yum install vim net-tools ftp epel-release -y
安装L2TP和IPsec客户端(其实也可以用于服务端,只不过本例中,服务器端使用的是libreswan,而客户端使用的是strongswan)
代码语言:javascript复制[root@dev ~]# yum install strongswan xl2tpd -y
配置文件
代码语言:javascript复制[root@dev ~]# vim /etc/strongswan/ipsec.conf //将默认配置内容情况,直接填写下面内容
config setup
conn �fault
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
keyexchange=ikev1
authby=secret
ike=aes128-sha1-modp1024,3des-sha1-modp1024!
esp=aes128-sha1-modp1024,3des-sha1-modp1024!
conn myvpn
keyexchange=ikev1
left=�faultroute
auto=add
authby=secret
type=transport
leftprotoport=17/1701
rightprotoport=17/1701
right=113.110.186.117 //这个是L2TP服务器端的公网ip地址
[root@dev ~]# vim /etc/strongswan/ipsec.secrets //连接L2TP的秘钥
: PSK "huanqiuwangshibo"
[root@dev ~]# vim /etc/xl2tpd/xl2tpd.conf //将默认配置内容情况,直接填写下面内容
[lac myvpn]
lns = 113.110.186.117
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd.client
length bit = yes
[root@dev ~]# vim /etc/ppp/options.xl2tpd.client
ipcp-accept-local
ipcp-accept-remote
refuse-eap
require-chap
noccp
noauth
mtu 1280
mru 1280
noipdefault
defaultroute
usepeerdns
connect-delay 5000
name wangshibo //连接L2TP的用户名
password wangshibo@123 //连接L2TP的密码
启动客户端
代码语言:javascript复制[root@dev ~]# /etc/init.d/strongswan start
[root@dev ~]# /etc/init.d/xl2tpd start
[root@dev ~]# /etc/init.d/strongswan status
[root@dev ~]# /etc/init.d/xl2tpd status
打开连接通道,successfully表示通道顺畅
代码语言:javascript复制[root@dev ~]# strongswan up myvpn
initiating Main Mode IKE_SA myvpn[9] to 103.10.86.17
generating ID_PROT request 0 [ SA V V V V ]
sending packet: from 192.168.9.200[500] to 103.10.86.17[500] (188 bytes)
received packet: from 103.10.86.17[500] to 192.168.9.200[500] (144 bytes)
parsed ID_PROT response 0 [ SA V V V ]
received DPD vendor ID
received FRAGMENTATION vendor ID
received NAT-T (RFC 3947) vendor ID
generating ID_PROT request 0 [ KE No NAT-D NAT-D ]
sending packet: from 192.168.9.200[500] to 103.10.86.17[500] (244 bytes)
received packet: from 103.10.86.17[500] to 192.168.9.200[500] (244 bytes)
parsed ID_PROT response 0 [ KE No NAT-D NAT-D ]
local host is behind NAT, sending keep alives
remote host is behind NAT
generating ID_PROT request 0 [ ID HASH N(INITIAL_CONTACT) ]
sending packet: from 192.168.9.200[4500] to 103.10.86.17[4500] (108 bytes)
received packet: from 103.10.86.17[4500] to 192.168.9.200[4500] (76 bytes)
parsed ID_PROT response 0 [ ID HASH V ]
received unknown vendor ID: 49:4b:45:76:32
IKE_SA myvpn[9] established between 192.168.9.200[192.168.9.200]...103.10.86.17[103.10.86.17]
scheduling reauthentication in 3387s
maximum IKE_SA lifetime 3567s
generating QUICK_MODE request 3871448243 [ HASH SA No KE ID ID NAT-OA NAT-OA ]
sending packet: from 192.168.9.200[4500] to 103.10.86.17[4500] (364 bytes)
sending retransmit 1 of request message ID 3871448243, seq 4
sending packet: from 192.168.9.200[4500] to 103.10.86.17[4500] (364 bytes)
sending retransmit 2 of request message ID 3871448243, seq 4
sending packet: from 192.168.9.200[4500] to 103.10.86.17[4500] (364 bytes)
sending retransmit 3 of request message ID 3871448243, seq 4
sending packet: from 192.168.9.200[4500] to 103.10.86.17[4500] (364 bytes)
received packet: from 103.10.86.17[4500] to 192.168.9.200[4500] (300 bytes)
parsed QUICK_MODE response 3871448243 [ HASH SA No KE ID ID ]
CHILD_SA myvpn{22} established with SPIs c4f970c1_i 69e2edb7_o and TS 192.168.9.200/32[udp/l2tp] === 103.10.86.17/32[udp/l2tp]
connection 'myvpn' established successfully
创建xl2tpd控制文件
代码语言:javascript复制[root@dev ~]# mkdir -p /var/run/xl2tpd
[root@dev ~]# touch /var/run/xl2tpd/l2tp-control
连接L2TP/IPsec的命令(L2TP服务端的ipsec和xl2tpd重启后,客户端的V**连接就会断开,需要重新连接)
代码语言:javascript复制[root@dev ~]# echo "c myvpn" > /var/run/xl2tpd/l2tp-control
稍等片刻,大概3-5秒钟就可以看到已经连上去了(ifconfig查看发现有了ppp0的ip信息,这是连接V**后分配过来的ip)
代码语言:javascript复制[root@dev ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 44:8A:5B:BD:43:1B
inet addr:192.168.9.200 Bcast:192.168.9.255 Mask:255.255.255.0
inet6 addr: fe80::468a:5bff:febd:431b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:118805419 errors:0 dropped:0 overruns:0 frame:0
TX packets:42102770 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:79901529647 (74.4 GiB) TX bytes:3904418043 (3.6 GiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:11097085 errors:0 dropped:0 overruns:0 frame:0
TX packets:11097085 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3951878331 (3.6 GiB) TX bytes:3951878331 (3.6 GiB)
ppp0 Link encap:Point-to-Point Protocol
inet addr:192.168.1.190 P-t-P:192.168.1.17 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1280 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:64 (64.0 b) TX bytes:82 (82.0 b)
设置路由
代码语言:javascript复制# route delete default
# route add 服务器端的IP gw 本机网关
# ip route add default via 192.168.1.117 dev ppp0 (192.168.1.117是服务器端的内网ip)
验证,如果顺利返回了服务器端的IP,说明已经成功了!
代码语言:javascript复制[root@dev ~]# wget -qO- http://ipv4.icanhazip.com; echo
113.110.186.117
[root@dev ~]#
客户机连上V**后,ping www.baidu.com,发现夜里速度比白天慢一些,和DNS解析有关~
代码语言:javascript复制# vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
关闭L2TP/IPsec的命令
代码语言:javascript复制# echo "d myvpn" > /var/run/xl2tpd/l2tp-control