NetworkManager

来自 Arch Linux 中文维基

NetworkManager 是一个为系统提供检测和配置功能以便自动连接到网络的程序。NetworkManager 的功能对无线和有线网络都很有用。对于无线网络,NetworkManager 偏好已知的无线网络,并能切换到最可靠的网络。能感知 NetworkManager 的应用程序可以切换在线和离线模式。比起无线连接,NetworkManager 更偏好有线连接,且支持调制解调器连接和一些类型的 VPN。NetworkManager 最初由 Red Hat 开发,现在由 GNOME 管理。

警告: 默认情况下,文件系统中的 root 用户和通过 GUI(如 nm-applet)访问设置的用户可以访问机密信息(如 WiFi 密码)。参见#加密的 Wi-Fi 密码

安装[编辑 | 编辑源代码]

可以通过安装 networkmanager 软件包获取 NetworkManager。此软件包包含一个守护程序、一个命令行界面(nmcli)和一个基于 curses 的界面(nmtui)。

启用 NetworkManager[编辑 | 编辑源代码]

安装完成后,启动/启用 NetworkManager.service。NetworkManager 守护进程启动后,会自动连接到已配置且可用的“系统连接”。“用户连接”和未配置的连接需要通过 nmcli 或 applet 进行配置和连接。

注意:
  • 请确保没运行其它网络配置服务;多个网络服务会发生冲突。可以用 systemctl --type=service 查看正在运行的服务列表,然后停止它们。参见#配置以启用 NetworkManager 服务。
  • 如果 systemd-resolved 没有启动,错误消息将会淹没日志。详见后面的 Unit dbus-org.freedesktop.resolve1.service not found 部分。

额外的界面[编辑 | 编辑源代码]

移动网络支持[编辑 | 编辑源代码]

NetworkManager 使用 ModemManager 提供移动宽带连接。

安装 modemmanagerusb_modeswitch。然后启用启动 ModemManager.service

可能需要重新启动 NetworkManager.service 才能使其检测 ModemManager。重新启动后,重新插入调制解调器应该就可以识别了。

从前端界面(例如 nm-connection-editor)添加连接并将连接类型选择为 broadband,选择 ISP 和套餐, mobile-broadband-provider-info 会自动填入 APN 和其它设置。

PPPoE / DSL 支持[编辑 | 编辑源代码]

安装 rp-pppoe 以支持 PPPoE / DSL 连接。使用 nm-connection-editor 添加一个新的 DSL/PPPoE 连接。

VPN 支持[编辑 | 编辑源代码]

从 1.16 版本开始 NetworkManager 原生支持 WireGuard,它只需要 wireguard 内核模块。详见 WireGuard in NetworkManager 博客文章

对其他类型 VPN 的支持基于一个插件系统。它们在以下软件包中提供:

警告: 有很多与 VPN 支持有关的错误。Check the daemon processes options set via the GUI correctly and double-check with each package release.
注意:
  • 要在使用 VPN 时获得完整的 DNS 解析,请设置条件转发
  • 这些插件可能没有文档化的命令行界面,或者在没有运行 applet 的情况下根本无法工作。如果您使用的是常规 DE,那么这不是问题;如果不是,那么您应该在配置或激活连接时运行 #nm-applet,来显示必要的对话框。[1]

使用[编辑 | 编辑源代码]

NetworkManager 附带 nmcli(1)nmtui(1)

nmcli 示例[编辑 | 编辑源代码]

显示附近的 Wi-Fi 网络:

$ nmcli device wifi list

连接到 Wi-Fi 网络:

$ nmcli device wifi connect SSID_或_BSSID password 密码

连接到隐藏的 Wi-Fi 网络:

$ nmcli device wifi connect SSID_或_BSSID password 密码 hidden yes

连接到 wlan1 网络接口上的 Wi-Fi:

$ nmcli device wifi connect SSID_或_BSSID password 密码 ifname wlan1 profile_name

断开网络接口上的连接:

$ nmcli device disconnect ifname eth0

显示连接列表及其名称、UUID、类型和支持设备:

$ nmcli connection show

激活连接(即使用现有配置文件连接到网络):

$ nmcli connection up name_或_uuid

删除连接:

$ nmcli connection delete name_或_uuid

显示所有网络设备及其状态:

$ nmcli device

关闭 Wi-Fi:

$ nmcli radio wifi off

编辑连接[编辑 | 编辑源代码]

设置的完整列表参见 nm-settings(5)

首先需要获取连接列表:

$ nmcli connection
NAME                UUID                                  TYPE      DEVICE
Wired connection 2  e7054040-a421-3bef-965d-bb7d60b7cecf  ethernet  enp5s0
Wired connection 1  997f2782-f0fc-301d-bfba-15421a2735d8  ethernet  enp0s25
MY-HOME-WIFI-5G     92a0f7b3-2eba-49ab-a899-24d83978f308  wifi       --

第一列可以作为连接 connection-id, 本示例中使用 Wired connection 2

创建后有三种方法可以配置 Wired connection 2

nmcli 交互式编辑器
nmcli connection edit 'Wired connection 2'.
Usage is well documented from the editor.
nmcli 命令行界面
nmcli connection modify 'Wired connection 2' setting.property value. 用法参见 nmcli(1) 。 For example you can change its IPv4 route metric to 200 using nmcli connection modify 'Wired connection 2' ipv4.route-metric 200 command.

To remove a setting pass an empty field ("") to it like this:

nmcli connection modify 'Wired connection 2' setting.property ""
连接文件
/etc/NetworkManager/system-connections/ 中修改对应的 Wired connection 2.nmconnection 文件。
别忘了使用 nmcli connection reload 重新加载配置文件。

前端[编辑 | 编辑源代码]

为把 NetworkManager 与桌面环境集成,您可以安装一个 applet。这不仅便于您轻松访问网络选择和配置界面,更能提供一个安全存储网络密钥的代理程序。这种图形前端往往显示在系统托盘(或通知区域),从而允许用户选择网络或者配置 NetworkManager。各种桌面环境都有自己的 applet。你也可以使用 #nm-applet

GNOME[编辑 | 编辑源代码]

GNOME网络设置 里有内置的配置工具。

KDE Plasma[编辑 | 编辑源代码]

安装 plasma-nm 软件包。然后通过 面板的选项 > 添加部件 > 网络 将其添加到 KDE 的任务栏上。

nm-applet[编辑 | 编辑源代码]

network-manager-applet 是一个工作在 Xorg 环境下的 GTK 3 前端,带有一个系统托盘。

为了存储连接密码,您可以安装实现了 Secret Service D-Bus API 的应用程序,如 GNOME/KeyringKDE WalletKeePassXC

请注意,如果对某个连接启用了 对其他用户可用 选项,NetworkManager 就会明文存储密码,不过相应的文件只能被 root 或者其他使用 nm-applet 的用户访问。参照 #加密的 Wi-Fi 密码

要在没有系统托盘的情况下运行 nm-applet,可以使用 trayerstalonetray。例如,可以在自己的可执行文件路径中添加这样的脚本:

nmgui
#!/bin/sh
nm-applet    2>&1 > /dev/null &
stalonetray  2>&1 > /dev/null
killall nm-applet

当关闭 stalonetray 窗口的时候,它也会关闭 nm-applet,所以当你完成网络配置后它就不会再占用内存。

applet 可以显示一些事件的通知,比如连接或断开 WiFi。要显示这些消息,确保你已安装了一个通知服务器——请参见 Desktop notifications。如果在没有通知服务器的情况下使用,那么可能会在 stdout/stderr 中看到一些消息,并且 applet可能会挂起。请参见[2]

要在禁用通知的情况下使用 nm-applet,请使用以下命令运行 applet:

$ nm-applet --no-agent
提示:nm-applet 可能被自启动桌面配置项自动启动, 这种情况下请在那里的 Exec 一行添加 --no-agent 选项:
Exec=nm-applet --no-agent

Appindicator[编辑 | 编辑源代码]

从 1.18.0 版本开始,官方软件包 network-manager-applet包含 Appindicator 支持。要在 Appindicator 环境中使用 nm-applet,请使用以下命令启动 applet:

$ nm-applet --indicator

networkmanager-dmenu[编辑 | 编辑源代码]

