我想知道,从系统恢复中唤醒计算机后,如何使 openvpn 重新连接?我正在尝试使用 openvpn cli,而不是通过带有 .ovpn 文件的网络管理器。
我发现已经有一个 openvpn 启动脚本可以扫描 .conf 文件(您可以将扩展名从 .ovpn 更改为 .conf)。不过,为了在唤醒我的计算机后重新启动 openvpn,我能够创建一个适用于 Ubuntu 16.04 LTS 的 systemd 服务。
将文件命名为 openvpn-reconnect.service 并将以下内容放入其中:
[Unit]
Description=Restart OpenVPN after suspend
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target
[Service]
ExecStart=/bin/systemctl restart openvpn.service
[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
Run Code Online (Sandbox Code Playgroud)
我建议首先确保默认情况下仅使用您的 VPN 的 dns 服务器。通过注释掉(添加#)来禁用ubuntu 使用的错误和完全毫无意义的 dnsmasq 本地 dns 服务器,它会破坏 dns 分辨率(请参阅此处的文章:disabling-dnsmasq-as-your-local-dns-server-in-ubuntu)到 dns=dnsmasq 行的开头,同时编辑网络管理器配置文件,如下所示:
sudo emacs /etc/NetworkManager/NetworkManager.conf
Run Code Online (Sandbox Code Playgroud)
然后,保存文件后,单击 GUI 网络管理器小程序,选择“编辑连接”,选择您使用的主要(非 VPN)网络,单击“IPv6 设置”选项卡,单击“忽略 IPv6 流量” IPv6 设置”选项卡并在“方法”菜单中选择“忽略”。保存更改,然后重新启动网络管理器
sudo service network-manager restart
Run Code Online (Sandbox Code Playgroud)
现在让 ovpn 通过命令行工作:
创建一个 auth.txt 文件,其中包含:
<username>
<password>
Run Code Online (Sandbox Code Playgroud)
例如,示例 auth.txt 可以包含如下两行:
user1234
password1234
Run Code Online (Sandbox Code Playgroud)
编辑 .ovpn(或 .conf)文件以使用 auth.txt 进行自动登录:找到包含 auth-user-pass 的行并将 auth.txt 附加到末尾,如下所示:
...
auth-user-pass auth.txt
...
Run Code Online (Sandbox Code Playgroud)
如果您的文件包含选项 auth-nocache,请删除该行。auth-user-pass 和 auth-nocache 不兼容!openvpn 手册页明确说
Further, using --daemon together with --auth-user-pass (entered
on console) and --auth-nocache will fail as soon as key renego?
tiation (and reauthentication) occurs.
Run Code Online (Sandbox Code Playgroud)
这个行话本质上意味着 openvpn 会在您浏览或流式传输时自发终止,或者如果您在配置文件中包含 auth-nocache 行。
将 .ovpn 文件重命名为 .conf 文件:
mv <filename>.ovpn <filename>.conf
Run Code Online (Sandbox Code Playgroud)
将必要的 .conf、.pem 和 .crt 文件放在目录中 将目录内容复制到 /etc/openvpn:
sudo cp <path-to-auth-crt-pem-and-conf-files>/* /etc/openvpn
Run Code Online (Sandbox Code Playgroud)
通过编辑 /etc/default/openvpn(/etc/init.d/openvpn 的配置文件)指定 openvpn 启动时应自动启动的连接
sudo emacs /etc/default/openvpn
Run Code Online (Sandbox Code Playgroud)
在#AUTOSTART="all" 下面添加 .conf 文件的名称减去扩展名,例如,如果我有两个名为 US-East 和 US-East-Strong 的 .conf 文件,
#AUTOSTART="all"
#AUTOSTART="US-East"
#AUTOSTART="US-East-Strong"
Run Code Online (Sandbox Code Playgroud)
从要激活的单个连接的 AUTOSTART 指令中取消注释(删除 # 符号)。每次更改要自动启动的连接时,请运行:
sudo systemctl daemon-reload
sudo systemctl stop openvpn*
sudo systemctl start openvpn.service
Run Code Online (Sandbox Code Playgroud)
要在挂起后重新连接 openvpn,请使用 openvpn-reconnect.service
将服务文件放在 systemd 可以找到的地方:
sudo cp openvpn-reconnect.service /lib/systemd/system
Run Code Online (Sandbox Code Playgroud)
使用以下方法启用和创建符号链接:
sudo systemctl enable openvpn-reconnect.service
Run Code Online (Sandbox Code Playgroud)
要检查 openvpn 连接的状态,请使用:
systemctl status openvpn@<conf-filename-minus-extension>.service
Run Code Online (Sandbox Code Playgroud)
例如,如果我的 conf 文件名为 US-East.conf:
systemctl status openvpn@US-East.service
Run Code Online (Sandbox Code Playgroud)
如果可滚动,键入 q 退出到 shell。
| 归档时间: |
|
| 查看次数: |
2072 次 |
| 最近记录: |