Arch Linux SSL VPN 客戶端配置

安恆信息是科創板上市公司,旗下有多個安全產品,有些產品集成有SSL VPN功能。

當你使用Linux的時候,你會發現這VPN產品默認沒有Linux客戶端,Windows和MacOS有,我在公司使用Mac,在家用Linux,每天背電腦到處跑這種事很無聊。

問題來了,Linux下用什麼客戶端能連上這個VPN服務呢? 我查看了Mac 客戶端是基於Tunnelblick開發的,而Tunnelblick是OpenVPN的GUI客戶端。所以理論上只要在Linux配置好OpenVPN客戶端即可。

1. Arch下安裝OpenVPN客戶端

在Arch下安裝openvpn很簡單

1sudo pacman -S openvpn

客戶端和服務端公用一套程序,只是配置目錄不同,很科學,很強大。

2. 配置客戶端

因爲我場景只是去連服務端,所以只用配置客戶端相關文件。

一半來說,Mac客戶端怎麼連的,Linux類似操作,只是客戶端不同,可能一個是GUI,一個命令行。公司會給你一份.ovpn 結尾的文件,不用怕就是一個普通文本文件。

裏面的內容和示例文件/usr/share/openvpn/examples/client.conf 差不多,只是後綴不一。

假設公司給你的教client.ovpn, 重命名成 client.conf, 放到 /etc/openvpn/client/client.conf 這個路徑。

示例如下

 1[root@minipc ~]# cat /etc/openvpn/client/client.conf
 2client
 3proto tcp-client
 4remote your_server_ip_or_domain
 5port 1194
 6dev tun
 7nobind
 8verb 4
 9auth-nocache
10auth-user-pass /etc/openvpn/client/auth.txt
11push-peer-info
12
13keepalive 60 300
14static-challenge "two-factor" 1
15<ca>
16-----BEGIN CERTIFICATE-----
17此處省略,密鑰文件嘛
18-----END CERTIFICATE-----
19
20</ca>
21tun-mtu 1514
22reneg-sec 0

關節點爲:

  • remote # vpn服務端地址
  • port # 端口 默認1194
  • auth-user-pass your_password_file # 存儲用戶密碼的文件,建議寫絕對路徑,這個文件是選配的, 不填就要在命令行交互的時候填入。不喜歡輸用戶密碼的建議填上,節約時間。

其他的配置也很簡單,不瞭解的查看openvpn文檔。

祕密文件權限設置爲600,保險點。

1[root@minipc ~]# cat /etc/openvpn/client/auth.txt
2xiaomin
3xiaomin_password
4[root@minipc ~]# ls -al /etc/openvpn/client/auth.txt
5-rw------- 1 root root 19 Oct  1 13:34 /etc/openvpn/client/auth.txt

用戶名一行,密碼一行,別問我爲什麼這樣,規定的,不信你搞到一行space/tab分割試試看。

3. 啓動運行

Mac下是直接運行GUI軟件,Linux下也有幾個OpenVPN GUI客戶端,老讀者都知道,我用的labwc環境,簡單樸素,還是命令行方便。

 1➜  ~ sudo openvpn --config /etc/openvpn/client/client.conf
 22024-10-01 15:00:17 us=265636 Note: --cipher is not set. OpenVPN versions before 2.5 defaulted to BF-CBC as fallback when cipher negotiation failed in this case. If you need this fallback please add '--data-ciphers-fallback BF-CBC' to your configuration and/or add BF-CBC to --data-ciphers.
 32024-10-01 15:00:17 us=267321 Note: Kernel support for ovpn-dco missing, disabling data channel offload.
 42024-10-01 15:00:17 us=267340 Current Parameter Settings:
 52024-10-01 15:00:17 us=267345   config = '/etc/openvpn/client/client.conf'
 62024-10-01 15:00:17 us=267349   mode = 0
 7...中間省略一萬行...
 82024-10-01 15:02:45 us=238580   client = ENABLED
 92024-10-01 15:02:45 us=238584   pull = ENABLED
