在前一章,我們理解了 VPN 的本質——隱形斗篷與星際傳送門。如今,修行者的試煉來到下一步:親手打造一座 SoftEther VPN 伺服器,並運用 dnsmasq 與 iptables 為通道加上智慧結界。
這不再只是理論,而是將魔法文字轉化為現實陣法,讓隊伍能在數位迷宮中自由穿梭,同時守護每位冒險者的資料安全。
SoftEther VPN(Software Ethernet)是一座多協定兼容的數位工坊。它的力量在於同時支持 OpenVPN、L2TP/IPSec、SSL-VPN、WireGuard 等多種協定,讓不同冒險者都能找到合適的入口。
sudo apt update && sudo apt upgrade -y
sudo apt install build-essential wget unzip dnsmasq iptables -y
確保伺服器乾淨穩定,為鍛造做好基底。
wget https://github.com/SoftEtherVPN/SoftEtherVPN_Stable/releases/download/v4.41-9787-rtm/softether-vpnserver-*.tar.gz
tar xzvf softether-vpnserver-*.tar.gz
cd vpnserver
make
選擇三次 1,在符文卷軸上刻下安全契約。
sudo mv vpnserver /usr/local/
cd /usr/local/vpnserver
sudo chmod 600 *
sudo chmod 700 vpncmd vpnserver
確保只有持有者能啟動,避免黑暗勢力入侵。
sudo /usr/local/vpnserver/vpnserver start
VPN 的靈脈正式流動。
sudo nano /etc/systemd/system/vpnserver.service
內容:
[Unit]
Description=SoftEther VPN Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/vpnserver/vpnserver start
ExecStop=/usr/local/vpnserver/vpnserver stop
ExecReload=/usr/local/vpnserver/vpnserver restart
[Install]
WantedBy=multi-user.target
啟動:
sudo systemctl enable vpnserver
sudo systemctl start vpnserver
🔑 目的:確保伺服器開機自動啟動,VPN 靈脈隨系統常駐流動。
/usr/local/vpnserver/vpncmd
選擇 1 → Enter → ServerPasswordSet 設定管理員密碼。
SoftEther 內建 DHCP 雖方便,但靈活性不足、管理多 hub 或 MAC 白名單有限。使用 dnsmasq 可以:
範例設定:
interface=tap_soft5
dhcp-range=tap_soft5,192.168.11.50,192.168.11.150,12h
dhcp-option=tap_soft5,3,192.168.11.1
# 只給白名單設備分配 IP
dhcp-ignore=tap_soft5,tag:!known
dhcp-host=<MAC_ADDRESS>,192.168.11.50
重啟 dnsmasq:
sudo /etc/init.d/dnsmasq restart
🔑 目的:將 IP 分配變為可控魔法,避免陌生設備入侵內網。
iptables 是 VPN 的防火牆與 NAT 精靈,能:
範例規則:
# NAT 映射
iptables -t nat -A POSTROUTING -s 192.168.11.0/24 -j SNAT --to-source <SERVER_PUBLIC_IP>
# 封鎖內網特定服務
iptables -I FORWARD -i tap_soft5 -d <SENSITIVE_IP> -j DROP
常用操作:
# 查詢 POSTROUTING
iptables -t nat -v -L POSTROUTING -n --line-numbers
# 刪除 POSTROUTING
iptables -t nat -D POSTROUTING <line-number>
# 查詢 FORWARD 封鎖規則
iptables -L FORWARD -n -v --line-numbers
# 刪除 FORWARD 規則
iptables -D FORWARD <line-number>
保存規則:
iptables-save > /etc/iptables/rules.v4
重啟 VPN 服務:
sudo /etc/init.d/vpnserver restart
SoftEther VPN + dnsmasq + iptables 是多層次安全結界:
修行者不只是使用者,而是通道鍛造師與守護者,讓每一條隱形通道既自由又安全。