leighsharpe has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I need to change the IP address on a debian linux box. Has anybody done this already? Since I already have Template::Toolkit loaded on this particular program, I'm currently thinking of something using Template::Toolkit and rewriting /etc/network/interfaces with a template something like this:
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface allow-hotplug eth0 iface eth0 inet static address [% address %] netmask [% mask %] gateway [% gateway %]
But /etc/network/interfaces is only writeable by root, which makes perfect sense, of course. However, my program won't be running as root, for all the obvious reasons. The user it will be running as does, however, have passwordless sudo access. Anybody got any better ideas?

Replies are listed 'Best First'.
Re: Changing IP address on linux box
by aquarium (Curate) on Nov 08, 2007 at 02:34 UTC
    ifconfig eth0 192.168.1.102 netmask 255.255.255.0 broadcast 192.168.1.255
    you may need to also run "ifconfig eth0 down" and then "ifconfig eth0 up", or /etc/network/restart or such.
    the hardest line to type correctly is: stty erase ^H
Re: Changing IP address on linux box
by KurtSchwind (Chaplain) on Nov 08, 2007 at 03:35 UTC
    You essentially want to wrap ifconfig with sudo or a sticky bit. I think this would be pretty easy in a shell script.
    #!/bin/sh sudo /etc/init.d/network stop sudo ifconfig . . . sudo /etc/init.d/network start
    I don't have debian in front of me, but I'm pretty sure that that is the location of the network start/stop script. At any rate you can take some params going into the script if you want, but I'm not sure what Template::Toolkit will really buy you in this case.
    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re: Changing IP address on linux box
by tirwhan (Abbot) on Nov 08, 2007 at 09:22 UTC

    Have your program write the new interfaces file into a location to which only trusted users have access. Then write a small script which swaps the new file into /etc/network/ and restarts networking (via /etc/init.d/networking) and either run that script via cron regularly or have the user run it via sudo. You can also do some syntax checking in the script to make sure you're not creating an invalid configuration which leaves the network interfaces down.

    Also, you didn't say why you want to do this, and most usage scenarios I can think of are better handled via DHCP, so maybe check that out first.


    All dogma is stupid.
Re: Changing IP address on linux box
by moritz (Cardinal) on Nov 08, 2007 at 07:45 UTC
    Regarding the permission issue you could "upgrade" your script to run as root:
    if ($> != 0){ exec($^X, $0, @ARGV); }

    (Not tested)

Re: Changing IP address on linux box
by Anonymous Monk on Nov 09, 2007 at 10:12 UTC
    Net::Ifconfig::Wrapper -- provides a unified way to configure network interfaces on FreeBSD, OpenBSD, Solaris, Linux, OS X, and WinNT (from Win2K).
Re: Changing IP address on linux box
by naikonta (Curate) on Nov 09, 2007 at 13:39 UTC
    I'm sorry if this disappoints you. I use network-config program to manage multiple network configurations on my debian laptop. The program uses its own configuration and applies the network setting directly with ifconfig. So I think it works for all distros or *nix types.

    I work interchangeably at two offices (of the same company), both use wifi connection. At home, I use ADSL connection. Having all my configs done correctly, all I do each time I switch work place (and thus network connection) is fire up the network-config program, choose a suitable network profile, click the apply button, and I'm connected. BTW, it's written in Perl with Gtk2 for its nice and simple GUI.


    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Changing IP address on linux box
by leighsharpe (Monk) on Nov 09, 2007 at 00:10 UTC
    Perhaps some context would be helpful.
    This is on a box which will be deployed remotely, and only accessed over SSH (or possibly telnet). All users except root will have their 'shell' set to run my perl script. Among other things, it will have some system admin capability. One of those capabilities is to change the box's IP address. It will have the option of being DHCP or static. Obviously, if they change the IP address, they will lose connectivity to the box as soon as it is applied. Unfortunately, restarting networking will drop the ssh session, which will stop the script, so networking never starts again. Therefore, I am happy to have it change the bootup configuration on the network, and have the changes applied at reboot.
    I don't particularly wish to give anyone else write access to /etc/network/interfaces, and I was wondering if there was any way to do the job from within perl, rather than messing about with system files myself.
      Gah! Don't do it as a reboot. You can nohup this, so that when the ssh connection is dropped, the script continues. Please don't bounce a unix server for something as trivial as a changing the IP address.
      --
      I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.