NetworkManager (简体中文)
| Summary |
|---|
| 网络管理器是简化网络管理的工具,本文介绍网络管理器的安装和配置。 |
| 概览 |
Arch Linux provides netctl for network management. netctl supports wired connections on desktops and servers, as well as wireless setups and roaming for mobile users, facilitating easy management of network profiles. NetworkManager and Wicd are popular third-party alternatives.
|
网络管理器(NetworManager)是检测网络、自动连接网络的程序。无论是无线还是有线连接,它都可以令您轻松管理。对于无线网络,网络管理器可以自动切换到最可靠的无线网络。利用网络管理器的程序可以自由切换在线和离线模式。网络管理器可以优先选择有线网络,支持 VPN。网络管理器最初由 Redhat 公司开发,现在由 GNOME 管理。
Contents |
安装
NetworkManager 可以在接在 [extra] 源中进行安装
# pacman -S networkmanager
需要 VNC 支持需要从官方软件仓库安装:
图形前端
为了方便使用网络管理器进行管理和配置,通常需要安装托盘组件。图形前端往往显示在系统托盘(或通知区域),从而允许用户选择网络或者配置 NetworkManager。各种桌面环境的安装方法如下:
Gnome环境
Gnome的network-manager-applet是一个轻量级的全能组件,几乎可以运行在所有的桌面环境下。
如果你想储存验证信息(Wireless/DSL),并提供给所有用户使用,那么您还需要安装和配置GNOME Keyring
KDE4
Plasma-nm 前端可以通过官方软件仓库中的 kdeplasma-applets-plasma-nm 安装。老的 KNetworkManager 前端已经移到了AUR 软件包 kdeplasma-applets-networkmanagement 。
如果同时安装了 KNetworkManager 和 nm-applet,在使用 KDE 时不想使用 nm-applet,将下行加入 /etc/xdg/autostart/nm-applet.desktop
NotShowIn=KDE
详情参阅 Userbase 页面。
XFCE
nm-applet 可以在 XFCE 下正常工作,但是为了可以显示通知信息,包括错误信息, nm-applet 需要一个 Freedesktop 桌面通知扩展(查阅 [1])。xfce4-notifyd 就是这么一个扩展。
# pacman -S network-manager-applet xfce4-notifyd
如果这个扩展没有运行守护进程,nm-applet 就会输出下面的错误到 stdout/stderr:
(nm-applet:24209): libnotify-WARNING **: Failed to connect to proxy ** (nm-applet:24209): WARNING **: get_all_cb: couldn't retrieve system settings properties: (25) Launch helper exited with unknown return code 1. ** (nm-applet:24209): WARNING **: fetch_connections_done: error fetching connections: (25) Launch helper exited with unknown return code 1. ** (nm-applet:24209): WARNING **: Failed to register as an agent: (25) Launch helper exited with unknown return code 1
尽管没有通知系统,nm-applet 仍然会正常工作。
Openbox
GNOME applet 和 xfce4-notifyd 结合可以很好的工作:
# pacman -S network-manager-applet xfce4-notifyd hicolor-icon-theme gnome-icon-theme
如果你想储存验证信息(Wireless/DSL),请安装:
# pacman -S gnome-keyring
要让 Openbox autostart 启动 nm-applet,需要删除文件/etc/xdg/autostart/nm-applet.desktop。每次更新 network-manager-applet 都需要删除这个文件。
在 autostart 中加入:
# (sleep 3 && /usr/bin/nm-applet --sm-disable) &
其它桌面和窗口管理器
推荐使用 GNOME 组件,需要安装 GNOME hicolor 主题:
# pacman -S hicolor-icon-theme gnome-icon-theme
不使用系统托盘,可以使用 trayer 或 stalonetray。例如,在路径中加入 "nmgui" 脚本:
#!/bin/sh nm-applet > /dev/null 2>/dev/null & stalonetray > /dev/null 2>/dev/null killall nm-applet
关闭 stalonetray 窗口时,将会同时关闭 nm-applet,所以完成网络设置后不会使用额外的内存。
命令行
网络管理器 0.8.1 版之后包含 nmcli
配置
NetworkManager 需要做这么几步保证正常运行。
先验证 /etc/hosts 配置正确,如果配置不正确,网络管理器可能修改它。示例:
/etc/hosts
127.0.0.1 localhost ::1 localhost
启用 NetworkManager
NetworkManager 守护进程启动后,会自动连接到已经配置的系统连接。用户连接或未配置的连接需要通过nmcli或桌面工具进行配置和连接。
开机启用 NetworkManager:
# systemctl enable NetworkManager
立即启动 NetworkManager:
# systemctl start NetworkManager
设置 PolicyKit 权限
参照General Troubleshooting#Session permissions建立一个工作会话. 在工作会话中,你有三种方式授予NetworkManager工作所必须的权限.
方式 1. 登录后运行PolicyKit认证代理,比如 /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 (part of polkit-gnome). 当你添加和删除一个网络链接时会提示输入密码.
方式 2. 将你的账户加入wheel账户组. 管理网络时你将不需要输入密码,但注意你的账户同时被赋予了此账户组的其他权限,比如运行sudo命令是无需密码.
方式 3. 将你的账户加入network账户组,同时创建以下文件:
/etc/polkit-1/localauthority/50-local.d/org.freedesktop.NetworkManager.pkla
[nm-applet] Identity=unix-group:network Action=org.freedesktop.NetworkManager.* ResultAny=yes ResultInactive=no ResultActive=yes
所有在network账户群中的用户都能免密码管理网络. 但是如果你没有在systemd-logind中拥有一个活跃会话的话, 在systemd下此方式将失效.
网络分配器
有些服务只有联网时才有意义,例如 OpenNTPD 网络文件系统挂载(netfs)。网络管理器可以在连接网络后启动这些服务,并在网络关闭时停止它们。
要使用这个功能,可以将脚本加到 /etc/NetworkManager/dispatcher.d 目录。这些脚本需要有可执行和用户权限。为了安全,可以设置为属于 root:root 并且只有用户可写。禁用组和其它的写权限,使用 755 mask。否则可能无法执行脚本,在 /var/log/messages.log 报
nm-dispatcher.action: Script could not be executed: writable by group or other, or set-UID
脚本将在连接网络时按字母表顺序运行,并在网络停止时反向停止。要保证启动顺序,可以在前面加数字,例如 10_portmap 或 30_netfs 这样就能保证 portmapper 在 NFS 挂载之前启动。
下面脚本启动 openntpd,保存为文件 /etc/NetworkManager/dispatcher.d/20_openntpd 并加上执行权限。
#!/bin/sh
INTERFACE=$1 # The interface which is brought up or down
STATUS=$2 # The new state of the interface
case "$STATUS" in
'up') # $INTERFACE is up
exec /etc/rc.d/openntpd start
;;
'down') # $INTERFACE is down
# Check for active interface and down if no one active
if [ ! `nm-tool|grep State|cut -f2 -d' '` = "connected" ]; then
exec /etc/rc.d/openntpd stop
fi
;;
esac
启动 openntpd
以下示例演示如何启动OpenNTPD, 将此保存为文件/etc/NetworkManager/dispatcher.d/20_openntpd ,并且它赋予执行权限
#!/bin/sh
INTERFACE=$1 # The interface which is brought up or down
STATUS=$2 # The new state of the interface
case "$STATUS" in
'up') # $INTERFACE is up
exec /etc/rc.d/openntpd start
;;
'down') # $INTERFACE is down
# Check for active interface and down if no one active
if [ ! `nm-tool|grep State|cut -f2 -d' '` = "connected" ]; then
exec /etc/rc.d/openntpd stop
fi
;;
esac
使用sshfs挂载远程文件夹
以下文本需要在特定环境中运行,为了连接上SSH agent,你必须export SSH_AUTH_SOCK. 这里有几种不同方式, 参照here 获取更多详细信息. 以下示例需要gnome-keyring, 如果gnome-keyring没解锁,将需要你输入密码. 如果networkmanager设置为登录后自动连接, 很有可能因为gnome-keyring还没启动导致失败(转入睡眠). 对应的UUID保持在/etc/NetworkManager/system-connections/
#!/bin/bash
USER=<your sshfs user>
if [ $CONNECTION_UUID == <connection UUID> ]; then
case "$2" in
up)
#sleep 10
export SSH_AUTH_SOCK=$(find /tmp/keyring-*/ -type s -user $USER -group users -name ssh)
su $USER -c "/usr/bin/sshfs user@host:/remote/folder /local/folder/"
;;
down)
fusermount -u /local/folder
;;
esac
fi
使用分配器在网络连接建立后连接 vpn
此部分示例演示如果自动连接到NetworkManager已定义的vpn-connection.首先创建调度脚本定义vpn连接之后的事务
1、创建调度脚本
/etc/NetworkManager/dispatcher.d/vpn-up
case "$2" in
up)
sudo -u username DISPLAY=:0 /usr/bin/python /etc/NetworkManager/vpn-up.py
;;
esac
记住,使用chmod +x命令赋予此文本可执行权限,使用正确账户代替username参数.
2、创建文本
/etc/NetworkManager/vpn-up.py 将 network-ESSID 换成你自己的.
现在NetworkManager可以尝试连接到你账户中定义的vpn了。
最新版本的NetworkManager已经改变了上述脚本中的python接口,所以以上脚本可能失效。可选方案是使用nmcli命令:
#! /bin/bash
REQUIRED_CONNECTION_NAME=""
VPN_CONNECTION_NAME=""
activ_con=$(nmcli con status | grep "${REQUIRED_CONNECTION_NAME}")
activ_vpn=$(nmcli con status | grep "${VPN_CONNECTION_NAME}")
if [ "${activ_con}" -a ! "${activ_vpn}" ];
then
nmcli con up id "${VPN_CONNECTION_NAME}"
fi
代理设置
NetworkManager不直接处理代理设置,但是如果你使用GNOME,你可以使用 proxydriver配合NetworkManager。 proxydriver软件包位于 AUR.
为使proxydriver设置代理,你需要在设置GNOME自动启动进程( System->Preferences->Startup Applications):
xhost +si:localuser:your_username
参照: Proxy settings
测试
NetworkManager 托盘组件被设计成开机自动启动,所以对大部分用户来说,并不需要过多配置。 但是如果你手动停用旧有的网络设置断网,你需要测试一下 NetworkManager 是否正常工作。 首先启动守护进程:
/etc/rc.d/networkmanager start
有些托盘组件会提供给你一个 .desktop 文件以便通过系统菜单运行。 如果没有,那你就需要通过命令或者注销重登录系统来让托盘组件运行。 一旦托盘组件运行了,它会自动请求网络连接并通过 DHCP 服务器来进行网络配置。
在一些 non-xdg-compliant 窗口系统,比如 Awesome 中启动 GNOME applet:
nm-applet --sm-disable &
如果需要静态 IP,你需要配置 NetworkManager。一般来说,在托盘图标上面点击右键, 选择「编辑连接」即可。
常见问题
常见问题.
PPTP 通道中无流量
PPTP连接正常,可以正常看到VPN IP,但是不能ping通远端IP,这是由于Arch pppd缺少MPPE (Microsoft Point-to-Point Encryption) 支持. 推荐首先使用ppp。
同时安装 ppp-mppe
网络管理功能失效
有时NetworkManager关闭了,但对应的pid文件却没有移除,同时你得到提示 'Network management disabled'. 你可以手工处理:
# rm /var/lib/NetworkManager/NetworkManager.state
假如在系统重启后,此现象依然,你可以在
/etc/rc.local启动过程中添加:
nmpid=/var/lib/NetworkManager/NetworkManager.state [ -f $nmpid ] && rm $nmpid
使用 resolv.conf.head 和 resolv.conf.tail
请阅读 resolv.conf 并确保 NetworkManager 使用的是 dhcpcd 而不是 dhclient。如果要使用 dhclient,可以试试AUR里面的 networkmanager-dispatch-resolv。
在resolv.conf中保留改动
NetworkManager试图将DHCP中获取的DNS信息写入/etc/resolv.conf,导致原文件被覆盖,你可以在文件属性中设置i参数避免文件被修改
# chattr +i /etc/resolv.conf
如果你要修改此文件,移除i参数:
# chattr -i /etc/resolv.conf
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 eth0 命令
对某些不兼容的路由器,你必须在/etc/dhcpcd.conf (注意此文件有别于dhcpd.conf)文件中注释
require dhcp_server_identifier
这样应该可以工作了,但是如果你的网络中不幸存在多个DHCP服务器的话,你还需要参照 this page 获取更多信息.
主机名 problems
在/etc/NetworkManager/NetworkManager.conf加入:
dhcp=dhcpcd
重启服务.
systemctl restart NetworkManager
source https://bbs.archlinux.org/viewtopic.php?id=152376
缺少默认路由 route
至少在KDE4系统中,当使用NetworkManager Wireless_Setup_(简体中文)链接时不会建立缺省路由. 可以通过在无线链接路由配置中移除"Use only for resources on this connection"部分解决问题
没有探测到 3G 模块
如果NetworkManager(从v0.7.999)没有探测到你的3G模块,但是你仍然可以使用wvdial连接, 可以尝试安装modemmanager,并使用rc.d restart networkmanager重启服务,你可能需要重插或重启你的3G模块, 这可以让NetworkManager支持默认数据库中缺失的硬件模块
在笔记本上切换网络
有时候, NetworkManager在你关闭和开启WIFI适配器后会无法工作,这常常是rfkill的问题,请从official repositories安装rfkill并使用
$ watch -n1 rfkill list all
检测驱动rfkill是否上报无线适配器的状态. 如果你开启适配器后,其标识符仍然显示blocked,你可以尝试如下命令,手动unblock(X是前一条命令的identifier编号)
# rfkill event unblock X
静态 IP 设置 变成 DHCP
这里有个BUG,当你将缺省链接设置成静态IP时,nm-applet 可能不能保存你的IP配置,而自动转变为DHCP模式。
对于这个问题,你不得不在首先在nm-applet改变连接的名称(比如将"Auto eth0"变成“my eth0”),去掉“Available to all users”的勾号。输入你的配置IP地址,然后点击“Apply”,这样就能保存你的配置
如果你不希望默认链接自动连接网络,运行
nm-connection-editor (not as root). 在链接配置窗口,选中默认配置(eg "Auto eth0") 去掉"Connect automatically". 点击 Apply.
普通用户无法编辑链接
See #Set_up_PolicyKit_permissions.
删除隐蔽无线网络链接
因为隐蔽无线网络不出现在无线列表中,所以不能在GUI中删除,你可以试用以下命令:
# rm /etc/NetworkManager/system-connections/[SSID]
此命令对所用所有连接有效 This works for any other connection.
GNOME VPN失效问题
在Gnome系统中用NetworkManager 设立OpenConnect或VPN链接,有时会无法跳出对话框,在/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 NM Applet在/usr/lib/gnome-shell中读取脚本, 而NetworkManager安装包将脚本安装/usr/lib/networkmanager中. 临时解决方法可以文件夹中创建软连接
# For OpenConnect ln -s /usr/lib/networkmanager/nm-openconnect-auth-dialog /usr/lib/gnome-shell/
# For VPNC (i.e. Cisco VPN) ln -s /usr/lib/networkmanager/nm-vpnc-auth-dialog /usr/lib/gnome-shell/
这种方法对其他类型的NM VPN插件也适用,不过上述两种VPN是最平常的。
Tips and tricks
将internet连接设置成WIFI网络共享
你可以适用nm分享你的internet连接(3G或有线),硬件上你需要有WIFI无线网卡(最好基于Atheros AR9xx or at least AR5xx)
Ad-hoc
- pacman -S dnsmasq
- custom dnsmasq.conf may interfere with nm (not sure about this, but i think so)
- Click on nm-applet -> Create new wireless network
- Follow wizard (if using WEP be sure to use 5 or 13 charactes long password, different lengths will fail)
- Settings will remain stored for next time you'll need it
Real AP
Support of infrastructure mode (which is needed by Andoid phones as they don't intentionally support ad-hoc) is not currently supported by NetworkManager, but is in active development...
See: http://fedoraproject.org/wiki/Features/RealHotspot
在cron脚本中检查网络是否OK
某些cron jobs需要在网络OK的状态下工作,你可能希望在网络无法连接时不启动这些cron. 你可以在脚本中使用
NetworkManager's 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
This useful for a cron.hourly script that runs fpupdate for the F-Prot virus scanner signature update, as an example. Another way it might be useful, with a little modification, is to differentiate between networks using various parts of the output from nm-tool; for example, since the active wireless network is denoted with an asterisk, you could grep for the network name and then grep for a literal asterisk.
Automatically unlock keyring after login
GNOME
- Right click on the
nm-appleticon in your panel and select Edit Connections and open the Wireless tab - Select the connection you want to work with and click the Edit button
- Check the boxes “Connect Automatically” and “Available to all users”
Log out and log back in to complete.
- In
/etc/pam.d/gdm(or your corresponding daemon in/etc/pam.d), add these lines at the end of the "auth" and "session" blocks if they do not exist already:
auth optional pam_gnome_keyring.so session optional pam_gnome_keyring.so auto_start
- In
/etc/pam.d/passwd, use this line for the 'password' block:
password optional pam_gnome_keyring.so
- Next time you log in, you should be asked if you want the password to be unlocked automatically on login.
KDE
Put a script like the following in ~/.kde4/Autostart:
#!/bin/sh echo PASSWORD | /usr/bin/pam-keyring-tool --unlock --keyring=default -s
Similar should work with Openbox, LXDE, etc.
SLiM login manager
- In
/etc/pam.d/slim, add these lines at the end of the "auth" and "session" blocks if they do not exist already:
auth optional pam_gnome_keyring.so session optional pam_gnome_keyring.so auto_start
- In
/etc/pam.d/passwd, use this line for the 'password' block:
password optional pam_gnome_keyring.so
- In
~/.xinitrc, add this at the very top, before launching your window manager and other applications:
# test for an existing bus daemon, just to be safe
if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
# if not found, launch a new one
eval `dbus-launch --sh-syntax --exit-with-session`
echo "D-Bus per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
fi
- Next time you log in, you should be asked if you want the password to be unlocked automatically on login.
Ignore specific devices
Sometimes it may be desired that NetworkManager ignores specific devices and does not try to configure addresses and routes for them.
- 1. You can quickly and easily ignore devices by MAC by using the following in
/etc/NetworkManager/NetworkManager.conf:
[keyfile] unmanaged-devices=mac:00:22:68:1c:59:b1;mac:00:1E:65:30:D1:C4
- After you have put this in, restart NetworkManager, and you should be able to configure interfaces without NetworkManager altering what you have set.
- 2. If that is not appropriate, you could ignore by HAL.
- First you have to find out the Hal UDI (e.g. with
lshal):
- First you have to find out the Hal UDI (e.g. with
... info.product = 'Networking Interface' (string) info.subsystem = 'net' (string) info.udi = '/org/freedesktop/Hal/devices/net_00_1f_11_01_06_55' (string) linux.hotplug_type = 2 (0x2) (int) linux.subsystem = 'net' (string) ...
- Add the udi to
/etc/NetworkManager/nm-system-settings.conf:
- Add the udi to
[keyfile] unmanaged-devices=/org/freedesktop/Hal/devices/net_00_1f_11_01_06_55
- Multiple devices can be specified, delimited by semicolons:
[keyfile] unmanaged-devices=/org/freedesktop/Hal/devices/net_00_1f_11_01_06_55;/org/freedesktop/Hal/devices/net_00_2c_6d_e2_08_af
- You do not need to restart NetworkManager for the changes to take effect.
- 3. Devices could also be ignored at boot time by using following script (change
NetworkManager.confwithnm-system-settings.confif using a version of NetworkManager smaller than 0.8.1):
#!/bin/sh
# author: tim noise <darknoise@drkns.net>
COUNT=0
TARGET_FILE="/etc/NetworkManager/NetworkManager.conf"
for i in `lshal | grep -A6 'Networking Interface' | awk -F "'" '/info.udi = / {print $2}'`; do
if [ $COUNT = 0 ]; then
COUNT=$COUNT+1;
echo "unmanaged-devices=$i" >> $TARGET_FILE
else
echo -n ";$i" >> $TARGET_FILE
fi
done
printf "\n" >> $TARGET_FILE
- It can be changed to ignore WiFi devices, etc. being used on a non-persistant filesystem.
Connect faster
Disabling IPv6
Slow connection or reconnection to the network may be due to superfluous IPv6 queries in NetworkManager. If there is no IPv6 support on the local network, connecting to a network may take longer than normal while NetworkManager tries to establish an IPv6 connection that eventually times out. The solution is to disable IPv6 within NetworkManager which will make network connection faster. This has to be done once for every network you connect to.
- Right-click on the network status icon.
- Click on "Edit Connections".
- Go to the "Wired" or "Wireless" tab, as appropriate.
- Select the name of the network.
- Click on "Edit".
- Go to the "IPv6 Settings" tab.
- In the "Method" dropdown, choose "Ignore/Disabled".
- Click on "Save".
Speed up DHCP by disabling ARP probing in DHCPCD
dhcpcd contains an implementation of a recommendation of the DHCP standard (RFC2131 section 2.2) to check via ARP if the assigned IP address is really not taken. This seems mostly useless in home networks, so you can save about 5 seconds on every connect by adding the following line to /etc/dhcpcd.conf:
noarp
This is equivalent to passing --noarp to dhcpcd, and disables the described ARP probing, speeding up connections to networks with DHCP.
开启 OpenDNS 服务
Create /etc/resolv.conf.opendns with the nameservers:
nameserver 208.67.222.222 nameserver 208.67.220.220
And have the dispatcher replace the discovered DHCP servers with the OpenDNS ones:
/etc/NetworkManager/dispatcher.d/dns-servers-opendns
#!/bin/bash # Use OpenDNS servers over DHCP discovered servers cp -f /etc/resolv.conf.opendns /etc/resolv.conf
Make the script executable:
# chmod +x /etc/NetworkManager/dispatcher.d/dns-servers-opendns
其它资源
- Wireless Setup -- 无线配置(wiki)
- NetworkManager - 网络管理器的官方主页