Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

In this tutorial, let’s look at how we can switch from Netplan to /etc/network/interfaces. Networking in Linux involves configuring and managing interfaces that allow our Linux systems to access networks. This can either be a local network, the internet, or any other type of network infrastructure.

Understanding Linux networking is crucial for setting up and maintaining reliable and secure connections. Linux offers us various tools we can use to manage a network.

2. Linux Networking

Linux networking is a fundamental aspect of managing and configuring network interfaces on a Linux-based system. Some of the most commonly used methods for configuring network interfaces are Netplan and the traditional /etc/network/interfaces file. Both have distinct purposes and are used depending on the distribution and version of Linux.

2.1. Netplan

Netplan is a YAML-based network configuration utility that simplifies the management of network settings. Netplan allows us to define network configurations in a more human-readable format, using YAML syntax. The configuration files for Netplan are typically located in /etc/netplan/ and have .yaml extensions.

Netplan abstracts the underlying networking stack and allows us to easily switch between different networking backends, such as networkd and NetworkManager. This flexibility makes it easier to adapt to various networking environments. With Netplan, we can define our network interfaces, IP addresses, gateways, DNS servers, and other settings in a single YAML file. After editing the configuration, we apply the changes using the command sudo netplan apply, which will translate the YAML configuration into the appropriate format for the selected backend.

2.2. /etc/network/interfaces

Aside from Netplan, the traditional method of configuring network interfaces on many Linux distributions, especially Debian-based ones, is through the /etc/network/interfaces file. This file uses a simpler, line-based syntax to define network interfaces and their respective settings.

In the /etc/network/interfaces file, we define each network interface with directives such as iface, auto, allow-hotplug, address, netmask, and gateway. This method requires us to manually edit the file and sometimes restart the networking services for the changes to take effect.

Some of the differences between Netplan and /etc/network/interfaces include:

  • Firstly, Netplan uses YAML, which is more structured and human-readable, while /etc/network/interfaces uses a simpler line-based syntax.
  • Secondly, Netplan provides greater flexibility by supporting different networking backends, making it easier to switch between them. On the other hand /etc/network/interfaces lacks this flexibility.

To summarize, while both Netplan and /etc/network/interfaces serve the same purpose of configuring network interfaces, they cater to different needs and preferences. Netplan offers a more modern approach, while /etc/network/interfaces provides a simpler, traditional method.

3. Switching to the /etc/network/interfaces

Now, let’s look at how we can switch back to using /etc/network/interfaces from Netplan on a Linux system. On systems that use Netplan as default e.g. Ubuntu, we’ll need to deactivate Netplan and install the necessary tools.

Firstly, for systems having Netplan set as default, we should back up our current Netplan configuration files in case we need to revert to them later:

$ sudo cp /etc/netplan/*.yaml /etc/netplan/backup/

Following, let’s install:

$ apt-get update
$ apt-get install ifupdown net-tools

Next, to disable Netplan, we can either remove or rename the Netplan configuration files so that Netplan doesn’t manage our network anymore:

$ sudo mv /etc/netplan/*.yaml /etc/netplan/backup/

To ensure that Netplan doesn’t attempt to start on boot, we can mask the netplan service:

$ sudo systemctl mask systemd-networkd

Alternatively, we can just remove the files:

$ sudo rm /etc/netplan/*.yaml

Next, we’ll create or edit the /etc/network/interfaces file to add the configurations we need:

$ sudo nano /etc/network/interfaces

For example, let’s set up a static IP address for our ethernet interface eth0:

allow-hotplug eth0
auto eth0
iface eth0 inet static
address 192.168.1.128
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.4

#dns
dns-nameservers 1.1.1.1 8.8.8.8

By adding these configurations, we’ve assigned the computer a static IP address of 192.168.1.128 to eth0, with a subnet mask of 255.255.255.0 and a gateway of 192.168.1.4.

If we want it to obtain IP addresses automatically, we use the following directive:

iface eth0 inet dhcp

Next, let’s activate the configurations, this doesn’t require a reboot:

$ sudo ifdown --force eth0 lo && ifup -a
$ sudo systemctl unmask networking
$ sudo systemctl enable networking
$ sud systemctl restart networking

The command sudo ifdown –force eth0 lo && ifup -a is used in Linux to manage network interfaces, i.e. to bring them down by force and then bring them back up.

Next, we unmask the networking service if it was masked, enable it then restart it.

Let’s check the status of the networking service:

$ sudo systemctl status networking
● networking.service - Raise network interfaces
     Loaded: loaded (/usr/lib/systemd/system/networking.service; enabled; pre>
     Active: active (exited) since Sun 2024-08-11 08:21:55 EDT; 54min ago
       Docs: man:interfaces(5)
    Process: 20247 ExecStart=/usr/sbin/ifup -a --read-environment (code=exite>
    Process: 20364 ExecStart=/bin/sh -c if [ -f /run/network/restart-hotplug >
   Main PID: 20364 (code=exited, status=0/SUCCESS)
        CPU: 299ms

Lastly, let’s verify the IP address:

$ ip a 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:fe:d9:2f brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.128/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever

4. Conclusion

In this tutorial, we’ve looked at how we can switch back from Netplan to using the /etc/network/interfaces file. Linux networking is a comprehensive system that involves configuring network interfaces, managing IP addresses, setting up DNS, and ensuring secure and efficient data routing.

Lastly, Linux networking is very dynamic. It offers various options that can be used to configure both simple and complex network settings.