Change IP address on Ubuntu Server

You have two options when configuring the IP address on your Ubuntu Server, and that is either a static IP address or DHCP. A static IP address allows you to manually select your IP address by configuring it on the Linux system, whereas DHCP relies on the router or DHCP server to lease you an IP address – either a reserved one or the next available one that is currently free, depending on the setup.

In addition to the IP address configuration, Linux administrators may also need to set the DNS server and default gateway, or setup a virtual IP address. In this tutorial, you will see how to change to static IP or DHCP, and edit the other aforementioned network settings on Ubuntu Server.

In this tutorial you will learn:

  • How to change IP address by setting a static IP
  • How to switch to DHCP
  • How to configure DNS servers and default gateway
  • How to create a virtual IP address
Change IP address on Ubuntu Server
Change IP address on Ubuntu Server
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux Server
Software Netplan
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Change to Static IP Address



DID YOU KNOW?
When it comes to changing the IP address on Ubuntu, we are referring to the internal IP address. In order to change your external/public IP address, you would need to go through your internet service provider.

Changing to a static IP address will allow you to manually choose an IP address on Ubuntu Server. On the other hand, using DHCP leaves it up to the router or DHCP server’s configuration to choose which IP address your machine will have. Follow the step by step instructions below to change the IP address on server by setting a static IP.

  1. Locate and edit with administrative privileges the /etc/netplan/50-cloud-init.yaml file (it may be called /etc/netplan/01-netcfg.yaml instead) with the following configuration. Update your desired static IP address, DNS server and gateway where appropriate. Save and exit the file after you have applied your changes.
    network:
        ethernets:
            enp0s3:
                dhcp4: false
                addresses: [192.168.1.202/24]
                gateway4: 192.168.1.1
                nameservers:
                  addresses: [8.8.8.8,8.8.4.4,192.168.1.1]
        version: 2
    
  2. To apply the new Netplan changes execute:
    $ sudo netplan apply
    

    Alternatively, if you run into some issues run:

    $ sudo netplan --debug apply
    
  3. Confirm your new static IP address by using the ip a command:
    $ ip a
    

Change to DHCP

If you have previously configured a static IP on Ubuntu Server and wish to revert to using DHCP for your network settings, all you need to do is open your /etc/netplan/50-cloud-init.yaml file (it may be called /etc/netplan/01-netcfg.yaml instead) as shown above and change the dhcp to true.

            dhcp4: true

How to create a virtual IP Address

  1. To configure a second IP address, we will start by once again opening the /etc/netplan/50-cloud-init.yaml or /etc/netplan/01-netcfg.yaml file. The following configuration contains two IP addresses, and as you can see they are separated by a comma.
    network:
        ethernets:
            enp0s3:
                dhcp4: false
                addresses: [192.168.1.202/24, 192.168.1.204]
                gateway4: 192.168.1.1
                nameservers:
                  addresses: [8.8.8.8,8.8.4.4,192.168.1.1]
        version: 2
    

    Our secondary or virtual IP address in this case is 192.168.1.204.

  2. To apply the new Netplan changes execute:


    $ sudo netplan apply
    

    Alternatively, if you run into some issues run:

    $ sudo netplan --debug apply
    
  3. Confirm that you now have a virtual IP address by using the ip a command:
    $ ip a
    

Temporary Virtual IP

If, instead, you would like to configure a temporary virtual IP address, you can use the following command syntax:

$ sudo ip addr add 192.168.1.204/24 dev enp0s3 label enp0s3:1

This will once again configure 192.168.1.204 as the virtual IP address. However, this configuration will not persist after reboot.

Closing Thoughts

In this tutorial, we saw how to configure a static IP address or revert to DHCP configuration on Ubuntu Server. We also saw how to set the DNS servers and default gateway, which is all done in the YAML configuration file for Netplan. In addition, we learned how to configure a temporary or permanent IP address. Network administration is an important part of configuring your Ubuntu Server, and these should be most of the settings that you need in order to get started.



Comments and Discussions
Linux Forum