How to set static IP for Ubuntu 18.04 Server

How to set static IP for Ubuntu 18.04 Server

1. Background

Netplan is a new command-line network configuration utility introduced in Ubuntu 17.10 for easily managing and configuring network settings in Ubuntu systems. It allows you to configure network interfaces using the YAML abstraction. It works with NetworkManager and the systemd-networkd network daemon (called renderers, you can choose which one to use) as an interface to the kernel.

It reads the network configuration described in /etc/netplan/*.ymal and can store the configuration of all network interfaces in these files.

In this article. We will explain how to configure a network static or dynamic IP address for a network interface in Ubuntu 18.04 using the Netplan utility.

2. Solution

List all active network interfaces on Ubuntu

First, you need to determine which network interface you want to configure. You can use ifconfig command to list all connected network interfaces in your system as shown.

ifconfig -a

Checking Network Interfaces in Ubuntu

From the output of the above command, we have 2 interfaces connected to our Ubuntu system: 1 Ethernet interface and the loopback interface.

Ubuntu sets a static IP address

In this example, we will configure the ens33 Ethernet network interface with a static IP. As shown, use vim to open the netplain configuration file.

Important: If the YAML file was not created by the distribution installer, you can use this command to generate the required configuration for the renderer.

sudo netplan generate

Additionally, the auto-generated files may have different file names on your desktop, server, cloud instance, etc. (e.g. 01-network-manager-all.ymal or 01-netcfg.yaml), but all files under /etc/netplan/*.yaml will be read by netplan.

sudo vim /etc/netplan/xxxx.ymal

Then add the following configuration in the ethernet section.

network:
  ethernets:
    ens33:
      addresses:
      - 192.168.4.254/24
      dhcp4: false
      gateway4: 192.168.4.2
      nameservers:
        addresses:
        - 8.8.8.8
        search: []
  version: 2

illustrate:

•ens33: Network interface name
•dhcp4: Receive dhcp properties of IPV4 interface
•dhcp6: Receive dhcp properties of IPV6 interface
•addresses: static address sequence of the interface
•gateway4: The IPv4 address of the default gateway
•Nameservers: DNS server address, separated by ,

Once added, your configuration file should have the following contents as shown in the following screenshot.

The address property of an interface expects a sequence entry such as [192.168.4.254/24,"20001:1::1/64"] or [192.168.1.254/24,] (refer to the netplan man page for more information).

Configure Static IP in Ubuntu

Save the file and exit. Then apply the recent network changes using the following netplan command.

sudo netplan apply

Now verify all available network interfaces again. The ens33 Ethernet interface should now be connected to the local network and have an IP address as shown in the following screenshot.

ifconfig -a

Verify Network Interface in Ubuntu

Ubuntu sets a dynamic IP address

To configure the ens33 Ethernet interface to receive an IP address dynamically via DHCP, simply use the following configuration.

network:
  ethernets:
    ens33:
      dhcp6: true
      dhcp4: true
  version: 2

Save the file and exit. Then apply the recent network changes using the following netplan command.

sudo netplan apply
ifconfig -a

From now on, your system will obtain an IP address dynamically from the router.

You can find more information and configuration options by viewing the netplan man page.

man netplan

At this point, you have successfully configured a network static IP address to your Ubuntu server.

Summarize

The above is the method of setting static IP for Ubuntu 18.04 Server introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to set static IP address in Ubuntu 14.04 under VMware
  • Detailed explanation of how to set static IP in Ubuntu 14.04
  • Ubuntu16.04 static IP address setting (NAT method)
  • How to set static IP in ubuntu
  • Ubuntu 20.04 sets a static IP address (including different versions)

<<:  Detailed explanation of Vue's caching method example

>>:  Join operation in Mysql

Recommend

Detailed explanation of Mysql logical architecture

1. Overall architecture diagram Compared to other...

An article to master MySQL index query optimization skills

Preface This article summarizes some common MySQL...

Detailed explanation of the basic use of centos7 firewall in linux

1. Basic use of firewalld start up: systemctl sta...

The most common declaration merge in TS (interface merge)

Table of contents 1. Merge interface 1.1 Non-func...

Mysql 8.0.18 hash join test (recommended)

Hash Join Hash Join does not require any indexes ...

Steps to install superset under win10 system

Superset is a lightweight self-service BI framewo...

How to verify whether MySQL is installed successfully

After MySQL is installed, you can verify whether ...

CentOS8 - bash: garbled characters and solutions

This situation usually occurs because the Chinese...

CentOS 7.6 installation of MySQL 5.7 GA version tutorial diagram

Table of contents Environment Preparation Environ...

Analysis of the operating principle and implementation process of Docker Hub

Similar to the code hosting service provided by G...

How to solve the problem that mysql cannot be closed

Solution to mysql not closing: Right-click on the...