[ale] Update ifcfg-eth0 file from command line? (need to re-ip 50 servers)

Lightner, Jeffrey JLightner at dsservices.com
Mon Mar 20 09:01:50 EDT 2017


As at least one other has said be sure to save copies of the original ifcfg-<ifc> files.

However, be sure NOT to save that copy in same directory as the original (e.g. /etc/sysconfig/network-scripts on RHEL/CentOS).   On networking start it actually tries to load all ifcfg files even if they are describing the same interface and the last one found wins so you might end up with unexpected settings.

You want to make sure you have  console access available just in case something goes wrong and networking doesn’t start.   In general though restarting networking is fairly painless.


From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of Jim Kinney
Sent: Sunday, March 19, 2017 9:50 AM
To: Atlanta Linux Enthusiasts - Yes! We run Linux!
Subject: Re: [ale] Update ifcfg-eth0 file from command line? (need to re-ip 50 servers)

sed -i -e 'edit #1' -e 'edit #2' ... -e 'edit #n’

The edits happen in sequence per line.

On Mar 19, 2017 12:46 AM, "Raj Wurttemberg" <rajaw at c64.us<mailto:rajaw at c64.us>> wrote:
Hey Phil,

Can't say that I even knew that sed could do multiple replacements in one
line. :)  (I've only been scripting with bash for about two years... I'm a
noob) I just Googled it and came up with this...

Is this kind of what you are talking about? (I've never used mktemp either)

TMPIPCFG=$(mktemp /etc/sysconfig/network-scripts/tmpipcfg.XXXX)
sed -i 's/IPADDR=.*/IPADDR=1.2.3.4/<http://1.2.3.4/>; s/BROADCAST=.*/BROADCAST=1.2.3.0/<http://1.2.3.0/>;
s/GATEWAY=.*/GATEWAY=1.2.3.1/<http://1.2.3.1/>' $TMPIPCFG
mv $TMPIPCFG /etc/sysconfig/network-scripts/ifcfg-eth0

Learned something new tonight...

Thanks!
/Raj

-----Original Message-----
From: ale-bounces at ale.org<mailto:ale-bounces at ale.org> [mailto:ale-bounces at ale.org<mailto:ale-bounces at ale.org>] On Behalf Of Phil
Turmel
Sent: Saturday, March 18, 2017 9:56 PM
To: ale at ale.org<mailto:ale at ale.org>
Subject: Re: [ale] Update ifcfg-eth0 file from command line? (need to re-ip
50 servers)

Aaaaahhhhhhgggg!

Raj, Leam, y'all making me cringe with those procedures.  For pity's sake,
use mktemp to safely make an empty temp file in the *same* folder, stream
from the original to the temp file with *one* instance of sed, then 'mv' the
temp file back over the original.  At no point will you have a partially
configured network -- filesystems are required to guarantee that renaming
over file atomically replaces it.  And at no point will you have an unsecure
temp file exposed.

Phil

On 03/18/2017 09:03 PM, Raj Wurttemberg wrote:
> Yeah, I was actually going to do a sed to just replace the old values and
do most everything in one step:
>
> sed -i 's/IPADDR=.*/IPADDR=1.2.3.4/<http://1.2.3.4/>'
> /etc/sysconfig/network-scripts/ifcfg-eth0 2> /dev/null sed -i
> 's/BROADCAST=.*/BROADCAST=1.2.3.0/<http://1.2.3.0/>'
> /etc/sysconfig/network-scripts/ifcfg-eth0 2> /dev/null sed -i
> 's/GATEWAY=.*/GATEWAY=1.2.3.1/<http://1.2.3.1/>'
> /etc/sysconfig/network-scripts/ifcfg-eth0 2> /dev/null
>
> haha... yes, I noticed a long time ago that the RHEL tries to use any file
in the /etc/sysconfig/network-scripts directory! Doh!
>
> Thanks,
> /Raj
>
>
> -----Original Message-----
> From: ale-bounces at ale.org<mailto:ale-bounces at ale.org> [mailto:ale-bounces at ale.org<mailto:ale-bounces at ale.org>] On Behalf Of
> Leam Hall
> Sent: Saturday, March 18, 2017 8:32 PM
> To: Atlanta Linux Enthusiasts <ale at ale.org<mailto:ale at ale.org>>
> Subject: Re: [ale] Update ifcfg-eth0 file from command line? (need to
> re-ip 50 servers)
>
> Untested, and I tend to do things in multiple steps.
>
> cp /etc/sysconfig/network-scripts/ifcfg-eth0 /tmp
>
> cat /tmp/ifcfg-eth0 | egrep -iv "ipaddr|broadcast|gateway" >
> /etc/sysconfig/network-scripts/ifcfg-eth0
>
> echo "IPADDR=1.2.3.4" >> /etc/sysconfig/network-scripts/ifcfg-eth0
> echo "BROADCAST=1.2.3.0" >> /etc/sysconfig/network-scripts/ifcfg-eth0
> echo "GATEWAY=1.2.3.1" >> /etc/sysconfig/network-scripts/ifcfg-eth0
>
> service network restart

_______________________________________________
Ale mailing list
Ale at ale.org<mailto:Ale at ale.org>
http://mail.ale.org/mailman/listinfo/ale
See JOBS, ANNOUNCE and SCHOOLS lists at
http://mail.ale.org/mailman/listinfo

_______________________________________________
Ale mailing list
Ale at ale.org<mailto:Ale at ale.org>
http://mail.ale.org/mailman/listinfo/ale
See JOBS, ANNOUNCE and SCHOOLS lists at
http://mail.ale.org/mailman/listinfo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20170320/3d084168/attachment.html>


More information about the Ale mailing list