- 安裝 EPEL 套件:
# dnf install epel-release elrepo-release -y
# dnf upgrade
# shutdown -r now
- 安裝必要的 WireGuard 套件:
# dnf install kmod-wireguard wireguard-tools -y
- 建立/etc/wireguard 目錄:
# mkdir /etc/wireguard
- 一口氣產生 Server 用的公私鑰,放置於 /etc/wireguard 目錄下:
# wg genkey | sudo tee /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key
- 設定 wg0 虚擬網卡:
# vim /etc/wireguard/wg0.conf
[Interface]
Address = 10.10.10.1/24 (VPN連線之後,Server端的IP)
SaveConfig = true
ListenPort = 51820 (對外開放的 UDP port)
PrivateKey = 4NI5crFXXXXX..... (這個是 Server 私鑰內容)
(以下資料可能會隨環境變化)
[Peer]
PublicKey = KotWuoSAHQLzxxx.... (這個是 Client 端的公鑰內容,不是上一個步驟的公鑰內容)
AllowedIPs = 10.10.10.0/24 (期望 VPN 使用的網段)
Endpoint = 192.168.1.12:64099 (隨時會變動的設定值,不用太計較)
- 變更 wg0.conf 檔案權限:
# chmod 600 /etc/wireguard/wg0.conf
- 設定 sysctl.conf 檔案:
# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
- 導入 sysctl.conf 檔案:
# sysctl -p
- 設定防火牆:
# firewall-cmd --zone=public --permanent --add-masquerade
# firewall-cmd --zone=public --add-masquerade
# firewall-cmd --permanent --add-port=51820/udp
# firewall-cmd --add-port=51820/udp
# firewall-cmd --zone=public --permanent --add-rich-rule='rule family="ipv4" source address="10.10.10.0/24" accept'
# firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="10.10.10.0/24" accept'
- 第一次啟動 WireGuard:
# wg-quick up /etc/wireguard/wg0.conf
# wg-quick down /etc/wireguard/wg0.conf
- 設定成系統服務:
# systemctl enable --now wg-quick@wg0.service
- 查看 WireGuard 服務:
# systemctl status wg-quick@wg0.service -l
# wg
- Client 端的部份,請從官網下載合適的軟體,設定檔可參考下列設定內容:
[Interface]
PrivateKey = 0CqEO+vL4RwLk...... (自已產生的私鑰內容)
Address = 10.10.10.2/24 (自已需要使用的 VPN IP位址)
[Peer]
PublicKey = pzX02R3jqMth..... (Server 產生的公鑰,不是自已產生的)
AllowedIPs = 0.0.0.0/1, 128.0.0.0/1 (允許未經過隧道的流量)
Endpoint = 192.168.1.133:51820 (Server 對外連線的 IP,不是 VPN 的 IP)
PersistentKeepalive = 25 (內定值)