networkmanager-dmenu-gitAUR 是一个通过 dmenurofi 而不是 nm-applet 管理 NetworkManager 连接的小脚本。它提供了所有必要的功能, 例如连接到已有的 NetworkManager wifi 或有线网络、连接到新的 wifi 网络、在需要的时候询问密码、连接到已有的 VPN、启用/停用网络连接、运行 nm-connection-editor 图形界面、连接到蓝牙网络等等。

配置[编辑 | 编辑源代码]

NetworkManager 需要一些额外的步骤才能正常运行。请确保已按照网络配置#设置计算机名一节的描述配置 /etc/hosts

NetworkManager 的全局配置文件位于 /etc/NetworkManager/NetworkManager.conf。额外的配置文件可以放在 /etc/NetworkManager/conf.d/ 中。全局的默认配置通常不需要改动。

编辑配置文件后,可以使用以下命令应用更改:

# nmcli general reload

NetworkManager-wait-online[编辑 | 编辑源代码]

Enabling NetworkManager.service also enables NetworkManager-wait-online.service, which is a oneshot system service that waits for the network to be configured. The latter has WantedBy=network-online.target, so it will finish only when network-online.target itself is enabled or pulled in by some other unit. See also systemd#Running services after the network is up.

By default, NetworkManager-wait-online.service waits for NetworkManager startup to complete, rather than waiting for network connectivity specifically (see nm-online(1)). If NetworkManager-wait-online.service finishes before the network is really up, resulting in failed services on boot, extend the unit to remove the -s from the ExecStart line:

[Service]
ExecStart=
ExecStart=/usr/bin/nm-online -q

注意这可能会导致其他问题

在某些情况下,由于超时太短,服务仍然无法在启动时成功启动。编辑服务,将 NM_ONLINE_TIMEOUT60 改为更大的值。

设置 PolicyKit 权限[编辑 | 编辑源代码]

默认情况下,活动的本地会话中的所有用户都能在没有密码的情况下改变大多数网络设置。参照 General troubleshooting#会话权限检查会话类型。在大多数情况下,一切都应该开箱即用。

一些操作(例如改变系统计算机名)需要管理员密码。这时,需要将自己添加wheel 用户组并运行一个 polkit 身份认证组件,它将提示输入密码。

对于远程会话(例如 headless VNC),有几种方法获得使用 NetworkManager 所需的权限:

  1. 将自己添加wheel 用户组。执行每个操作时都需要输入自己的密码。注意你的账户同时被赋予了此账户组的其他权限,例如使用 sudo 命令时无需输入 root 密码。
  2. 将自己添加network 用户组,同时创建一个包含以下内容的 /etc/polkit-1/rules.d/50-org.freedesktop.NetworkManager.rules 文件:
    polkit.addRule(function(action, subject) {
      if (action.id.indexOf("org.freedesktop.NetworkManager.") == 0 && subject.isInGroup("network")) {
        return polkit.Result.YES;
      }
    });
    
    network 用户组中的所有用户都将能免密码管理网络,这意味着不需要运行 polkit 身份认证组件,因此在 SSH 会话中也能工作。

代理设置[编辑 | 编辑源代码]

NetworkManager 不直接处理代理设置,但如果你使用 GNOMEKDE,你可以使用 proxydriverAUR,它将使用 NetworkManager 的信息处理代理设置。

要让 proxydriver 能够改变代理设置,你需要执行下面的命令,来让NetworkManager作为GNOME启动过程的一部分。(参见 GNOME#自启动)。

$ xhost +si:localuser:username

参见 Proxy server

检查互联网可连接性[编辑 | 编辑源代码]

NetworkManager 可以在连接到网络后尝试访问一个 Web 服务器,以确定该网络是否需要进行进一步认证(比如基于强制网络门户的网页认证)。 默认网址(在 /usr/lib/NetworkManager/conf.d/20-connectivity.conf 中配置)是 ping.archlinux.org。要使用其他 Web 服务器或禁用连接检查,请创建 /etc/NetworkManager/conf.d/20-connectivity.conf,请参见 NetworkManager.conf(5) § CONNECTIVITY SECTION。如下配置文件示例使用了由 GNOME 提供的另一个 Web 服务器 (并不一定要使用 GNOME 桌面环境):

/etc/NetworkManager/conf.d/20-connectivity.conf
[connectivity]
uri=http://nmcheck.gnome.org/check_network_status.txt

若要禁用 NetworkManager 的连接性检查(有时候一些 VPN 会阻止这类检查),请使用以下配置:

/etc/NetworkManager/conf.d/20-connectivity.conf
[connectivity] 
enabled=false
注意: 尽管自动连接检查理论上可能会泄露隐私,但 Arch Linux 默认设置的连接检查 Web 服务器承诺不会记录任何访问。 请参考 [3] [4].

强制网络门户[编辑 | 编辑源代码]

如果您连接的网络提供了 强制门户(一般用于网页认证), 桌面管理器会自动打开一个窗口并要求您输入凭据。如果您的桌面管理器没有这么做,则您可以安装 capnet-assist 软件包 (然而其 NetworkManager 的 disapatcher 脚本目前是坏的)。或者您也可以参考以下内容创建一个自己的 dispatcher 脚本。

/etc/NetworkManager/dispatcher.d/90-open_captive_portal
#!/bin/sh -e
# Script to dispatch NetworkManager events
#
# Runs shows a login webpage on walled garden networks.
# See NetworkManager(8) for further documentation of the dispatcher events.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

if [ -x "/usr/bin/logger" ]; then
    logger="/usr/bin/logger -s -t captive-portal"
else
    logger=":"
fi

wait_for_process() {
    PNAME=$1
    while [ -z "$(/usr/bin/pgrep $PNAME)" ]; do
        sleep 3;
    done
}

#launch the browser, but on boot we need to wait that nm-applet starts
start_browser() {
    local user="$1"
    local display="$2"

    export DISPLAY="$display"
    wait_for_process nm-applet

    export XAUTHORITY="/home/$user/.Xauthority"

    $logger "Running browser as '$user' with display '$display' to login in captive portal"
    sudo -u "$user" --preserve-env=DISPLAY,XAUTHORITY -H xdg-open http://capnet.elementary.io 2>&1 > /dev/null
}

# Run the right scripts
case "$2" in
    connectivity-change)
    $logger -p user.debug "dispatcher script triggered on connectivity change: $CONNECTIVITY_STATE"
    if [ "$CONNECTIVITY_STATE" = "PORTAL" ]; then
        # Match last column of who's output with ' :[at least one digit] '
        who | awk '$NF ~ /\(:[0-9]+\)/ { print $1 " " substr($NF, 2, length($NF)-2) };' | \
        while read user display; do
            start_browser $user $display || $logger -p user.err "Failed for user: '$user' display: '$display'"
        done
    fi
    ;;
    *)
    # In a down phase
    exit 0
    ;;
esac

您需要 重启 一次 NetworkManager.service 服务或者重启电脑才能让以上脚本生效。然后该脚本应该会在网络要求强制门户的时候自动为您打开登录窗口。

另一个解决方案是基于 Google Chrome 的 captive-browser-gitAUR

DHCP 客户端[编辑 | 编辑源代码]

NetworkManager 默认使用内置的 DHCP 客户端。内置的 DHCPv4 插件基于 nettools 的 n-dhcp4 库,而内置的 DHCPv6 插件的代码基于 systemd-networkd。

要使用不同的 DHCP 客户端,安装以下之一:

要更改 DHCP 客户端后端,请在 /etc/NetworkManager/conf.d/ 中的配置文件中设置选项 main.dhcp=dhcp 客户端名称。例如:

/etc/NetworkManager/conf.d/dhcp-client.conf
[main]
dhcp=dhclient
注意:
  • NetworkManger does not support using dhcpcd for IPv6. See NetworkManager issue #5.如果将 dhcpcd 设置为 DHCP 客户端,NetworkManager 将为 DHCPv6 使用内置 DHCP 客户端。
  • 不要启用 dhclientdhcpcd 软件包提供的 systemd 单元。它们会与 NetworkManager 冲突,详情请参见#安装中的注释。

DNS 管理[编辑 | 编辑源代码]

NetworkManager 的 DNS 管理在 GNOME 项目的 wiki 页面中进行了描述——Projects/NetworkManager/DNS

DNS 缓存和条件转发[编辑 | 编辑源代码]

