从命令行将 VPN 配置文件导入 NetworkManager

从命令行将 VPN 配置文件导入 NetworkManager

可以.ovpn通过 GUI 工具将文件导入到 NetworkManager nm-connection-editor(右键单击nm-applet并单击Edit connections):Add-> Import a saved VPN configuration....

我的目标是做同样的事情,但是通过命令行使用nm-cli.有可能吗?

答案1

我使用 nmcli 版本 1.2.6,我可以用它导入 openvpn 配置。

nmcli connection import type openvpn file ~/myconfig.ovpn

从现在开始,即使在 NetworkManager UI 中我的 VPN 也是可见的。

要进一步配置连接,您可以使用该show命令查找连接名称并modify更改配置值。

nmcli connection show
nmcli connection modify myvpnconnectionname +vpn.data username=myusername

答案2

我用来解决基于 debian 的系统上 nmcli 缺乏功能的问题的解决方法是使用命令将 /etc/NetworkManager/system-settings 文件夹中的现有 VPN 配置文件复制到新文件(以 root 身份,当然)在同一文件夹中,并对新副本中允许的用户、网关、用户名和密码值进行字符串替换。然后我重新启动网络管理器以应用更改。

例如:

/etc/NetworkManager/system-settings 文件夹中的典型配置文件可能如下所示:

[connection]
id=<<id>>
uuid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
type=vpn
permissions=user:<<permissions_user>>:;
autoconnect=false

[vpn]
password-flags=0
service-type=org.freedesktop.NetworkManager.pptp
require-mppe-128=yes
mppe-stateful=yes
user=<<user>>
refuse-eap=yes
refuse-chap=yes
gateway=<<gateway>>
refuse-pap=yes

[vpn-secrets]
password=<<password>>

[ipv4]
method=auto

...所以您可以创建一个与上面的类似的新配置文件...

cd /etc/NetworkManager/system-settings
cp "existing-working-vpn-config-file" "new-vpn-config-file"

...然后将上面的“<<>>”值替换为您自己的 VPN 设置,例如:

sed -i "s/<<permissions_user>>/my_permissions_user/g" new-vpn-config-file
sed -i "s/<<user>>/my_user/g" new-vpn-config-file
sed -i "s/<<gateway>>/my_gateway/g" new-vpn-config-file
sed -i "s/<<password>>/my_password/g" new-vpn-config-file

...然后最后通过以下命令重新启动网络管理器:

service network-manager restart

注意:UUID 设置似乎并不重要,即使它不是唯一的。不知道为什么。东西就可以了。

另外,如果您要添加新文件而不是复制文件,请确保该文件的权限设置为 600(读取和写入),并且所有者为 root。

尝试一下并告诉我你的想法。它对我有用,全部通过命令行。

答案3

我用它从根运行:

USER=japie
runuser -l $USER -c "sudo -S nmcli connection import type openvpn file ~/vpn_clients/flappie3-TO-IPFire.ovpn"

连接“flappie3-TO-IPFire”(26d0b28e-9212-4e71-90dc-3911ddf231e5) 已成功添加。

相关内容