102024-10-01 15:02:45 us=238588   auth_user_pass_file = '/etc/openvpn/client/auth.txt'
112024-10-01 15:02:45 us=238594 OpenVPN 2.6.12 [git:makepkg/038a94bae57a446c+] x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] [DCO] built on Jul 18 2024
122024-10-01 15:02:45 us=238605 library versions: OpenSSL 3.3.2 3 Sep 2024, LZO 2.10
132024-10-01 15:02:45 us=238613 DCO version: N/A
14CHALLENGE: two-factor

因爲公司開啓2步驗證,所以最後會提示輸入驗證碼,一般是手機上裝個類似微軟Authenticator的軟件,隨時間變化的6位數號碼,輸入後即可正常撥通。如果沒有開啓兩步驗證,直接運行就會連上。

連上後,你會多個tun虛擬網卡,layer 3的,下面所示的tun1就是,tun0是sing-box的,懂的都懂。

 1➜  ~ ip a
 21: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
 3    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 4    inet 127.0.0.1/8 scope host lo
 5       valid_lft forever preferred_lft forever
 6    inet6 ::1/128 scope host noprefixroute
 7       valid_lft forever preferred_lft forever
 82: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
 9    link/ether 70:70:fc:06:ac:aa brd ff:ff:ff:ff:ff:ff
103: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
11    link/ether e4:c7:67:3f:6a:06 brd ff:ff:ff:ff:ff:ff
12    inet 192.168.124.5/24 brd 192.168.124.255 scope global dynamic noprefixroute wlp2s0
13       valid_lft 75818sec preferred_lft 75818sec
14    inet6 fe80::a531:de1c:800e:ed07/64 scope link noprefixroute
15       valid_lft forever preferred_lft forever
164: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 9000 qdisc fq_codel state UNKNOWN group default qlen 500
17    link/none
18    inet 172.19.0.1/30 brd 172.19.0.3 scope global tun0
19       valid_lft forever preferred_lft forever
20    inet6 fe80::ed5c:8b63:307:ee6a/64 scope link stable-privacy proto kernel_ll
21       valid_lft forever preferred_lft forever
227: tun1: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1514 qdisc fq_codel state UNKNOWN group default qlen 500
23    link/none
24    inet 172.20.0.2/24 scope global tun1
25       valid_lft forever preferred_lft forever
26    inet6 fe80::352:fae8:59c6:8e92/64 scope link stable-privacy proto kernel_ll
27       valid_lft forever preferred_lft forever

4. 其它

上述運行方式一直在前臺運行,不小心中斷了怎麼辦?我一般用tmux,終端多開,問題不大。

不用tmux 呢?不想前臺運行呢?用systemd接管即可。

話不多說,看下面的操作過程。

先cat給你看配置,再start輸入two-factor,tun1出現說明成功,停止後tun1消失,說明關停了,我想應該滿意了😅。

 1➜  ~ sudo systemctl cat openvpn-client@.service
 2[sudo] password for mephisto:
 3# /usr/lib/systemd/system/openvpn-client@.service
 4[Unit]
 5Description=OpenVPN tunnel for %I
 6After=network-online.target
 7Wants=network-online.target
 8Documentation=man:openvpn(8)
 9Documentation=https://openvpn.net/community-resources/reference-manual-for-openvpn-2-6/