NetworkManager 有一个插件,用于启用 DNS 缓存和条件转发 (以前 在 NetworkManager 的文档中称为“拆分 DNS”)。此设置的优点是 DNS 查找将被缓存,缩短了解析时间,并且 VPN 主机的 DNS 查找将路由到相关 VPN 的 DNS 服务器。如果您连接到多个 VPN,这尤其有用。

注意: 如果 /etc/resolv.conf 是指向 /run/systemd/resolve/stub-resolv.conf/run/systemd/resolve/resolv.conf/lib/systemd/resolv.conf/usr/lib/systemd/resolv.conf 的符号链接,NetworkManager 将自动选择 systemd resolved。要使用 dnsmasq,必须首先删除该符号链接,然后重新启动 NetworkManager。
dnsmasq[编辑 | 编辑源代码]

确保已安装 dnsmasq。然后在 /etc/NetworkManager/conf.d/ 中的配置文件中设置 main.dns=dnsmasq

/etc/NetworkManager/conf.d/dns.conf
[main]
dns=dnsmasq

现在以 root 身份运行 nmcli general reload。NetworkManager 将自动启动 dnsmasq 并将 127.0.0.1 添加到 /etc/resolv.conf。原来的 DNS 服务器可以在 /run/NetworkManager/no-stub-resolv.conf 中找到。You can verify dnsmasq is being used by doing the same DNS lookup twice with drill example.com and verifying the server and query times.

注意:
  • 无需启动 dnsmasq.service 或编辑 /etc/dnsmasq.conf。NetworkManager 不通过 systemd 服务启动 dnsmasq,也不会读取 dnsmasq 的默认配置文件。
  • The dnsmasq instance started by NetworkManager will bind to 127.0.0.1:53, you cannot run any other software (including dnsmasq.service) on the same address and port.
自定义 dnsmasq 配置[编辑 | 编辑源代码]

可以通过在 /etc/NetworkManager/dnsmasq.d/ 中创建配置文件为 dnsmasq 创建自定义配置。例如,要更改 DNS 缓存(存储在 RAM 中)的大小:

/etc/NetworkManager/dnsmasq.d/cache.conf
cache-size=1000

可以使用以下命令检查配置文件语法:

$ dnsmasq --test --conf-file=/dev/null --conf-dir=/etc/NetworkManager/dnsmasq.d

所有可用选项请参见 dnsmasq(8)

IPv6[编辑 | 编辑源代码]

本文或本章节的事实准确性存在争议。

原因: This does not solve the issue because NetworkManager does not add ::1 to /etc/resolv.conf. Unless @::1 is manually passed to drill, it will still fail with Error: error sending query: No (valid) nameservers defined in the resolver.(在 Talk:NetworkManager 中讨论)


Enabling dnsmasq in NetworkManager may break IPv6-only DNS lookups (i.e. drill -6 [hostname]) which would otherwise work. In order to resolve this, creating the following file will configure dnsmasq to also listen to the IPv6 loopback:

/etc/NetworkManager/dnsmasq.d/ipv6-listen.conf
listen-address=::1

此外,dnsmasq 也不优先考虑上游 IPv6 DNS。遗憾的是,NetworkManager 无法执行此操作 (Ubuntu Bug)。解决方法是在 NetworkManager 配置中禁用 IPv4 DNS(如果有)。

DNSSEC[编辑 | 编辑源代码]

默认情况下,NetworkManager 启动的 dnsmasq 实例不会验证 DNSSEC,因为它是使用 --proxy-dnssec 选项启动的。它将信任从上游 DNS 服务器获得的任何 DNSSEC 信息。

要让 dnsmasq 验证 DNSSEC(这会破坏不支持 DNSSEC 的 DNS 服务器的解析),请创建以下配置文件:

/etc/NetworkManager/dnsmasq.d/dnssec.conf
conf-file=/usr/share/dnsmasq/trust-anchors.conf
dnssec
systemd-resolved[编辑 | 编辑源代码]

NetworkManager 能把 systemd-resolved 作为DNS解析器和缓存。在使用它之前,确保你已正确配置 systemd-resolved 并且 systemd-resolved.service 已经启动

如果 /etc/resolv.conf 是一个指向 /run/systemd/resolve/stub-resolv.conf/run/systemd/resolve/resolv.conf/usr/lib/systemd/resolv.conf符号链接,systemd-resolved会自动启用。

你也可以通过编写 /etc/NetworkManager/conf.d/ 下的配置文件,设置 main.dns=systemd-resolved 来显式启用它:

/etc/NetworkManager/conf.d/dns.conf
[main]
dns=systemd-resolved
DNS resolver with an openresolv subscriber[编辑 | 编辑源代码]

If openresolv has a subscriber for your local DNS resolver, set up the subscriber and configure NetworkManager to use openresolv.

Because NetworkManager advertises a single "interface" to resolvconf, it is not possible to implement conditional forwarding between two NetworkManager connections. See NetworkManager issue 153.

This can be partially mitigated if you set private_interfaces="*" in /etc/resolvconf.conf[5]. Any queries for domains that are not in search domain list will not get forwarded. They will be handled according to the local resolver's configuration, for example, forwarded to another DNS server or resolved recursively from the DNS root.

自定义 DNS 服务器[编辑 | 编辑源代码]

设置自定义全局 DNS 服务器[编辑 | 编辑源代码]

要为所有连接设置 DNS 服务器,请在 NetworkManager.conf(5) 中的 [global-dns-domain-*] 的部分下使用 servers=serveripaddress1,serveripaddress2,serveripaddress3 的格式指定它们。例如:

/etc/NetworkManager/conf.d/dns-servers.conf
[global-dns-domain-*]
servers=::1,127.0.0.1
注意:
设置自定义连接 DNS 服务器[编辑 | 编辑源代码]
设置自定义连接 DNS 服务器(GUI)[编辑 | 编辑源代码]

Setup will depend on the type of front-end used; the process usually involves right-clicking on the applet, editing (or creating) a profile, and then choosing DHCP type as Automatic (specify addresses). The DNS addresses will need to be entered and are usually in this form: 127.0.0.1, DNS-server-one, ....

设置自定义连接 DNS 服务器(nmcli / 连接文件)[编辑 | 编辑源代码]

To setup DNS Servers per connection, you can use the dns field (and the associated dns-search and dns-options) in the connection settings[损坏的链接:无效的章节].

If method is set to auto (when you use DHCP), you need to set ignore-auto-dns to yes.

/etc/resolv.conf[编辑 | 编辑源代码]

NetworkManager 对 /etc/resolv.conf 的管理方式可以通过 main.rc-manager 选项配置。 networkmanager 会把它设置为 symlink,而不是上游默认的 auto。 具体设置及其值在 NetworkManager.conf(5) 手册页中有说明。

提示:使用 openresolv 可以让 NetworkManager 和其他管理 resolvconf 的软件共存。比如如果要运行一个本地 DNS 缓存和拆分 DNS 解析器,openresoly可以提供一个 subscriber。不过将 NetworkManager 与 openresolv 一起使用时,要注意条件转发仍未得到完全支持

NetworkManager 还通过调度程序脚本提供钩子,可用于在网络变化后修改 /etc/resolv.conf。有关更多信息,请参见 #使用 NetworkManager 调度网络服务NetworkManager(8)

注意:
  • 如果 NetworkManager 被配置为使用 dnsmasqsystemd-resolved,则相应的环回地址将写入 /etc/resolv.conf
  • NetworkManager (要)写入 /etc/resolv.confresolv.conf 文件位于 /run/NetworkManager/resolv.conf
  • /run/NetworkManager/no-stub-resolv.conf 中包含 NetworkManager 获取到的 DNS 服务器和搜索域。
不让 NetworkManager 管理 /etc/resolv.conf[编辑 | 编辑源代码]

要防止 NetworkManager 修改 /etc/resolv.conf,请在 /etc/NetworkManager/conf.d/ 下的配置文件中设置 main.dns=none 选项:

/etc/NetworkManager/conf.d/dns.conf
[main]
dns=none
提示:You might also want to set main.systemd-resolved=false, so that NetworkManager does not send the DNS configuration to systemd-resolved.
注意: See DNS caching and conditional forwarding, to configure NetworkManager using other DNS backends like dnsmasq and systemd-resolved, instead of using main.dns=none.

After that /etc/resolv.conf might be a broken symlink that you will need to remove. Then, just create a new /etc/resolv.conf file.

Use openresolv[编辑 | 编辑源代码]
注意: NetworkManager 不支持使用由 systemd-resolvconf 提供的 systemd-resolved 的 resolvconf 接口 (resolvectl(1) § COMPATIBILITY WITH RESOLVCONF(8))

要将 NetworkManager 配置为使用 openresolv,请在 /etc/NetworkManager/conf.d/ 目录下的配置文件中设置 main.rc-manager=resolvconf:

/etc/NetworkManager/conf.d/rc-manager.conf
[main]
rc-manager=resolvconf

防火墙[编辑 | 编辑源代码]

可以根据当前的连接分配 firewalld 区域。例如,在工作时限制较多,在家时限制较少。

也可以通过 NetworkManager 调度实现。

使用 NetworkManager 调度网络服务[编辑 | 编辑源代码]

有些服务只有联网时才有意义,例如 NFS, SMBNTPd。NetworkManager 可以在连接网络后启动这些服务,并在网络关闭时停止它们。

要使用这一功能, 需要 enablestart NetworkManager-dispatcher.service

服务启动后,可以将脚本加到 /etc/NetworkManager/dispatcher.d 目录。

这些脚本必须属于 root, 否则不会被执行。为了安全起见, 所属 用户组也设置为 root:

# chown root:root /etc/NetworkManager/dispatcher.d/10-script.sh

而且脚本必须是 可执行 的。

# chmod 755 scriptname

脚本将在连接网络时按字母表顺序运行,并在网络停止时反向停止。要保证启动顺序,可以在前面加数字,例如 10_portmap30_netfs 这样就能保证 portmapper 在 NFS 挂载之前启动。

脚本将接收如下参数:

  • Interface name: 例如 eth0
  • Action: up, down, vpn-up, vpn-down, ... (完整列表请参考 NetworkManager(8))
警告: 如果没有连接到外部网络,请注意启动的服务和需要它们的程序。如果连接公共网络时启动了错误的服务,可能导致安全问题。

避免超时[编辑 | 编辑源代码]

如果一切运行良好, 那么这一节就可以跳过了。然而,如果你要运行的 dispatcher 脚本需要花费更多时间来运行,则可能出现一些问题。一开始,默认的内部超时时间为 3 秒,如果脚本未在该时间内执行完毕,则会被终止。后来这一超时被延长到大约 20 秒(参见 Bugtracker)。如果 20 秒仍然不够,那么您可以通过修改 dispatcher 服务文件 /usr/lib/systemd/system/NetworkManager-dispatcher.service 来让它在退出后保持活动。

/etc/systemd/system/NetworkManager-dispatcher.service
.include /usr/lib/systemd/system/NetworkManager-dispatcher.service
[Service]
RemainAfterExit=yes

运行修改后的 NetworkManager-dispatcher 服务并将其设置为开机启动。

警告: 加入了 RemainAfterExit 那一行后会让 dispatcher 一直运行。不幸的是, dispatcher 必须在再次运行脚本前被关闭。这意味着脚本只能被运行一次。因此, 除非超时确实引起了问题, 否则不要修改服务文件。

Dispatcher 示例[编辑 | 编辑源代码]

使用 sshfs 挂载远程目录[编辑 | 编辑源代码]

由于该脚本在一个非常受限制的环境中运行, 为了连接上SSH agent, 你必须 export SSH_AUTH_SOCK。 这里有几种不同方式, 参照 here 获取更多详细信息. 以下示例需要 gnome-keyring , 如果 gnome-keyring 没解锁,将需要你输入密码. 如果 NetworkManager 设置为登录后自动连接, 很有可能因为 gnome-keyring 还没启动导致失败(转入睡眠). 对应的 UUID 可以通过命令 nmcli con statusnmcli con list 查得。

#!/bin/sh
USER='username'
REMOTE='user@host:/remote/path'
LOCAL='/local/path'

interface=$1 status=$2
if [ "$CONNECTION_UUID" = "uuid" ]; then
  case $status in
    up)
      # sleep 10
      SSH_AUTH_SOCK=$(find /tmp -maxdepth 1 -type s -user "$USER" -name 'ssh')
      export SSH_AUTH_SOCK
      su "$USER" -c "sshfs $REMOTE $LOCAL"
      ;;
    down)
      fusermount -u "$LOCAL"
      ;;
  esac
fi

挂载 SMB 共享[编辑 | 编辑源代码]

一些 SMB 共享仅在某些网络或位置(例如在家中)可用。您可以使用 dispatcher 脚本来仅挂载当前位置中存在的 SMB 共享。

下面的脚本将检查我们是否连接到一个特定的网络,并挂载共享 :

/etc/NetworkManager/dispatcher.d/30-mount-smb.sh
#!/bin/sh

# Find the connection UUID with "nmcli connection show" in terminal.
# All NetworkManager connection types are supported: wireless, VPN, wired...
if [ "$2" = "up" ]; then
  if [ "$CONNECTION_UUID" = "uuid" ]; then
    mount /your/mount/point & 
    # add more shares as needed
  fi
fi

以下脚本将在正常断开与特定网络的连接之前卸载所有 SMB 共享:

/etc/NetworkManager/dispatcher.d/pre-down.d/30-umount-smb.sh
#!/bin/sh

if [ "$CONNECTION_UUID" = "uuid" ]; then
  umount -a -l -t cifs
fi
注意: 如上所示,请确保该脚本位于 pre-down.d 子目录中,否则它将在任何连接状态更改时卸载所有共享。

下面的脚本将尝试在意外断开与特定网络的连接后卸载所有 SMB 共享 :

/etc/NetworkManager/dispatcher.d/40-umount-smb.sh
#!/bin/sh

if [ "$CONNECTION_UUID" = "uuid" ]; then
  if [ "$2" = "down" ]; then
    umount -a -l -t cifs
  fi
fi
注意:
模板错误: 您在尝试使用 = 标志吗? 有关解决方法,请访问 Help:Template#Escape template-breaking characters

另一种方法是使用 NFS#Using a NetworkManager dispatcher 中的脚本 :

/etc/NetworkManager/dispatcher.d/30-smb.sh
#!/bin/sh

# Find the connection UUID with "nmcli con show" in terminal.
# All NetworkManager connection types are supported: wireless, VPN, wired...
WANTED_CON_UUID="CHANGE-ME-NOW-9c7eff15-010a-4b1c-a786-9b4efa218ba9"

if [ "$CONNECTION_UUID" = "$WANTED_CON_UUID" ]; then
    
    # Script parameter $1: network interface name, not used
    # Script parameter $2: dispatched event
    
    case "$2" in
        "up")
            mount -a -t cifs
            ;;
        "down"|"pre-down"|"vpn-pre-down")
            umount -l -a -t cifs >/dev/null
            ;;
    esac
fi
注意: 此脚本会忽略带有 noauto 选项的挂载,删除此挂载选项或使用 auto 选项来允许 dispatcher 管理这些挂载。

/etc/NetworkManager/dispatcher.d/pre-down/ 目录下创建一个符号链接来捕获 pre-down 事件 :

# ln -s ../30-smb.sh /etc/NetworkManager/dispatcher.d/pre-down.d/30-smb.sh

挂载 NFS 共享[编辑 | 编辑源代码]

参见 NFS#Using a NetworkManager dispatcher.

当插入网线时用 dispatcher 脚本自动关闭无线网络[编辑 | 编辑源代码]

其想法是,仅当 LAN 电缆拔下时(例如,从笔记本电脑底座上拔下时)才打开 Wi-Fi,一旦 LAN 电缆再次插入,Wi-Fi 将自动禁用。

创建以下 dispatcher 脚本 [6],并将其中的 LAN_interface 替换为您自己的网络接口。

/etc/NetworkManager/dispatcher.d/wlan_auto_toggle.sh
#!/bin/sh

if [ "$1" = "LAN_interface" ]; then
    case "$2" in
        up)
            nmcli radio wifi off
            ;;
        down)
            nmcli radio wifi on
            ;;
    esac
elif [ "$(nmcli -g GENERAL.STATE device show LAN_interface)" = "20 (unavailable)" ]; then
    nmcli radio wifi on
fi

{{注意|您可以使用 nmcli 命令获得接口列表。以太网(LAN)接口的前缀为 en,例如 enp0s5}}

