CentOS 7 是個分水嶺:預設防火牆從 iptables 換成了 firewalld,VPN 的設定方式也跟著不同。以下是以 pptpd 為例的完整建置流程,並附上兩種防火牆寫法。
modprobe ppp-compress-18 && echo yes
回傳 yes 代表核心支援 MPPE 壓縮。接著檢查 TUN 裝置:
cat /dev/net/tun
若出現 File descriptor in bad state 這類訊息,代表 TUN 可用;反之則需手動啟用。
yum -y install ppp pptpd
如果提示找不到 pptpd,先掛上外部來源再安裝:
rpm -i https://poptop.sourceforge.net/yum/stable/rhel7/pptp-release-current.noarch.rpm
把下列兩行前方的註解移除:
localip 192.168.0.1 remoteip 192.168.0.234-238,192.168.0.245
remoteip 是分配給用戶端的區段,範圍內挑幾個即可。即使用戶端本身的區域網恰好也是 192.168.0.234,因為走的是不同介面,一般也不會衝突。
編輯 /etc/ppp/options.pptpd,在檔案結尾加上:
ms-dns 8.8.8.8 ms-dns 8.8.4.4
也可以自行挑選延遲較低的國外 DNS,用 ping 實測哪個快就用哪個。
編輯 /etc/ppp/chap-secrets,格式為「帳號 pptpd 密碼 *」,最後的星號代表不限制來源 IP:
帳號 pptpd 密碼 *
在 /etc/sysctl.conf 結尾加上一行,然後讓設定生效:
net.ipv4.ip_forward = 1 sysctl -p
若使用 iptables(記得先關掉 firewalld):
iptables -t nat -A POSTROUTING -s 192.168.0.234/24 -o eth0 -j MASQUERADE iptables -I INPUT -p tcp --dport 1723 -j ACCEPT iptables -I INPUT -p tcp --dport 47 -j ACCEPT iptables -I INPUT -p gre -j ACCEPT service iptables save
清除既有 nat 規則可用:
iptables -F -t nat iptables -X -t nat iptables -Z -t nat
CentOS 7 重開機後規則會失效,因此需寫入 /etc/rc.d/rc.local:
chmod +x /etc/rc.d/rc.local vim /etc/rc.d/rc.local
把轉送規則貼到檔案最後並存檔,最後重啟服務:service pptpd restart。
若使用 firewalld,則改用直接規則:
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -i eth0 -p tcp --dport 1723 -j ACCEPT firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPT firewall-cmd --permanent --direct --add-rule ipv4 filter POSTROUTING 0 -t nat -o eth0 -j MASQUERADE firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i ppp+ -o eth0 -j ACCEPT firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i eth0 -o ppp+ -j ACCEPT firewall-cmd --reload
chkconfig pptpd on
以上就是大致流程。這份紀錄整理自公開資料與實際操作經驗,並非權威文件,環節細節仍建議以官方說明為準;若發現可疑之處,也歡迎指正。