Foreword
The method of configuring the IP address in Ubuntu 18.04 LTS is very different from the previous configuration method. The difference from the old version is that Ubuntu 18.04 uses Netplan to configure the IP address. Netplan is a new command line network configuration tool. In fact, Ubuntu developers have already introduced Netplan in Ubuntu 17.10. The new IP configuration method to be introduced next will no longer use the /etc/network/interfaces file, instead it will be a YAML file. The default Netplan configuration file is generally in the /etc/netplan directory.
In this tutorial, we will learn to configure static IP and dynamic IP in the minimal server of Ubuntu 18.04 LTS.
Configure static IP address in Ubuntu 18.04 LTS
First find out where Netplan's default network configuration file is located:
$ ls /etc/netplan/50-cloud-init.yaml
We can see that the default network configuration file is 50-cloud-init.yaml, which is a YAML file.
Then we look at what the content of this file is:
$ cat /etc/netplan/50-cloud-init.yaml
When I installed Ubuntu 18.04 before, I had already done the relevant configuration of the network card in order to obtain the IP address from the DHCP server, so the detailed configuration directly looks at the following figure:
You can see that there are two network cards here, enp0s3 and enp0s8, and both of them are configured to obtain IP from the DHCP server.
Now we configure the two network cards as static IP addresses, first use any editor to edit the configuration file.
$ sudo nano /etc/netplan/50-cloud-init.yaml
Next we add IP address, subnet mask, gateway, DNS server and other configurations respectively. Use 192.168.225.50 as the IP address of the network card enp0s3, 192.168.225.51 as the IP address of the network card enp0s8, 192.168.225.1 as the gateway address, and 255.255.255.0 as the subnet mask. Then use the two DNS server IPs 8.8.8.8 and 8.8.4.4.
One thing to note is that in Ubuntu 18.04, each line of this configuration file must be indented by spaces and cannot be replaced by TAB, otherwise the configuration will not work. As shown in the figure above, the indentation of each line in the configuration file is achieved by the space bar.
At the same time, in Ubuntu 18.04, when we define the subnet mask, we do not divide the IP and subnet mask into two configurations like the old version. In the old version of Ubuntu, we generally configure the IP and subnet mask like this:
address =192.168.225.50
netmask =255.255.255.0
In netplan, we combine these two into one, like this:
addresses :[192.168.225.50/24]
Save and close the configuration file after the configuration is complete. Then use the following command to apply the configuration just now:
$ sudo netplan apply
If there is a problem when applying the configuration, you can use the following command to check what went wrong with the configuration just now.
$ sudo netplan --debug apply
This command will output these debug information:
**( generate:1556): DEBUG:09:14:47.220: Processing input file //etc/netplan/50-cloud-init.yaml..**(generate:1556): DEBUG:09:14:47.221: starting newprocessing pass
**( generate:1556): DEBUG:09:14:47.221: enp0s8: setting default backend to 1**(generate:1556): DEBUG:09:14:47.222: enp0s3: setting default backend to 1**(generate:1556): DEBUG:09:14:47.222: Generating output files..**(generate:1556): DEBUG:09:14:47.223: NetworkManager: definition enp0s8 is not forus(backend 1)**(generate:1556): DEBUG:09:14:47.223: NetworkManager: definition enp0s3 is not forus(backend 1)
DEBUG:netplan generated networkd configuration exists, restarting networkd
DEBUG:no netplan generated NM configuration exists
DEBUG:device enp0s3 operstate is up, not replugging
DEBUG:netplan triggering .link rules for enp0s3
DEBUG:device lo operstate is unknown, not replugging
DEBUG:netplan triggering .link rules for lo
DEBUG:device enp0s8 operstate is up, not replugging
DEBUG:netplan triggering .link rules for enp0s8
If the configuration is normal and effective, we can use the following command to check the ip:
$ ip addr
After the configuration is completed in my Ubuntu 18.04, the output of the command output is as follows:
So far, we have successfully completed the static IP configuration with Netplan in Ubuntu 18.04 LTS.
More information about Netplan can be found in the manual with the man command:
$ man netplan
Configure dynamic IP address in Ubuntu 18.04 LTS
In fact, the initial configuration in the configuration file is the configuration of dynamic IP, so you don't need to do any configuration operations if you want to use dynamic IP. If you have configured a static IP address and want to restore the previous dynamic IP configuration, just delete the relevant configuration items added in the static IP configuration above, and restore the entire configuration file to the form shown in Figure 1 above. .
Now you have learned to configure static and dynamic IP addresses in Ubuntu 18.04. Personally, I don't really like this method. The old configuration method is simpler. what do y'all think?
to sum up
The above is the entire content of this article. I hope that the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message and exchange. Thank you for your support to ZaLou.Cn.
Recommended Posts