Use NetworkManager to configure network settings

From FriendlyELEC WiKi
Revision as of 07:18, 19 June 2017 by Yftan (Talk | contribs) (哪些系统预装了NetworkManager)

Jump to: navigation, search

查看中文

1 Introduction to NetworkManager

The NetworkManager is a utility to check network status and manage network connectivity. It can be used to manage both wireless and wired network.
The NetworkManager has a commandline utility nmcli, a commandline based GUI nmtui, and an X-Windows based GUI nm-connection-editor.

2 Images with NetworkManager

All the image files of Linux variants(Debian and Ubuntu) developed by FriendlyElec have this utility. This utility works with the following FriendlyElec boards:

2.1 Board Model

S5P4418 Based Boards:
NanoPi2 / NanoPC-T2 / NanoPi-S2 / Smart4418 / NanoPi2-Fire / NanoPi-M2 / NanoPi-M2A
S5P6818 Based Boards:
NanoPC-T3 / NanoPC-T3T / NanoPi-M3
Allwinner SoC Based Boards:
NanoPi-M1 /NanoPi-NEO /NanoPi-NEO-Air / NanoPi-M1-Plus / NanoPi-NEO2 / NanoPi-M1-Plus2 / NanoPi-NEO-Plus2 / NanoPi-NEO-Core / NanoPi-Duo

2.2 Applicable OS

Debian 8.x jessie (for S5P4418 and S5P6818 only)
Ubuntu core xenial 16.04.x (for all FriendlyElec's nano boards)
These OS images include those based on armhf and aarch64.

3 使用NetworkManager连接WiFi

3.1 命令行操作(nmcli)

要连接WiFi,相关的命令如下:

  • 查看网络设备列表
$ sudo nmcli dev

注意,如果列出的设备状态是 unmanaged 的,说明网络设备不受NetworkManager管理,你需要清空 /etc/network/interfaces下的网络设置,然后重启.

  • 开启WiFi
$ sudo nmcli r wifi on
  • 扫描附近的 WiFi 热点
$ sudo nmcli dev wifi
  • 连接到指定的 WiFi 热点
$ sudo nmcli dev wifi connect "SSID" password "PASSWORD"

请将 SSID和 PASSWORD 替换成实际的 WiFi名称和密码。
连接成功后,下次开机,WiFi 也会自动连接。

  • 多个WiFi硬件时,可以用 ifname参数指定要操作的WiFi设备,例如wlan1:
sudo nmcli device wifi connect "ssid" password "password" ifname wlan1

3.2 命令行下的图形化操作(nmtui)

nmtui 是一个基于curses的图形化前端界面,输入以下命令即可进入:

$ sudo nmtui

使用信息参见man nmtui。

3.3 图形界面操作

在Debian的桌面环境下,有两个入口可以管理网络:

一是点击桌面任务栏右下角的网络图标,会弹出 NetworkManger 的菜单,列出当前的网络连接状态,如果有WiFi网络,会列出周边的无线热点,如下图所示:

NetworkManagerIcon

你可以点击菜单上的无线热点,即可连接到该热点。

二是通过系统菜单 Preferences -> Network Connections进入:

NetworkManangerMenu

4 配置静态IP地址

4.1 方法1:不使用NetworkManager管理,手动设置

以下文字介绍不使用 NetworkManager 来管理网络,改为手动设置,设置静态IP更灵活:

sudo vi /etc/NetworkManager/NetworkManager.conf

编辑 NetworkManger.conf,将ifupdown中的managed设置为false,如下所示:

[ifupdown]
managed=false

再将网络设置加入 /etc/network/interfaces 即可:

$ sudo vi /etc/network/interfaces

/etc/network/interfaces文件内容如下:

# The loopback network interface
auto lo
iface lo inet loopback
 
# network interface not managed by Network Manager
allow-hotplug eth0
iface eth0 inet static
address 192.168.2.199
netmask 255.255.255.0
gateway 192.168.2.1
dns-nameservers 192.168.2.1

4.2 方法2:使用NetworkManager设置

下面我们将以有线网络为例,介绍如何配置静态IP地址。
先通过命令看一下当前的连接信息:

pi@Smart4418SDK:~$ sudo nmcli connection show
NAME                UUID                                  TYPE            DEVICE 
Wired connection 1  cffa785e-0425-3777-b670-092c79328a1f  802-3-ethernet  eth0

上面的输出信息中,可以看到有个名称为Wired connection 1的有线连接,接下来用以下命令更改Wired connection 1为静态IP地址:

sudo nmcli connection modify 'Wired connection 1' connection.autoconnect yes ipv4.method manual ipv4.address 192.168.2.199/24 ipv4.gateway 192.168.2.1 ipv4.dns 192.168.2.1
sudo reboot

上例中,更新有线网络为静态IP, 地址为192.168.2.199,网关和DNS为192.168.2.1。

5 NetworkManager的卸载

NetworkManager虽然方便,但如果你还是觉得传统的 wpa_supplicant 网络管理方式更顺手,你可以通过以下方式卸载NetworkManager:

创建一个脚本文件例如 rmnm.sh:

$vi rmnm.sh

脚本内容如下:

#!/bin/bash
 
apt-get --yes --force-yes remove --auto-remove --purge network-manager
apt autoremove -y
 
cat >/etc/network/interfaces <<INTERFACEEOL
source-directory /etc/network/interfaces.d
 
auto lo
iface lo inet loopback
 
allow-hotplug wlan0
iface wlan0 inet dhcp
        wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
INTERFACEEOL
 
cat >/etc/network/interfaces.d/eth0 <<ETH0EOLB
auto eth0
    iface eth0 inet dhcp
    hwaddress 76:92:d4:85:f3:0f
ETH0EOLB

然后执行脚本:

$chmod 755 rmnm.sh
$ sudo ./rmnm.sh

完成后重启即可。

6 其他参考资料

更详细的NetworkManager使用指南可参考这篇文章: NetworkManager