Centos7 开始,网络由 NetworkManager 服务负责管理,相对于旧的 /etc/init.d/network 脚本,NetworkManager是动态的、事件驱动的网络管理服务。旧的 /etc/init.d/network 以及 ifup,ifdown 等依然存在,但是处于备用状态,即NetworkManager运行时,多数情况下这些脚本会调用NetworkManager去完成网络配置任务;NetworkManager没有运行时,这些脚本就按照老传统管理网络。NetworkManager 可用于以下连接类型:以太网、VLAN、网桥、绑定、成组、Wi-Fi、移动宽带(比如移/动网络 3G)及 IP-over-InfiniBand。在这些连接类型中,NetworkManager 可配置网络别名、IP 地址、静态路由器、DNS 信息及 VPN 连接以及很多具体连接参数。最后,NetworkManager 通过 D-bus 提供 API,D-Bus 允许应用程序查询并控制网络配置及状态。
1、网络配置文件:
/etc/sysconfig/network 说是全局设置,默认里面啥也没有,可以添加全局默认网关
/etc/hostname 用nmtui修改hostname后,主机名保存在这里
/etc/resolv.conf 保存DNS设置,不需要手工改,nmtui里面设置的DNS会出现在这里
/etc/sysconfig/network-scripts/ 连接配置信息 ifcfg 文件
/etc/NetworkManager/system-connections/ VPN、移动宽带、PPPoE连接
2、 联网工具及应用程序概述
应用程序或工具 |
描述 |
NetworkManager |
默认联网守护进程 |
nmtui |
NetworkManager 的使用光标的简单文本用户界面(TUI) |
nmcli |
允许用户及脚本与 NetworkManager 互动的命令行工具 |
gnome-control-center |
GNOME Shell 提供的图形用户界面工具 |
nm-connection-editor |
这是一个 GTK+ 3 应用程序,可用于尚未由 gnome-control-center 处理的某些任务 |
# systemctl status network.service -l
● network.service - LSB: Bring up/down networking
Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
Active: active (exited) since Mon 2017-08-21 16:07:00 CST; 14min ago
Docs: man:systemd-sysv-generator(8)
Aug 21 16:06:59 lux-cobbler.gw.local systemd[1]: Starting LSB: Bring up/down networking...
Aug 21 16:07:00 lux-cobbler.gw.local network[7734]: Bringing up loopback interface: [ OK ]
Aug 21 16:07:00 lux-cobbler.gw.local network[7734]: Bringing up interface ens33: Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
Aug 21 16:07:00 lux-cobbler.gw.local network[7734]: [ OK ]
Aug 21 16:07:00 lux-cobbler.gw.local systemd[1]: Started LSB: Bring up/down networking.
$ systemctl status NetworkManager.service -l
● NetworkManager.service - Network Manager
Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2017-08-21 13:08:36 CST; 2h 54min ago
Docs: man:NetworkManager(8)
Main PID: 792 (NetworkManager)
CGroup: /system.slice/NetworkManager.service
└─792 /usr/sbin/NetworkManager --no-daemon
Aug 21 15:52:07 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301927.9418] device (ens33): state change: prepare -> config (reason 'none') [40 50 0]
Aug 21 15:52:07 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301927.9439] device (ens33): state change: config -> ip-config (reason 'none') [50 70 0]
Aug 21 15:52:08 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301928.0081] device (ens33): state change: ip-config -> ip-check (reason 'none') [70 80 0]
Aug 21 15:52:08 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301928.0295] device (ens33): state change: ip-check -> secondaries (reason 'none') [80 90 0]
Aug 21 15:52:08 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301928.0302] device (ens33): state change: secondaries -> activated (reason 'none') [90 100 0]
Aug 21 15:52:08 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301928.0304] manager: NetworkManager state is now CONNECTED_LOCAL
Aug 21 15:52:08 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301928.0434] manager: NetworkManager state is now CONNECTED_GLOBAL
Aug 21 15:52:08 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301928.0436] policy: set 'ens33' (ens33) as default for IPv4 routing and DNS
Aug 21 15:52:08 lux-cobbler.gw.local NetworkManager[792]: <info> [1503301928.0487] device (ens33): Activation: successful, device activated.
Aug 21 16:01:06 lux-cobbler.gw.local NetworkManager[792]: <info> [1503302466.7982] audit: op="connection-update" uuid="a0606da4-d759-4d55-9045-ce43cd812584" name="ens33" args="connection.timestamp,802-3-ethernet.duplex,ipv6.ip6-privacy,ipv6.method" pid=6676 uid=1000 result="success"
nmtui配置基本网络连接
nmtui 属于curses-based text user interface(文本用户界面), 类似 Centos6 的 setup 工具,但只能编辑连接、启用/禁用连接、更改主机名。系统初装之后可以第一时间用nmtui配置网络设置。
# nmcli --help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
OPTIONS
-t[erse] terse output
-p[retty] pretty output
-m[ode] tabular|multiline output mode
-c[olors] auto|yes|no whether to use colors in output
-f[ields] <field1,field2,...>|all|common specify fields to output
-e[scape] yes|no escape columns separators in values
-a[sk] ask for missing parameters
-s[how-secrets] allow displaying passwords
-w[ait] <seconds> set timeout waiting for finishing operations
-v[ersion] show program version
-h[elp] print this help
OBJECT
g[eneral] NetworkManager's general status and operations
n[etworking] overall networking control
r[adio] NetworkManager radio switches
c[onnection] NetworkManager's connections
d[evice] devices managed by NetworkManager
a[gent] NetworkManager secret agent or polkit agent
m[onitor] monitor NetworkManager changes
OBJECT和COMMAND可以用全称也可以用简称,最少可以只用一个字母,建议用头三个字母。OBJECT里面我们平时用的最多的就是connection和device,这里需要简单区分一下connection和device。
device叫网络接口,是物理设备
connection是连接,偏重于逻辑设置
多个connection可以应用到同一个device,但同一时间只能启用其中一个connection。这样的好处是针对一个网络接口,我们可以设置多个网络连接,比如静态IP和动态IP,再根据需要up相应的connection。
nmcli con show
nmcli con up eth0
nmcli dev show
$ nmcli general
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
connected full enabled enabled enabled enabled
$ nmcli device
DEVICE TYPE STATE CONNECTION
ens33 ethernet connected ens33
lo loopback unmanaged --
$ nmcli connection
NAME UUID TYPE DEVICE
ens33 a0606da4-d759-4d55-9045-ce43cd812584 802-3-ethernet ens33
用nmtui编辑一个连接后,网卡连接配置文件ifcfg-ifname也会相应地改动。直接手工修改ifcfg-ifname,nmtui中用你看到。但不论是用nmtui编辑还是手工修改ifcfg文件,想让新的配置生效,我们需要让 NetworkManager 重新读取该ifcfg配置文件,并重新启用该连接。方法是作为 root 运行以下命令:
# nmcli connection reload
或者只重新载入那些有变化的文件 ifcfg-ifname
# nmcli con load /etc/sysconfig/network-scripts/ifcfg-ifname
# nmcli dev con connection-name