- By Mohit Muthanna -
Most Linux laptop users don't leave home without their trusted sword. They use it at home, at work, at client sites and on the bus. A problem while moving around with these laptops is profile management; having to change IP addresses, DNS addresses, gateways, proxy servers, etc., every time one changes his location is simply a pain. Most people a trouble remembering what files to edit, or just typing in the new addresses all the time. I had this problem, and most of the utilities available on the net were incomplete, limited, or just did not do what I want.
Enter QuickSwitch, a free, Open-Sourced utility that helps Linux laptop users maintain roaming network profiles. I initially wrote it to accomplish a simple task as painlessly as possible. It consisted of a Perl script and a configuration file. You entered the details of your different environments as "profiles" in the configuration file and ran the script whenever you needed to change environments; and in an instant it reconfigures the network card, updates the routing table and DNS entries and switched a bunch of important files around. Since then, QuickSwitch has evolved to a mature feature-rich utility used by a large population of Linux laptop users.
QuickSwitch now sports many useful features, some of which include: DHCP/BOOTP support; wireless LAN configuration; selecting profiles on startup through kernel parameters; a text mode graphical interface; and dynamic file swapping. And it does all this on the fly, without having to restart the machine. Visit the QuickSwitch home page for more details on what QuickSwitch can accomplish.
QuickSwitch's latest version (at the time of writing this article) is 0.14-2. QuickSwitch requires there to be a working Perl 5 interpreter, which is installed by default with most distributions of Linux.
To install QuickSwitch, first download the tarball from the downloads section of the QuickSwitch site, then:
tar -zxvf QuickSwitch-0.14.tar.gz
This installs the "switchto" executable in /usr/bin and copies a sample switch.conf file to /etc. Red Hat users note that a file called switchto already exists in /usr/bin, and you may want to rename the QuickSwitch program, or install it somewhere else. The installation script is nothing more than a shell script that copies the files to their destinations.
Configuring QuickSwitch is simply a matter of editing the self-documenting sample configuration file located in /etc/switch.conf. The sample file includes some application defaults and about three sample profiles. The configuration file is split into three sections, one global section for defaults such as paths etc., one for the dynamic file swapping feature and one for the profiles.
To demonstrate, we will setup Quickswitch to switch between our home and work profiles.
Let us first examine a few of the lines in the global section. This section is named config and identified by the title in the square brackets. The first thing you have here is the default network device, which is set to eth0. This tells QuickSwitch which card to configure when switching profiles and can be overridden inside a profile. The servicefilename token specifies the location of the file to store the last working configuration. This is handy when you're restarting your laptop and just want QuickSwitch to use the last used profile.
Below are paths to some of the standard programs used by QuickSwitch. Notice they are all commented out. In general, if an option in the file is commented out, it represents a default value which can be changed by uncommenting the line and changing it.
# etc? Etc?
The lines below are DHCP defaults. This depends on the DHCP client software you're using; the default is set to pump, which is the client included with Red Hat.
# For DHCP/BOOTP on RedHat systems (default)
# dhcpconfig=pump -i
# Uncomment for DHCP on other systems
The following lines are used to execute scripts before and after a profile switch. They can be overridden inside of a profile. The example here executes the command xhost + before a profile switch. This is handy if you're changing profiles inside of an X window session, and it prevents unauthorized connection errors.
Setting useansi to 0 disables the color debug messages on ANSI terminals. The colors that follow it are user definable.
As you can see, most of the above were commented out and were defaults and did not even have to be included in the file. You will only need to change them if you have a notoriously non-standard Linux installation.
Now let us create two profiles, one for home and one for work. Naming the profiles is a matter of entering a name inside of the square brackets that begins a section. The lines that follow it are the configuration values for that profile.
Here, you see the work profile.
The top line (in the square brackets) defines the name of the profile as identifiable by QuickSwitch. The next line defines a description which is used by the GUI switcher. All downhill from here, you have the IP address, network mask, default gateway, hostname, fully qualified hostname etc. The domain and search tokens are used to specify the default domain name and the default search domains during name resolution. And dns1 and dns2 are primary and secondary DNS addresses.
Our home profile is a bit more complicated. (Shouldn't it be? We are geeks, aren't we?)
search= supercamel.myip.net edgesolutions.ca
Here, we're using DHCP to configure our card and hence not specifying the address, netmask, etc. The usehosts token sets up a symbolic link to an alternate hosts file and nfsexports sets up a link to an alternate exports file. An NIS domain name is also set up here. Notice also that we have overridden the global device and are configuring another card. Also note that QuickSwitch calls exportfs automatically to tell the NFS daemon to reread its configuration file.
All you have to do now when you get to work is type:
And as if by magic, you're ready to roll. If you want to see what's happening behind the scenes, use the -d parameter which sets the debug level:
switchto -d 2 work
If you want to back up those files (/etc/hosts, /etc/resolv.conf etc) before switching them, use the -b switch.
To see a list of available profiles, run QuickSwitch with the -l switch.
Now you want this toy to set you up on startup. If you're using LILO as your boot manager you can simply append the parameter SWITCHTO=profilename on startup, as so:
LILO: linux SWITCHTO=work
Or you can use the append token in your LILO configuration file as so:
image=zImage label=home root=/dev/sda1 append="SWITCHTO=home" read-only image=/boot/zImage label=work root=/dev/sda1 append="netswitch=work" read-only image=zImage label=last root=/dev/sda1 read-only
All you need to do to enable this feature is add the line /usr/bin/switchto -s to your rc.local or similar startup file. Using this method, if you do not specify a kernel parameter; QuickSwitch uses the last selected profile.
QuickSwitch parameters can be supplied on the command line for those one-time client visits. This is a very useful feature that lightens the burden of having to go out and edit all the required files.
To temporarily switch the network profile, type the new details in as so:
switchto ip:192.168.1.12 netmask:255.255.255.0
QuickSwitch then goes about its business reconfiguring the card, and swapping the files around. To learn more about the available parameters just type in switchto without any parameters or visit the QuickSwitch page.
Dynamic File Swapping
As of version 0.12, QuickSwitch comes with a feature called dynamic file swapping. With this, users have more flexibility and control over which files can be swapped for which profiles. Let us say, for example, a user uses a different printer and plugs into a separate monitor when she gets to work. Using the dynamic file switching feature, the /etc/printcap file and the XF86Setup files can be swapped in the work profile. To use this feature, we first define some swappable files in the files section, as so:
The tokens on the left of the "=" sign are used as identifiers inside of a profile, when when used cause the file to be symbolically linked. Let us edit our work profile as an example, using our newly defined files section.
When QuickSwitch switches to the profile named work, it notices the printcap and xconfig parameters in the both the files section and the work profile. It now knows that these files need to be switched and it creates a symbolic link named /etc/printcap pointing to /etc/printcap.work. It does the same for xconfig. This way a user can maintain configurations for other programs that QuickSwitch does not know about.
If a file needs a daemon to be restarted after a switch, add the command to the execpost parameter. For example, if you're swapping your Samba shares around, you can add the following line to the profile to have the Samba daemon restarted:
Using the GUI
QuickSwitch comes with a text-based graphical profile selector you could use on startup to switch your profiles around. For the GUI to run, you must first install the Perl Curses and the Curses::Widgets modules. These modules are easily available on CPAN and straightforward to set up.
The program is called switcher and is included with the QuickSwitch distribution. If you have installed the main QuickSwitch executable in a non-standard location, or changed its name, edit /etc/switch.conf and set the parameter switchtopath to the new path. Switcher uses this to call QuickSwitch to set a profile.
Switcher can be made to run on startup by adding it to your rc.local or equivalent startup file.
A lot of information about QuickSwitch was covered in this article, and as you can see, it is a very configurable and easy-to-use tool. QuickSwitch can also be integrated with other software to have it auto-detect a network, can be used to configure wireless LAN cards, and make you coffee every morning. OK, maybe not coffee, but there are still far too many features to be covered here. You can get more information by visiting the QuickSwitch home page.
Mohit Muthanna is a systems/network
integration based in Toronto, Ontario.