使用 dispatcher 在网络连接建立后连接 vpn[编辑 | 编辑源代码]

此部分示例演示如果自动连接到NetworkManager已定义的vpn-connection.首先创建调度脚本定义vpn连接之后的事务

注意: 要使用 iwgetid,需要安装 wireless_tools
/etc/NetworkManager/dispatcher.d/vpn-up
#!/bin/sh
VPN_NAME="name of VPN connection defined in NetworkManager"
ESSID="Wi-Fi network ESSID (not connection name)"

interface=$1 status=$2
case $status in
  up|vpn-down)
    if iwgetid | grep -qs ":\"$ESSID\""; then
      nmcli connection up id "$VPN_NAME"
    fi
    ;;
  down)
    if iwgetid | grep -qs ":\"$ESSID\""; then
      if nmcli connection show --active | grep "$VPN_NAME"; then
        nmcli connection down id "$VPN_NAME"
      fi
    fi
    ;;
esac

如果想在任意 Wi-Fi 网络都可以自动连接 VPN, 你可以用这样给 ESSID 赋值: ESSID=$(iwgetid -r)。记住要给脚本设置相应的权限, 参见 上文

由于 VPN secrets 保管方式的原因, 连接依然可能会失败并且 NetworkManager-dispatcher.service 会报错 'no valid VPN secrets'。这样就需要用下面方法让脚本可以获取 VPN 密码:

或者直接在 VPN 配置文件中加入 vpn-secrets 并写入密码:

1: 可以选择编辑 VPN 连接的配置文件让 NetworkManager 自己储存 secrets 而不是把 secrets 保存在 不能被root访问的 keyring 中: 打开 /etc/NetworkManager/system-connections/name of your VPN connection, 把 password-flags 以及 secret-flags1 改为 0

如果还不行,需要在安全位置创建 passwd-file,权限和 dispatcher 脚本一致,并添加如下内容:

/path/to/passwd-file
vpn.secrets.password:YOUR_PASSWORD

脚本也需要做出相应修改:

/etc/NetworkManager/dispatcher.d/vpn-up
#!/bin/sh
VPN_NAME="name of VPN connection defined in NetworkManager"
ESSID="Wi-Fi network ESSID (not connection name)"

interface=$1 status=$2
case $status in
  up|vpn-down)
    if iwgetid | grep -qs ":\"$ESSID\""; then
      nmcli connection up id "$VPN_NAME" passwd-file /path/to/passwd-file
    fi
    ;;
  down)
    if iwgetid | grep -qs ":\"$ESSID\""; then
      if nmcli connection show --active | grep "$VPN_NAME"; then
        nmcli connection down id "$VPN_NAME"
      fi
    fi
    ;;
esac

2: 修改 password-flags,将密码加入配置文件的 vpn-secrets 部分:

 [vpn]
 ....
 password-flags=0
 
 [vpn-secrets]
 password=your_password
注意: 可能需要打开配置界面,重新设置 VPN 密码。

用 dispatcher 来禁用 VPN 上的 IPv6 连接[编辑 | 编辑源代码]

许多商业 VPN 供应商仅支持 IPv4。这意味着所有 IPv6 流量都会绕过 VPN,使 VPN 变得毫无用处。为了避免这种情况,可以使用 dispatcher 在 VPN 连接启动时禁用所有 IPv6 流量。

/etc/NetworkManager/dispatcher.d/10-vpn-ipv6
#!/bin/sh

case "$2" in
	vpn-up)
		echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
		;;
	vpn-down)
		echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6
		;;
esac

启动 OpenNTPD[编辑 | 编辑源代码]

安装 networkmanager-dispatcher-openntpdAUR 软件包

使用 systemd-timesyncd 动态设置通过 DHCP 获取到的 NTP 服务器[编辑 | 编辑源代码]

当你在不同的网络之间漫游时 ( 例如公司的局域网,家里的 WiFi,以及不时出现的其他 WiFi) ,你可能需要把 timesyncd 使用的 NTP 服务器设置为 DHCP 提供的服务器。但是,NetworkManager 本身不能与 systemd-timesyncd 通信来设置 NTP 服务器。

dispatcher 可以解决这个问题。

Create the overlay directory for your systemd-timesyncd configuration /etc/systemd/timesyncd.conf.d if it does not already exist. Inside /etc/NetworkManager/dispatcher.d, put the following:

/etc/NetworkManager/dispatcher.d/10-update-timesyncd
#!/bin/sh

[ -z "$CONNECTION_UUID" ] && exit 0
INTERFACE="$1"
ACTION="$2"

case $ACTION in
up | dhcp4-change | dhcp6-change)
    [ -n "$DHCP4_NTP_SERVERS" ] || exit
    mkdir -p /etc/systemd/timesyncd.conf.d
    cat <<-THE_END >"/etc/systemd/timesyncd.conf.d/${CONNECTION_UUID}.conf"
        [Time]
        NTP=$DHCP4_NTP_SERVERS
    THE_END
    systemctl restart systemd-timesyncd.service
    ;;
down)
    rm -f "/etc/systemd/timesyncd.conf.d/${CONNECTION_UUID}.conf"
    systemctl restart systemd-timesyncd.service
    ;;
esac

每次 NetworkManager 设置新的网络连接(ACTION=up)或获得现有连接的一些更新(ACTION=dhcp4-changeACTION=dhcp6-change),并且提供的连接数据包含有关 NTP 服务器(DHCP4_NTP_SERVERS)的信息时,都会将特定于连接的覆盖配置文件写入 /etc/systemd/timesyncd.conf.d,其中包含获取到的 NTP 服务器。每当连接被断开(ACTION=down)时,特定于连接的覆盖文件就会被删除。每次更改 systemd-timesyncd 的配置后,都会重新启动此服务以获取更新的配置。为每个连接使用单独的配置文件是为了避免 NetworkManager 并行管理两个或多个连接时,配置中的不同 NTP 服务器名称被覆盖,因为 updhcp4-changedhcp6-changedown 操作可能以任意顺序出现。

测试[编辑 | 编辑源代码]

NetworkManager 托盘组件被设计成开机自动启动,所以对大部分用户来说,并不需要过多配置。但是如果你手动停用旧有的网络设置断网,你需要测试一下 NetworkManager 是否正常工作。首先启动 NetworkManager.service.

有些托盘组件会提供给你一个 .desktop 文件以便通过系统菜单运行。如果没有,那你就需要通过命令或者注销重登录系统来让托盘组件运行。 一旦托盘组件运行了,它会自动请求网络连接并通过 DHCP 服务器来进行网络配置。

在一些 non-xdg-compliant 窗口系统,比如在 awesome 中启动 GNOME applet:

nm-applet --sm-disable &

如果需要静态 IP,你需要配置 NetworkManager。一般来说,在托盘图标上面点击右键,选择「编辑连接」即可。

提示与技巧[编辑 | 编辑源代码]

加密的 Wi-Fi 密码[编辑 | 编辑源代码]

NetworkManager 默认会在 /etc/NetworkManager/system-connections/ 中的连接文件当中以明文形式存储密码。要显示存储的密码,使用以下命令:

# grep -r '^psk=' /etc/NetworkManager/system-connections/

密码可以被文件系统中的 root 用户和通过 GUI(如 nm-applet)访问设置的用户访问。

最好将密码以加密形式保存在密钥环中,而不是明文。这样做的缺点是必须为每个用户设置连接。

更好的做法是以加密形式将密码存储在密钥环中而不是以明文形式存储,使用密钥环的缺点是需要为每一个用户单独配置连接。

为了对密钥环进行读写操作,必须有一个可用的机密代理,比如:

  • nmcli with the --ask option
  • #前端 中提及的任一图形界面

如果这两者都不可用,那么身份验证将失败,并提示错误 no secrets: No agents were available for this request.

使用 GNOME 密钥环[编辑 | 编辑源代码]

需要启动密钥环守护程序并解锁密钥环才能让之后的东西工作。

此外,需要将 NetworkManager 配置为不存储所有用户的密码。使用 GNOME 的 network-manager-applet,从终端运行 nm-connection-editor,选择一个网络连接,单击 编辑,选择 Wi-Fi 安全性 选项卡,单击密码的右图标并选择 只为此用户存储密码

使用 KDE Wallet[编辑 | 编辑源代码]

