Skip to main content Configuring specific interface with a script : r/linuxquestions

Configuring specific interface with a script

Advice

What is the best method to configure an interface automatically (using script)? Please note that I'm a beginner sysadmin so please don't be harsh. I need the script to be able to read user input and then pass those arguments (doable using 'read'). Note that the configuration here includes changing the interface-specific IP address, netmask, gateway, and dns server, and ALSO able to remove those specific configuration. It also should be able to target specific interface in case there were many interfaces used in the system.

It's a bit simpler to do this in a Linux with NetworkManager installed (Ubuntu for example) thanks to the nice YAML file. However what I use here is mainly it is for Debian 11. It does not use NetworkManager, rather it uses the classic /etc/network/interfaces.

Below are a small portion of the script, the original script included switch-case statements, while loop, and read command for example.

I want the script to be able to configure an interface from dhcp to static, and otherwise. For DHCP it was simple, but for static modes they were quite a pain. I've been thinking of using sed like this for example :

sed -i "s|allow-hotplug $INT|auto $INT|" /etc/network/interfaces
sed -i "s|iface $INT inet dhcp|iface $INT inet static\n\taddress $IP\n\tnetmask $MASK\n\tgateway $GW\n\tdns-nameservers $DNS|" /etc/network/interfaces

But if I keep using sed with similar syntax above it won't work in every case. For example, what if the interface in /etc/network/interfaces is not even configured? Like I'm using a VM, at first I set it to only use one interface, but someday later I might need to add more interface to it. Or maybe what if there is another case (example below).

Then we can also use echo and pipe it to tee, for example :
echo "address $ip_address" | tee -a /etc/network/interfaces
echo "netmask $netmask" | tee -a /etc/network/interfaces

Now, what would happen if we have configured an interface with "address x.x.x.x" and "netmask x.x.x.x" option? Or maybe we re-run the script with those echo commands, it will duplicate those options.

Also what if the file contains many interfaces, like this for example :

auto enp0s3
iface enp0s3 inet static
      address 192.168.10.2
      netmask 255.255.255.0
      gateway 192.168.10.254
      dns-nameservers 1.1.1.1

auto enp0s8
iface enp0s8 inet dhcp

auto enp0s9
iface enp0s9 inet static
      address 172.16.212.25/22
      gateway 172.16.212.1

What if I need to configure enp0s3 to be dhcp, and remove all of those options? The logic of some portion of the script above is too basic to be able to do the intended behaviour. Please give me your wisdom, O Linux sysadmin of reddit. Some source and further reading to learn more about bash or Linux commands that will help me to do so are very welcomed.

4 upvotes · 14 comments
Archived post. New comments cannot be posted and votes cannot be cast.
Be honest: You never imagined you’d have the opportunity to use an AI-powered PDF editor that automates, summarizes, and translates. What a time to be alive.
Thumbnail image: Be honest: You never imagined you’d have the opportunity to use an AI-powered PDF editor that automates, summarizes, and translates. What a time to be alive.
Sort by:
Q&A (Default)
Open comment sort options

However what I use here is mainly it is for Debian 11. It does not use NetworkManager, rather it uses the classic /etc/network/interfaces.

Debian uses whatever you want it to use. You don't have to use ifupdown if you don't want to use it.

I know that. But let me clarify, I just want to not use any other kind of network managing services. Sorry for the lack of clarity.

More replies