Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-09-05 20:50:35 +08:00
commit 6c989e1d13
6 changed files with 701 additions and 384 deletions

View File

@ -1,22 +1,24 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-12582-1.html)
[#]: subject: (Create and run Python apps on your Android phone)
[#]: via: (https://opensource.com/article/20/8/python-android-mobile)
[#]: author: (Phani Adabala https://opensource.com/users/adabala)
在你的 Android 手机上创建和运行 Python 应用
======
使用 Termux 和 Flask 在你的移动设备上创建、开发和运行一个网络应用。
![Tux and Android stuffed animals on shelf][1]
学习和使用 Python 是很有趣的。由于它越来越受欢迎,有大量的方式可以让计算世界比现在更好
> 使用 Termux 和 Flask 在你的移动设备上创建、开发和运行一个网页应用。
想象一下,构建和运行 python 应用,无论是开发一个命令行工具,从互联网上获取你最喜欢的文章,还是启动一个直接在掌上运行的网络服务器,所有这些都只需要一个 Android 移动设备和开源工具。这将完全改变你对移动设备的看法,将它从一个仅仅让你消费内容的设备变成一个帮助你发挥创造力的设备。
![](https://img.linux.net.cn/data/attachment/album/202009/04/214901zsohgryndzrcs6zz.jpg)
在本文中,我将演示运行和测试一个简单的 Python 应用所需的所有的工具、软件包、步骤等等。我使用 [Flask 框架][2]来创建一个简单的 “Hello, World!” 应用,并在一个简单而强大的 Web 服务器上运行。最棒的是,这一切都发生在手机上。不需要笔记本或台式机。
学习和使用 Python 是很有趣的。由于它越来越受欢迎,有越来越多的方式可以让计算世界比现在更好。
想象一下,只需一个 Android 移动设备和开源工具,就可以构建和运行一个 Python 应用,无论是开发一个命令行工具从互联网上获取你最喜欢的文章,还是启动一个直接在掌上设备运行的网页服务器,所有这些都可以实现。这将完全改变你对移动设备的看法,将它从一个仅仅让你消费内容的设备变成一个帮助你发挥创造力的设备。
在本文中,我将演示运行和测试一个简单的 Python 应用所需的所有的工具、软件包、步骤和各种零零散散的东西。我使用 [Flask 框架][2]来创建一个简单的 “Hello, World!” 应用,并在一个简单而强大的网页服务器上运行。最棒的是,这一切都发生在手机上。不需要笔记本或台式机。
### 在 Android 上安装 Termux
@ -24,27 +26,24 @@
![Welcome to Termux][6]
安装 Termux 后,启动它并使用 Termux 的 **pkg** 命令执行一些必要的软件安装。
订阅额外的仓库 “root-repo”
安装 Termux 后,启动它并使用 Termux 的 `pkg` 命令执行一些必要的软件安装。
订阅附加仓库 `root-repo`
```
`$ pkg install root-repo`
$ pkg install root-repo
```
执行更新,使所有安装的软件达到最新状态。
```
`$ pkg update`
$ pkg update
```
最后,安装 Python
```
`$ pkg install python`
$ pkg install python
```
![Install Python][7]
@ -57,7 +56,6 @@
首先创建一个项目目录:
```
$ mkdir Source
$ cd Source
@ -65,55 +63,50 @@ $ cd Source
接下来,创建一个 Python 虚拟环境。这是 Python 开发者的常见做法,它有助于让你的 Python 项目独立于你的开发系统(在本例中是你的手机)。在你的虚拟环境中,你将能够安装特定于你应用的 Python 模块。
```
`$ python -m venv venv`
$ python -m venv venv
```
激活你的新虚拟环境(注意,开头的两个点用空格隔开)
```
$ . ./venv/bin/activate
(env)$
```
请注意你的 shell 提示符现在以 **(env)** 开头,表示你在虚拟环境中。
现在使用 **pip** 安装 Flask Python 模块。
请注意你的 shell 提示符现在以 `(env)` 开头,表示你在虚拟环境中。
现在使用 `pip` 安装 Flask Python 模块。
```
`(env) $ pip install flask`
(env) $ pip install flask
```
### 在 Android 上写 Python 代码
你已经准备好了。现在你需要为你的应用编写代码。
要做到这一点,你需要有经典文本编辑器的经验。我使用的是 **vi**。如果你不熟悉 **vi**,请安装并试用 **vimtutor**,它(如其名称所暗示的)可以教你如何使用这个编辑器。如果你有其他你喜欢的编辑器,如 **jove**、**jed**、**joe** 或 **emacs**,你可以安装并使用其中一个。
现在,由于这个演示程序非常简单,你也可以直接使用 shell 的 **heredoc** 功能,它允许你直接在提示符中输入文本。
要做到这一点,你需要有经典文本编辑器的经验。我使用的是 `vi`。如果你不熟悉 `vi`,请安装并试用 `vimtutor`,它(如其名称所暗示的)可以教你如何使用这个编辑器。如果你有其他你喜欢的编辑器,如 `jove`、`jed`、`joe` 或 `emacs`,你可以安装并使用其中一个。
现在,由于这个演示程序非常简单,你也可以直接使用 shell 的 heredoc 功能,它允许你直接在提示符中输入文本。
```
(env)$ cat << EOF >> hello_world.py
> from flask import Flask
> app = Flask(__name__)
>
> @app.route('/')
> def hello_world():
>     return 'Hello, World!'
> EOF
(env)$ cat << EOF >> hello_world.py
> from flask import Flask
> app = Flask(__name__)
>
> @app.route('/')
> def hello_world():
> return 'Hello, World!'
> EOF
(env)$
```
是仅六行的代码,但有了它,你可以导入 Flask创建一个应用并将传入流量路由到名为 **hello_world** 的函数。
只有六行代码,但有了它,你可以导入 Flask创建一个应用并将传入流量路由到名为 `hello_world` 的函数。
![Vim on Android][8]
现在你已经准备好了 Web 服务器的代码。现在是时候设置一些[环境变量][9],并在你的手机上启动一个 Web 服务器了。
现在你已经准备好了网页服务器的代码。现在是时候设置一些[环境变量][9],并在你的手机上启动一个网页服务器了。
```
(env) $ export FLASK_APP=hello_world.py
@ -125,32 +118,30 @@ $ . ./venv/bin/activate
启动应用后,你会看到这条消息:
```
`serving Flask app… running on http://127.0.0.1:5000/`
serving Flask app… running on http://127.0.0.1:5000/
```
这表明你现在在 **localhost**(也就是你的设备)上运行着一个微型 Web 服务器。该服务器正在监听来自 5000 端口的请求。
这表明你现在在 localhost也就是你的设备上运行着一个微型网页服务器。该服务器正在监听来自 5000 端口的请求。
打开你的手机浏览器并进入到 **<http://localhost:5000>**,查看你的网络应用。
打开你的手机浏览器并进入到 `http://localhost:5000`,查看你的网页应用。
![Your web app][11]
你并没有损害手机的安全性。你只运行了一个本地服务器,这意味着你的手机不接受来自外部世界的请求。只有你可以访问你的 Flask 服务器。
为了让别人看到你的服务器,你可以在**运行**命令中加入 **/\--host=0.0.0.0** 来禁用 Flask 的调试模式。这确实会打开手机上的端口,所以要谨慎使用。
为了让别人看到你的服务器,你可以在 `run` 命令中加入 `--host=0.0.0.0` 来禁用 Flask 的调试模式。这会打开你的手机上的端口,所以要谨慎使用。
```
(env) $ export FLASK_ENV=””
(env) $ flask run host=0.0.0.0
```
**Ctrl+C** 停止服务器(使用特殊的 Termux 控制键)
`Ctrl+C` 停止服务器(使用特殊的 `Termux` 键来作为 `Ctrl` 键)
### 决定下一步怎么做
你的手机可能不是一个严肃的网应用的理想服务器平台,但这个例子证明了可能性是无限的。你可能会在 Android 手机上编程,只是因为这是一种方便的实践方式,或者因为你有一个令人兴奋的本地化网应用的新想法,或者你只是碰巧使用 Flask 应用来完成自己的日常任务。正如爱因斯坦曾经说过的”想象力比知识更重要“,对于任何一个新手编码者,或者一个经验丰富的 Linux 或 Android 爱好者来说,这是一个有趣的小项目。它可以扩展到无穷的层次,所以让你的好奇心接手,并做出一些令人兴奋的东西!
你的手机可能不是一个严肃的网应用的理想服务器平台,但这个例子证明了可能性是无限的。你可能会在 Android 手机上编程,只是因为这是一种方便的实践方式,或者因为你有一个令人兴奋的本地化网应用的新想法,或者你只是碰巧使用 Flask 应用来完成自己的日常任务。正如爱因斯坦曾经说过的“想象力比知识更重要”,对于任何一个新手编码者,或者一个经验丰富的 Linux 或 Android 爱好者来说,这是一个有趣的小项目。它可以扩展到无穷的层次,所以让你的好奇心接手,并做出一些令人兴奋的东西!
--------------------------------------------------------------------------------
@ -159,7 +150,7 @@ via: https://opensource.com/article/20/8/python-android-mobile
作者:[Phani Adabala][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -1,8 +1,8 @@
[#]: collector: (lujun9972)
[#]: translator: (koolape)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-12581-1.html)
[#]: subject: (Soon Youll be Able to Convert Any Website into Desktop Application in Linux Mint)
[#]: via: (https://itsfoss.com/web-app-manager-linux-mint/)
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
@ -22,13 +22,11 @@
![][1]
在[最近的文章][2]中Linux Mint 团队揭示了正在开发一个名叫 「网页应用程序」Web App Manager的新工具。
在[最近的一篇文章][2]中Linux Mint 团队披露了正在开发一个名叫“<ruby>网页应用管理器<rt>Web App Manager</rt></ruby>的新工具。
该工具让你能够像使用桌面程序那样以独立窗口运行你最喜爱的网页。
在将网页添加为网页应用程序的时候,你可以给这个程序取名字并添加图标。也可以将它添加到不同的分类,以便在菜单中搜索这个应用。
还可以指定用什么浏览器打开应用。启用/禁用导航栏的选项也有。
在将网页添加为网页应用程序的时候,你可以给这个程序取名字并添加图标。也可以将它添加到不同的分类,以便在菜单中搜索这个应用。还可以指定用什么浏览器打开应用。启用/禁用导航栏的选项也有。
![在 Linux Mint 中添加网页应用程序][3]
@ -40,20 +38,28 @@
![YouTube 网页应用程序][5]
网页应用程序拥有常规桌面应用程序有的大多数功能特点,如使用 Alt+Tab 切换。
网页应用程序拥有常规桌面应用程序有的大多数功能特点,如使用 `Alt+Tab` 切换。
![Web App in Alt Tab Switcher][6]
![使用 Alt+Tab 切换网页应用][6]
甚至还能将应用固定到面板/任务栏方便打开。
![YouTube Web App added to the panel][7]
![添加到面板的 YouTube 网页应用][7]
该管理器目前处于 beta 开发阶段,但已经使用起来已经相对比较稳定了。不过目前还没有面向大众发放,因为翻译工作还未完成。
如果你在使用 Linux Mint 并想尝试这个工具,可在下方下载 beta 版本的 deb 文件:
[下载 beta 版][8]
- [下载 beta 版][8]
### 网页应用的好处
有读者问到这个网页应用管理器与 Chrome 和其他一些网页浏览器中已有的其他类似功能相比的好处。让我来展开一下这个话题。
- 你可以使用 URL 的特定部分example.com/tool 而不是 example.com作为应用程序。
- 添加自定义图标的可能性对于没有清晰的 favicon 的网站来说非常方便。
- 你可以使用一个没有任何扩展的轻量级浏览器来打开网页应用,而不是像 Chrome/Chromium 这样的常规网页浏览器。它的速度应该更快。
- 你的网页应用可以被整合到应用菜单中。你可以像其他应用程序一样搜索它。
### 网页应用程序在桌面环境的 Linux 中不是什么新事物
@ -74,7 +80,7 @@ via: https://itsfoss.com/web-app-manager-linux-mint/
作者:[Abhishek Prakash][a]
选题:[lujun9972][b]
译者:[koolape](https://github.com/koolape)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -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 [&lt;setting&gt;.&lt;prop&gt;]' 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&gt;
```
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&gt; 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&gt;
```
The property you want to change is `method`. Type `set method auto` and hit **Enter**:
```
nmcli ipv4&gt; 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&gt; save
Connection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully updated.
nmcli ipv4&gt;
```
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

View File

@ -0,0 +1,111 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Build a remote management console using Python and Jupyter Notebooks)
[#]: via: (https://opensource.com/article/20/9/remote-management-jupyter)
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
Build a remote management console using Python and Jupyter Notebooks
======
Turn Jupyter into a remote administration console.
![Computer laptop in space][1]
Secure shell (SSH) is a powerful tool for remote administration, but it lacks some niceties. Writing a full-fledged remote administration console sounds like it would be a lot of work. Surely, someone in the open source community has already written something?
They have, and its name is [Jupyter][2]. You might think Jupyter is one of those tools data scientists use to analyze trends in ad clicks over a week or something. This is not wrong—they do, and it is a great tool for that. But that is just scratching its surface.
### About SSH port forwarding
Sometimes, there is a server that you can SSH into over port 22. There is no reason to assume you can connect to any other port. Maybe you are SSHing through another "jumpbox" server that has more access or there are host or network firewalls that restrict ports. There are good reasons to restrict IP ranges for access, of course. SSH is a secure protocol for remote management, but allowing anyone to connect to any port is quite unnecessary.
Here is an alternative: Run a simple SSH command with port forwarding to forward a local port to a _remote_ _local_ connection. When you run an SSH port-forwarding command like `-L 8111:127.0.0.1:8888`, you are telling SSH to forward your _local_ port `8111` to what the _remote_ host thinks `127.0.0.1:8888` is. The remote host thinks `127.0.0.1` is itself.
Just like on _Sesame Street_, "here" is a subtle word.
The address `127.0.0.1` is how you spell "here" to the network.
### Learn by doing
This might sound confusing, but running this is less complicated than explaining it:
```
$ ssh -L 8111:127.0.0.1:8888 moshez@172.17.0.3
Linux 6ad096502e48 5.4.0-40-generic #44-Ubuntu SMP Tue Jun 23 00:01:04 UTC 2020 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Aug  5 22:03:25 2020 from 172.17.0.1
$ jupyter/bin/jupyter lab --ip=127.0.0.1
[I 22:04:29.771 LabApp] JupyterLab application directory is /home/moshez/jupyter/share/jupyter/lab
[I 22:04:29.773 LabApp] Serving notebooks from local directory: /home/moshez
[I 22:04:29.773 LabApp] Jupyter Notebook 6.1.1 is running at:
[I 22:04:29.773 LabApp] <http://127.0.0.1:8888/?token=df91012a36dd26a10b4724d618b2e78cb99013b36bb6a0d1>
&lt;MORE STUFF SNIPPED&gt;
```
Port-forward `8111` to `127.0.0.1` and start Jupyter on the remote host that's listening on `127.0.0.1:8888`.
Now you need to understand that Jupyter is lying. It thinks you need to connect to port `8888`, but you forwarded that to port `8111`. So, after you copy the URL to your browser, but before clicking Enter, modify the port from `8888` to `8111`:
![Jupyter remote management console][3]
(Moshe Zadka, [CC BY-SA 4.0][4])
There it is: your remote management console. As you can see, there is a "Terminal" icon at the bottom. Click it to get a terminal:
![Terminal in Jupyter remote console][5]
(Moshe Zadka, [CC BY-SA 4.0][4])
You can run a command. Creating a file will show it in the file browser on the side. You can click on that file to open it in an editor that is running locally:
![Opening a file][6]
(Moshe Zadka, [CC BY-SA 4.0][4])
You can also download, rename, or delete files:
![File options in Jupyter remote console][7]
(Moshe Zadka, [CC BY-SA 4.0][4])
Clicking on the little **Up arrow** will let you upload files. Why not upload the screenshot above?
![Uploading a screenshot][8]
(Moshe Zadka, [CC BY-SA 4.0][4])
As a nice final tidbit, Jupyter lets you view the remote images directly by double-clicking on them.
Oh, right, and if you want to do systems automation using Python, you can also use Jupyter to open a notebook.
So the next time you need to remotely manage a firewalled environment, why not use Jupyter?
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/9/remote-management-jupyter
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space)
[2]: https://jupyter.org/
[3]: https://opensource.com/sites/default/files/uploads/output_1_0.png (Jupyter remote management console)
[4]: https://creativecommons.org/licenses/by-sa/4.0/
[5]: https://opensource.com/sites/default/files/uploads/output_3_0.png (Terminal in Jupyter remote console)
[6]: https://opensource.com/sites/default/files/uploads/output_5_0.png (Opening a file)
[7]: https://opensource.com/sites/default/files/uploads/output_7_0.png (File options in Jupyter remote console)
[8]: https://opensource.com/sites/default/files/uploads/output_9_0.png (Uploading a screenshot)

View File

@ -0,0 +1,229 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Managing a non-profit organization's supply chain with Groovy)
[#]: via: (https://opensource.com/article/20/9/groovy)
[#]: author: (Chris Hermansen https://opensource.com/users/clhermansen)
Managing a non-profit organization's supply chain with Groovy
======
Let's use Groovy to solve a charity's distribution problem.
![Jars with food inside on a shelf][1]
There are many reasons I'm a big fan of [Java][2], but perhaps most of all, because of the particular combo of static typing and object-orientedness that imbues its design. However, when I need a quick solution, especially to a "solve it and forget it" problem dealing with data, I usually reach for [Groovy][3] (or sometimes [Python][4]) instead, especially if the library that addresses my problem exists and is well-documented. Sometimes even [awk][5] will do. But I keep meaning to start using [Julia][6] more, and then there's [Go][7].
Every so often, I run across a different kind of problem, and when it is sufficiently compact, sometimes I will solve it in a few languages, just to learn more about how each addresses the problem.
Recently, a non-programmer colleague introduced me to just such a problem. It goes like this:
> Many people living in community XYZ struggle to make ends meet on a daily basis. Employment opportunities in the community are limited and tend to be low-paying. The cost of living is comparatively high: water, electricity, and healthcare are expensive. Post-secondary education, whether academic or technical, means moving to the nearest city. On the plus side, the community is small and close-knit. People help each other out as much as their circumstances permit.
>
> COVID-19 has hit this community hard in the economic sense. Although there haven't been any infections yet, the two main employers in the town are facing financial ruin and have laid off almost all of their workers. The government has helped out, but the amount of help is not enough for the families struggling the hardest.
>
> A local branch of a national charity has received some funding to provide support to families in need. Seeking to stretch this funding as much as possible, the charity arranges to buy bulk lots of food and household supplies, then break up the bulk lots into family hampers of approximately equal monetary value. Their question is, how to do so?
My colleague thought that perhaps I could help him with a spreadsheet to handle the distribution. However, to me, this seemed to be the perfect little problem to solve with a small program. What might the steps be?
1. Unpack the bulk packages into their individual units.
2. While there are still units left:
1. Grab a fresh hamper.
2. Set the hamper value to zero.
3. While the hamper value is less than the ideal hamper value and there are still units left:
1. Pick a unit at random.
2. If that unit isn't in the hamper and if the hamper value wouldn't be too high by adding it:
1. Move the unit to the hamper.
2. Increment the hamper value by the unit price.
That seems like a good first approximation. It also seems like the perfect small algorithm to implement in Groovy.
### The Groovy solution
In Java, I find myself declaring utility classes to hold tuples of data (the new record feature is going to be great for that). In Groovy, I tend to use the language support for maps. Let's use a list of maps to hold the bulk items picked up from the wholesaler:
```
def packs = [
    [item:'Rice',brand:'Best Family',units:10,price:5650,quantity:1],
    [item:'Spaghetti',brand:'Best Family',units:1,price:327,quantity:10],
    [item:'Sardines',brand:'Fresh Caught',units:3,price:2727,quantity:3],
    [item:'Chickpeas',brand:'Southern Style',units:2,price:2600,quantity:5],
    [item:'Lentils',brand:'Southern Style',units:2,price:2378,quantity:5],
    [item:'Vegetable oil',brand:'Crafco',units:12,price:10020,quantity:1],
    [item:'UHT milk',brand:'Atlantic',units:6,price:4560,quantity:2],
    [item:'Flour',brand:'Neighbor Mills',units:10,price:5200,quantity:1],
    [item:'Tomato sauce',brand:'Best Family',units:1,price:190,quantity:10],
    [item:'Sugar',brand:'Good Price',units:1,price:565,quantity:10],
    [item:'Tea',brand:'Superior',units:5,price:2720,quantity:2],
    [item:'Coffee',brand:'Colombia Select',units:2,price:4180,quantity:5],
    [item:'Tofu',brand:'Gourmet Choice',units:1,price:1580,quantity:10],
    [item:'Bleach',brand:'Blanchite',units:5,price:3550,quantity:2],
    [item:'Soap',brand:'Sunny Day',units:6,price:1794,quantity:2]]
```
There is one bulk pack of 10 bags of rice and 10 bulk packs with one bag each of spaghetti. In the above, the variable `packs` is set to a list (actually a Java `ArrayList` underneath) of maps (actually a Java `HashMap` underneath). Because Groovy is dynamically typed (by default, anyway), I use `def` to declare the `packs` variable and am happy to have both `String` and `Integer` values in my maps.
And yes, those prices do look a bit strange, but this problem happened in a place with a different currency.
The next step is to unpack these bulk packages. Unpacking the single bulk package of rice yields 10 units of rice; that is, the total number of units yielded is `units * quantity`. Groovy provides a handy function called `collectMany` that can be used to flatten lists of lists, so the code to carry out the unpacking is really straightforward:
```
def units = packs.collectMany { pack -&gt;
    [[item:pack.item, brand:pack.brand, price:(pack.price / pack.units)]] *
                (pack.units * pack.quantity)
}
```
Note that `collectMany` takes a `Closure` as its argument; so this is a kind of locally declared function with a single parameter, `pack`, that returns a list of (`units * quantity`) maps, with each map including the item, brand, and calculated unit price from the corresponding bulk pack. Of note here is that the Groovy multiply operator (`*`) with a list on the left side and a number (`N`) on the right will produce a list with the original items replicated in order `N` times.
The final step is to repack the units into the hampers for distribution. But first, I need to get a bit more specific about the ideal hamper value, and I might as well not be overly restrictive when there are just a few units left:
```
def valueIdeal = 5000
def valueMax = valueIdeal * 1.1
```
OK! Let's repack the hampers:
```
def rnd = new [Random][8]()
def hamperNumber = 0    // [1]
while (units.size()) {  // [2]
    hamperNumber++
    def hamper = []
    def value = 0       // [2.1]
    for (boolean canAdd = true; canAdd; ) {        // [2.2]
        int u = rnd.nextInt(units.size())          // [2.2.1]
        canAdd = false                             // [2.2.2]
        for (int o = 0; o &lt; units.size(); o++) {   // [2.2.3]
            int uo = (u + o) % units.size()
            def unit = units[uo]                   // [2.2.3.1]
            if (units.size() &lt; 3 ||
                        !(unit in hamper) &amp;&amp;
                        (value + unit.price) &lt; valueMax) { // [2.2.3.2]
                hamper.add(unit)
                value += unit.price
                units.remove(uo)                   // [2.2.3.3]
                canAdd = units.size() &gt; 0
                break                              // [2.2.3.4]
            }
        }                                          // [2.2.4]
    }
    println ""
    println "Hamper $hamperNumber value $value:"
    hamper.each { item -&gt;
        printf "%-25s%-25s%7.2f\n",item.item,item.brand,item.price
    }                                                                   // [2.3]
    println "Remaining units ${units.size()} average price = $avgPrice" // [2.4]
}
```
Some clarification, with numbers in brackets in the comments above (e.g., _[1]_) corresponding to the clarifications below:
* 1\. Initialize Groovy's random number generator and the hamper number.
* 2\. This `while {}` loop will redistribute units into hampers as long as there are more available:
* 2.1 Increment the hamper number, get a new empty hamper (a list of units), and set its value to 0.
* 2.2 This `for {}` loop will add as many units to the hamper as possible:
* 2.2.1 Get a random number between zero and the number of remaining units minus 1.
* 2.2.2 Assume you can't find more units to add.
* 2.2.3 This `for {}` loop, starting at the randomly chosen index, will try to find a unit that can be added to the hamper.
* 2.2.3.1 Figure out which unit to look at.
* 2.2.3.2 Add this unit to the hamper if there are only a few left or if the value of the hamper isn't too high once the unit is added.
* 2.2.3.3 Add the unit to the hamper, increment the hamper value by the unit price, and remove the unit from the available units list.
* 2.2.3.4 As long as there are units left, you can add more, so break out of this loop to keep looking.
* 2.2.4 On exit from this `for {}` loop, if you inspected every remaining unit and could not find one to add to the hamper, the hamper is complete; otherwise, you found one and can continue looking for more.
* 2.3 Print out the contents of the hamper.
* 2.4 Print out the remaining units info.
When you run this code, the output looks like:
```
Hamper 1 value 5414:
Vegetable oil            Crafco                    835.00
Coffee                   Colombia Select          2090.00
Tofu                     Gourmet Choice           1580.00
Sardines                 Fresh Caught              909.00
Remaing units 151
Hamper 2 value 5309:
Flour                    Neighbor Mills            520.00
Sugar                    Good Price                565.00
Vegetable oil            Crafco                    835.00
Coffee                   Colombia Select          2090.00
Rice                     Best Family               565.00
Tomato sauce             Best Family               190.00
Tea                      Superior                  544.00
Remaing units 144
Hamper 3 value 5395:
Flour                    Neighbor Mills            520.00
UHT milk                 Atlantic                  760.00
Tomato sauce             Best Family               190.00
Tofu                     Gourmet Choice           1580.00
Spaghetti                Best Family               327.00
Sugar                    Good Price                565.00
Sardines                 Fresh Caught              909.00
Tea                      Superior                  544.00
Remaing units 136
Hamper 23 value 5148:
Flour                    Neighbor Mills            520.00
Tea                      Superior                  544.00
Chickpeas                Southern Style           1300.00
Lentils                  Southern Style           1189.00
Vegetable oil            Crafco                    835.00
UHT milk                 Atlantic                  760.00
Remaing units 3
Hamper 24 value 3955:
Chickpeas                Southern Style           1300.00
Sugar                    Good Price                565.00
Coffee                   Colombia Select          2090.00
Remaing units 0
```
The last hamper is abbreviated in contents and value.
### Closing thoughts
Note there is some fiddly business about being able to add units to the hamper. Basically, you pick a random position in the list of units and, starting at that position, iterate through the list until you either find a unit whose price allows it to be included or until you exhaust the list. Also, when there are only a few items left, you just toss them into the last hamper.
Another issue worth mentioning: This isn't a particularly efficient approach. Removing elements from `ArrayLists`, letting Groovy use its default `BigDecimal`, and a few other things make this less suitable for a huge redistribution problem. Still, it runs quite rapidly on my aging dual-core machine.
And one final thought—using `while { … }` and `for { … }`? Really? Not some cool functional code? Afraid so. I couldn't think of a way to use map and reduce style closures in Groovy in collaboration with a random selection of units for repackaging. Can you?
In another article article, I'll solve this in Python, and future articles will do it in Java, Julia, and Go.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/9/groovy
作者:[Chris Hermansen][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/clhermansen
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/OSDC_secret_ingredient_520x292.png?itok=QbKzJq-N (Jars with food inside on a shelf)
[2]: https://www.java.com/en/
[3]: https://groovy-lang.org/
[4]: https://www.python.org/
[5]: https://www.gnu.org/software/gawk/manual/gawk.html
[6]: https://julialang.org/
[7]: https://golang.org/
[8]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+random

View File

@ -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