Interface configuration via command line / script

Hello! I am fairly new to the Linux world, and I’ve been mainly using openSUSE since I started out. It’s what I’m most familiar and comfortable with.

I know that to set a static address without using the interactive yast, you need to edit (/etc/syconfig/network) ifcfg-eth0, routes and resolv.conf (+ /etc/hostname).

How would I go about to configure this via commands, rather than manually editing files?
Surely there’s also a way to “find and replace” via some above-my-knowledge text manipulation tools like awk, but I’m hoping for a simpler solution I can implement into a script I’m working on (also bash scripting newbie).

Cheers, K

I do not quite understand your goal.

You either know how it should be done “manually” (using a text editor), or you use a good system management tool and on openSUSE that is YaST.

My goal is to set a static address on my system via a script I’m writing. Ergo I cannot manually edit files or use any interactive management tools like YaST.

Use Input/Output Redirection with > or >>

That would work, but not optimal having to echo out all these lines in the script.

echo “BOOTPROTO=‘static’” > /etc/sysconfig/network/ifcfg-eth0
echo “IPADDR=‘192.168.1.10’” >> /etc/sysconfig/network/ifcfg-eth0
echo “MTU=‘1500’” >> /etc/sysconfig/network/ifcfg-eth0

So what I take from the replies so far is that configuring the IP address via a one-liner is not something you generally do in SUSE.

Well, when you want to make your script as large as possible. This is more what I probably would do:


echo "BOOTPROTO='static'
IPADDR='192.168.1.10'
MTU='1500'
...
... " > /etc/sysconfig/network/ifcfg-eth0

Well, when we prefer to use Wicked, we use YaST. Just once at installation (probably by doing it through the installer), and that is it.

Maybe once in so many years you need to change your IP address, netmask, etc. and then we use YaST.

Many people that have openSUSE on “walk around” devices (lap-tops and the like), use Network Manager.
That is IMHO how it is “generally done on openSUSE” (not SUSE, or maybe it is also done on SUSE, but that is not subject op these forums).

Script is just a tool to achieve some goal. You do not explain, why you need to use this script. So far it sounds like school homework assignment.

Anyway - the tool to edit network configuration files is YaST. I am not aware of any other tool. It is trivial to write shell script to do it (it is less trivial to write good script with input check and error handling) but it hardly belongs to Network/Internet - ask in Programming section.

The obvious alternative would be to use the ‘ip’ command with which you
can add/remove IPs, add/remove routes, enable/disable network devices, etc.

https://www.ghacks.net/2018/04/15/back-to-basics-part-6-using-the-ip-command-in-gnu-linux-for-beginners/


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below.

If you want to send me a private message, please let me know in the
forum as I do not use the web interface often.