若使用 KDE 的 plasma-nm,请单击该小部件,然后单击右上角的 设置 图标,再单击网络连接,在 常规配置 选项卡中,取消选中 允许所有用户连接到此网络。如果勾选该选项,即使正在运行 keyring 守护程序,密码仍将以明文形式存储。

如果之前选择了该选项又取消勾选了,则可能必须首先使用 重置 选项才能使密码从文件中消失。或者,首先删除连接并重新设置它。

通过 Wi-Fi 共享网络连接(移动热点)[编辑 | 编辑源代码]

使用 NetworkManager,您只要点击几下就可以共享你的 Internet 连接(例如,3G或者有线)。请注意 可能会干扰该功能。

您需要一个支持 AP 模式的 Wi-Fi 卡,请参阅 软件接入点#无线网卡必须支持AP模式 了解详细信息。

安装dnsmasq 软件包。请注意 NetworkManager 会启动自己的独立于dnsmasq.servicednsmasq 实例,作为 DHCP 服务器。有关注意事项,请参见 #dnsmasq

创建共享连接:

  • 点击小部件并选择 创建新无线网络.
  • 按照向导操作(选择 WPA2 或更高版本,请确保使用至少 8 个字符长的密码,密码太短会创建失败)。
    • 选择 Hotspot(热点) 或 Ad-hoc 作为 Wi-Fi 模式。

该连接将被保存供下次使用。

注意: Android 不支持连接到 Ad-hoc 网络。要与 Android 共享连接,请使用基础结构模式(即将 Wi-Fi 模式设置为“热点”)。

通过以太网共享连接(作为WISP路由器)[编辑 | 编辑源代码]

场景:您的设备能通过 Wi-Fi 上网,并且您希望通过以太网与其他设备共享互联网连接,这一般被称为 WISP。

要求:

  • 安装dnsmasq 软件包。请注意 NetworkManager 会启动自己的独立于dnsmasq.servicednsmasq 实例,作为 DHCP 服务器。有关注意事项,请参见 #dnsmasq
  • 您的互联网连接设备和其他设备通过合适的以太网电缆连接(这通常意味着网线直连或两者接到同一个交换机上)。
  • 防火墙没有阻止网络连接共享。

步骤:

  • 在终端里运行 nm-connection-editor
  • 添加新的以太网连接
  • 为新的连接起一个合适的名字
  • 转到 “IPv4 设置” 选项
  • 在“方法”下选择“共享给其他计算机”
  • 保存

现在,您应该可以在 NetworkManager 的有线连接列表中看到您新创建的连接。

在cron任务(jobs)或脚本中检查网络是否连接[编辑 | 编辑源代码]

有些cron任务需要可用的网络连接才能成功。你可能希望在网络不可用时避免运行这些任务。为此,添加一个网络的if 测试,该测试询问NetworkManager的nm-tool 并检查网状态。下面展示的测试当任何接口可用时成功,当所有借口都不可用时失败。对于可能连接有线,无线或者关闭网络的笔记本来说这很方便。

if [ $(nm-tool|grep State|cut -f2 -d' ') == "connected" ]; then
    #Whatever you want to do if the network is online
else
    #Whatever you want to do if the network is offline - note, this and the else above are optional
fi

例如,这对于一个运行fpupdate来更新F-Prot病毒扫描签名的cron.hourly脚本很有帮助。另外,经过小的修改,使用nm-tool输出的不同部分可以区分不同的网络;例如,因为活跃的无线网络连接有一个星号(asterisk'*')表示,你可以grep网络名,然后grep星号。

登录后自动解锁秘钥环[编辑 | 编辑源代码]

NetworkManager 默认不会连接到需要密码的网络,仅在登录后才会连接,要不登录就连接网络,请使用下面方式配置:

  1. 右击面板中的nm-applet图标,并选择编辑连接(Edit Connections),打开无线(wireless)选项卡
  2. 选择你想要使用的连接并单击编辑
  3. 勾选"自动连接"(“Connect Automatically”)框和"对所有用户可用"(“Available to all users”)框

登出并重新登录,完成。

有密码认证的KDE and OpenConnect VPN[编辑 | 编辑源代码]

虽然可以在连接时直接输入,plasma-nm 0.9.3.2-1及以上版本还支持从 KWallet 中获取OpenConnect用户名和密码。

打开"KDE Wallet Manager"在"Network Management|Maps"下面查找你的OpenConnect VPN连接。点击"Show values"并在这个表的键值"VpnSecrets"下输入你的凭据(相应替代'username'和'password'):

form:main:username%SEP%username%SEP%form:main:password%SEP%password

下次你连接时,用户名和密码会出现在"VPN secrets"对话框中。

忽略特定设备[编辑 | 编辑源代码]

有时,可能希望Networkmanager忽略特定的设备,并且不为他们配置地址和路由。通过在/etc/NetworkManager/NetworkManager.conf中使用下述配置,你可以快速轻松地按照MAC或者接口名忽略设备。

[keyfile]
unmanaged-devices=mac:00:22:68:1c:59:b1;mac:00:1E:65:30:D1:C4;interface-name:eth0

填入上述内容后,执行 # nmcli general reload 后,应该能在NetworkManager不改变已完成设置的情况下配置接口。

配置 MAC 地址随机化[编辑 | 编辑源代码]

注意: 有时候可能需要禁用 MAC 地址随机化功能以获得(稳定的)链路连接(比如[7])。也有些网络会基于 MAC 地址来限制设备接入或限速。

MAC 地址随机化可以通过不向网络公开真实的 MAC 地址来提高隐私。

NetworkManager 支持两种类型的 MAC 地址随机化:扫描期间的随机化和网络连接时的随机化。这两种模式都可以通过修改 /etc/NetworkManager/NetworkManager.conf 或在 /etc/NetworkManager/conf.d/ 中创建单独的配置文件来配置,推荐使用后者,因为 NetworkManager 可能会覆盖前者的配置文件。

Wi-Fi 扫描期间的随机化默认情况下是启用的,但是可以通过在 /etc/NetworkManager/NetworkManager.conf/etc/NetworkManager/conf.d 下的专用配置文件中添加以下行来禁用 :

/etc/NetworkManager/conf.d/wifi_rand_mac.conf
[device]
wifi.scan-rand-mac-address=no

可以为无线和以太网接口设置不同的网络连接时的 MAC 地址随机化模式。有关不同模式的更多细节,请参见 GNOME 博客文章

在 MAC 地址随机化方面,最重要的模式是 stablerandom。当您连接到新网络时,stable 会生成一个随机 MAC 地址,并将两者永久关联。这意味着您每次连接到该网络时都将使用相同的 MAC 地址。相比之下,random 每次连接到新的或以前已知的网络时都会生成一个新的 MAC 地址。您可以通过在 /etc/NetworkManager/conf.d 下添加所需配置来配置 MAC 随机化:

/etc/NetworkManager/conf.d/wifi_rand_mac.conf
[device-mac-randomization]
# "yes" is already the default for scanning
wifi.scan-rand-mac-address=yes
 
[connection-mac-randomization]
# Randomize MAC for every ethernet connection
ethernet.cloned-mac-address=random
# Generate a random MAC for each WiFi and associate the two permanently.
wifi.cloned-mac-address=stable

参考这篇 GNOME 博客文章 了解更多详情。

启用IPv6隐私扩展[编辑 | 编辑源代码]

参见 IPv6#NetworkManager

给每个网络连接配置单独的 DUID[编辑 | 编辑源代码]

DHCPv6 唯一标识符(DUID)是 DHCPv6 客户端用于向 DHCPv6 服务器标识自身的值。NetworkManager 支持 3 种类型的 DUID:

  • DUID-UUID (RFC 6355): 从通用唯一标识符(UUID)生成。
  • DUID-LL (RFC 3315): 从链路层地址 ( 即 MAC 地址 ) 生成。
  • DUID-LLT (RFC 3315): 从链路层地址加上时间戳生成。

如果使用了 NetworkManager 内置的 DHCP 客户端(默认如此),则他会使用从机器 ID (/etc/machine-id) 生成的 DUID 来标识自身。这意味着机器上的所有网络连接都用的是同一个 DUID,这可能会造成隐私泄露。

