L2TP over IPsec 是兼容性最廣的 VPN 協議,原生支持 Windows/macOS/iOS/Android 無客戶端接入。本文記錄 CentOS 5.5 雙網卡網關模式下,xl2tpd 1.2.7 從源碼編譯到系統服務化的完整落地過程。
Internet → Router → Firewall (192.166.1.1)
→ [VPN Server eth0: 192.166.1.2/24]
→ [VPN Server eth1: 192.168.67.2/24]
→ Core Switch (192.168.67.1) → Intranet
關鍵:必須雙網卡網關模式;單網卡旁路模式會因路由非對稱導致 TCP 重傳、MTU 黑洞等疑難雜症。
# 1. 依賴包
yum install -y libpcap-devel ppp gcc make
# 2. 下載編譯
wget https://www.xelerance.com/software/xl2tpd/xl2tpd-1.2.7.tar.gz
tar zxvf xl2tpd-1.2.7.tar.gz && cd xl2tpd-1.2.7
make && make install
# 默認安裝至 /usr/local/sbin/xl2tpd, 配置目錄 /etc/xl2tpd
[global]
listen-addr = 192.166.1.2 # 綁定 WAN 口 IP
auth file = /etc/ppp/chap-secrets # CHAP 認證文件
debug tunnel = yes
[lns default]
ip range = 192.168.67.3-192.168.67.254 # 客戶端地址池
local ip = 192.168.67.2 # VPN 服務器虛擬網關 (LAN 口 IP)
require chap = yes
refuse pap = yes
require authentication = yes
name = xxbl2tpserver
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
ipcp-accept-local
ipcp-accept-remote
ms-dns 8.8.8.8
ms-dns 221.7.17.21
noccp
auth
crtscts
idle 1800
mtu 1500
mru 1500
nodefaultroute
debug
lock
logfile /var/log/l2tpd.log
proxyarp
connect-delay 5000
receive-all
ktune
# client server secret IP addresses
"test" * "test" *
# 1. 創建啟動腳本 /etc/init.d/xl2tpd
#!/bin/sh
# chkconfig: - 80 30
# description: Layer 2 Tunnelling Protocol Daemon (RFC 2661)
SERVICE=xl2tpd
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ ${NETWORKING} = "no" ] && exit 0
[ -x /usr/local/sbin/$SERVICE ] || exit 0
start() {
echo -n "Starting $SERVICE: "
[ -d /var/run/xl2tpd ] || mkdir -p /var/run/xl2tpd
daemon /usr/local/sbin/$SERVICE -c /etc/xl2tpd/xl2tpd.conf
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SERVICE
echo
return $RETVAL
}
stop() {
echo -n "Stopping $SERVICE: "
killproc $SERVICE
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$SERVICE
echo
return $RETVAL
}
case "$1" in
start|stop|restart|reload|status) $1 ;;
*) echo "Usage: $0 {start|stop|status|restart|reload}"; exit 1 ;;
esac
# 2. 權限與路徑修正
chmod +x /etc/init.d/xl2tpd
cp /usr/local/sbin/xl2tpd /usr/sbin/ # 腳本中調用 /usr/sbin/xl2tpd
# 3. 註冊服務
chkconfig --add xl2tpd
chkconfig xl2tpd on # 運行級 3/5 自啟
xl2tpd -c /etc/xl2tpd/xl2tpd.conf -D 觀察隧道建立、CHAP 協商、IPCP 分配 IP 全過程。/var/log/l2tpd.log (ppp) + /var/log/messages (xl2tpd)。mtu 1400 mru 1400echo 1 > /proc/sys/net/ipv4/ip_forward 持久化至 /etc/sysctl.confL2TP/IPsec 雖配置鏈路長,但標準協議保證了極致兼容性,是企業遠程接入的首選基石。