# 1. 安装 yum install openswan xl2tpd openswan-doc lsof libpcap-devel
# 2. 配置
vim /etc/ipsec.conf
# 编辑 dumpdir=/var/run/pluto/
# 编辑 virtual_private=%v4:10.0.0.0/8,%v4:192.168.11.0/24,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v6:fd00::/8,%v6:fe80::/10
# /etc/ipsec.d/xl2tpd.conf
cat > /etc/ipsec.d/xl2tpd.conf <<EOF
# Add connections here
# sample VPN connection
# for more examples, see /etc/ipsec.d/examples/
#conn sample
# # Left security gateway, subnet behind it, nexthop toward right.
# left=10.0.0.1
# leftsubnet=172.16.0.0/24
# leftnexthop=10.22.33.44
# # Right security gateway, subnet behind it, nexthop toward left.
# right=10.12.12.1
# rightsubnet=192.168.0.0/24
# rightnexthop=10.101.102.103
# # To authorize this connection, but not actually start it,
# # at startup, uncomment this.
# #auto=add
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=[本机IP或域名] #这里写公网IP,没固定IP的就到花生壳弄个动态域名解析。
leftid=[本机IP或域名]
leftprotoport=17/1701
right=%any
EOF;
# 设置预共享密钥
vim /etc/ipsec.d/xl2tpd.secrets
echo ': PSK "l2tpd.owent.net"' > /etc/ipsec.d/xl2tpd.secrets;
# 3. 网络设置
sysctl -w net.ipv4.ip_forward=1
sysctl -w net.ipv4.conf.default.rp_filter=0
sysctl -w net.ipv4.conf.default.send_redirects=0
sysctl -w net.ipv4.conf.default.accept_redirects=0
# 建议把以上内容写进 /etc/sysctl.conf 后 执行 sysctl -p
# 3. 启动和测试
service ipsec start
ipsec verify
# 全部通过或N/A就可以了
# 4. xl2tpd设置
vim /etc/ppp/options.xl2tpd
# 去除require-mschap-v2前注释
# name l2tpd
vim /etc/xl2tpd/xl2tpd.conf
# 改写以下内容
# [global]
# listen-addr = [服务器IP]
# ipsec saref = yes # 如果 ipsec verify 返回 SAref kernel support [N/A] 则改成 no
# [lns default]
# ip range = 192.168.11.128-192.168.11.254
# local ip = 192.168.11.1
# name = l2tpd
vim /etc/ppp/chap-secrets
# 设置用户名密码 [用户名] l2tpd [密码] *
# 5. iptables 规则
iptables -A INPUT -p tcp --dport 1194 -j ACCEPT
iptables -A INPUT -p udp --dport 1701 -j ACCEPT
iptables -A INPUT -p udp --dport 500 -j ACCEPT
iptables -A INPUT -p udp --dport 4500 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.11.0/24 -o eth0 -j MASQUERADE
iptables -A FORWARD -s 192.168.11.0/24 -j ACCEPT
iptables -A FORWARD -d 192.168.11.0/24 -j ACCEPT
service iptables save
# 6. 启动xl2tpd和自动运行
chkconfig --level 2345 ipsec on
chkconfig --level 2345 xl2tpd on