In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use nmcli to manage network connections on the command line. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.
The nmcli command gives you the ability to operate the NetworkManager tool directly from the Linux command line.
The nmcli command gives you the ability to operate the NetworkManager tool directly from the Linux command line. It is part of the integration of the NetworkManager package and acquires the functionality of NetworkManager by using some application program interfaces (API).
Nmcli was released in 2010 as an alternative to other methods of configuring network interfaces and connections, such as ifconfig. Because it is a command line interface (CLI) tool designed for terminal windows and scripts, it is an ideal tool for administrators working on systems without a graphical user interface (GUI).
Syntax of ncmli
The nmcli command can use options to change its behavior, use subcommands to tell nmcli which part of its functionality you want to use, and use actions to tell nmcli what you want to do.
$nmcli
There are 8 subcommands in nmcli-1, each of which has some related network operations:
Help provides help on nmcli commands and how to use them
General returns the status and overall configuration information of NetworkManager
Networking provides commands to query the status of a network connection and the ability to start and disable a connection.
Radio provides commands to query the status of a WiFi network connection and the ability to start and disable a connection.
Monitor provides commands to monitor NetworkManager activity and observe changes in the state of network connections
Connection provides commands to enable or disable network interfaces, add new connections, delete existing connections, and so on.
Device is mainly used to change the connection parameters associated with a device (such as the interface name) or to connect the device using an existing connection
Secret registers nmcli as a secret agent for NetworkManager to listen for secret messages. This subcommand is rarely used because nmcli does these things automatically when you connect to the network
A simple example
First, let's verify that NetworkManager is running and that nmcli can communicate with it:
$nmcli generalSTATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN connected full enabled enabled
Probing is always the first part of managing a system. To list the network connection configuration on memory or disk, you can use the following command:
$nmcli connection showNAME UUID TYPE DEVICEWired connection 1 ac3241e4-b424-35d6-aaa7-07498561688d ethernet enp0s3Wired connection 2 2279d917-fa02-390c-8603-3083ec5a1d3e ethernet enp0s8Wired connection 3 52d89737-de92-35ec-b082-8cf2e5ac36e6 ethernet enp0s9
The above command uses the show operation in the connection subcommand.
The test machine used to run the above example runs Ubuntu 20.04, which installs three network adapters: enp0s3, enp0s8, and enp0s9.
Connection management
It is important to understand the terminology of nmcli. A network connection connection contains all the information about a connection. You can think of it as a network configuration configuration. "connection" contains all the information related to it, including data link layer and IP address information. They are layers 2 and 3 of the OSI network model.
When you configure a network on a Linux, you are usually configuring a connection for a network device (which is a network interface installed on a computer). When a connection is used by a device, it can be said that the connection is activated active or online up, otherwise inactive or offline down is disabled.
Add a network connection
Nmcli allows you to quickly create a network connection and specify parameters for that connection at the same time. To create a new connection by using the Wired connection 2 enp0s8, you can use sudo to run the following command:
$sudo nmcli connection add type ethernet ifname enp0s8Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully added.
The type option specifies that an Ethernet type connection is required, while the ifname (interface name) option specifies the network interface device you want to use for this connection.
Let's see what has changed:
$nmcli connection showNAME UUID TYPE DEVICEWired connection 1 ac3241e4-b424-35d6-aaa7-07498561688d ethernet enp0s3Wired connection 2 2279d917-fa02-390c-8603-3083ec5a1d3e ethernet enp0s8Wired connection 3 52d89737-de92-35ec-b082-8cf2e5ac36e6 ethernet enp0s9ethernet-enp0s8 09d26960-25a0-440f-8b20-c684d7adc2f5 ethernet--
From the figure above, you can see that the new connection ethernet-enp0s8 has been created. Its universal unique identifier Universally Unique IDentifier (UUID) is also assigned, and its connection type is "Ethernet". We can use the up subcommand plus the connection name (or UUID) to make the connection active:
$nmcli connection up ethernet-enp0s8Connection successfully activated (D-Bus active path: / org/freedesktop/NetworkManager/ActiveConnection/4)
View the active connection again:
$nmcli connection show-- activeNAME UUID TYPE DEVICEWired connection 1 ac3241e4-b424-35d6-aaa7-07498561688d ethernet enp0s3ethernet-enp0s8 09d26960-25a0-440f-8b20-c684d7adc2f5 ethernet enp0s8Wired connection 3 52d89737-de92-35ec-b082-8cf2e5ac36e6 ethernet enp0s9
You can see that the new connection ethernet-enp0s8 is now activated and bound to the enp0s8 network interface device.
Adjust connection
The nmcli command makes it easier to adjust the parameters of an existing connection. You may want to change a network interface from dynamic Host configuration Protocol Dynamic Host Configuration Protocol (DHCP) to a static IP address.
Suppose you need to assign a fixed IP address of 192.168.4.26 to your new connection, then you need to use two commands, one to set the IP address and the other to change the method of obtaining the IP address to manual (manual):
$nmcli connection modify ethernet-enp0s8 ipv4.address 192.168.4.26 Compact 24$ nmcli connection modify ethernet-enp0s8 ipv4.method manual
Remember to specify the subnet mask, which in the connection we tested is 255.255.255.0 or / 24 in classless inter-domain routing Classless Inter-Domain Routing (CIDR).
In order for your changes to take effect, you need to stop and re-enable the connection. The first command below is to disable the connection, and the second command is to enable it:
$nmcli connection down ethernet-enp0s8Connection 'ethernet-enp0s8' successfully deactivated (D-Bus active path: / org/freedesktop/NetworkManager/ActiveConnection/4) $nmcli connection up ethernet-enp0s8Connection successfully activated (D-Bus active path: / org/freedesktop/NetworkManager/ActiveConnection/6)
If you want to set the connection to use DHCP, you need to change the above manual to auto (automatic):
$nmcli connection modify ethernet-enp0s8 ipv4.method auto device Management
The device subcommand in the nmcli command allows you to manage the network interface installed on your computer.
Check equipment status
You can use the following command to quickly check the status of all network interfaces:
$nmcli device statusDEVICE TYPE STATE CONNECTION enp0s3 ethernet connected Wired connection 1enp0s8 ethernet connected ethernet-enp0s8 enp0s9 ethernet connected Wired connection 3lo loopback unmanaged-displays device details
To check the details of a network interface, you can use the show operation in the device subcommand. If you do not provide the name of a device, the details of all devices will be obtained and displayed. You can flip up and down to see this information.
To view the device enp0s8 for your recently added connection, you can use the following command to verify that the IP address it uses is the one you asked for earlier:
$nmcli device show enp0s8GENERAL.DEVICE: enp0s8GENERAL.TYPE: ethernetGENERAL.HWADDR: 08:00:27:81:16:20GENERAL.MTU: 1500GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: ethernet-enp0s8GENERAL. CON-PATH: / org/freedesktop/NetworkManager/ActiveConnection/6WIRED-PROPERTIES.CARRIER: onIP4.ADDRESS [1]: 192.168.4.26/24IP4.GATEWAY:-- IP4.ROUTE [1]: dst = 192.168.4.0 Nh = 0.0.0.0, mt = 103IP6.ADDRESS [1]: fe80::6d70:90de:cb83:4491/64IP6.GATEWAY:-- IP6.ROUTE [1]: dst = fe80::/64, nh =:, mt = 103IP6.ROUTE [2]: dst = ff00::/8, nh =:: Mt = 256, table=255
The above output is very detailed, and it mainly shows the following:
The name of the network interface, in this case enp0s8, which is assigned by udev
Network connection type, in this case a physical Ethernet connection
Of equipment.
Media access Control media access control
(MAC) address, which is used to identify the device in the network
Maximum transmission unit, the size of the maximum protocol data unit in a single transmission, any data larger than this size will be divided into multiple packets for transmission
The device is currently in a connected state
The connection name used by this device is ethernet-enp0s8
The IP address used by this device is set to 192.168.4.26 ax 24 as required above.
The other information is the default route and gateway settings information related to the network to which this device is connected.
Interactive editor for nmcli
Although nmcli is a command line tool, it also includes a basic interactive editor, and the edit subcommand opens an interactive editor for the connection you specify, such as:
$nmcli connection edit ethernet-enp0s8
It will display a small amount of help text, followed by a command prompt for nmcli:
= | nmcli interactive connection editor | = Editing existing '802-3 murethernet' connection: 'ethernet-enp0s8' Type' help' or'?' For available commands.Type 'print' to show all the connection properties.Type' describe [.]' For detailed property description. You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, ipv4, ipv6, tc, proxynmcli >
If you type print and then press the Enter key, nmcli will list all the properties associated with this interface. There are many of these properties, and you can scroll up and down to see this list:
= Connection profile details (ethernet-enp0s8) = connection.id: ethernet-enp0s8connection.uuid: 09d26960-25a0-440f-8b20-c684d7adc2f5connection.stable-id:-- connection.type: 802-3-ethernetconnection.interface-name: enp0s8connection.autoconnect: Yesconnection.autoconnect-priority: 0connection.autoconnect-retries:-1 (default) connection.multi-connect: 0 (default) connection.auth-retries:-1connection.timestamp: 1593967212connection.read-only: noconnection.permissions:-- connection.zone: -- connection.master:-- connection.slave-type:-- connection.autoconnect-slaves:-1 (default) connection.secondaries:-
If you want to change your connection to DHCP, type goto ipv4 and then press the Enter key:
Nmcli > goto ipv4You 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-timeoutnmcli ipv4 >
The property you want to change is method, then continue to hit set method auto and then hit Enter:
Nmcli ipv4 > set method autoDo you also want to clear 'ipv4.addresses'? [yes]:
If you want the connection to erase the static IP address, press the Enter key, or if you want to keep it, type no and then press the Enter key. If you want to use it again in the future, you can keep this IP address. Even if a static IP address is stored, if method is set to auto, it will still use DHCP.
Finally, type save to save your changes:
Nmcli ipv4 > saveConnection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully updated.nmcli ipv4 >
Enter quit to leave the interactive editor window of nmcli. If you don't want to leave, type back to return to the original command prompt interface, and then continue to use the editor.
More about nmcli
Browse the interactive editor and you can see how many settings nmcli has and how many attributes each setting has. Interactive editors are a simple tool, but if you need to use nmcli on the command line or in scripts, you still need to use the regular command line version.
The above is all the contents of the article "how to use nmcli to manage network connections on the command line". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.