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
欢迎关注微信公众号,留言交流。

相关文章:

翻译: