When migrating a server from one provider to another, the network interface name may sometimes change. Below, we show you the steps required to rename the network interface and configure a static IP address on an Ubuntu 16.04 or Ubuntu 18.04 server.
Steps before renaming the network interface in Ubuntu 16.04 and 18.04
The first step is to find the current name of the network interface. To do this, run the following command:
# ip a
This will return output similar to the following:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:9d:3a:6f brd ff:ff:ff:ff:ff:ff
inet 46.183.114.61/23 brd 46.183.115.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fe9d:3a6f/64 scope link
valid_lft forever preferred_lft forever
As you can see, instead of the typical "eth0", we have "ens33", which is commonly used on a "VMware" virtual server.
To restore "eth0", run the following commands:
# vi /etc/default/grub
Find the "GRUB_CMDLINE_LINUX" variable and add "net.ifnames=0 biosdevname=0". The variable should look like this:
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
Once you have edited the variable, save the file and create the new Grub file with the following command:
# grub-mkconfig -o /boot/grub/grub.cfg
Rename the network interface and assign a static IP address in Ubuntu 16.04
Depending on the migrated server's configuration, the configuration file may be in "/etc/network/interfaces" or "/etc/network/interfaces.d". You will usually find it in the first location, with a configuration similar to the following:
auto lo
iface lo inet loopback
auto ens33
iface ens33 inet dhcp
To change the interface name from "ens33" to "eth0", edit the configuration so that it looks like this:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
To also configure a static IP address, add the following variables, adjusting the values to match the network you are configuring:
auto eth0
iface eth0 inet static
address 46.183.114.61
netmask 255.255.254.0
dns-nameservers 8.8.8.8
gateway 46.183.114.32
Restart the server with the command:
# reboot
After restarting, check that the interface is now named eth0.
Rename the network interface and assign a static IP address in Ubuntu 18.04
In Ubuntu 18.04, you may need to configure the interface using "Netplan". To see which configuration files are available, run:
# ls -la /etc/netplan/
This will produce output similar to:
-rw-r--r-- 1 root root 299 May 7 2018 10-interfaces.yaml
Edit the "10-interfaces.yaml" file to change ens33 to eth0:
network:
ethernets:
eth0:
dhcp4: true
optional: true
version: 2
To also configure a static IP address, add the following variables, adjusting the values to match the network you are configuring:
network:
ethernets:
eth0:
dhcp4: no
addresses: [85.208.23.104/23]
gateway4: 85.208.23.254
nameservers:
addresses: [8.8.8.8,8.8.4.4]
version: 2
Finally, run the following command to apply the changes:
# netplan apply