Tswj kev sib txuas hauv network hauv Linux siv nmcli console utility

Ua kom zoo dua ntawm NetworkManager network tswj cov cuab yeej ntawm Linux hais kom ua kab siv nmcli utility.

Tswj kev sib txuas hauv network hauv Linux siv nmcli console utility

Π’ΠΈΠ»ΠΈΡ‚Π° nmcli ua ncaj qha hu rau API kom nkag mus rau NetworkManager ua haujlwm.

Nws tshwm sim hauv xyoo 2010 thiab rau ntau tus tau dhau los ua lwm txoj hauv kev los teeb tsa kev sib txuas hauv network thiab kev sib txuas. Txawm hais tias qee tus neeg tseem siv ifconfig. Vim hais tias nmcli yog ib qho kev hais kom ua kab interface (CLI) cov cuab yeej tsim los siv rau hauv lub qhov rais davhlau ya nyob twg thiab cov ntawv sau, nws yog qhov zoo tagnrho rau cov thawj coj ua haujlwm tsis muaj GUI.

ncmli command syntax

Feem ntau, cov syntax zoo li no:

$ nmcli <options> <section> <action>

  • cov kev xaiv yog cov kev ntsuas uas txiav txim siab lub subtleties ntawm nmcli kev ua haujlwm,
  • seem (section) - txiav txim siab seb cov yam ntxwv ntawm kev siv hluav taws xob siv,
  • kev txiav txim - tso cai rau koj los qhia meej tias qhov yuav tsum tau ua tiag tiag.

Muaj 8 ntu hauv tag nrho, txhua qhov cuam tshuam nrog qee cov lus txib (ua haujlwm):

  • pab muab kev pab txog ncmcli cov lus txib thiab lawv cov kev siv.
  • General rov qab cov xwm txheej NetworkManager thiab kev teeb tsa thoob ntiaj teb.
  • Tshua suav nrog cov lus txib kom nug cov xwm txheej sib txuas hauv network thiab pab kom / kaw kev sib txuas.
  • xov tooj cua suav nrog cov lus txib kom nug WiFi network kev twb kev txuas raws li txoj cai thiab qhib / kaw kev sib txuas.
  • saib suav nrog cov lus txib rau kev saib xyuas NetworkManager kev ua haujlwm thiab saib xyuas kev hloov pauv hauv lub xeev ntawm kev sib txuas hauv network.
  • Kev twb kev txuas suav nrog cov lus txib rau kev tswj hwm network interfaces, ntxiv cov kev sib txuas tshiab thiab tshem tawm cov uas twb muaj lawm.
  • Ntaus Feem ntau yog siv los hloov cov cuab yeej ntsig txog qhov tsis sib xws (xws li lub npe interface) lossis los txuas cov cuab yeej siv qhov kev sib txuas uas twb muaj lawm.
  • Secret sau npe nmcli ua NetworkManager "tus neeg saib xyuas zais cia" uas mloog cov lus zais. Tshooj lus no tsis tshua siv, vim tias nmcli ua haujlwm li no los ntawm lub neej ntawd thaum txuas rau tes hauj lwm.

Cov qauv yooj yim

Ua ntej koj pib, xyuas kom NetworkManager khiav thiab nmcli tuaj yeem sib txuas lus nrog nws:

$ nmcli general
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN    
connected  full          enabled  enabled  enabled  enabled

Kev ua haujlwm feem ntau pib los ntawm saib tag nrho cov kev sib txuas hauv network:

$ nmcli connection show
NAME                UUID                                  TYPE      DEVICE
Wired connection 1  ac3241e4-b424-35d6-aaa7-07498561688d  ethernet  enp0s3
Wired connection 2  2279d917-fa02-390c-8603-3083ec5a1d3e  ethernet  enp0s8
Wired connection 3  52d89737-de92-35ec-b082-8cf2e5ac36e6  ethernet  enp0s9

Cov lus txib no siv kev ua qhia rau seem txuas.

