mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
[Translated] tech/20200721 Manage network connections from the Linux command line with nmcli.md
Signed-off-by: FSSlc <fsslc235@gmail.com>
This commit is contained in:
parent
c025d7e37d
commit
fe8d7cea5f
@ -1,325 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (FSSlc)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: subject: (Manage network connections from the Linux command line with nmcli)
|
|
||||||
[#]: via: (https://opensource.com/article/20/7/nmcli)
|
|
||||||
[#]: author: (Dave McKay https://opensource.com/users/davemckay)
|
|
||||||
|
|
||||||
Manage network connections from the Linux command line with nmcli
|
|
||||||
======
|
|
||||||
Tap into the power of the NetworkManager tool directly from the Linux
|
|
||||||
command line with nmcli.
|
|
||||||
![Business woman on laptop sitting in front of window][1]
|
|
||||||
|
|
||||||
The [nmcli][2] command lets you tap into the power of the NetworkManager tool directly from the Linux command line. It's an integral part of the NetworkManager package that makes use of an [application programmer's interface][3] (API) to access NetworkManager's functionality.
|
|
||||||
|
|
||||||
nmcli was released in 2010 and replaces other modes of configuring network interfaces and connections, such as [ifconfig][4]. Because it is a [command-line interface][5] (CLI) tool designed to be used in terminal windows and scripts, it is ideal for system administrators working on systems without a [graphical user interface][6] (GUI).
|
|
||||||
|
|
||||||
### ncmli syntax
|
|
||||||
|
|
||||||
The nmcli command accepts _options_ that modify nmcli's behavior, _sections_ that tell nmcli which of its capabilities you want to use, and _actions_ that tell it what you want it to do:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`$ nmcli <options> <section> <action>`
|
|
||||||
```
|
|
||||||
|
|
||||||
There are eight sections, each related to a specific set of network actions:
|
|
||||||
|
|
||||||
* **Help** provides help about ncmcli's commands and usage.
|
|
||||||
* **General** retrieves NetworkManager's status and global configuration.
|
|
||||||
* **Networking** provides commands to query a network connection's status and enable or disable connections.
|
|
||||||
* **Radio** provides commands to query a WiFi network connection's status and enable or disable connections.
|
|
||||||
* **Monitor** provides commands to monitor NetworkManager activity and observe network connections' status changes.
|
|
||||||
* **Connection** provides commands to bring network interfaces up and down, to add new connections, and to delete existing connections.
|
|
||||||
* **Device** is mainly used to modify parameters associated with a device (e.g., the interface name) or to connect a device using an existing connection.
|
|
||||||
* **Secret** registers nmcli as a NetworkManager secret agent listening for secret messages. This is very rarely required because nmcli does this automatically when connecting to networks.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Simple examples
|
|
||||||
|
|
||||||
As a first check, verify NetworkManager is running and nmcli can communicate with it:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ nmcli general
|
|
||||||
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
|
|
||||||
connected full enabled enabled enabled enabled
|
|
||||||
```
|
|
||||||
|
|
||||||
Reconnaissance is often the first part of administering a system. To list all in-memory and on-disk network connection profiles:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ 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
|
|
||||||
```
|
|
||||||
|
|
||||||
This command uses the `show` action from the `connection` section.
|
|
||||||
|
|
||||||
The test machine used for this example is running Ubuntu 20.04. It has three network adaptors installed: `enp0s3`, `enp0s8`, and `enp0s9`.
|
|
||||||
|
|
||||||
### Connection management
|
|
||||||
|
|
||||||
It's important to understand nmcli's nomenclature. A network **connection** is something that holds all the information about a connection. You can think of it as a network **configuration**. A connection encapsulates all the information related to a connection, including the [data-link layer][7] and the [IP-addressing information][8]. That's layer 2 and layer 3 in the [OSI networking model][9].
|
|
||||||
|
|
||||||
When you are configuring networking on Linux, you're usually configuring connections that will eventually bind to networking devices, which are the network interfaces installed in a computer. When a connection is used by a device, the connection is said to be **active** or **up**. The opposite of active is **inactive** or **down**.
|
|
||||||
|
|
||||||
#### Adding network connections
|
|
||||||
|
|
||||||
The ncmli command allows you to quickly create network connections and specify elements of their configuration at the same time. To add a new connection using wired connection 2, `enp0s8`, you need to use `sudo`:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo nmcli connection add type ethernet ifname enp0s8
|
|
||||||
Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully added.
|
|
||||||
```
|
|
||||||
|
|
||||||
The `type` option requests an [Ethernet][10] connection, and the `ifname` (interface name) option specifies the network interface device you want the connection to use.
|
|
||||||
|
|
||||||
Check what happened:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ 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 --
|
|
||||||
```
|
|
||||||
|
|
||||||
Your new connection, `ethernet-enp0s8`, was created. Its [universally unique identifier][11] (UUID) was assigned, and the connection type is Ethernet. Make it active with the `up` command followed by the connection name (or the UUID):
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ nmcli connection up ethernet-enp0s8
|
|
||||||
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
|
|
||||||
```
|
|
||||||
|
|
||||||
Check your active connections once more:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ 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
|
|
||||||
```
|
|
||||||
|
|
||||||
Your new connection, `ethernet-enp0s8`, is now active and bound to the `enp0s8` network interface device.
|
|
||||||
|
|
||||||
#### Adjusting connections
|
|
||||||
|
|
||||||
The ncmli command makes it easy to adjust existing connections' parameters. Perhaps you want to switch one network interface from [Dynamic Host Configuration Protocol][12] (DHCP) to a static IP address.
|
|
||||||
|
|
||||||
Suppose you need a fixed IP address of `192.168.4.26` for your new connection. To achieve that, you need to issue two commands. One to set the IP address, and one to set the connection's method of obtaining an IP address to `manual`:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ nmcli connection modify ethernet-enp0s8 ipv4.address 192.168.4.26/24
|
|
||||||
$ nmcli connection modify ethernet-enp0s8 ipv4.method manual
|
|
||||||
```
|
|
||||||
|
|
||||||
Remember to specify the [subnet mask][13]. On this test network, it is `255.255.255.0`, or `/24` in [Classless Inter-Domain Routing][14] (CIDR).
|
|
||||||
|
|
||||||
For your changes to take effect, you need to _bounce_ the connection by stopping it and bringing it back up again. The first command takes the connection down and the second brings it back up:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ 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/ActiveConnection/6)
|
|
||||||
```
|
|
||||||
|
|
||||||
If you want to set the connection to use DHCP, use `auto` instead of `manual`:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`$ nmcli connection modify ethernet-enp0s8 ipv4.method auto`
|
|
||||||
```
|
|
||||||
|
|
||||||
### Device management
|
|
||||||
|
|
||||||
The commands in the `device` section of the nmcli command allow you to manage the network interfaces installed on your computer.
|
|
||||||
|
|
||||||
#### Checking device status
|
|
||||||
|
|
||||||
To quickly check the status of all the network interfaces:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ 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 --
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Showing device details
|
|
||||||
|
|
||||||
To examine the details of a network interface, use the `show` action from the `device` section. If you do not provide a device name, the details of all devices are retrieved and displayed. You can scroll and page up and down to review them.
|
|
||||||
|
|
||||||
Take a look at `enp0s8`, the device your new connection is using. Verify that the IP address in use is the address that you previously requested:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ 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
|
|
||||||
```
|
|
||||||
|
|
||||||
The response is quite detailed. Among other things, it shows:
|
|
||||||
|
|
||||||
* The **network interface name**, which in this case is `enp0s8`, which is assigned to it by [udev][15].
|
|
||||||
* The **network connection type**, which in this case is a physical Ethernet connection.
|
|
||||||
* The device's **[media access control][16] (MAC) address**, which identifies the device on the network.
|
|
||||||
* The [maximum transmission unit][17], which is the size of the largest protocol data unit that can be transmitted in a single transaction. Anything larger than this is split into several packets.
|
|
||||||
* This device is **currently connected**.
|
|
||||||
* The **name of the connection** using this device is `ethernet-enp0s8`.
|
|
||||||
* The **IP address of the connection** using this device. As requested, it is set to `192.168.4.26/24`.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The other information relates to the default routing and gateway settings that were applied to this connection, according to the network it is connected to.
|
|
||||||
|
|
||||||
#### nmcli's interactive editor
|
|
||||||
|
|
||||||
Although it is a command-line tool, nmcli includes an elementary interactive editor. The `edit` action will open the interactive editor on the connection you specify:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`$ nmcli connection edit ethernet-enp0s8`
|
|
||||||
```
|
|
||||||
|
|
||||||
It displays a small amount of help text, then the nmcli command prompt:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
===| 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>
|
|
||||||
```
|
|
||||||
|
|
||||||
If you type `print` and hit **Enter**, nmcli will list all the properties associated with the connection. There are many properties. You can scroll up and down through the list:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
===============================================================================
|
|
||||||
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: --
|
|
||||||
```
|
|
||||||
|
|
||||||
Change your connection back to use DHCP. Type `goto ipv4` and hit **Enter**:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
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>
|
|
||||||
```
|
|
||||||
|
|
||||||
The property you want to change is `method`. Type `set method auto` and hit **Enter**:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
nmcli ipv4> set method auto
|
|
||||||
Do you also want to clear 'ipv4.addresses'? [yes]:
|
|
||||||
```
|
|
||||||
|
|
||||||
If you want the connection to purge the static IP address, press **Enter**. To keep it, type `no` and hit **Enter**. You can keep it if you think you might use it again in the future. Even with a stored static IP address, if `method` is set to `auto`, it will use DHCP.
|
|
||||||
|
|
||||||
Type `save` to save your changes:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
nmcli ipv4> save
|
|
||||||
Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully updated.
|
|
||||||
nmcli ipv4>
|
|
||||||
```
|
|
||||||
|
|
||||||
Type `quit` to exit the nmcli interactive editor. If you don't want to quit, type `back` to go back to the main level, and carry on using the editor.
|
|
||||||
|
|
||||||
### There's much more to nmcli
|
|
||||||
|
|
||||||
Browse around the interactive editor and see just how many settings there are and how many properties each setting has. The interactive editor is a neat tool, but for nifty one-liners or to use nmcli in scripts, you'll need the regular command-line version.
|
|
||||||
|
|
||||||
Now that you have the basics in hand, check out the nmcli [man page][2] to see what else it can offer.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://opensource.com/article/20/7/nmcli
|
|
||||||
|
|
||||||
作者:[Dave McKay][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[FSSlc](https://github.com/FSSlc)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://opensource.com/users/davemckay
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF (Woman using laptop concentrating)
|
|
||||||
[2]: https://developer.gnome.org/NetworkManager/stable/nmcli.html
|
|
||||||
[3]: https://en.wikipedia.org/wiki/Application_programming_interface
|
|
||||||
[4]: https://man7.org/linux/man-pages/man8/ifconfig.8.html
|
|
||||||
[5]: https://en.wikipedia.org/wiki/Command-line_interface
|
|
||||||
[6]: https://en.wikipedia.org/wiki/Graphical_user_interface
|
|
||||||
[7]: https://en.wikipedia.org/wiki/Data_link_layer
|
|
||||||
[8]: https://en.wikipedia.org/wiki/IP_address
|
|
||||||
[9]: https://en.wikipedia.org/wiki/OSI_model
|
|
||||||
[10]: https://en.wikipedia.org/wiki/Ethernet
|
|
||||||
[11]: https://en.wikipedia.org/wiki/Universally_unique_identifier
|
|
||||||
[12]: https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol
|
|
||||||
[13]: https://en.wikipedia.org/wiki/Subnetwork
|
|
||||||
[14]: https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
|
|
||||||
[15]: https://en.wikipedia.org/wiki/Udev
|
|
||||||
[16]: https://en.wikipedia.org/wiki/MAC_address
|
|
||||||
[17]: https://en.wikipedia.org/wiki/Maximum_transmission_unit
|
|
@ -0,0 +1,305 @@
|
|||||||
|
[#]: collector: "lujun9972"
|
||||||
|
[#]: translator: "FSSlc"
|
||||||
|
[#]: reviewer: " "
|
||||||
|
[#]: publisher: " "
|
||||||
|
[#]: url: " "
|
||||||
|
[#]: subject: "Manage network connections from the Linux command line with nmcli"
|
||||||
|
[#]: via: "https://opensource.com/article/20/7/nmcli"
|
||||||
|
[#]: author: "Dave McKay https://opensource.com/users/davemckay"
|
||||||
|
|
||||||
|
在命令行中使用 nmcli 来管理网络连接
|
||||||
|
======
|
||||||
|
|
||||||
|
nmcli 命令赋予你直接在 Linux 命令行操作 NetworkManager 工具的能力。
|
||||||
|
|
||||||
|
![坐在窗前笔记本电脑的一位商业女士][1]
|
||||||
|
|
||||||
|
[nmcli][2] 命令赋予你直接在 Linux 命令行操作 NetworkManager 工具的能力。它是 NetworkManager 软件包集成的一部分,通过使用一些 [应用程序接口][3](API)来获取 NetworkManager 的功能。
|
||||||
|
|
||||||
|
nmcli 于 2010 年发布,用以替代其他配置网络接口和连接的方法,例如 [ifconfig][4]。因为它是 [命令行界面][5](CLI)工具, 被设计用在终端窗口和脚本中,所以对于那些工作在没有 [图形界面][6](GUI)的系统管理员来说是它是一个非常理想的工具。
|
||||||
|
|
||||||
|
### ncmli 的语法
|
||||||
|
|
||||||
|
nmcli 命令接受 __选项__ (options) 来更改 nmcli 的行为,接受 __子命令__ (section) 来告诉 nmcli 想使用它的那部分功能,使用 __操作__ (action) 来告诉 nmcli 你想执行什么操作。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ nmcli <options> <section> <action>
|
||||||
|
```
|
||||||
|
|
||||||
|
nmcli 一共有 8 个子命令,每个子命令有一些相关的网络操作:
|
||||||
|
|
||||||
|
* **help** 提供有关 nmcli 命令和使用方法的帮助信息
|
||||||
|
* **general** 返回 NetworkManager 的状态和总体配置信息
|
||||||
|
* **networking** 提供命令来查询某个网络连接的状态和启动、禁用连接的功能
|
||||||
|
* **radio** 提供命令来查询某个 WiFi 网络连接的状态和启动、禁用连接的功能
|
||||||
|
* **monitor** 提供命令来监控 NetworkManager 的活动并观察网络连接的状态改变
|
||||||
|
* **connection** 提供命令来启用或禁用网络接口、添加新的连接、删除已有连接等功能
|
||||||
|
* **device** 主要被用于更改与某个设备(例如接口名称)相关联的连接的参数或者使用一个已有的连接来连接设备
|
||||||
|
* **secret** 注册 nmcli 来作为一个 NetworkManager 的秘密代理,用以监听秘密信息。这个子命令很少会被用到,因为当连接到网络时, nmcli 会自动做这些事
|
||||||
|
|
||||||
|
### 简单的示例
|
||||||
|
|
||||||
|
首先,我们验证一下 NetworkManager 正在运行并且 nmcli 可以与之通信:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ nmcli general
|
||||||
|
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
|
||||||
|
connected full enabled enabled enabled enabled
|
||||||
|
```
|
||||||
|
|
||||||
|
探测总是管理一个系统的首要部分。为了列出内存或磁盘上的网络连接配置,可以使用下面的命令:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ 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
|
||||||
|
```
|
||||||
|
|
||||||
|
上面的命令使用了 `connection` 子命令中的 `show` 操作。
|
||||||
|
|
||||||
|
用来运行上面这个例子的测试机器上运行着 Ubuntu 20.04,它安装了 3 个网络适配器:`enp0s3`、`enp0s8`和 `enp0s9`。
|
||||||
|
|
||||||
|
### 连接管理
|
||||||
|
|
||||||
|
理解 nmcli 的术语是非常重要的。一个网络 **连接** 包含了一个连接的所有信息。你可以将它看作一个网络**配置**。一个连接包含了与一个连接相关的所有信息,包括 [数据链路层][7] 和 [IP 地址信息][8] 。它们是 [OSI 网络模型][9] 中的第 2 和第 3 层。
|
||||||
|
|
||||||
|
当你在 LInux 上配置网络时,通常来说你在为某个网络设备(它们是安装在一个电脑中的网络接口)配置连接。当一个连接被某个设备所使用,那么就可以说这个连接被**激活**或者**启用**了,反之是**停用**或**下线**。
|
||||||
|
|
||||||
|
#### 添加网络连接
|
||||||
|
|
||||||
|
nmcli 允许你快速地创建网络连接并同时为该连接指定参数。为了通过使用 有线连接 2 `enp0s8`来创建一个新的连接,你可以利用`sudo` 来运行下面的命令:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo nmcli connection add type ethernet ifname enp0s8
|
||||||
|
Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully added.
|
||||||
|
```
|
||||||
|
|
||||||
|
其中 `type` 选项指定需要一个 [Ethernet][10] 的连接,而 `ifname`(接口名)选项指定你想要为这个连接使用的网络接口设备。
|
||||||
|
|
||||||
|
让我们看看发生了什么变化:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ 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 --
|
||||||
|
```
|
||||||
|
|
||||||
|
通过上图可以看到新的连接 `ethernet-enp0s8` 已经创建好了。它的 [通用唯一标识符][11](UUID)也一同被赋予,并且其连接类型为 Ethernet。我们可以使用 `up` 子命令再加上连接名称(或 UUID)来使得这个连接被激活:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ nmcli connection up ethernet-enp0s8
|
||||||
|
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
|
||||||
|
```
|
||||||
|
|
||||||
|
再次查看激活的连接:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ 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
|
||||||
|
```
|
||||||
|
|
||||||
|
可以看到新的连接 `ethernet-enp0s8` 现在已经被激活了,并且与 `enp0s8` 网络接口设备绑定。
|
||||||
|
|
||||||
|
#### 调整连接
|
||||||
|
|
||||||
|
nmcli 命令使得调整现有连接的参数变得更加容易。也许你想将某个网络接口从 [动态主机配置协议][12](DHCP)改为静态 IP 地址。
|
||||||
|
|
||||||
|
假设你需要为你的新连接分配一个固定的 IP 地址 `192.168.4.26`,那么你需要使用两个命令,一个用于设定 IP 地址,另一个用来将获取 IP 地址的方法改为 `manual`:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ nmcli connection modify ethernet-enp0s8 ipv4.address 192.168.4.26/24
|
||||||
|
$ nmcli connection modify ethernet-enp0s8 ipv4.method manual
|
||||||
|
```
|
||||||
|
|
||||||
|
记得特别指定 [子网掩码][13],在我们这个测试的连接中,它是 [无类域间路由][14](CIDR)中的 `255.255.255.0` 或 `/24`
|
||||||
|
|
||||||
|
为了使得你的更改生效,你需要通过停止再重新启用该连接。下面的第一个命令是停用该连接,第二个命令则是启用它:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ 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/ActiveConnection/6)
|
||||||
|
```
|
||||||
|
|
||||||
|
假如你想将连接设置为使用 DHCP,则需要将上面的 manual` 改为 `auto`:
|
||||||
|
|
||||||
|
```
|
||||||
|
`$ nmcli connection modify ethernet-enp0s8 ipv4.method auto`
|
||||||
|
```
|
||||||
|
|
||||||
|
### 设备管理
|
||||||
|
|
||||||
|
nmcli 命令中的 `device` 子命令允许你管理安装在你电脑中的网络接口。
|
||||||
|
|
||||||
|
#### 检查设备状态
|
||||||
|
|
||||||
|
可以使用下面的命令来快速检查所有网络接口的状态:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ 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 --
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 显示设备详情
|
||||||
|
|
||||||
|
为了检查某个网络接口的详情,可以使用 `device` 设备中的 `show` 操作。假如你不提供某个设备的名称,那么所有设备的详情都将会被获取再进行展示。你可以上下翻动来查看这些信息。
|
||||||
|
|
||||||
|
要查看你最近添加的连接所对应的设备 `enp0s8`,你可以使用下面的命令,请注意验证它使用的 IP 地址是否为先前你请求的那个:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ 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
|
||||||
|
```
|
||||||
|
|
||||||
|
上面的输出非常细致,它主要显示了下面这些内容:
|
||||||
|
|
||||||
|
* **网络接口名称**,在这个示例中是`enp0s8`,它是 [udev][15] 分配的
|
||||||
|
* **网络连接类型**,在这个示例中是物理的 Ethernet 连接
|
||||||
|
* 设备的 **媒体访问控制(MAC)地址**,它被用来在网络中识别该设备
|
||||||
|
* [最大传输单元][17],在单个传输中最大协议数据单位的大小,任何大于这个大小的数据将被分为多个包来进行传输
|
||||||
|
* 该设备 **当前已经处于连接状态**
|
||||||
|
* 这个设备使用的连接名称是`ethernet-enp0s8`
|
||||||
|
* 这个设备使用的 IP 地址如上面所请求的那样,被设置为 `192.168.4.26/24`
|
||||||
|
|
||||||
|
其他的信息则是与这个设备连接的网络相关的默认路由和网关设置信息。
|
||||||
|
|
||||||
|
#### nmcli 的交互式编辑器
|
||||||
|
|
||||||
|
尽管 nmcli 是一个命令行工具,但它还包含一个交互式工具, `edit` 子命令将为你指定的连接打开一个交互式编辑器,例如:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ nmcli connection edit ethernet-enp0s8
|
||||||
|
```
|
||||||
|
|
||||||
|
它将显示少量的帮助文字,接着是 nmcli 的命令提示符:
|
||||||
|
|
||||||
|
```
|
||||||
|
===| 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>
|
||||||
|
```
|
||||||
|
|
||||||
|
假如你输入 `print` 然后敲击 **Enter** 键, nmcli 将列举出与这个接口相关的所有属性。这些属性有很多,你可以上下翻动来查看这个列表:
|
||||||
|
|
||||||
|
```
|
||||||
|
===============================================================================
|
||||||
|
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: --
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你想将你的连接改为 DHCP,则请输入 `goto ipv4` 然后敲 **Enter**键:
|
||||||
|
|
||||||
|
```
|
||||||
|
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>
|
||||||
|
```
|
||||||
|
|
||||||
|
你想改变的属性是 `method`,再继续敲 `set method auto` 然后敲 **Enter** 键:
|
||||||
|
|
||||||
|
```
|
||||||
|
nmcli ipv4> set method auto
|
||||||
|
Do you also want to clear 'ipv4.addresses'? [yes]:
|
||||||
|
```
|
||||||
|
|
||||||
|
假如你想让这个连接清除掉这个静态 IP 地址,则请敲 **Enter**键,如果要保留,则输入 `no` 然后敲 **Enter**键。假如你想在将来再次使用它,你可以保留这个 IP 地址。即便存储了一个静态的 IP 地址,如果 `method` 被设置为 `auto` ,它仍然会使用 DHCP。
|
||||||
|
|
||||||
|
最后输入 `save` 来保存你的更改:
|
||||||
|
|
||||||
|
```
|
||||||
|
nmcli ipv4> save
|
||||||
|
Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully updated.
|
||||||
|
nmcli ipv4>
|
||||||
|
```
|
||||||
|
|
||||||
|
输入 `quit` 来离开 nmcli 的交互式编辑器窗口。假如你不想离开,可以输入 `back` 来回到最开始的命令行提示符界面,然后继续使用这个编辑器。
|
||||||
|
|
||||||
|
### nmcli 的更多内容
|
||||||
|
|
||||||
|
浏览交互式编辑器,你就可以看到 nmcli 有多少设定和每个设定有多少属性。交互式编辑器是一个简洁的工具,但如果需要在命令行或者在脚本中使用 nmcli,你还是需要使用常规的命令行版本。
|
||||||
|
|
||||||
|
现在你有了这些基础知识,你还可以查看 nmcli 的 [man 页面][2] 来查看它还可以提供什么额外功能。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://opensource.com/article/20/7/nmcli
|
||||||
|
|
||||||
|
作者:[Dave McKay][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[FSSLC](https://github.com/FSSlc)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://opensource.com/users/davemckay
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF "Woman using laptop concentrating"
|
||||||
|
[2]: https://developer.gnome.org/NetworkManager/stable/nmcli.html
|
||||||
|
[3]: https://en.wikipedia.org/wiki/Application_programming_interface
|
||||||
|
[4]: https://man7.org/linux/man-pages/man8/ifconfig.8.html
|
||||||
|
[5]: https://en.wikipedia.org/wiki/Command-line_interface
|
||||||
|
[6]: https://en.wikipedia.org/wiki/Graphical_user_interface
|
||||||
|
[7]: https://en.wikipedia.org/wiki/Data_link_layer
|
||||||
|
[8]: https://en.wikipedia.org/wiki/IP_address
|
||||||
|
[9]: https://en.wikipedia.org/wiki/OSI_model
|
||||||
|
[10]: https://en.wikipedia.org/wiki/Ethernet
|
||||||
|
[11]: https://en.wikipedia.org/wiki/Universally_unique_identifier
|
||||||
|
[12]: https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol
|
||||||
|
[13]: https://en.wikipedia.org/wiki/Subnetwork
|
||||||
|
[14]: https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
|
||||||
|
[15]: https://en.wikipedia.org/wiki/Udev
|
||||||
|
[16]: https://en.wikipedia.org/wiki/MAC_address
|
||||||
|
[17]: https://en.wikipedia.org/wiki/Maximum_transmission_unit
|
||||||
|
|
Loading…
Reference in New Issue
Block a user