服務端部署完成後,接下來就是客戶端。以下分別以 Windows 與 Linux 為例說明。
1. 安裝 OpenVPN GUI for Windows
Windows 上的客戶端叫 OpenVPN GUI for Windows,必須安裝才能連線。官網被牆,需先翻牆下載;安裝流程沒什麼特別,一路下一步即可。補充一個坑:我的筆電是 Windows 7 64 位元,官網的 openvpn-2.0.9-gui-1.0.3-install.exe 裝完一直報「All TAP-Win32 adapters on this system are currently in use.」,換成 openvpn-2.1.1-gui-1.0.3-install-cn.exe 就正常了,推測是舊版對 XP 以後的系統支援不佳。現在很多人已改用 OpenVPN 2.3 以上版本。相關討論可参考:/content/10381244.html
2. 準備客戶端憑證
從服務端下載 ca.crt、tom.crt、tom.key,在 C:\Program Files (x86)\OpenVPN\config\ 下建立名為 tom 的資料夾並放入這三個檔(使用者叫 jerry 就建 jerry 資料夾,以此類推)。
3. 修改客戶端設定檔
從原始碼的 sample-config-files/client.conf 複製一份到 tom 資料夾,改名為 tom.ovpn,用記事本或 Notepad++ 打開修改:
client dev tun proto tcp remote 192.168.100.120 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt cert tom.crt key tom.key comp-lzo verb 3
4. 連線
開啟 OpenVPN GUI,工作列右下角會出現圖示,右鍵選 Connect。設定無誤就會連上並取得 10.8.0.0 網段位址。
看到這個畫面確實有點小成就感。
不過這個結果有點悶——問題遲早會解決,只差時間。這裡先不處理,接著看 Linux 客戶端。
我的 Linux 客戶端網卡使用 VMnet8,位址 192.168.100.170(因為 192.168.100.120 並非外網 IP,只有筆電上有 192.168.100.1,所以無法用橋接網路)。
可以 ping 通 192.168.100.120,但 ping 不到 172.16.100.120,更別說 172.16.100.128。
1. 安裝 openvpn 方式與服務端相同,直接照做即可。
2. 放置憑證與設定檔 把 Windows 用的 tom 資料夾打包成 zip 上傳:
mkdir -p /etc/openvpn cd /etc/openvpn rz -y # 上傳 tom.zip unzip tom.zip mv tom/* . rm -rf tom.zip tom mv tom.ovpn client.conf [root@localhost openvpn]# ll total 16 -rw-r--r--. 1 root root 1298 Sep 3 15:53 ca.crt -rw-r--r--. 1 root root 3838 Sep 3 15:45 tom.crt -rw-r--r--. 1 root root 916 Sep 3 15:45 tom.key -rw-r--r--. 1 root root 157 Sep 3 15:55 client.conf
3. 啟動
/usr/local/sbin/openvpn --config /etc/openvpn/client.conf &
日誌中可以看到 TLS 驗證通過、PUSH_REPLY 下發位址、tun0 建立完成:
[root@localhost openvpn]# /usr/local/sbin/openvpn --config /etc/openvpn/client.conf & [1] 33934 Sun Sep 4 00:20:59 2016 OpenVPN 2.0.9 x86_64-unknown-linux [SSL] [LZO] [EPOLL] built on Sep 4 2016 Sun Sep 4 00:20:59 2016 Attempting to establish TCP connection with 192.168.100.120:1194 Sun Sep 4 00:20:59 2016 TCP connection established with 192.168.100.120:1194 Sun Sep 4 00:20:59 2016 VERIFY OK: depth=1, /C=CN/ST=GD/L=Shenzhen/O=contoso.com/OU=Tech/CN=contoso.com_CA/emailAddress=admin@contoso.com Sun Sep 4 00:20:59 2016 VERIFY OK: depth=0, /C=CN/ST=GD/L=Shenzhen/O=contoso.com/OU=Tech/CN=server/emailAddress=admin@contoso.com Sun Sep 4 00:21:00 2016 Data Channel Encrypt: Cipher 'BF-CBC' initialized with 128 bit key Sun Sep 4 00:21:00 2016 Control Channel: TLSv1, cipher TLSv1/SSLv3 DHE-RSA-AES256-SHA, 1024 bit RSA Sun Sep 4 00:21:01 2016 PUSH: Received control message: 'PUSH_REPLY,route 10.8.0.1,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5' Sun Sep 4 00:21:01 2016 TUN/TAP device tun0 opened Sun Sep 4 00:21:01 2016 /sbin/ifconfig tun0 10.8.0.6 pointopoint 10.8.0.5 mtu 1500 Sun Sep 4 00:21:01 2016 Initialization Sequence Completed Sun Sep 4 00:21:05 2016 Connection reset, restarting [0] Sun Sep 4 00:21:05 2016 SIGUSR1[soft,connection-reset] received, process restarting Sun Sep 4 00:21:12 2016 Preserving previous TUN/TAP instance: tun0 Sun Sep 4 00:21:12 2016 Initialization Sequence Completed
看到 Initialization Sequence Completed 基本就代表成功。此時 ifconfig 會多一個 tun0,位址 10.8.0.6,也能 ping 通服務端的 10.8.0.1。
到服務端看日誌,會記錄到 192.168.100.170 這個客戶端成功連線,Linux 客戶端到此完成。
本文出自「IT小二郎」博客,請務必保留此出處 https://jerry12356.blog.51cto.com/4308715/1846080