Lub tshuab xeem tau khiav Ubuntu 20.04. Hauv qhov no, peb pom peb qhov txuas txuas: enp0s3, enp0s8, thiab enp0s9.

Tswj kev sib txuas

Nws yog ib qho tseem ceeb kom nkag siab tias nyob rau hauv nmcli, los ntawm lo lus Connection peb txhais tau hais tias ib qho chaw uas muaj tag nrho cov ntaub ntawv hais txog kev sib txuas. Nyob rau hauv lwm yam lus, nws yog lub network configuration. Kev sib txuas encapsulates tag nrho cov ntaub ntawv hais txog kev sib txuas, suav nrog txheej txheej txuas thiab cov ntaub ntawv IP chaw nyob. Cov no yog Txheej 2 thiab Txheej 3 hauv OSI networking qauv.

Thaum koj teeb tsa lub network hauv Linux, feem ntau koj tab tom teeb tsa kev sib txuas uas yuav xaus rau kev khi rau cov khoom siv network, uas nyob rau hauv lem yog network interfaces ntsia rau hauv lub computer. Thaum lub cuab yeej siv kev sib txuas, nws suav hais tias yog ua haujlwm lossis nce siab. Yog tias qhov kev sib txuas tsis siv, nws tsis ua haujlwm lossis rov pib dua.

Ntxiv kev sib txuas hauv network

Ncmli qhov hluav taws xob tso cai rau koj kom sai ntxiv thiab teeb tsa kev sib txuas tam sim. Piv txwv li, ntxiv Wired kev twb kev txuas 2 (nrog enp0s8), koj yuav tsum tau khiav cov lus txib nram qab no ua tus superuser:

$ sudo nmcli connection add type ethernet ifname enp0s8
Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully added.

Hauv hom kev xaiv peb qhia tias qhov no yuav yog kev sib txuas Ethernet, thiab hauv ifname (interface name) kev xaiv peb qhia lub network interface uas peb xav siv.

Nov yog qhov yuav tshwm sim tom qab khiav qhov hais kom ua:

$ nmcli connection show
NAME                UUID                                  TYPE      DEVICE
Wired connection 1  ac3241e4-b424-35d6-aaa7-07498561688d  ethernet  enp0s3
Wired connection 2  2279d917-fa02-390c-8603-3083ec5a1d3e  ethernet  enp0s8
Wired connection 3  52d89737-de92-35ec-b082-8cf2e5ac36e6  ethernet  enp0s9
ethernet-enp0s8     09d26960-25a0-440f-8b20-c684d7adc2f5  ethernet  --  

Kev sib txuas tshiab tau tsim, ethernet-enp0s8. Nws tau muab UUID thiab hom kev sib txuas yog Ethernet. Cia peb tsa nws siv qhov hais kom ua:

$ nmcli connection up ethernet-enp0s8
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)

Cia peb txheeb xyuas cov npe ntawm cov kev sib txuas nquag dua:

$ nmcli connection show --active
NAME                UUID                                  TYPE      DEVICE
Wired connection 1  ac3241e4-b424-35d6-aaa7-07498561688d  ethernet  enp0s3
ethernet-enp0s8     09d26960-25a0-440f-8b20-c684d7adc2f5  ethernet  enp0s8
Wired connection 3  52d89737-de92-35ec-b082-8cf2e5ac36e6  ethernet  enp0s9

Ib qho kev sib txuas tshiab ethernet-enp0s8 tau ntxiv lawm, nws ua haujlwm thiab siv enp0s8 network interface.

Kev teeb tsa kev sib txuas

Ncmli kev siv hluav taws xob tso cai rau koj kom yooj yim hloov cov kev sib txuas uas twb muaj lawm. Piv txwv li, koj yuav tsum hloov koj qhov chaw nyob IP dynamic (DHCP) mus rau qhov chaw nyob IP zoo li qub.

Wb hais tias peb yuav tsum teeb tus IP chaw nyob rau 192.168.4.26. Ua li no peb siv ob lo lus txib. Tus thawj yuav ncaj qha teeb tus IP chaw nyob, thiab qhov thib ob yuav hloov tus IP chaw nyob txoj kev mus rau phau ntawv:

$ nmcli connection modify ethernet-enp0s8 ipv4.address 192.168.4.26/24
$ nmcli connection modify ethernet-enp0s8 ipv4.method manual

Tsis txhob hnov ​​​​qab los teeb lub subnet npog. Rau peb qhov kev sib txuas ntawm qhov kev xeem no yog 255.255.255.0, lossis nrog /24 rau kev ua tsis tiav (CIDR).

Txhawm rau kom muaj kev hloov pauv, koj yuav tsum deactivate thiab rov qhib qhov kev sib txuas:

$ nmcli connection down ethernet-enp0s8
Connection 'ethernet-enp0s8' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
$ nmcli connection up ethernet-enp0s8
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveC

Yog tias, ntawm qhov tsis sib xws, koj yuav tsum tau nruab DHCP, siv nws pib siv phau ntawv:

$ nmcli connection modify ethernet-enp0s8 ipv4.method auto

Ua haujlwm nrog cov khoom siv

Rau qhov no peb siv cov khoom siv ntu.

Tshawb xyuas cov xwm txheej ntawm cov cuab yeej

$ nmcli device status
DEVICE  TYPE      STATE      CONNECTION        
enp0s3  ethernet  connected  Wired connection 1
enp0s8  ethernet  connected  ethernet-enp0s8    
enp0s9  ethernet  connected  Wired connection 3
lo      loopback  unmanaged  --  

Thov cov ntaub ntawv ntaus ntawv

Txhawm rau ua qhov no, siv qhov kev ua yeeb yam los ntawm ntu Ntaus Ntaus (koj yuav tsum qhia lub npe ntaus ntawv). Lub tshuab hluav taws xob tso tawm ntau cov ntaub ntawv, feem ntau ntawm ntau nplooj ntawv.
Cia peb saib ntawm enp0s8 interface uas peb qhov kev sib txuas tshiab siv. Cia peb nco ntsoov tias nws siv raws nraim qhov chaw nyob IP uas peb tau teev ua ntej:

$ nmcli device show enp0s8
GENERAL.DEVICE:                         enp0s8
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:81:16:20
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     ethernet-enp0s8
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/6
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.4.26/24
IP4.GATEWAY:                            --
IP4.ROUTE[1]:                           dst = 192.168.4.0/24, nh = 0.0.0.0, mt = 103
IP6.ADDRESS[1]:                         fe80::6d70:90de:cb83:4491/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 103
IP6.ROUTE[2]:                           dst = ff00::/8, nh = ::, mt = 256, table=255

Muaj cov ntaub ntawv ntau heev. Cia peb qhia qhov tseem ceeb:

  • Network interface lub npe: enp0s8.
  • Hom kev sib txuas: wired Ethernet kev twb kev txuas.
  • Peb pom MAC chaw nyob ntawm lub cuab yeej.
  • Qhov siab tshaj plaws kis tau tus mob unit (MTU) teev - qhov loj tshaj plaws ntawm cov ntaub ntawv tseem ceeb ntawm ib pob ntawv uas tuaj yeem kis tau los ntawm cov txheej txheem yam tsis muaj kev tawg.
  • Ntaus ntawv txuas tam sim no.
  • Kev sib txuas npeCov cuab yeej twg siv: ethernet-enp0s8.
  • Cov cuab yeej siv lub IP chaw nyob, uas peb ntsia ua ntej: 192.168.4.26/24.

Lwm cov ntaub ntawv muaj feem xyuam rau lub default routing thiab kev twb kev txuas gateway parameters. Lawv nyob ntawm lub network tshwj xeeb.

Sib tham sib nmcli editor

nmcli kuj muaj qhov sib tham sib tham yooj yim editor, uas tej zaum yuav yooj yim dua rau qee tus ua haujlwm nrog. Txhawm rau khiav nws ntawm kev sib txuas ethernet-enp0s8 piv txwv, siv kev ua kho kom raug:

$ nmcli connection edit ethernet-enp0s8

Nws kuj muaj ib qho kev pab me me, uas, txawm li cas los xij, yog qhov me me dua li console version:

===| nmcli interactive connection editor |===
Editing existing '802-3-ethernet' connection: 'ethernet-enp0s8'
Type 'help' or '?' for available commands.
Type 'print' to show all the connection properties.
Type 'describe [<setting>.<prop>]' for detailed property description.
You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, ipv4, ipv6, tc, proxy
nmcli>

Yog tias koj ntaus cov ntawv luam tawm thiab nias Enter, nmcli yuav tso tawm tag nrho cov khoom sib txuas:

===============================================================================
                 Connection profile details (ethernet-enp0s8)
===============================================================================
connection.id:                          ethernet-enp0s8
connection.uuid:                        09d26960-25a0-440f-8b20-c684d7adc2f5
connection.stable-id:                   --
connection.type:                        802-3-ethernet
connection.interface-name:              enp0s8
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.autoconnect-retries:         -1 (default)
connection.multi-connect:               0 (default)
connection.auth-retries:                -1
connection.timestamp:                   1593967212
connection.read-only:                   no
connection.permissions:                 --
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:                 --

Piv txwv li, txhawm rau teeb tsa kev sib txuas rau DHCP, ntaus goto ipv4 thiab nyem Sau:

nmcli> goto ipv4
You may edit the following properties: method, dns, dns-search, 
dns-options, dns-priority, addresses, gateway, routes, route-metric, 
route-table, routing-rules, ignore-auto-routes, ignore-auto-dns, 
dhcp-client-id, dhcp-iaid, dhcp-timeout, dhcp-send-hostname, 
dhcp-hostname, dhcp-fqdn, dhcp-hostname-flags, never-default, may-fail, 
dad-timeout
nmcli ipv4>

Tom qab ntawd sau cov txheej txheem pib thiab nyem Sau:

nmcli ipv4> set method auto
Do you also want to clear 'ipv4.addresses'? [yes]:

Yog tias koj xav tshem tawm qhov chaw nyob IP zoo li qub, nyem Sau. Txwv tsis pub, ntaus tsis yog thiab nias Enter. Koj tuaj yeem txuag tau yog tias koj xav tias koj yuav xav tau nws yav tom ntej. Tab sis txawm tias muaj qhov chaw nyob IP uas tau txais kev cawmdim, DHCP yuav raug siv yog tias txoj kev teem rau nws pib.

Siv cov lus txib txuag kom txuag koj cov kev hloov pauv:

nmcli ipv4> save
Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully updated.
nmcli ipv4>

Ntaus tawm kom tawm ntawm nmcli Sib tham sib Editor. Yog tias koj hloov koj lub siab txog kev tawm mus, siv cov lus txib rov qab.

Thiab qhov ntawd tsis yog tag nrho

Qhib nmcli Interactive Editor thiab saib seb muaj pes tsawg qhov chaw muaj thiab pes tsawg lub zog txhua qhov chaw muaj. Kev sib tham sib editor yog ib qho cuab yeej zoo, tab sis yog tias koj xav siv nmcli hauv ib-liners lossis cov ntawv sau, koj yuav xav tau cov kab lus hais kom ua li niaj zaus.

Tam sim no koj muaj cov hauv paus, kos tawm paj man nmcli kom pom tias nws tuaj yeem pab koj li cas.

Rau Txoj Cai Kev Tshaj Tawm

Epic servers Yog virtual servers ntawm Windows los yog Linux nrog cov haib AMD EPYC tsev neeg processors thiab ceev heev Intel NVMe drives. Ceev nrooj kom xaj!

Tswj kev sib txuas hauv network hauv Linux siv nmcli console utility

Tau qhov twg los: www.hab.com

Ntxiv ib saib