Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS 5.5 雙網卡 L2TP/IPsec VPN 服務器搭建全記錄:xl2tpd + 自簽證書 + 系統服務化

編輯:Linux 教學

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
  • eth0 (WAN): 192.166.1.2/24,對接邊界防火牆
  • eth1 (LAN): 192.168.67.2/24,對接核心交換機,作為 VPN 虛擬網關 IP
  • 客戶端地址池: 192.168.67.3 - 192.168.67.254

關鍵:必須雙網卡網關模式;單網卡旁路模式會因路由非對稱導致 TCP 重傳、MTU 黑洞等疑難雜症。

核心軟體棧

  • xl2tpd 1.2.7 (L2TP 守護進程,監聽 UDP 1701)
  • openswan/strongSwan (IPsec IKE 協商,本文假設已獨立部署)
  • ppp (數據鏈路層封裝)

源碼編譯安裝 xl2tpd

# 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

核心配置文件

/etc/xl2tpd/xl2tpd.conf

[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

/etc/ppp/options.xl2tpd

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

/etc/ppp/chap-secrets

# client  server  secret  IP addresses
"test" * "test" *

系統服務化 (SysV init)

# 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 過大導致分片失敗 → 客戶端 MTU 1400 / 服務端 mtu 1400 mru 1400
    • 防火牆未放行 UDP 500/4500 (IPsec) + UDP 1701 (L2TP) + ESP 協議 (IP 協議 50)
    • 內核轉發未開啟 → echo 1 > /proc/sys/net/ipv4/ip_forward 持久化至 /etc/sysctl.conf

L2TP/IPsec 雖配置鏈路長,但標準協議保證了極致兼容性,是企業遠程接入的首選基石。

Copyright © Windows教程網 All Rights Reserved