translated

This commit is contained in:
LuMing 2018-10-01 00:54:55 +08:00
parent fb3dc29640
commit e998a73575
2 changed files with 215 additions and 230 deletions

View File

@ -1,230 +0,0 @@
LuuMing translating
How to Use the Netplan Network Configuration Tool on Linux
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/netplan.jpg?itok=Gu_ZfNGa)
For years Linux admins and users have configured their network interfaces in the same way. For instance, if youre a Ubuntu user, you could either configure the network connection via the desktop GUI or from within the /etc/network/interfaces file. The configuration was incredibly easy and never failed to work. The configuration within that file looked something like this:
```
auto enp10s0
iface enp10s0 inet static
address 192.168.1.162
netmask 255.255.255.0
gateway 192.168.1.100
dns-nameservers 1.0.0.1,1.1.1.1
```
Save and close that file. Restart networking with the command:
```
sudo systemctl restart networking
```
Or, if youre not using a non-systemd distribution, you could restart networking the old fashioned way like so:
```
sudo /etc/init.d/networking restart
```
Your network will restart and the newly configured interface is good to go.
Thats how its been done for years. Until now. With certain distributions (such as Ubuntu Linux 18.04), the configuration and control of networking has changed considerably. Instead of that interfaces file and using the /etc/init.d/networking script, we now turn to [Netplan][1]. Netplan is a command line utility for the configuration of networking on certain Linux distributions. Netplan uses YAML description files to configure network interfaces and, from those descriptions, will generate the necessary configuration options for any given renderer tool.
I want to show you how to use Netplan on Linux, to configure a static IP address and a DHCP address. Ill be demonstrating on Ubuntu Server 18.04. I will give you one word of warning, the .yaml files you create for Netplan must be consistent in spacing, otherwise theyll fail to work. You dont have to use a specific spacing for each line, it just has to remain consistent.
### The new configuration files
Open a terminal window (or log into your Ubuntu Server via SSH). You will find the new configuration files for Netplan in the /etc/netplan directory. Change into that directory with the command cd /etc/netplan. Once in that directory, you will probably only see a single file:
```
01-netcfg.yaml
```
You can create a new file or edit the default. If you opt to edit the default, I suggest making a copy with the command:
```
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak
```
With your backup in place, youre ready to configure.
### Network Device Name
Before you configure your static IP address, youll need to know the name of device to be configured. To do that, you can issue the command ip a and find out which device is to be used (Figure 1).
![netplan][3]
Figure 1: Finding our device name with the ip a command.
[Used with permission][4]
Ill be configuring ens5 for a static IP address.
### Configuring a Static IP Address
Open the original .yaml file for editing with the command:
```
sudo nano /etc/netplan/01-netcfg.yaml
```
The layout of the file looks like this:
network:
Version: 2
Renderer: networkd
ethernets:
DEVICE_NAME:
Dhcp4: yes/no
Addresses: [IP/NETMASK]
Gateway: GATEWAY
Nameservers:
Addresses: [NAMESERVER, NAMESERVER]
Where:
* DEVICE_NAME is the actual device name to be configured.
* yes/no is an option to enable or disable dhcp4.
* IP is the IP address for the device.
* NETMASK is the netmask for the IP address.
* GATEWAY is the address for your gateway.
* NAMESERVER is the comma-separated list of DNS nameservers.
Heres a sample .yaml file:
```
network:
version: 2
renderer: networkd
ethernets:
ens5:
dhcp4: no
addresses: [192.168.1.230/24]
gateway4: 192.168.1.254
nameservers:
addresses: [8.8.4.4,8.8.8.8]
```
Edit the above to fit your networking needs. Save and close that file.
Notice the netmask is no longer configured in the form 255.255.255.0. Instead, the netmask is added to the IP address.
### Testing the Configuration
Before we apply the change, lets test the configuration. To do that, issue the command:
```
sudo netplan try
```
The above command will validate the configuration before applying it. If it succeeds, you will see Configuration accepted. In other words, Netplan will attempt to apply the new settings to a running system. Should the new configuration file fail, Netplan will automatically revert to the previous working configuration. Should the new configuration work, it will be applied.
### Applying the New Configuration
If you are certain of your configuration file, you can skip the try option and go directly to applying the new options. The command for this is:
```
sudo netplan apply
```
At this point, you can issue the command ip a to see that your new address configurations are in place.
### Configuring DHCP
Although you probably wont be configuring your server for DHCP, its always good to know how to do this. For example, you might not know what static IP addresses are currently available on your network. You could configure the device for DHCP, get an IP address, and then reconfigure that address as static.
To use DHCP with Netplan, the configuration file would look something like this:
```
network:
version: 2
renderer: networkd
ethernets:
ens5:
Addresses: []
dhcp4: true
optional: true
```
Save and close that file. Test the file with:
```
sudo netplan try
```
Netplan should succeed and apply the DHCP configuration. You could then issue the ip a command, get the dynamically assigned address, and then reconfigure a static address. Or, you could leave it set to use DHCP (but seeing as how this is a server, you probably wont want to do that).
Should you have more than one interface, you could name the second .yaml configuration file 02-netcfg.yaml. Netplan will apply the configuration files in numerical order, so 01 will be applied before 02. Create as many configuration files as needed for your server.
### Thats All There Is
Believe it or not, thats all there is to using Netplan. Although it is a significant change to how were accustomed to configuring network addresses, its not all that hard to get used to. But this style of configuration is here to stay… so you will need to get used to it.
Learn more about Linux through the free ["Introduction to Linux" ][5]course from The Linux Foundation and edX.
--------------------------------------------------------------------------------
via: https://www.linux.com/learn/intro-to-linux/2018/9/how-use-netplan-network-configuration-tool-linux
作者:[Jack Wallen][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/jlwallen
[1]: https://netplan.io/
[3]: https://www.linux.com/sites/lcom/files/styles/rendered_file/public/netplan_1.jpg?itok=XuIsXWbV (netplan)
[4]: /licenses/category/used-permission
[5]: https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux

View File

@ -0,0 +1,215 @@
如何在 Linux 上使用网络配置工具 Netplan
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/netplan.jpg?itok=Gu_ZfNGa)
多年以来 Linux 管理员和用户们使用相同的方式配置他们的网络接口。例如,如果你是 Ubuntu 用户,你能够用桌面 GUI 配置网络连接,也可以在 /etc/network/interfaces 文件里配置。配置相当简单且从未失败。在文件中配置看起来就像这样:
```
auto enp10s0
iface enp10s0 inet static
address 192.168.1.162
netmask 255.255.255.0
gateway 192.168.1.100
dns-nameservers 1.0.0.1,1.1.1.1
```
保存并关闭文件。使用命令重启网络:
```
sudo systemctl restart networking
```
或者如果你使用不带systemd 的发行版,你可以通过老办法来重启网络:
```
sudo /etc/init.d/networking restart
```
你的网络将会重新启动,新的配置将会生效。
这就是多年以来的做法。但是现在,在某些发行版上(例如 Ubuntu Linux 18.04),网络的配置与控制发生了很大的变化。不需要那个 interfaces 文件和 /etc/init.d/networking 脚本,我们现在转向使用 [Netplan][1]。Netplan 是一个在某些 Linux 发行版上配置网络连接的命令行工具。Netplan 使用 YAML 描述文件来配置网络接口,然后,通过这些描述为任何给定的呈现工具生成必要的配置选项。
我将向你展示如何在 Linux 上使用 Netplan 配置静态 IP 地址和 DHCP 地址。我会在 Ubuntu Server 18.04 上演示。有句忠告,你创建的 .yaml 文件中的间距必须保持一致,否则将会失败。你不用为每行使用特定的间距,只需保持一致就行了。
### 新的配置文件
打开终端窗口(或者通过 SSH 登录进 Ubuntu 服务器)。你会在 /etc/netplan 文件夹下发现 Netplan 的新配置文件。使用 cd/etc/netplan 命令进入到那个文件夹下。一旦进到了那个文件夹,也许你就能够看到一个文件:
```
01-netcfg.yaml
```
你可以创建一个新的文件或者是编辑默认文件。如果你打算修改默认文件,我建议你先做一个备份:
```
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak
```
备份好后,就可以开始配置了。
### 网络设备名称
在你开始配置静态 IP 之前,你需要知道设备名称。要做到这一点,你可以使用命令 ip a然后找出哪一个设备将会被用到图 1
![netplan][3]
图 1使用 ip a 命令找出设备名称
[Used with permission][4] (译注:这是什么鬼?)
我将为 ens5 配置一个静态的 IP。
### 配置静态 IP 地址
使用命令打开原来的 .yaml 文件:
```
sudo nano /etc/netplan/01-netcfg.yaml
```
文件的布局看起来就像这样:
network:
Version: 2
Renderer: networkd
ethernets:
DEVICE_NAME:
Dhcp4: yes/no
Addresses: [IP/NETMASK]
Gateway: GATEWAY
Nameservers:
Addresses: [NAMESERVER, NAMESERVER]
其中:
* DEVICE_NAME 是需要配置设备的实际名称。
* yes/no 代表是否启用 dhcp4。
* IP 是设备的 IP 地址。
* NETMASK 是 IP 地址的掩码。
* GATEWAY 是网关的地址。
* NAMESERVER 是由逗号分开的 DNS 服务器列表。
这是一份 .yaml 文件的样例:
```
network:
version: 2
renderer: networkd
ethernets:
ens5:
dhcp4: no
addresses: [192.168.1.230/24]
gateway4: 192.168.1.254
nameservers:
addresses: [8.8.4.4,8.8.8.8]
```
编辑上面的文件以达到你想要的效果。保存并关闭文件。
注意,掩码已经不用再配置为 255.255.255.0 这种形式。取而代之的是,掩码已被添加进了 IP 地址中。
### 测试配置
在应用改变之前,让我们测试一下配置。为此,使用命令:
```
sudo netplan try
```
上面的命令会在应用配置之前验证其是否有效。如果成功你就会看到配置被接受。换句话说Netplan 会尝试将新的配置应用到运行的系统上。如果新的配置失败了Netplan 会自动地恢复到之前使用的配置。成功后,新的配置就会被使用。
### 应用新的配置
如果你确信配置文件没有问题,你就可以跳过测试环节并且直接使用新的配置。它的命令是:
```
sudo netplan apply
```
此时,你可以使用 ip a 看看新的地址是否正确。
### 配置 DHCP
虽然你可能不会配置 DHCP 服务,但通常还是知道比较好。例如,你也许不知道网络上当前可用的静态 IP 地址是多少。你可以为设备配置 DHCP获取到 IP 地址,然后将那个地址重新配置为静态地址。
在 Netplan 上使用 DHCP配置文件看起来就像这样
```
network:
version: 2
renderer: networkd
ethernets:
ens5:
Addresses: []
dhcp4: true
optional: true
```
保存并退出。用下面命令来测试文件:
```
sudo netplan try
```
Netplan 应该会成功配置 DHCP 服务。这时你可以使用 ip a 命令得到动态分配的地址,然后重新配置静态地址。或者,你可以直接使用 DHCP 分配的地址(但看看这是一个服务器,你可能不想这样做)。
也许你有不只一个的网络接口,你可以命名第二个 .yaml 文件为 02-netcfg.yaml 。Netplan 会按照数字顺序应用配置文件,因此 01 会在 02 之前使用。根据你的需要创建多个配置文件。
### 就是这些了
不管你信不信,那些就是所有关于使用 Netplan 的东西了。虽然它对于我们习惯性的配置网络地址来说是一个相当大的改变,但并不是所有人都用的惯。但这种配置方式值得一提...因此你会适应的。
在 Linux Foundation 和 edX 上通过 ["Introduction to Linux"] 课程学习更多关于 Linux 的内容。
--------------------------------------------------------------------------------
via: https://www.linux.com/learn/intro-to-linux/2018/9/how-use-netplan-network-configuration-tool-linux
作者:[Jack Wallen][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[LuuMing](https://github.com/LuuMing)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/jlwallen
[1]: https://netplan.io/
[3]: https://www.linux.com/sites/lcom/files/styles/rendered_file/public/netplan_1.jpg?itok=XuIsXWbV (netplan)
[4]: /licenses/category/used-permission
[5]: https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux