This tutorial was written after my attempts to fulfill a specific need I had for easily using my laptop on different networks. I got sick of resetting network parameters and decided to try and automate it and the following tutorial is the result. There is plenty of room for expansion of this concept to include setting up printers etc. for each network but so far I haven't needed that functionality so I haven't worried about it. The steps in this tutorial worked for me, however I make no guarantees that it will work on your particular system and if you decide to try it you do so at your own risk.
This is the second version of this tutorial. The first version was written for use on a slackware system which uses wholly different init file system. This version is designed for use with Red Hat 7.2 using the grub boot loader, but should adaptable for use on other system V type systems.
The first thing we want to do is prepare the different network configuration files. One for each network to which you will be connecting the computer.
To accomplish this make copies of the current version of the /etc/sysconfig/networking/devices/ifcfg-eth0 with filenames like home.ifcfg-eth0, dhcp.ifcfg-eth0, etc.
Next modify each one of these new files with the correct network information. the files will be in a format similar to the following...
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.2
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
TYPE=Ethernet
USERCTL=no
NETWORK=192.168.1.0
BROADCAST=192.168.1.255
PEERDNS=no
Or for a DHCP configuration simply..
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
Next we will work on the grub.conf file in the /boot/grub directory. This step is the key that makes this all work. What we need to do is add a new boot option for each network configuration. We will then add an identifying parameter to the kernel command of each boot option. Perhaps the examples below will make this easier to understand.
default=0
timeout=10
splashimage=(hd0,1)/grub/splash.xpm.gz
title linux
root (hd0,1)
kernel /vmlinuz-2.4.7-10 ro root=/dev/hda5
initrd /initrd-2.4.7-10.img
title Windows
rootnoverify (hd0,0)
chainloader +1
default=0
timeout=10
splashimage=(hd0,1)/grub/splash.xpm.gz
title linux-home
root (hd0,1)
kernel /vmlinuz-2.4.7-10 ro root=/dev/hda5 boot_profile=linux-home
initrd /initrd-2.4.7-10.img
title linux-dhcp
root (hd0,1)
kernel /vmlinuz-2.4.7-10 ro root=/dev/hda5 boot_profile=linux-dhcp
initrd /initrd-2.4.7-10.img
title Windows
rootnoverify (hd0,0)
chainloader +1
As you can see I added a boot_profile parameter to each kernel command with a value indicative of the boot option. The kernel will ignore this parameter but it will write it to the cmdline file in /proc.
Incidentally you may have noticed the splashimage line in the grub.conf. The splashimage line tells grub what background image to use on the boot menu. If you don't happen to like the default image you can change the splashimage entry to point to a different image file. The default image is a 640x480 pixel image with 16 bit color. You will want any alternate images to share these properties.
Almost there, this is where we use the information we stuck in the /proc/cmdline file. We are going to create the file called setnet in the /etc/rc.d/init.d directory. You should be able to pretty much copy the file I will show you below and make whatever modifications needed by your particular setup. I am quite sure there are better ways to extract the information we want from this file but this works for me and doesn't require anything but commands available on most any system. Make this file executable with a 'chmod 755 setnet' command once you get it saved to the /etc/rc.d/init.d directory.
#!/bin/sh
# chkconfig: 35 09 90
# description: Set's up networking based on the boot option selected from \
# the Grub menu.
BOOTPROFILE=`(cat /proc/cmdline; echo) | /bin/cut -d" " -f3 | /bin/cut -d= -f2`
case "$BOOTPROFILE" in
linux-home)
echo "Setting up network for home"
cp /etc/sysconfig/networking/devices/home.ifcfg-eth0 \
/etc/sysconfig/networking/devices/ifcfg-eth0
# Set up hostname entries
echo "host.domain.com" > /etc/HOSTNAME
hostname -F /etc/HOSTNAME
echo "NETWORKING=yes" > /etc/sysconfig/network
echo "HOSTNAME=host" >> /etc/sysconfig/network
;;
linux-dhcp)
echo "Setting up DHCP"
cp /etc/sysconfig/networking/devices/dhcp.ifcfg-eth0 \
/etc/sysconfig/networking/devices/ifcfg-eth0
;;
esac
Remember to make this file executable or none of this will work.
This is the last step. We need to call the setnet file on bootup, (you did make it executable, right?). To do this we need to make a symbolic link to it in each runlevel directory that we want the script used in. We need it to run before the network script which is S10 so we will make our link S09setnet.
If you wondered about the second and third lines in the setnet script here is where they are used. Red Hat was nice enough to provide an easy way to create symbolic links for starting and stopping services called chkconfig. The second line in setnet tells chkconfig to put start links of a priority 09 in runlevels 3 and 5 and a kill link of priority 90 in the other run level directories.
Here is the command...
#chkconfig --add setnet
Whoo Hoo! that should do it. All that is left now is to reboot and try it out. If you decide you want to disable this capability for whatever reason you just need to make sure you have a working ifcfg-eth0 file and run the command chkconfig --del setnet.
One problem I have noticed is that if your hostname can not be resolved gnome will not start. One solution would be to put an entry in the /etc/hosts file for each host that you set up with a static IP.