OpenVPN 服务如何默认启动连接?

OpenVPN 服务如何默认启动连接?

我安装的是 官方openvpn版本2.4.6-xenial0存储库。看起来该服务已经自动启用:

cat /etc/systemd/system/multi-user.target.wants/openvpn.service

# This service is actually a systemd target,
# but we are using a service since targets cannot be reloaded.

[Unit]
Description=OpenVPN service
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecReload=/bin/true
WorkingDirectory=/etc/openvpn

[Install]
WantedBy=multi-user.target

重启后,我可以看到配置的 VPN 连接/etc/openvpn已启动。但是,从单元文件中我不清楚这实际上是如何发生的。有人能解释一下吗?

答案1

由于openvpn使用了混合系统安全systemd脚本。

总结

可以在 中配置此行为/etc/default/openvpn。以下是该包提供的文件的摘录openvpn

...
# Start only these VPNs automatically via init script.
# Allowed values are "all", "none" or space separated list of
# names of the VPNs. If empty, "all" is assumed.
# The VPN name refers to the VPN configutation file name.
# i.e. "home" would be /etc/openvpn/home.conf
#
# If you're running systemd, changing this variable will
# require running "systemctl daemon-reload" followed by
# a restart of the openvpn service (if you removed entries
# you may have to stop those manually)
#
#AUTOSTART="all"
#AUTOSTART="none"
#AUTOSTART="home office"
...

因此AUTOSTART没有设置,所以目录.conf中的所有文件/etc/openvpn都在启动时启动。

使用现代systemd 方式

  1. 设置AUTOSTART="none"/etc/default/openvpn
  2. sudo systemctl daemon-reload

现在您可以使用它systemctl来管理您的 openvpn 连接,该服务以配置文件命名,如下所示:

/etc/openvpn/ABC.conf->[email protected]

/etc/openvpn/XYZ.conf->[email protected]

等等...

例子

因此,如果您想启动 XYZ openvpn 连接,命令应该是:

sudo systemctl start openvpn@XYZ

要在启动时启用此连接:

sudo systemctl enable openvpn@XYZ

要获取此连接的状态:

sudo systemctl status openvpn@XYZ

相关内容