幸运的是,NetworkManager 能够为每个连接提供唯一的 DUID,这些 DUID 来自连接的 stable-id 和每个主机的唯一密钥。您可以通过在 /etc/NetworkManager/conf.d 下添加以下配置来启用它 :

/etc/NetworkManager/conf.d/duid.conf
[connection]
ipv6.dhcp-duid=stable-uuid

该选项同时还支持 stable-llstable-llt 等值。有关更多信息,请阅读 nm-settings(5) § ipv6 settingdhcp-duid 的说明。

Working with wired connections[编辑 | 编辑源代码]

By default, NetworkManager generates a connection profile for each wired ethernet connection it finds. At the point when generating the connection, it does not know whether there will be more Ethernet adapters available. Hence, it calls the first wired connection "Wired connection 1". You can avoid generating this connection, by configuring no-auto-default (see NetworkManager.conf(5)), or by simply deleting it. Then NetworkManager will remember not to generate a connection for this interface again.

You can also edit the connection (and persist it to disk) or delete it. NetworkManager will not re-generate a new connection. Then you can change the name to whatever you want. You can use something like nm-connection-editor for this task.

Using iwd as the Wi-Fi backend[编辑 | 编辑源代码]

注意:
  • Do not enable iwd.service or manually configure iwd. NetworkManager will start and manage it itself.
  • Consider existing issues before switching to iwd.

To enable the experimental iwd backend, first 安装 iwd and then create the following configuration file:

/etc/NetworkManager/conf.d/wifi_backend.conf
[device]
wifi.backend=iwd

Alternatively, you can install networkmanager-iwdAUR, a modified package configured to build NetworkManager working exclusively with iwd, with the main difference being that iwd is required and wpa_supplicant can be uninstalled after building.

注意: You may need to convert existing NetworkManager network profiles after switching to iwd.

Running in a network namespace[编辑 | 编辑源代码]

If you would like to run NetworkManager inside a network namespace (e.g., to manage a specific device which should be use by selected applications), bring the device down before moving it to the namespace:

$ ip link set dev MY_DEVICE down
$ ip link set dev MY_DEVICE netns MY_NAMESPACE
$ ip netns exec MY_NAMESPACE NetworkManager
...
$ ip netns exec MY_NAMESPACE killall NetworkManager

otherwise NetworkManager will later fail to establish the connection with a device is strictly unmanaged error.

Automatically connect to VPN[编辑 | 编辑源代码]

NetworkManager can be set to automatically connect to a VPN when connecting to the internet, on a per network basis. The VPN connection itself can be added in GNOME's NetworkManager front-end, but to make it automatically use the VPN nmcli must be used. Other front-ends might not have this limitation.

First, make sure to make the VPN connection available to all users. In the GNOME this is a matter of checking a box under the details tab. Under the Identity tab, in the password field, click the icon on the right side in the field, and set it to Store the password for all users.

Then find the UUID of the VPN connection, and add that to connection.secondaries of the Internet connection:

# UUID=$(nmcli --get-values connection.uuid connection show name-of-VPN-connection)
# nmcli connection modify name-of-Internet-connection connection.secondaries "$UUID"

Now when NetworkManager is restarted and you connect to the Internet connection you have configured, you should automatically get connected to the VPN.

问题解决[编辑 | 编辑源代码]

安全 Wi-Fi 网络不提示输入密码[编辑 | 编辑源代码]

尝试连接到安全的 Wi-Fi 网络时,既不提示输入密码,也不建立连接。如果没有安装密钥环软件包,这种情况就会发生。一个简单的解决方案是安装 gnome-keyring。如果希望密码以加密的形式存储,按照 GNOME/Keyring 来设置 gnome-keyring-daemon

Network management disabled[编辑 | 编辑源代码]

当 NetworkManager 关闭但 pid(state)文件未删除时,就会看到 Network management disabled 提示。此时,请手动删除文件:

# rm /var/lib/NetworkManager/NetworkManager.state

内置 DHCP 客户端的问题[编辑 | 编辑源代码]

如果在使用内置 DHCP 客户端获取 IP 地址时遇到问题,请考虑换一个 DHCP 客户端,有关说明,请参阅 #DHCP 客户端。这种解决方法可能会解决大型无线网络(如 eduroam)中的问题。

使用 dhclient 时的 DHCP 问题[编辑 | 编辑源代码]

如果在通过 DHCP 获取 IP 地址时遇到问题,尝试在 /etc/dhclient.conf 中添加以下内容:

 interface "eth0" {
   send dhcp-client-identifier 01:aa:bb:cc:dd:ee:ff;
 }

其中 aa:bb:cc:dd:ee:ff 是网卡的 MAC 地址。MAC 地址可以使用 iproute2 包中的 ip link show 接口 命令获得。

未检测到 3G 调制解调器[编辑 | 编辑源代码]

参见 Mobile broadband modem#NetworkManager

在笔记本电脑上关闭 WLAN[编辑 | 编辑源代码]

有时候在使用笔记本上的开关禁用 Wi-Fi 然后重新启用后,NetworkManager 无法工作。这常常是 rfkill 的问题。要检查驱动程序是否已告知 rfkill 无线适配器的状态,请使用:

$ watch -n1 rfkill list all

如果开启适配器后其标识符仍然显示为 blocked,可尝试如下命令手动 unblock(其中 X 是上面输出的标识符编号):

# rfkill event unblock X

静态 IP 地址设置恢复为 DHCP[编辑 | 编辑源代码]

由于未解决的错误,将默认连接更改为静态 IP 地址时,nm-applet 可能无法正确存储配置更改,并将恢复为自动 DHCP。

要解决这个问题,必须在 nm-applet 中编辑默认连接(例如 "Auto eth0")、改变连接名称(例如改成 "my eth0")、取消勾选"对所有用户可用"复选框并根据需要改变静态 IP 地址设置,最后点击应用。这将保存一个具有给定名称的新连接。

接下来,要让默认连接不自动连接。要做到这一点,请以 root 身份运行 nm-connection-editor。在连接编辑器中,编辑默认连接(例如 "Auto eth0"),取消勾选"自动连接"。点击应用,然后关闭连接编辑器。

普通用户无法编辑连接[编辑 | 编辑源代码]

参见 #设置 PolicyKit 权限

忘记隐藏无线网络[编辑 | 编辑源代码]

由于隐藏网络不会显示在无线视图的选择列表中,因此无法使用 GUI 忘记(删除)它们。可以使用以下命令删除:

# rm /etc/NetworkManager/system-connections/SSID

这也适用于任何其他连接。

GNOME 中 VPN 不工作[编辑 | 编辑源代码]

使用 GNOME 时,在 NetworkManager 中设置 OpenConnect 或 vpnc 连接时,有时不会看到弹出对话框,并且 /var/log/errors.log 中会出现以下错误:

localhost NetworkManager[399]: <error> [1361719690.10506] [nm-vpn-connection.c:1405] get_secrets_cb(): Failed to request VPN secrets #3: (6) No agents were available for this request.

这是由于 GNOME NetworkManager Applet 期望对话框脚本位于 /usr/lib/gnome-shell,而 NetworkManager 的包将它们放入 /usr/lib/networkmanager。 作为"临时"修复(这个错误已经存在一段时间了),请创建以下符号链接:

  • 对于 OpenConnect:ln -s /usr/lib/networkmanager/nm-openconnect-auth-dialog /usr/lib/gnome-shell/
  • 对于 VPNC(即 Cisco VPN):ln -s /usr/lib/networkmanager/nm-vpnc-auth-dialog /usr/lib/gnome-shell/

对其他的 NetworkManager VPN 插件可能也需要做类似的事情,不过以上两个最常见。

无法连接到可见的欧洲无线网络[编辑 | 编辑源代码]

WLAN 芯片附带默认的监管区域[损坏的链接:无效的章节]。如果接入点不在这些限制范围内运行,就无法连接到网络。解决这个问题很容易:

  1. 安装 wireless-regdb
  2. 取消注释 /etc/conf.d/wireless-regdom 中正确的国家代码。
  3. 重新启动系统,因为该设置仅在启动时读取。

启动时自动连接到 VPN 不工作[编辑 | 编辑源代码]

当系统(即以 root 用户运行的 NetworkManager)尝试建立一个 VPN 连接时问题就会发生,因为密码存储在某一个特定用户的 GNOME Keyring 中,所以无法获取密码。