10Documentation=https://community.openvpn.net/openvpn/wiki/HOWTO
11
12[Service]
13Type=notify
14PrivateTmp=true
15WorkingDirectory=/etc/openvpn/client
16ExecStart=/usr/bin/openvpn --suppress-timestamps --nobind --config %i.conf
17User=root
18Group=network
19AmbientCapabilities=CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SETPCAP CAP_SYS_CHROOT CAP_DAC_OVERRIDE
20CapabilityBoundingSet=CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SETPCAP CAP_SYS_CHROOT CAP_DAC_OVERRIDE
21LimitNPROC=10
22DeviceAllow=/dev/null rw
23DeviceAllow=/dev/net/tun rw
24ProtectSystem=true
25ProtectHome=true
26KillMode=process
27
28[Install]
29WantedBy=multi-user.target
30➜  ~ sudo systemctl start openvpn-client@client.service
31
32Broadcast message from root@minipc (Tue 2024-10-01 15:14:28 CST):
33
34Password entry required for 'CHALLENGE: two-factor' (PID 81068).
35Please enter password with the systemd-tty-ask-password-agent tool.
36
37🔐 CHALLENGE: two-factor 951053
38➜  ~ ip a
391: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
40    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
41    inet 127.0.0.1/8 scope host lo
42       valid_lft forever preferred_lft forever
43    inet6 ::1/128 scope host noprefixroute
44       valid_lft forever preferred_lft forever
452: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
46    link/ether 70:70:fc:06:ac:aa brd ff:ff:ff:ff:ff:ff
473: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
48    link/ether e4:c7:67:3f:6a:06 brd ff:ff:ff:ff:ff:ff
49    inet 192.168.124.5/24 brd 192.168.124.255 scope global dynamic noprefixroute wlp2s0
50       valid_lft 75458sec preferred_lft 75458sec
51    inet6 fe80::a531:de1c:800e:ed07/64 scope link noprefixroute
52       valid_lft forever preferred_lft forever
534: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 9000 qdisc fq_codel state UNKNOWN group default qlen q500
54    link/none
55    inet 172.19.0.1/30 brd 172.19.0.3 scope global tun0
56       valid_lft forever preferred_lft forever
57    inet6 fe80::ed5c:8b63:307:ee6a/64 scope link stable-privacy proto kernel_ll
58       valid_lft forever preferred_lft forever
598: tun1: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1514 qdisc fq_codel state UNKNOWN group default qlen 500
60    link/none
61    inet 172.20.0.2/24 scope global tun1
62       valid_lft forever preferred_lft forever
63    inet6 fe80::a713:868:de2d:2bb0/64 scope link stable-privacy proto kernel_ll
64       valid_lft forever preferred_lft forever
65
66➜  ~ sudo systemctl stop  openvpn-client@client.service
67➜  ~ ip a
681: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
69    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
70    inet 127.0.0.1/8 scope host lo
71       valid_lft forever preferred_lft forever
72    inet6 ::1/128 scope host noprefixroute
73       valid_lft forever preferred_lft forever
742: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
75    link/ether 70:70:fc:06:ac:aa brd ff:ff:ff:ff:ff:ff
763: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
77    link/ether e4:c7:67:3f:6a:06 brd ff:ff:ff:ff:ff:ff
78    inet 192.168.124.5/24 brd 192.168.124.255 scope global dynamic noprefixroute wlp2s0
79       valid_lft 75259sec preferred_lft 75259sec
80    inet6 fe80::a531:de1c:800e:ed07/64 scope link noprefixroute
81       valid_lft forever preferred_lft forever
824: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 9000 qdisc fq_codel state UNKNOWN group default qlen 500
83    link/none
84    inet 172.19.0.1/30 brd 172.19.0.3 scope global tun0
85       valid_lft forever preferred_lft forever
86    inet6 fe80::ed5c:8b63:307:ee6a/64 scope link stable-privacy proto kernel_ll
87       valid_lft forever preferred_lft forever

不知道讀者你有沒有發現,我是十一國慶節下午在寫這個文章。

這意味着我犧牲了假期時間,在給中文世界做貢獻,如果你要表示支持,就關注下我的公衆號,別無所求,公衆號廣告能搞錢。

自我換了工作,比之前忙碌多了,一點摸魚時間都沒有,當然也能學到知識,畢竟又延遲退休了,能怎麼辦呢?寫文章的頻率直線下降,只能犧牲業餘時間。

關鍵碼字不賺錢,最近牛市我一天賺十幾萬人民幣,而寫這個文章,廣告收益幾乎爲0,平均寫一篇最少要2h左右,付出和收益完全不成比例,連域名費用都賺不回來。

轉念一想,互聯網上的中文資料日趨萎縮,我也查到過有價值的解決問題的中文內容,別人好像也沒搞到大錢,我爲什麼要這麼功利呢?單純做貢獻也不是不可以,簡直Q翻自己,萬一那天讀者變多了,日積月累,內容質量上升了呢?或者說某個苦難人剛好需要這個呢?

最後修改於: Tuesday, October 1, 2024

相關文章:

翻譯: