Linux Basics : Part11 : Network Configuration

In the last post we saw about the different text processing tools and if you have been following along the series and practicing on your own you should have got a hang of Linux by now.

That said let’s move on to configuring some important system parameters like network configuration.

We will use our knowledge of using text editors like "vim" for editing the network config files here.

Configuration Files

Back in the days there used to be only one network config file “ifcfg-ethx” located within "/etc/sysconfig/network-scripts/", but with the development of Linux and inception of new flavors we today have multiple network configuration files and locations for different flavors and different network managers to manage them.

We will have a look into few of them here and the different ways entries are made into them.

  1. Ubuntu 20.04 : /etc/netplan/50-cloud-init.yaml

  2. Older versions of Ubuntu: /etc/network/interfaces

  3. CentOS/RHEL systems: /etc/sysconfig/network-scripts/ifcfg-ethx
    where “x” is the interface number 0 or 1 and so on.

  4. SUSE based systems: /etc/sysconfig/network/ifcfg-ethx
    where “x” is the interface number 0 or 1 and so on.

So now that we have the config files, further we will see how to make entries within them and their syntax.

1. Netplan.

Netplan has been recently introduced from Ubuntu 20.04 systems onward and is pretty straight forward to configure it.
If you have knowledge of yaml it is even more easier.

To configure static IP you may use the below format:

#vim /etc/netplan/50-cloud-init.yaml
network:
ethernets:
eth1:
dhcp4: false
addresses: [172.16.1.4/16]
gateway4: 172.16.1.1
nameservers:
addresses: [172.16.1.1]
version: 2

If you want to further enhance your configuration you may as well use something like below:

network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 172.16.1.2/16
nameservers:
search: [yourlocaldomain, someotherdomain]
addresses: [172.16.1.1, x.x.x.x]
routes:
- to: default
via: 172.16.1.1

To find out the name of the device use the "ifconfig -a" command.
This will show you all the interfaces, which you can then select to configure.

For renderer you may use “NetworkManager” instead of the default "networkd", currently these are the only two supported.

The remaining parameters are pretty self-explanatory:

eth0: Name of the interface and can be something else like eth1, eth2, enp3s1, enp3s2 or something else, as mentioned above use the ifconfig command to check yours before configuring.

addresses: This is the IP that you want to assign to the interface. And it can take multiple IP’s in comma separated format.

nameservers: Here you assign the DNS IP Address and the Search Domains.

routes: Here you can add the routes you want.

Well this was our static configuration file, however when you first install the system and if you have your router configured as a DHCP server, chances are this file was already configured for DHCP.
If not the config is pretty simple.

network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: true

The explanation for the various parameters remains the same as above.

Once you make the required changes, make sure to save and exit the file. ":wq"

Once you are back in the prompt you will have to apply the configuration for the changes to take effect.

To apply the config:

#netplan apply

If you want to try the config before applying:

#netplan try

Netplan takes a lot of parameters and can be configured in multiple ways.
The parameters shown here are bare minimum to get you started and if you are using a simple server or a home system this should be enough, however if you wish to learn more on how to configure netplan in more advanced ways please feel free to check out the official netplan website here and a huge list of examples here.

2. Interfaces File.

The "/etc/network/interfaces" file is pretty simple and takes entries like a normal text file.

#vim /etc/network/interfaces

iface eth0 inet static
address 172.16.1.2
netmask 255.255.0.0
gateway 172.16.1.1

The above configuration is for assigning static IP. If you want to assign Dynamic IP, make entries like below:

auto eth0
iface eth0 inet dhcp

In both formats:

eth0: Is the name of the interface.

address: Is the static IP address you want to assign.

Just like netplan even here you can do advance configurations like below:

Adding broadcast and mentioning routes to bring up and down with network service

auto eth0
iface eth0 inet static
address 172.16.1.42
network 172.16.1.0
netmask 255.255.0.0
broadcast 172.16.1.0
up route add -net 172.16.1.21 netmask 255.255.0.21 gw 172.16.1.2
up route add default gw 172.16.1.40
down route del default gw 172.16.1.40
down route del -net 172.16.1.21 netmask 255.255.0.21 gw 172.16.1.2
Adding virtual interfaces:

auto eth0 eth0:1
iface eth0 inet static
address 172.16.3.10
network 172.16.3.0
netmask 255.255.0
gateway 172.16.3.1
iface eth0:1 inet static
address 172.16.3.20
network 172.16.3.0
netmask 255.255.0.0

Well now that the entries are made save and exit the file ":wq"

Once in the prompt restart the network service.

#service networking restart

With the pace that things are getting developed there is a high chance that you may not use the above to a great extent or at all, but it’s good to know just in case you come across a really old system one fine day !!

That said let’s go ahead and take a look at our next file which is:

3. The ifcfg file.

The location of the file is “/etc/sysconfig/network-scripts/ifcfg-ethx” in CentOS and “/etc/sysconfig/network/ifcfg-ethx” in SUSE based systems.

Both the files take similar configuration.

For static IP configuration the file will be:

DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
NETMASK=255.255.0.0

IPADDR=172.16.1.7
USERCTL=no

For Dynamic Configuration:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

Where:

1. BOOTPROTO=protocol
It can be:
1. none – No boot-time protocol should be used.
2. bootp – BOOTP protocol should be used.
3. dhcp – DHCP protocol should be used.

userctl: “yes” means non-root users are allowed to control the device and no means vice-versa.

The remaining parameters are self-explanatory.

Like the others even this file takes advance configurations, which can be explored here.

Once you are done with the configuration, save and exit the file ":wq"

Once at the prompt restart the network service

#service network restart

If you are using CentOS/RHEL 7 or above, you may have to use the systemctl to restart the network.

#systemctl status network.service

If you are configuring it for the new based systems you may have to use “wicked” to reload.

#wicked ifreload eth0

For CentOS/RHEL 8 based systems you may have to use “nmcli” tool

#nmcli networking off
#nmcli networking on
OR
#systemctl restart NetworkManager.service

Feel free to explore different options and parameters and learn more by practicing them.

The more you practice, the more comfortable you will be.

That’s it for this post, see you in the next post of blog.avoidingtech.com.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!