Error in configuring static IP in Ubuntu 18.04

Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>

Note:

Because many test environments are installed on this Ubuntu system, but due to the restart and power failure of the virtual server, every time IP changes, the environment has to be updated, so the IP of the environment needs to be configured as static, once and for all

According to the previous experience of configuring Ubuntu static IP, VI/etc/network/interfaces directly configures the static IP under this file. After resetting the network, it is found that it does not take effect

So I checked the version of Ubuntu, which is 18.04. Then Baidu found that “since 17.10, Ubuntu has given up the configuration of fixed IP in/etc/network/interfaces. Even if the configuration does not take effect, it will be changed to the netplan mode, and the configuration is written in/etc/netplan/01-netcfg.yaml or similar yaml file”. So I configured according to the found method, As a result, I encountered a lot of errors and almost vomited blood. Here I write down the errors I encountered

1. The initial configuration is as follows: invalid yaml at// etc/netplan/01-netcfg.yaml line 11 column 11: mapping values are not allowed in this context

Solution: yaml is a hierarchical structure, which needs to be indented. Colon (:) represents dictionary, hyphen (-) represents list, and there must be a space after colon

2. After adding a space after the colon (as shown in the figure below), execute netplan apply and report an error again: invalid yaml at// etc/netplan/01-netcfg.yaml line 11 column 6: did not find expected key

Solution: nameservers should be the fourth layer like gateway 4, and addresses after nameservers should be the fifth layer

The correct number of layers is as follows:

First floor –

network:

Layer 2 – Ethernet:

The third layer – ens33:

The fourth layer addresses: [172.18.0.31/24]

The fourth layer gateway4: 172.18.0.1
0

The fourth layer – nameservers:

The fifth floor

addresses: [114.114.114.114, 8.8.8.8]

3. After solving the problem according to the above method, execute netplan apply as shown in the figure below, and report an error again: error in network definition// etc/netplan/01-netcfg.yaml line 8 column 16: expected scalar

Solution: the address of gateway4 has no brackets. After modification, restart the network service and it will be normal (netplan apply)

So the correct configuration format should be as follows: in this way, the static IP is successfully configured

I didn’t get the wrong report:

A similar error appears: Line8 column 6: cloud not find expected ‘:’ # the prompt is colon: there is no space after it

Similar error occurred:

Netplan found character that cannot start any token, the prompt is that the configuration document is not written according to five levels, and the next level must be one or more spaces than the previous level.

In short: here restart the network times wrong, mostly the level is wrong, wrong format, no space

Similar Posts: