2025年7月8日 星期二

在 RHEL 8 / CentOS Stream 8 上架設 WireGuard VPN Server

學習目標:
  • 在 RHEL / CentOS Stream 8 上架設 WireGuard VPN Server
設定流程:
  1. 安裝 EPEL 套件:
    # dnf install epel-release elrepo-release -y
    # dnf upgrade
    # shutdown -r now
    
  2. 安裝必要的 WireGuard 套件:
    # dnf install kmod-wireguard wireguard-tools -y
  3. 建立/etc/wireguard 目錄:
    # mkdir /etc/wireguard
  4. 一口氣產生 Server 用的公私鑰,放置於 /etc/wireguard 目錄下:
    # wg genkey | sudo tee /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key
  5. 設定 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 (隨時會變動的設定值,不用太計較)
    
  6. 變更 wg0.conf 檔案權限:
    # chmod 600 /etc/wireguard/wg0.conf
  7. 設定 sysctl.conf 檔案:
    # vim /etc/sysctl.conf
        net.ipv4.ip_forward = 1
        
  8. 導入 sysctl.conf 檔案:
    # sysctl -p
        
  9. 設定防火牆:
    # 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'
        
  10. 第一次啟動 WireGuard:
        # wg-quick up /etc/wireguard/wg0.conf
        # wg-quick down /etc/wireguard/wg0.conf
        
  11. 設定成系統服務:
        # systemctl enable --now wg-quick@wg0.service
        
  12. 查看 WireGuard 服務:
        # systemctl status wg-quick@wg0.service -l
        # wg
        
  13. 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  (內定值)
        

在RHEL 8 / CentOS Stream 8 上設定 Serial Port

學習目標:
  • 在 RHEL / CentOS Stream 8 上設定硬體的 Serail Port
設定流程:
  1. 開機前,請設定 BIOS 中的 Serial Port。請注意其連線速度、使用的資料位數、檢驗值與停止值。
  2. 開機後,設定 GRUB 設定檔:
    # vim /etc/default/grub
    (找到 GRUB_CMDLINE_LINUX 這行,在這行的末尾添加以下參數,用空格分隔:)
    console=ttyS0,115200n8 console=tty0
    
    1. console=ttyS0,115200n8 設定了序列埠 ttyS0,連線速度為115200,8個資料位數,無檢驗值,1個停止值。
    2. console=tty0 保留了螢幕輸出。
  3. 存檔後,執行以下命令,更新GRUB設定,讓修改生效:
    # grub2-mkconfig -o /boot/grub2/grub.cfg
  4. 啟用 getty 服務:
    # systemctl enable --now serial-getty@ttyS0.service
  5. 使用 minicom 測試連線即可。