歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

CentOS7下的網絡最新工具nmcli

CentOS7下的網絡最新工具nmcli


CentOS7下的網絡最新工具nmcli


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~]#nmclihelpUsage: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|noescapecolumnsseparatorsinvalues-n[ocheck]don'tchecknmcliandNetworkManagerversions-a[sk]askformissingparameters-w[ait]<seconds>settimeoutwaitingforfinishingoperations-v[ersion]showprogramversion-h[elp]printthishelpOBJECTg[eneral]NetworkManager'sgeneralstatusandoperationsn[etworking]overallnetworkingcontrolr[adio]NetworkManagerradioswitchesc[onnection]NetworkManager'sconnectionsd[evice]devicesmanagedbyNetworkManagera[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~]#nmclidevicestatusWarning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.DEVICETYPESTATECONNECTIONvirbr0bridgeconnectedvirbr0eno16777736ethernetconnectedeno16777736loloopbackunmanaged--virbr0-nictapunmanaged--[root@centos7~]#nmclideviceshoweno16777736Warning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.GENERAL.DEVICE:eno16777736GENERAL.TYPE:ethernetGENERAL.HWADDR:00:0C:29:C5:A0:8DGENERAL.MTU:1500GENERAL.STATE:100(connected)GENERAL.CONNECTION:eno16777736GENERAL.CON-PATH:/org/freedesktop/NetworkManager/ActiveConnection/8WIRED-PROPERTIES.CARRIER:onIP4.ADDRESS[1]:10.10.10.133/24IP4.GATEWAY:10.10.10.2IP4.DNS[1]:10.10.10.2IP4.DNS[2]:8.8.8.8IP4.DOMAIN[1]:localdomainIP6.ADDRESS[1]:fe80::20c:29ff:fec5:a08d/64IP6.GATEWAY:[root@localhost~]#nmcliconnectionhelpUsage: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<filewithpasswords>]down[id|uuid|path|apath]<ID>...addCOMMON_OPTIONSTYPE_SPECIFIC_OPTIONSIP_OPTIONSmodify[--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>reloadload<filename>[<filename>...][root@centos7~]#nmcliconnectionshowWarning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.NAMEUUIDTYPEDEVICEvirbr06e28dbac-ef8c-4065-b235-26da2a96e261bridgevirbr0eno167777369abced2c-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-hostnameipv4.gatewayipv4.may-failipv4.route-metricipv4.dhcp-client-idipv4.dnsipv4.ignore-auto-dnsipv4.methodipv4.routesipv4.dhcp-hostnameipv4.dns-searchipv4.ignore-auto-routesipv4.never-default[root@localhost~]#nmcliconnectionmodifyeno16777736+ipv4.addresses10.10.10.134/24[root@localhost~]#ifconfig-aeno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)RXpackets26917bytes33375856(31.8MiB)RXerrors0dropped0overruns0frame0TXpackets5265bytes334974(327.1KiB)TXerrors0dropped0overruns0carrier0collisions0lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536inet127.0.0.1netmask255.0.0.0inet6::1prefixlen128scopeid0x10<host>looptxqueuelen0(LocalLoopback)RXpackets524bytes45724(44.6KiB)RXerrors0dropped0overruns0frame0TXpackets524bytes45724(44.6KiB)TXerrors0dropped0overruns0carrier0collisions0直接配置後,網卡的ip地址不能直接生效#----->如何讓添加的網卡生效[root@localhost~]#nmcliconnectiondowneno16777736;nmcliconnectionupeno16777736Connection'eno16777736'successfullydeactivated(D-Busactivepath:/org/freedesktop/NetworkManager/ActiveConnection/1)成功激活的連接(D-Bus激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/2#----->發現這裡面仍舊看不到新添加的接口的IP信息[root@localhost~]#ifconfig-aeno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)RXpackets57185bytes71800786(68.4MiB)RXerrors0dropped0overruns0frame0TXpackets10663bytes676788(660.9KiB)TXerrors0dropped0overruns0carrier0collisions0lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536inet127.0.0.1netmask255.0.0.0inet6::1prefixlen128scopeid0x10<host>looptxqueuelen0(LocalLoopback)RXpackets548bytes48084(46.9KiB)RXerrors0dropped0overruns0frame0TXpackets548bytes48084(46.9KiB)TXerrors0dropped0overruns0carrier0collisions0#----->查看網卡的配置文件[root@localhost~]#cat/etc/sysconfig/network-scripts/ifcfg-eno16777736TYPE=EthernetBOOTPROTO=dhcpDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noNAME=eno16777736UUID=9abced2c-1ebf-4a70-9ecc-fc9c9d14d01cDEVICE=eno16777736ONBOOT=yesIPADDR=10.10.10.134PREFIX=24PEERDNS=yesPEERROUTES=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_PRIVACY=no

#查看網卡的詳細信息

[root@localhost~]#nmclideviceshoweno16777736GENERAL.設備:eno16777736GENERAL.類型:ethernetGENERAL.硬盤:00:0C:29:C5:A0:8DGENERAL.MTU:1500GENERAL.狀態:100(連接的)GENERAL.CONNECTION:eno16777736GENERAL.CON-PATH:/org/freedesktop/NetworkManager/ActiveConnection/5WIRED-PROPERTIES.容器:開IP4.地址[1]:10.10.10.133/24IP4.地址[2]:10.10.10.134/24IP4.網關:10.10.10.2IP4.DNS[1]:10.10.10.2IP4.域[1]:localdomainIP6.地址[1]:fe80::20c:29ff:fec5:a08d/64IP6.網關:

#這裡如果發現是中文,可以使用以下命令進行修改

[root@localhost~]#localectllist-locales[root@localhost~]#localectlset-localeLANG=en_US.utf8#斷開連接,然後重新連接下[root@centos7~]#nmclideviceshoweno16777736Warning:nmcli(1.0.6)andNetworkManager(1.0.0)versionsdon'tmatch.Use--nochecktosuppressthewarning.GENERAL.DEVICE:eno16777736GENERAL.TYPE:ethernetGENERAL.HWADDR:00:0C:29:C5:A0:8DGENERAL.MTU:1500GENERAL.STATE:100(connected)GENERAL.CONNECTION:eno16777736GENERAL.CON-PATH:/org/freedesktop/NetworkManager/ActiveConnection/8WIRED-PROPERTIES.CARRIER:onIP4.ADDRESS[1]:10.10.10.133/24IP4.GATEWAY:10.10.10.2IP4.DNS[1]:10.10.10.2IP4.DNS[2]:8.8.8.8IP4.DOMAIN[1]:localdomainIP6.ADDRESS[1]:fe80::20c:29ff:fec5:a08d/64IP6.GATEWAY:

#消除我們添加的ip:10.10.10.134/24

[root@localhost~]#nmcliconnectionmodifyeno16777736-ipv4.addresses10.10.10.134/24[root@localhost~]#echo$?0[root@localhost~]#nmcliconnectiondowneno16777736;nmcliconnectionupeno16777736Connection'eno16777736'successfullydeactivated(D-Busactivepath:/org/freedesktop/NetworkManager/ActiveConnection/3)成功激活的連接(D-Bus激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/4[root@localhost~]#ifconfig-aeno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)RXpackets77939bytes96881263(92.3MiB)RXerrors0dropped0overruns0frame0TXpackets14880bytes957913(935.4KiB)TXerrors0dropped0overruns0carrier0collisions0lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536inet127.0.0.1netmask255.0.0.0inet6::1prefixlen128scopeid0x10<host>looptxqueuelen0(LocalLoopback)RXpackets560bytes49264(48.1KiB)RXerrors0dropped0overruns0frame0TXpackets560bytes49264(48.1KiB)TXerrors0dropped0overruns0carrier0collisions0[root@localhost~]#cat/etc/sysconfig/network-scripts/ifcfg-eno16777736TYPE=EthernetBOOTPROTO=dhcpDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noNAME=eno16777736UUID=9abced2c-1ebf-4a70-9ecc-fc9c9d14d01cDEVICE=eno16777736ONBOOT=yesPEERDNS=yesPEERROUTES=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_PRIVACY=no[root@localhost~]#nmclideviceshoweno16777736GENERAL.設備:eno16777736GENERAL.類型:ethernetGENERAL.硬盤:00:0C:29:C5:A0:8DGENERAL.MTU:1500GENERAL.狀態:100(連接的)GENERAL.CONNECTION:eno16777736GENERAL.CON-PATH:/org/freedesktop/NetworkManager/ActiveConnection/6WIRED-PROPERTIES.容器:開IP4.地址[1]:10.10.10.133/24IP4.網關:10.10.10.2IP4.DNS[1]:10.10.10.2IP4.域[1]:localdomainIP6.地址[1]:fe80::20c:29ff:fec5:a08d/64IP6.網關:[root@localhost~]#ifconfig-aeno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet10.10.10.133netmask255.255.255.0broadcast10.10.10.255inet6fe80::20c:29ff:fec5:a08dprefixlen64scopeid0x20<link>ether00:0c:29:c5:a0:8dtxqueuelen1000(Ethernet)RXpackets537996bytes673538844(642.3MiB)RXerrors0dropped0overruns0frame0TXpackets97698bytes6097864(5.8MiB)TXerrors0dropped0overruns0carrier0collisions0lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536inet127.0.0.1netmask255.0.0.0inet6::1prefixlen128scopeid0x10<host>looptxqueuelen0(LocalLoopback)RXpackets616bytes54280(53.0KiB)RXerrors0dropped0overruns0frame0TXpackets616bytes54280(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~]#nmclideviceconnectdeletedisconnecthelpshowstatuswifiwimax[root@localhost~]#nmcliconnectiondowneno16777736;nmcliconnectionupeno16777736Connection'eno16777736'successfullydeactivated(D-Busactivepath:/org/freedesktop/NetworkManager/ActiveConnection/6)成功激活的連接(D-Bus激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/7[root@localhost~]#nmclideviceshoweno16777736GENERAL.設備:eno16777736GENERAL.類型:ethernetGENERAL.硬盤:00:0C:29:C5:A0:8DGENERAL.MTU:1500GENERAL.狀態:100(連接的)GENERAL.CONNECTION:eno16777736GENERAL.CON-PATH:/org/freedesktop/NetworkManager/ActiveConnection/7WIRED-PROPERTIES.容器:開IP4.地址[1]:10.10.10.133/24IP4.網關:10.10.10.2IP4.DNS[1]:10.10.10.2IP4.DNS[2]:8.8.8.8IP4.域[1]:localdomainIP6.地址[1]:fe80::20c:29ff:fec5:a08d/64IP6.網關:

CentOS7下面有類似於CentOS6下面的setup工具的命令,nmtui命令

功能非常強大:可以管理IP的相關信息,添加子接口,橋接等。

http://xxxxxx/Linuxjc/1141685.html TechArticle

Copyright © Linux教程網 All Rights Reserved