一个解决办法是用明文存储 VPN 密码,如#使用 dispatcher 在网络连接建立后连接 vpn 步骤 (2.) 中所描述。

如果使用 nm-applet GUI 中的新的"自动连接 VPN"选项,就不再需要使用步骤 (1.) 所描述的 dispatcher 自动连接。

systemd 瓶颈[编辑 | 编辑源代码]

随着时间的推移,日志文件(/var/log/journal)可能会变得非常庞大。使用 NetworkManager 时,这可对启动性能产生大的影响,参考:systemd#启动的时间太长

使用archinstall脚本创建的系统关机慢(并且关机时kde右下角弹窗报错)[编辑 | 编辑源代码]

使用命令(journalctl -p5 按斜杠输入Killing

输出结果 NetworkManager[38]: <error> [1685410869.7713] platform-linux: sysctl: failed to open

可以尝试使用以下方法(基本可以解决)

sudo systemctl disable NetworkManager.service

reboot

sudo systemctl start NetworkManager.service

sudo systemctl enable NetworkManager.service

有规律的网络断开、延迟和丢包(WiFi)[编辑 | 编辑源代码]

NetworkManager 每 2 分钟扫描一次。

有些 WiFi 驱动在已连接/关联的同时扫描基站会有问题。症状包括 VPN 断开/重连和丢包,以及页面无法加载但之后刷新没有问题。

以 root 身份运行 journalctl -f 可以表明这种情况是否正在发生,类似下面的信息会在日志文件中以有规律的间隔出现:

NetworkManager[410]: <info>  (wlp3s0): roamed from BSSID 00:14:48:11:20:CF (my-wifi-name) to (none) ((none))

如果漫游不重要,可以通过在 WiFi 连接配置文件中锁定接入点的 BSSID 来禁用定期扫描行为。

联想笔记本电脑无法打开WiFi (IdeaPad, Legion, etc.)[编辑 | 编辑源代码]

There is an issue with the ideapad_laptop module on some Lenovo models due to the Wi-Fi driver incorrectly reporting a soft block. The card can still be manipulated with netctl, but managers like NetworkManager break. You can verify that this is the problem by checking the output of rfkill list after toggling your hardware switch and seeing that the soft block persists.

本文或本章节的事实准确性存在争议。

原因: Try to use rfkill.default_state and rfkill.master_switch_mode (see kernel-parameters.html) to fix the rfkill problem.(在 Talk:NetworkManager 中讨论)


Unloading the ideapad_laptop module should fix this. (warning: this may disable the laptop keyboard and touchpad also!).

不把主机名推送到DHCP服务器[编辑 | 编辑源代码]

NetworkManager 默认将主机名发送到 DHCP 服务器,目前界面上可以全局禁用。(Issue #584).

要针对某个连接禁用此配置,将如下内容加入配置文件:

/etc/NetworkManager/system-connections/your_connection_file
...
[ipv4]
dhcp-send-hostname=false
...
[ipv6]
dhcp-send-hostname=false
...

nm-applet disappears in i3wm[编辑 | 编辑源代码]

If you use the xfce4-notifyd.service for notifications you must edit the unit and add the following:

/etc/systemd/user/xfce4-notifyd.service.d/display_env.conf
[Service]
Environment="DISPLAY=:0.0"

After reloading the daemons restart xfce4-notifyd.service. Exit i3 and start it back up again and the applet should show on the tray.

Unit dbus-org.freedesktop.resolve1.service not found[编辑 | 编辑源代码]

If systemd-resolved.service is not started, NetworkManager will try to start it using D-Bus and fail:

dbus-daemon[991]: [system] Activating via systemd: service name='org.freedesktop.resolve1' unit='dbus-org.freedesktop.resolve1.service' requested by ':1.23' (uid=0 pid=1012 comm="/usr/bin/NetworkManager --no-daemon ")
dbus-daemon[991]: [system] Activation via systemd failed for unit 'dbus-org.freedesktop.resolve1.service': Unit dbus-org.freedesktop.resolve1.service not found.
dbus-daemon[991]: [system] Activating via systemd: service name='org.freedesktop.resolve1' unit='dbus-org.freedesktop.resolve1.service' requested by ':1.23' (uid=0 pid=1012 comm="/usr/bin/NetworkManager --no-daemon ")

This is because NetworkManager will try to send DNS information to systemd-resolved regardless of the main.dns= setting in NetworkManager.conf(5).[8]

This can be disabled with a configuration file in /etc/NetworkManager/conf.d/:

/etc/NetworkManager/conf.d/no-systemd-resolved.conf
[main]
systemd-resolved=false

See FS#62138.

Secrets were required, but not provided[编辑 | 编辑源代码]

If you received the following error when attempting to connect to a network:

$ nmcli device wifi connect SSID password password
Error: Connection activation failed: (7) Secrets were required, but not provided

This error can have numerous causes and you should read the journal (filter it with -u NetworkManager). For example, if NetworkManager took too long to establish connection, it will believe that the password is incorrect:

NetworkManager[1372]: <warn>  [1643991888.3808] device (wlan0): Activation: (wifi) association took too long
NetworkManager[1372]: <info>  [1643991888.3809] device (wlan0): state change: config -> need-auth (reason 'none', sys-iface-state: 'managed')
NetworkManager[1372]: <warn>  [1643991888.3838] device (wlan0): Activation: (wifi) asking for new secrets

You can try deleting the connection profile and creating a new one:

$ nmcli connection delete SSID
$ nmcli device wifi connect SSID password password

You can also try disabling MAC address randomization:

/etc/NetworkManager/conf.d/wifi_rand_mac.conf
[device]
wifi.scan-rand-mac-address=no

WPA Enterprise connection with iwd[编辑 | 编辑源代码]

If you try to connect to an WPA Enterprise network like 'eduroam' with NetworkManager with the iwd backend then you will get the following error from NetworkManager:

 Connection 'eduroam' is not avialable on device wlan0 because profile is not compatible with device (802.1x connections must have IWD provisioning files)

This is because NetworkManager can not configure a WPA Enterprise network. Therefore you have to configure it using an iwd configuration file /var/lib/iwd/essid.8021x like described in iwd#WPA Enterprise.

Failed to request VPN secrets[编辑 | 编辑源代码]

If you get this error:

Failed to request VPN secrets #1: No agents were available for this request.

It is either because the password is empty or you have to set up PolicyKit permissions.

OpenVPN connections fail with OpenSSL "ca md too weak" error[编辑 | 编辑源代码]

Since openssl was updated to version 3, certificates generated with legacy cryptographic algorithms are rejected by default. Attempting to use networkmanager-openvpn with such a setup can result in the following error in the logs:

nm-openvpn[14359]: OpenSSL: error:0A00018E:SSL routines::ca md too weak
nm-openvpn[14359]: Cannot load certificate file /home/archie/.local/share/networkmanagement/certificates/my_issued_cert.crt
nm-openvpn[14359]: Exiting due to fatal error

The correct approach is to have the OpenVPN server administrator generate and re-issue more secure certificates. However, as an immediate work-around, OpenVPN requires tls-cipher "DEFAULT:@SECLEVEL=0". This may not be possible through the plugin GUI, but it is possible with nmcli. Separately, you will also need to enable the legacy provider in OpenSSL.

Firstly, obtain the name of the VPN connection with the issue, from the output of the following:

$ nmcli connection show

Assuming the connection name is vpn.example.com, use nmcli like so:

$ nmcli connection modify vpn.example.com +vpn.data tls-cipher=DEFAULT:@SECLEVEL=0

The change should instantly be reflected in /etc/NetworkManager/system-connections/vpn.example.com.nmconnection.

As for OpenSSL, edit /etc/ssl/openssl.cnf as described on the OpenSSL wiki.

Specifically, at the end of the [provider_sect] section add legacy = legacy_sect. Under [default_sect] uncomment activate = 1. Lastly, add a new section [legacy_sect] that also contains the line activate = 1. Excluding most other preexisting configuration sections, the end result will look something like:

openssl_conf = openssl_init
  
[openssl_init]
providers = provider_sect
  
[provider_sect]
default = default_sect
legacy = legacy_sect
  
[default_sect]
activate = 1
  
[legacy_sect]
activate = 1

Finally, restart the NetworkManager.service to have the new OpenSSL configuration take effect.

参见[编辑 | 编辑源代码]