簡介
??NetworkManager由一個(gè)管理系統(tǒng)網(wǎng)絡(luò)連接、并且將其狀態(tài)通過D-BUS(是一個(gè)提供簡單的應(yīng)用程序互相通訊的途徑的自由軟件項(xiàng)目,它是做為freedesktoporg項(xiàng)目的一部分來開發(fā)的。)進(jìn)行報(bào)告的后臺服務(wù),以及一個(gè)允許用戶管理網(wǎng)絡(luò)連接的客戶端程序。在NetworkManager中有一個(gè)很好用的網(wǎng)絡(luò)配置工具就是nmcli。
nmcli命令集
nmcli語法
nmcli [ OPTIONS ] OBJECT { COMMAND | help }
nmcli 常用選項(xiàng)
connection #配置網(wǎng)絡(luò)連接
device #管理網(wǎng)卡
networking #管理網(wǎng)絡(luò)狀態(tài)
實(shí)例
connection 配置網(wǎng)絡(luò)連接
查看所有連接
[root@CentOS7.3 ~]#nmcli connection show
NAME UUID TYPE DEVICE
eth0 702eabdc-6623-4d51-b864-f6bfdb521b55 802-3-ethernet eth0
virbr0 40217849-abce-47d2-b2c6-dc5b3f8beadb bridge virbr0
eth1 9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04 802-3-ethernet --
僅查看活躍連接
[root@CentOS7.3 ~]#nmcli connection show --active
NAME UUID TYPE DEVICE
eth0 702eabdc-6623-4d51-b864-f6bfdb521b55 802-3-ethernet eth0
virbr0 40217849-abce-47d2-b2c6-dc5b3f8beadb bridge virbr0
創(chuàng)建新連接default,IP地址通過DHCP自動獲取
[root@CentOS7.3 ~]#nmcli connection add con-name default type ethernet ifname eth1
Connection 'default' (db8ef10f-c848-461a-9098-09501f20d32b) successfully added.
參數(shù)
con-name #指定連接名
type #指定連接類型
ifname #指定連接到那塊網(wǎng)卡
刪除連接
[root@CentOS7.3 ~]#nmcli connection delete default
Connection 'default' (db8ef10f-c848-461a-9098-09501f20d32b) successfully deleted.
創(chuàng)建新鏈接static,并指定靜態(tài)IP地址。
[root@CentOS7.3 ~]#nmcli connection add con-name static ifname eth1 autoconnect no type ethernet ipv4.method manual ipv4.addresses 192.168.0.8/24 gw4 192.168.0.1
Connection 'static' (e42075e8-4261-4890-b499-818b96d7411f) successfully added.
[root@CentOS7.3 ~]#nmcli connection show
NAME UUID TYPE DEVICE
eth0 702eabdc-6623-4d51-b864-f6bfdb521b55 802-3-ethernet eth0
eth1 9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04 802-3-ethernet eth1
virbr0 40217849-abce-47d2-b2c6-dc5b3f8beadb bridge virbr0
static e42075e8-4261-4890-b499-818b96d7411f 802-3-ethernet --
參數(shù)
autoconnect #是否自動鏈接到網(wǎng)卡,參數(shù)(yes/no)。
ipv4.method #設(shè)置IP地址是自動獲取還是靜態(tài)指定。
ipv4.addresses #指定靜態(tài)IPV4地址。
gw4 #指定靜態(tài)IPV4網(wǎng)關(guān)地址。
啟動static連接
[root@CentOS7.3 ~]#nmcli connection up static
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)
[root@CentOS7.3 ~]#nmcli connection show #再次查看連接
NAME UUID TYPE DEVICE
eth0 702eabdc-6623-4d51-b864-f6bfdb521b55 802-3-ethernet eth0
static e42075e8-4261-4890-b499-818b96d7411f 802-3-ethernet eth1 #static連接啟動成功
virbr0 40217849-abce-47d2-b2c6-dc5b3f8beadb bridge virbr0
eth1 9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04 802-3-ethernet --
修改連接配置
[root@CentOS7.3 ~]#nmcli connection modify "static" ipv4.dns 8.8.8.8 #為static連接添加一個(gè)dns地址。
[root@CentOS7.3 ~]#nmcli connection modify "static" ipv4.addresses 192.168.0.9 #修改static連接的IP地址。
[root@CentOS7.3 ~]#nmcli con down static #關(guān)閉再啟動連接配置生效。
[root@CentOS7.3 ~]#nmcli con up static
常用參數(shù)和網(wǎng)卡配置文件參數(shù)的對應(yīng)關(guān)系
| nmcli con add/mod | 網(wǎng)卡配置文件 |
|---|---|
| ipv4.method manual | BOOTPROTO=none |
| ipv4.method auto | BOOTPROTO=dhcp |
| ipv4.addresses | IPADDR、PREFIX、GATEWAY |
| ipve.dns | DNS |
| ipv4.dns-search example.com | DOMAIN=example.com |
| ipv4.ignore-auto-dns true | PEERDNS=no |
| connection.autoconnect yes | ONBOOT=yes |
| connection.id eth0 | NAME=eth0 |
| connection.interface-name eth0 | DEVICE=eth0 |
| 802-3-ethernet.mac-address . . . | HWADDR= . . . |
| ipv4.ignore-auto-dns yes | 手動設(shè)置dns |
device 管理網(wǎng)卡
查看網(wǎng)卡信息
[root@CentOS7.3 ~]#nmcli device #查看所有網(wǎng)卡的狀態(tài)
DEVICE TYPE STATE CONNECTION
virbr0 bridge connected virbr0
eth0 ethernet connected eth0
eth1 ethernet connected static
eth2 ethernet connected Wired connection 1
lo loopback unmanaged --
virbr0-nic tun unmanaged --
查看網(wǎng)卡詳細(xì)信息
[root@CentOS7.3 ~]#nmcli device show #查看所有網(wǎng)卡的詳細(xì)信息
#后面跟上網(wǎng)卡名可單獨(dú)查看某塊網(wǎng)卡的詳細(xì)信息
激活或關(guān)閉網(wǎng)卡
[root@CentOS7.3 ~]#nmcli device connect eth1 #激活eth1網(wǎng)卡,并自動連接到配置。
[root@CentOS7.3 ~]#nmcli device disconnect eth1 #斷開eth1網(wǎng)卡
修改網(wǎng)卡屬性,修改的配置立即生效,并不修改配置文件,重啟網(wǎng)卡失效。
[root@CentOS7.3 ~]#nmcli device modify eth1
刪除網(wǎng)卡,無法刪除物理網(wǎng)卡
[root@CentOS7.3 ~]#nmcli device delete virbr0
networking 管理網(wǎng)絡(luò)狀態(tài)
[root@CentOS7.3 ~]#nmcli networking #off關(guān)閉網(wǎng)絡(luò),on開啟網(wǎng)絡(luò)
connectivity help off on
[root@CentOS7.3 ~]#nmcli networking connectivity #查看網(wǎng)絡(luò)狀態(tài)
full #full啟動,none關(guān)閉
使用nmcli實(shí)現(xiàn)bonding
添加bonding接口
[root@CentOS7.3 ~]#nmcli connection add con-name mybond0 type bond ifname mybond0 mode active-backup
#mode 指定bonding接口的工作模式,active-backup表示主備模式。
為bonding0接口添加從屬接口
[root@CentOS7.3 ~]#nmcli con add type bond-slave ifname eth1 master mybond0
[root@CentOS7.3 ~]#nmcli con add type bond-slave ifname eth2 master mybond0
啟動bonding接口
[root@CentOS7.3 ~]#nmcli connection up bond-slave-eth1 #啟動bonding網(wǎng)卡需要先啟動從屬網(wǎng)卡。
[root@CentOS7.3 ~]#nmcli connection up bond-slave-eth2
[root@CentOS7.3 ~]#nmcli con up mybond0 #啟動mybond0網(wǎng)卡
[root@CentOS7.3 ~]#nmcli connection #查看連接信息
NAME UUID TYPE DEVICE
bond-slave-eth1 71c9dad8-7120-4b30-af01-be53337a359e 802-3-ethernet eth1
bond-slave-eth2 9103d9ea-fa2b-4df4-a5f4-3d615c53a94a 802-3-ethernet eth2
eth0 702eabdc-6623-4d51-b864-f6bfdb521b55 802-3-ethernet eth0
mybond0 ac079cc4-6a74-45ed-ab82-9c58a1415984 bond mybond0
virbr0 90d0d99c-4abd-4328-be7d-8d67812a3f68 bridge virbr0
Wired connection 1 2d3af334-7e54-311f-9abd-d05fc0143e43 802-3-ethernet --
eth1 9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04 802-3-ethernet --
static e42075e8-4261-4890-b499-818b96d7411f 802-3-ethernet --
bonding的7種工作模式,順序(0-6)
balance-rr #負(fù)載均衡模式,常用模式
active-backup #主備模式
balance-xor #XOR Hash負(fù)載分擔(dān),和交換機(jī)的聚合強(qiáng)制不協(xié)商方式配合。
broadcast #從所有物理接口發(fā)送數(shù)據(jù)包。
802.3ad #表示支持802.3ad協(xié)議,和交換機(jī)的聚合LACP方式配合。
balance-tlb #是根據(jù)每個(gè)slave的負(fù)載情況選擇slave進(jìn)行發(fā)送,接收時(shí)使用當(dāng)前輪到的slave。
balance-alb #在balance-tlb的基礎(chǔ)上增加了rlb。