I got tired of editing my /etc/hosts by hand to adapt my network configuration depending on if I'm at home or at the office. So I decided to hack something on the initscripts:
- Edit /boot/grub/menu.lst and duplicate some entries adding a LOCATION={home|office} parameter:
title Fedora Core (2.6.8-1.521) Office root (hd0,0) kernel /boot/vmlinuz-2.6.8-1.521 ro root=LABEL=/ rhgb quiet LOCATION=office initrd /boot/initrd-2.6.8-1.521.imgtitle Fedora Core (2.6.8-1.521) Home root (hd0,0) kernel /boot/vmlinuz-2.6.8-1.521 ro root=LABEL=/ rhgb quiet LOCATION=home initrd /boot/initrd-2.6.8-1.521.img
- Write a System V script (actually copying an existing one and adapting) called location which copies the right /etc/hosts. Note the chkconfig lines at the beginning of the file:
#!/bin/bash # # chkconfig: 2345 90 10 # description: little script to configure some stuff depending if the laptop \ # is at the office or at home
# source function library . /etc/init.d/functions
start () { echo $"$LOCATION" if [ -e /etc/hosts.$LOCATION ]; then cp /etc/hosts.$LOCATION /etc/hosts else echo $"localized hosts file not found" fi }
stop () { echo "Have a good day" }
case "$1" in start) start ;; stop) stop ;; restart|reload) stop start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac
exit 0
- Install the location script with chkconfig:
[root@i8100] chkconfig location on
- Write /etc/hosts.home and /etc/home.office
That's all. Next time I reboot I will have my right network configuration just by selecting one grub menu item. :-)
