Complete steps to configure IP address in Ubuntu 18.04 LTS

Complete steps to configure IP address in Ubuntu 18.04 LTS

Preface

The method of configuring IP addresses in Ubuntu 18.04 LTS is very different from the configuration methods used in the past. The difference from the previous versions is that Ubuntu 18.04 uses Netplan to configure IP addresses. Netplan is a new command line network configuration tool. In fact, Ubuntu developers had 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, but will use a YAML file instead. The default Netplan configuration file is usually in the /etc/netplan directory.

In this tutorial, we will learn to configure static and dynamic IP in a minimal server on Ubuntu 18.04 LTS.

Configure Static IP Address in Ubuntu 18.04 LTS

First find the location of Netplan's default network configuration file:

$ 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 let's take a look at the contents of this file:

$ cat /etc/netplan/50-cloud-init.yaml

When I installed Ubuntu 18.04, I had already configured the network card to obtain an IP address from the DHCP server, so please see the following figure for detailed configuration:

You can see that there are two network cards, enp0s3 and enp0s8, and both of them are configured to obtain IP from the DHCP server.

Now we configure both network cards with static IP addresses. First, edit the configuration file using any editor.

$ sudo nano /etc/netplan/50-cloud-init.yaml

Next, we add the IP address, subnet mask, gateway, DNS server and other configurations. Use 192.168.225.50 as the IP address of network card enp0s3, 192.168.225.51 as the IP address of 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 with 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 pressing the space bar.

At the same time, in Ubuntu 18.04, when we define the subnet mask, we do not separate the IP and subnet mask into two configurations like the old version. In older versions of Ubuntu, we usually configure the IP and subnet mask like this:

address = 192.168.225.50
netmask = 255.255.255.0

In netplan, we combine these two items into one, like this:

addresses : [192.168.225.50/24]

After the configuration is complete, save and close the configuration file. Then use the following command to apply the configuration:

$ sudo netplan apply

If there is a problem when applying the configuration, you can use the following command to check what is wrong with the configuration just now.

$ sudo netplan --debug apply

This command line will output the following 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 new processing 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 for us (backend 1)
** (generate:1556): DEBUG: 09:14:47.223: NetworkManager: definition enp0s3 is not for us (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 configuring in my Ubuntu 18.04, the command output is as follows:

So far, we have successfully completed the configuration of static IP using Netplan in Ubuntu 18.04 LTS.

More information about Netplan can be found in the manual page using 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 the dynamic IP, so if you want to use the dynamic IP, you don’t need to do any configuration operations. If you have already configured a static IP address and want to restore the previous dynamic IP configuration, just delete the relevant configuration items added in the above static IP configuration and restore the entire configuration file to the one shown in Figure 1 above.

Now you have learned how to configure static and dynamic IP addresses in Ubuntu 18.04. Personally, I don't really like this method, the old configuration method is much simpler. What do you think?

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed installation and use of SSH in Ubuntu environment
  • How to use cutecom for serial communication in Ubuntu virtual machine
  • Ubuntu basic settings: installation and use of openssh-server
  • Installation of Docker CE on Ubuntu
  • Upgrading python 3.7.1 process notes under Ubuntu (recommended)
  • Android's implementation method of executing shell scripts in the Linux terminal to directly print the log of the currently running app
  • How to remotely batch execute Linux command programs using pyqt
  • Use of Zabbix Api in Linux shell environment
  • How to recover accidentally deleted messages files in Linux
  • Detailed installation and configuration of hadoop2.7.2 under ubuntu15.10

<<:  Practical example of Vue virtual list

>>:  MySQL Basic Tutorial Part 1 MySQL5.7.18 Installation and Connection Tutorial

Recommend

Analysis of three parameters of MySQL replication problem

Table of contents 01 sql_slave_skip_counter param...

Node.js+express message board function implementation example

Table of contents Message Board Required librarie...

Mobile terminal adaptation makes px automatically converted to rem

Install postcss-pxtorem first: npm install postcs...

Css3 realizes seamless scrolling and anti-shake

question The seamless scrolling of pictures and t...

VSCode configuration Git method steps

Git is integrated in vscode, and many operations ...

Summary of react basics

Table of contents Preface start React Lifecycle R...

Several methods of implementing carousel images in JS

Carousel The main idea is: In the large container...

CSS implements five common 2D transformations

2D transformations in CSS allow us to perform som...

Detailed explanation of the Sidecar mode in Docker Compose

Table of contents What is Docker Compose Requirem...

Detailed explanation of HTML table inline format

Inline format <colgroup>...</colgroup>...

8 powerful techniques for HTML web page creation

<br />Although there are many web page creat...