CentOS搭建L2TP-IPsec SERVER
需求
无锡和扬州两地公司办公室需要使用SVN协作开发,通过V/P/N的方式将2边的办公网连接在一起。
一、环境准备
准备一台带有公网IP地址的云服务器,推荐用阿里云,好用便宜。
操作系统:CentOS7.6
1、开启tun
cat /dev/net/tun
如果tun已经开启,则提示:
cat: /dev/net/tun: File descriptor in bad state
如果没有以上提示,则执行以下命令:
cd /dev
mkdir net
mknod net/tun c 10 200
chmod 0666 net/tun
备注:tun是虚拟网卡模块,作用是把报文拦截转发给对应的进程
2、安装依赖
yum install -y epel-release libreswan
3、安装Xl2tp软件
yum install -y xl2tpd
二、配置
1、修改sysctl.conf配置文件
cd /etc/
vi sysctl.conf
1)、粘贴以下内容并覆盖原内容,保存退出**
vm.swappiness = 0
kernel.sysrq = 1
net.ipv4.neigh.default.gc_stale_time = 120
# see details in https://help.aliyun.com/knowledge_detail/39428.html
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.default.accept_source_route = 0
# see details in https://help.aliyun.com/knowledge_detail/41334.html
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.ip_forward=1
2)、执行sysctl -p生效。
sysctl -p
2、配置IPsec加密链接
IPsec可对流量进行加密,保护V/P/N通信安全。
vi /etc/ipsec.d/l2tp_ipsec.conf
保存为以下内容
conn L2TP-PSK-NAT
rightsubnet=0.0.0.0/0
also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
authby=secret
pfs=no
auto=add
keyingtries=3
dpddelay=30
dpdtimeout=120
dpdaction=clear
rekey=no
ikelifetime=8h
keylife=1h
type=transport
left=xx.xx.xx.xx #注意:这里配置为外网IP地址
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
1)、配置PSK
修改ipsec.secrets
vi /etc/ipsec.secrets
在新的一行输入以下命令
: PSK "123456"
小细节:PSK的配置命令规则:
#本地公网出口IP #对端公网出口IP #双方约定的秘钥
xx.xx.120.162 xx.xx.144.75 : PSK "123456"
2)、运行IPsec检查(可跳过)
systemctl start ipsec
ipsec verify
正常应打印以下信息,全部OK。
Verifying installed system and configuration files
Version check and ipsec on-path [OK]
Libreswan 3.25 (netkey) on 3.10.0-1160.45.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 [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]
可能会出现类似如下情况:
Checking rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/ens160/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/ens192/rp_filter [ENABLED]
这是内核参数没有生效,直接依次手动打开这些文件,将 1 改为 0,然后重新执行检查,输出如下内容则OK。
3)配置IPsec开机启动
systemctl enable ipsec
3、配置Xl2tpd
1)配置xl2tpd.conf文件,为V/P/N拨号的客户端规划IP地址
vi /etc/xl2tpd/xl2tpd.conf
[lns default]
ip range = 192.168.188.2-192.168.188.99 #IP地址池
local ip = xx.xx.xx.xx #本机内网IP
2)配置PPP
vi /etc/ppp/options.xl2tpd
主要修改DNS
ms-dns 8.8.8.8
ms-dns 8.8.4.4
ms-dns 4.2.2.4
嫌麻烦可以粘贴以下内容覆盖
ipcp-accept-local
ipcp-accept-remote
ms-dns 8.8.8.8
ms-dns 1.1.1.1
# ms-dns 192.168.1.1
# ms-dns 192.168.1.3
# ms-wins 192.168.1.2
# ms-wins 192.168.1.4
noccp
auth
#obsolete: crtscts
idle 1800
mtu 1410
mru 1410
nodefaultroute
debug
#obsolete: lock
proxyarp
connect-delay 5000
refuse-pap
refuse-mschap
refuse-mschap-v2
persist
logfile /var/log/xl2tpd.log
3)设置PPP账号密码
配置chap-secrets
vi /etc/ppp/chap-secrets
# client server secret IP addresses
用户名 * 密码 *
4)启动xl2tpd
systemctl start xl2tpd
systemctl enable xl2tpd #设置开启启动
4、设置防火强
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPT
firewall-cmd --permanent --zone=public --add-masquerade
firewall-cmd --permanent --add-rich-rule='rule protocol value="esp" accept'
firewall-cmd --permanent --add-rich-rule='rule protocol value="ah" accept'
firewall-cmd --permanent --add-port=1701/udp
firewall-cmd --permanent --add-port=500/udp
firewall-cmd --permanent --add-port=4500/udp
firewall-cmd --permanent --add-service="ipsec"
firewall-cmd --reload
补充:用云服务器的朋友,切记在云平台上开启相应的UDP端口
三、测试补充
windows 10配置V/P/N拨号的时候,注意:
以太网->更改适配器选项->选择vpn右键属性->安全->勾选以下按钮