Centos7 開始,網路由 NetworkManager 服務負責管理,相對於舊的 /etc/init.d/network 腳本,NetworkManager是動態的、事件驅動的網路管理服務。舊的 /etc/init.d/network 以及 ifup,ifdown 等依然存在,但是處於備用狀態, ...
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