1、nmcli相關介紹
CentOS 5以及CentOS 6版本的Linux發行版,我們都習慣使用直接編輯網絡配置文件或者使用類似於setup的命令對網絡進行配置,但是在CentOS7版本上,出現了一個名為nmcli的工具,使得我們在配置和管理我們的網絡時,變的非常方便。下面我們來了解下nmcli的一些常用功能:
CentOS 7中默認的網絡服務由NetworkManager提供,這是動態控制及配置網絡的守護進程,它用於保持當前網絡設備及連接處於工作狀態,同時也支持傳統的 ifcfg 類型的配置文件。
NetworkManager 可以用於以下類型的連接:Ethernet,VLANS,Bridges,Bonds,Teams,Wi-Fi,mobile boradband(如移動3G)以及 IP-over-InfiniBand。針對與這些網絡類型,NetworkManager 可以配置他們的網絡別名,IP 地址,靜態路由,DNS,VPN連接以及很多其它的特殊參數。
2、nmcli的使用
2.1 查看nmcli的幫助命令:
[root@ip-172-31-22-8~]
#nmclihelp
Usage:nmcli[OPTIONS]OBJECT{COMMAND|help}
OPTIONS
-t[erse]terseoutput
-p[retty]prettyoutput
-m[ode]tabular|multilineoutputmode
-f[ields]<field1,field2,...>|all|commonspecifyfieldstooutput
-e[scape]
yes
|noescapecolumnsseparators
in
values
-n[ocheck]don'tchecknmcliandNetworkManagerversions
-a[sk]ask
for
missingparameters
-w[ait]<seconds>
set
timeoutwaiting
for
finishingoperations
-
v
[ersion]showprogramversion
-h[elp]printthishelp
OBJECT
g[eneral]NetworkManager'sgeneralstatusandoperations
n[etworking]overallnetworkingcontrol
r[adio]NetworkManagerradioswitches
c[onnection]NetworkManager'sconnections
d[evice]devicesmanagedbyNetworkManager
a[gent]NetworkManagersecretagentorpolkitagent
說明:
a、device叫網絡接口,是物理設備
b、connection是連接,偏重於邏輯設置
c、多個connection可以應用到同一個device,但同一時間只能啟用其中一個connection。
d、這樣的好處是針對一個網絡接口,我們可以設置多個網絡連接,比如靜態IP和動態IP,再根據需要up相應的connection。
2.2 CentOS 7下網卡命名規則:
CentOS 7 開始對於網卡的編號則有另一套規則, 網卡的界面代號現在與網卡的來源有關,基本上的網卡名稱會是這樣分類的:
eno1 : 代表由主板 BIOS 內置的網卡
ens1 : 代表由主板 BIOS 內置的 PCI-E 界面的網卡
enp2s0 : 代表 PCI-E 界面的獨立網卡, 可能有多個插孔, 因此會有 s0, s1... 的編號
eth0 : 如果上述的名稱都不適用, 就回到原本的默認網卡編號
CentOS 7 也希望我們不要手動修改配置文件, 直接使用所謂的 nmcli 這個指令來設置網絡參數即可
2.3 查看網絡設備的相關狀態:
[[root@centos7~]
#nmclidevicestatus
Warning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.
DEVICETYPESTATECONNECTION
virbr0bridgeconnectedvirbr0
eno16777736ethernetconnectedeno16777736
loloopbackunmanaged--
virbr0-nictapunmanaged--
[root@centos7~]
#nmclideviceshoweno16777736
Warning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.
GENERAL.DEVICE:eno16777736
GENERAL.TYPE:ethernet
GENERAL.HWADDR:00:0C:29:C5:A0:8D
GENERAL.MTU:1500
GENERAL.STATE:100(connected)
GENERAL.CONNECTION:eno16777736
GENERAL.CON-PATH:
/org/freedesktop/NetworkManager/ActiveConnection/8
WIRED-PROPERTIES.CARRIER:on
IP4.ADDRESS[1]:10.10.10.133
/24
IP4.GATEWAY:10.10.10.2
IP4.DNS[1]:10.10.10.2
IP4.DNS[2]:8.8.8.8
IP4.DOMAIN[1]:localdomain
IP6.ADDRESS[1]:fe80::20c:29ff:fec5:a08d
/64
IP6.GATEWAY:
[root@localhost~]
#nmcliconnectionhelp
Usage:nmcliconnection{COMMAND|help}
COMMAND:={show|up|down|add|modify|edit|delete|reload|load}
show[--active][[--show-secrets][
id
|uuid|path|apath]<ID>]...
up[[
id
|uuid|path]<ID>][ifname<ifname>][ap<BSSID>][
passwd
-
file
<
file
withpasswords>]
down[
id
|uuid|path|apath]<ID>...
addCOMMON_OPTIONSTYPE_SPECIFIC_OPTIONSIP_OPTIONS
modify[--temporary][
id
|uuid|path]<ID>([+|-]<setting>.<property><value>)+
edit[
id
|uuid|path]<ID>
edit[
type
<new_con_type>][con-name<new_con_name>]
delete[
id
|uuid|path]<ID>
reload
load<filename>[<filename>...]
[root@centos7~]
#nmcliconnectionshow
Warning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.
NAMEUUIDTYPEDEVICE
virbr06e28dbac-ef8c-4065-b235-26da2a96e261bridgevirbr0
eno167777369abced2c-1ebf-4a70-9ecc-fc9c9d14d01c802-3-etherneteno16777736
2.4 配置接口的IP地址信息:
#----->在同一網卡上,配置多個IP地址
[root@localhost~]
#nmcliconnectionmodifyeno16777736+ipv4.addresses10.10.10.134/24
#----->如何讓新添加的IP地址生效
nmclicondowneno16777736;nmcliconupeno16777736
#----->我們添加網卡新的ip地址:10.10.10.134/24
[root@localhost~]
#nmcliconnectionmodifyeno16777736ipv4.
ipv4.addressesipv4.dhcp-send-
hostname
ipv4.gatewayipv4.may-failipv4.route-metric
ipv4.dhcp-client-
id
ipv4.dnsipv4.ignore-auto-dnsipv4.methodipv4.routes
ipv4.dhcp-
hostname
ipv4.dns-searchipv4.ignore-auto-routesipv4.never-default
[root@localhost~]
#nmcliconnectionmodifyeno16777736+ipv4.addresses10.10.10.134/24
[root@localhost~]
#ifconfig-a
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500
inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255
inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>
ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)
RXpackets26917bytes33375856(31.8MiB)
RXerrors0dropped0overruns0frame0
TXpackets5265bytes334974(327.1KiB)
TXerrors0dropped0overruns0carrier0collisions0
lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536
inet127.0.0.1netmask255.0.0.0
inet6::1prefixlen128scopeid0x10<host>
looptxqueuelen0(LocalLoopback)
RXpackets524bytes45724(44.6KiB)
RXerrors0dropped0overruns0frame0
TXpackets524bytes45724(44.6KiB)
TXerrors0dropped0overruns0carrier0collisions0
直接配置後,網卡的ip地址不能直接生效
#----->如何讓添加的網卡生效
[root@localhost~]
#nmcliconnectiondowneno16777736;nmcliconnectionupeno16777736
Connection
'eno16777736'
successfullydeactivated(D-Busactivepath:
/org/freedesktop/NetworkManager/ActiveConnection/1
)
成功激活的連接(D-Bus激活路徑:
/org/freedesktop/NetworkManager/ActiveConnection/2
)
#----->發現這裡面仍舊看不到新添加的接口的IP信息
[root@localhost~]
#ifconfig-a
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500
inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255
inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>
ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)
RXpackets57185bytes71800786(68.4MiB)
RXerrors0dropped0overruns0frame0
TXpackets10663bytes676788(660.9KiB)
TXerrors0dropped0overruns0carrier0collisions0
lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536
inet127.0.0.1netmask255.0.0.0
inet6::1prefixlen128scopeid0x10<host>
looptxqueuelen0(LocalLoopback)
RXpackets548bytes48084(46.9KiB)
RXerrors0dropped0overruns0frame0
TXpackets548bytes48084(46.9KiB)
TXerrors0dropped0overruns0carrier0collisions0
#----->查看網卡的配置文件
[root@localhost~]
#cat/etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=
yes
IPV4_FAILURE_FATAL=no
IPV6INIT=
yes
IPV6_AUTOCONF=
yes
IPV6_DEFROUTE=
yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=9abced2c-1ebf-4a70-9ecc-fc9c9d14d01c
DEVICE=eno16777736
ONBOOT=
yes
IPADDR=10.10.10.134
PREFIX=24
PEERDNS=
yes
PEERROUTES=
yes
IPV6_PEERDNS=
yes
IPV6_PEERROUTES=
yes
IPV6_PRIVACY=no
#查看網卡的詳細信息
[root@localhost~]
#nmclideviceshoweno16777736
GENERAL.設備:eno16777736
GENERAL.類型:ethernet
GENERAL.硬盤:00:0C:29:C5:A0:8D
GENERAL.MTU:1500
GENERAL.狀態:100(連接的)
GENERAL.CONNECTION:eno16777736
GENERAL.CON-PATH:
/org/freedesktop/NetworkManager/ActiveConnection/5
WIRED-PROPERTIES.容器:開
IP4.地址[1]:10.10.10.133
/24
IP4.地址[2]:10.10.10.134
/24
IP4.網關:10.10.10.2
IP4.DNS[1]:10.10.10.2
IP4.域[1]:localdomain
IP6.地址[1]:fe80::20c:29ff:fec5:a08d
/64
IP6.網關:
#這裡如果發現是中文,可以使用以下命令進行修改
[root@localhost~]
#localectllist-locales
[root@localhost~]
#localectlset-localeLANG=en_US.utf8
#斷開連接,然後重新連接下
[root@centos7~]
#nmclideviceshoweno16777736
Warning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.
GENERAL.DEVICE:eno16777736
GENERAL.TYPE:ethernet
GENERAL.HWADDR:00:0C:29:C5:A0:8D
GENERAL.MTU:1500
GENERAL.STATE:100(connected)
GENERAL.CONNECTION:eno16777736
GENERAL.CON-PATH:
/org/freedesktop/NetworkManager/ActiveConnection/8
WIRED-PROPERTIES.CARRIER:on
IP4.ADDRESS[1]:10.10.10.133
/24
IP4.GATEWAY:10.10.10.2
IP4.DNS[1]:10.10.10.2
IP4.DNS[2]:8.8.8.8
IP4.DOMAIN[1]:localdomain
IP6.ADDRESS[1]:fe80::20c:29ff:fec5:a08d
/64
IP6.GATEWAY:
#消除我們添加的ip:10.10.10.134/24
[root@localhost~]
#nmcliconnectionmodifyeno16777736-ipv4.addresses10.10.10.134/24
[root@localhost~]
#echo$?
0
[root@localhost~]
#nmcliconnectiondowneno16777736;nmcliconnectionupeno16777736
Connection
'eno16777736'
successfullydeactivated(D-Busactivepath:
/org/freedesktop/NetworkManager/ActiveConnection/3
)
成功激活的連接(D-Bus激活路徑:
/org/freedesktop/NetworkManager/ActiveConnection/4
)
[root@localhost~]
#ifconfig-a
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500
inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255
inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>
ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)
RXpackets77939bytes96881263(92.3MiB)
RXerrors0dropped0overruns0frame0
TXpackets14880bytes957913(935.4KiB)
TXerrors0dropped0overruns0carrier0collisions0
lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536
inet127.0.0.1netmask255.0.0.0
inet6::1prefixlen128scopeid0x10<host>
looptxqueuelen0(LocalLoopback)
RXpackets560bytes49264(48.1KiB)
RXerrors0dropped0overruns0frame0
TXpackets560bytes49264(48.1KiB)
TXerrors0dropped0overruns0carrier0collisions0
[root@localhost~]
#cat/etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=
yes
IPV4_FAILURE_FATAL=no
IPV6INIT=
yes
IPV6_AUTOCONF=
yes
IPV6_DEFROUTE=
yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=9abced2c-1ebf-4a70-9ecc-fc9c9d14d01c
DEVICE=eno16777736
ONBOOT=
yes
PEERDNS=
yes
PEERROUTES=
yes
IPV6_PEERDNS=
yes
IPV6_PEERROUTES=
yes
IPV6_PRIVACY=no
[root@localhost~]
#nmclideviceshoweno16777736
GENERAL.設備:eno16777736
GENERAL.類型:ethernet
GENERAL.硬盤:00:0C:29:C5:A0:8D
GENERAL.MTU:1500
GENERAL.狀態:100(連接的)
GENERAL.CONNECTION:eno16777736
GENERAL.CON-PATH:
/org/freedesktop/NetworkManager/ActiveConnection/6
WIRED-PROPERTIES.容器:開
IP4.地址[1]:10.10.10.133
/24
IP4.網關:10.10.10.2
IP4.DNS[1]:10.10.10.2
IP4.域[1]:localdomain
IP6.地址[1]:fe80::20c:29ff:fec5:a08d
/64
IP6.網關:
[root@localhost~]
#ifconfig-a
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500
inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255
inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>
ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)
RXpackets537996bytes673538844(642.3MiB)
RXerrors0dropped0overruns0frame0
TXpackets97698bytes6097864(5.8MiB)
TXerrors0dropped0overruns0carrier0collisions0
lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536
inet127.0.0.1netmask255.0.0.0
inet6::1prefixlen128scopeid0x10<host>
looptxqueuelen0(LocalLoopback)
RXpackets616bytes54280(53.0KiB)
RXerrors0dropped0overruns0frame0
TXpackets616bytes54280(53.0KiB)
TXerrors0dropped0overruns0carrier0collisions0
#----->大家可以測試下,重啟網絡服務對通過nmcli命令添加和減少的網卡信息是否有效,我這邊測試是沒有任何效果。
# 手動設置網關(如果網關沒有進行配置):
[root@localhost~]
#nmcliconnectionmodifyeno16777736ipv4.gateway10.10.10.2
#手動設置DNS(如果DNS沒有進行配置):
[root@localhost~]
#nmcliconnectionmodifyeno16777736+ipv4.dns8.8.8.8
[root@localhost~]
#echo$?
0
[root@localhost~]
#nmclidevice
connectdeletedisconnecthelpshowstatuswifiwimax
[root@localhost~]
#nmcliconnectiondowneno16777736;nmcliconnectionupeno16777736
Connection
'eno16777736'
successfullydeactivated(D-Busactivepath:
/org/freedesktop/NetworkManager/ActiveConnection/6
)
成功激活的連接(D-Bus激活路徑:
/org/freedesktop/NetworkManager/ActiveConnection/7
)
[root@localhost~]
#nmclideviceshoweno16777736
GENERAL.設備:eno16777736
GENERAL.類型:ethernet
GENERAL.硬盤:00:0C:29:C5:A0:8D
GENERAL.MTU:1500
GENERAL.狀態:100(連接的)
GENERAL.CONNECTION:eno16777736
GENERAL.CON-PATH:
/org/freedesktop/NetworkManager/ActiveConnection/7
WIRED-PROPERTIES.容器:開
IP4.地址[1]:10.10.10.133
/24
IP4.網關:10.10.10.2
IP4.DNS[1]:10.10.10.2
IP4.DNS[2]:8.8.8.8
IP4.域[1]:localdomain
IP6.地址[1]:fe80::20c:29ff:fec5:a08d
/64
IP6.網關:
CentOS7下面有類似於CentOS6下面的setup工具的命令,nmtui命令
功能非常強大:可以管理IP的相關信息,添加子接口,橋接等。
http://xxxxxx/Linuxjc/1141685.html TechArticle