Make Laptop Settings Roam with Your Network






Make Laptop Settings Roam with Your Network

Use the laptop-net program to configure settings that change as you connect to different networks. Make your work settings follow you to work, and your home settings follow you home.

When you use a laptop, particularly as your primary system, you start to notice some of the ways most systems are biased toward a desktop computer. Primarily, the assumption is that all of your settings will stay the same no matter where you are. Yet, if you take your laptop to and from work you know this simply isn't the case. Not only do network settings often change, but printers, file shares, and all sorts of other aspects of the system are different depending on where you are. As a laptop user, it would certainly be nice if there were a way to have classes of settings that applied based on where you were. Lucky for you, the laptop-net package takes care of these basic needs and more.

The laptop-net program does a number of things, but the primary thing it allows you to do is to run particular scripts and apply particular settings based on which IP an interface currently has. Even though your home, work, and friend's house may all use private IP address ranges, they all probably use slightly different schemes, and, more importantly, you probably get assigned a particular IP or range of IPs depending on which network you are on. With laptop-net, you can say "Whenever I have this IP, run these programs and enable these settings." Because of the way laptop-net is designed, what you can do is limited only by your scripting ability. In this hack, we will describe how to integrate laptop-net into an Ubuntu system and walk you through some common configuration schemes.

Install laptop-net

The first step is to install laptop-net. Using your preferred package manager install the laptop-net and laptop-net-doc packages. Once everything is installed, you will want to reconfigure laptop-net:

$ sudo dpkg-reconfigure laptop-net
            

Now, by default, laptop-net can do a number of nice things such as detect cable insertion and automatically bring up and take down interfaces. We've noticed, however, that these automatic functions can sometimes interfere with the way Ubuntu wants to configure the networkespecially if you are using a package like NetworkManagerso you need to disable some of this functionality in laptop-net.

As the laptop-net configuration program runs, be sure to tell it that you are going to use DHCP for your network and that your Ethernet adapter does not support MII, even if it does.

Since laptop-net won't automatically engage when every interface is brought up, you must add a special script to /etc/dhcp3/dhclient-exit-hooks.d. Scripts in this directory are executed when changes occur in dhclient (specifically, when the /etc/dhcp3/dhclient-script runs and executes the exit_with_hooks function). Name the following file /etc/dhcp3/dhclient-exit-hooks.d/laptop-net:

if [ $reason = 'BOUND' ]; then
   /usr/share/laptop-net/profile-change $interface
fi
if [ $reason = 'RELEASE' ]; then
   /usr/share/laptop-net/profile-change $interface down
fi

This script will tell laptop-net to change the profile based on the current interface when a new interface gets a lease, and when the DHCP lease is released, it will take that interface down.

We noticed a bug in our version of the package where the /var/run/laptop-net directory was not created, so profiles weren't being changed. To fix this, we added an extra line to /etc/init.d/laptop-net. Locate the following section in the file:

case "${1}" in
    ("start")
        [ $# -eq 1 ] || usage
        clean_state
        start_ifd
    ;;

and change it to:

case "${1}" in
    ("start")
        [ $# -eq 1 ] || usage
        mkdir -p /var/run/laptop-net
        clean_state
        start_ifd
    ;;

Now restart laptop-net:

$ sudo /etc/init.d/laptop-net restart
            

Configure laptop-net Profiles

laptop-net does most of its configuration work through different profiles that you need to configure. These profiles reside within the /etc/laptop-net/profiles directory and contain the configuration information for specific modes and networks that you might want to connect to. The /etc/laptop-net/profiles directory will end up containing one directory for each new profile you want to configure. A laptop-net profile itself can contain a number of different configuration files and scripts (check /usr/share/laptop-net-doc/ for more details), but here is a list of the main files:


patterns

This file contains a list of IP or state patterns to match. If laptop-net matches a pattern in this file, it will then select this particular profile.


files.d/

This directory contains a subset of your root filesystem. When this profile is selected, laptop-net will copy any files it finds here to the corresponding system directory, overwriting existing files with the same name. For instance, if you add a file in files.d/etc/fstab, when this profile is selected, that file will be copied over the top of the system /etc/fstab.


after-select

This is an optional shell script that laptop-net will execute after a profile has been selected. If there are any fancy tweaks or service restarts that you want to make, you can put them in this file.


before-deselect

This script is also optional and is like the after-select script, only it is executed before a particular profile is deselected. You might use this script to undo particular changes you have made to a system before the network interface is taken down.

No matter what networks you want to configure for yourself, there are a couple of default profiles you should set up first: one for when the network is offline and one for when you are on some unknown network.

Configure the offline profile

To configure the offline profile, create a new directory called offline in /etc/laptop-net/profiles and change to that directory:

[email protected]:~$ sudo mkdir /etc/laptop-net/profiles/offline
[email protected]:~$ cd /etc/laptop-net/profiles/offline
[email protected]:/etc/laptop-net/profiles/offline$

Now create a file in this directory called patterns and have it contain the following lines:

down
unknown

Next, create a files.d directory here. Whenever you change system files with other profiles, it's a good idea to store the original default version of the file in the offline profile; that way, everything will go back to normal when the network is offline, and you won't risk overwriting a file for good:

[email protected]:/etc/laptop-net/profiles/offline$ sudo mkdir files.d
               

The next step is to create a file called after-select and make it executable:

/etc/laptop-net/profiles/offline$ sudo touch after-select
/etc/laptop-net/profiles/offline$ sudo chmod a+x after-select
               

This file will run any programs or restart any scripts that you tell it to, but for now you can have it do something basic to give you diagnostic information when it is selected. Add the following lines to the file:

#!/bin/sh

echo "offline" > /tmp/laptop-net-state

Configure the default network profile

The next step is to create a "catch-all" default network profile. This profile will execute any network-based operations you want to perform whenever you have a network connection but don't quite know which network you are connected to. First, create the profile directory and name it zzz-default. The reason you start with the zzz is that laptop-net goes through the profiles sequentially. Since this profile will match all IP addresses, you want to give your other profiles a chance to be selected first:

[email protected]:/etc/laptop-net/profiles/$ sudo mkdir zzz-default
[email protected]:/etc/laptop-net/profiles/$ cd zzz-default
[email protected]:/etc/laptop-net/profiles/zzz-default$

Now create the same files.d directory and patterns file. If there are any configuration settings you want to always have whenever you are connected to a strange network, you can copy them to the files.d directory:

[email protected]:/etc/laptop-net/profiles/zzz-default$ sudo mkdir files.d
[email protected]:/etc/laptop-net/profiles/zzz-default$ sudo touch patterns
               

Place the following line in the patterns file:

*.*.*.*

This line will match any possible IP address.

The final step is to create the after-select script that is run when this profile is selected and to put the following lines in it for diagnostics:

#!/bin/sh

echo "zzz-default" > /tmp/laptop-net-state

Don't forget to run:

$ sudo chmod a+x after-select
               

to ensure the script is executable.

One thing we like to do when our zzz-default profiles are selected is to throw up a very restrictive firewall. Since we're connected to a presumably unknown network, this ensures that all the shields are up. You could also perform a number of other security-tightening measures in this script.


Test your configuration

With this new configuration in place, restart laptop-net:

$ sudo /etc/init.d/laptop-net restart
               

Now click SystemNetworking and deactivate your Ethernet connection (or wireless if that is what you are using). Now check /tmp/laptop-net-state:

$ cat /tmp/laptop-net-state
offline

Now activate your network connection and check again once it comes up:

$ cat /tmp/laptop-net-state
zzz-default

You can also verify what laptop-net is doing by examining the syslog file. laptop-net will log there when it changes network settings, so you can grep the file for laptop-net, which will show you only its log information:

$ grep laptop-net /var/log/syslog
...
Mar  4 16:12:56 ubuntu laptop-net: Selecting network profile "zzz-default" 
Mar  4 15:19:42 ubuntu laptop-net: Deselecting network profile "zzz-default" 
Mar  4 15:19:42 ubuntu laptop-net: Selecting network profile "offline" 
Mar  4 15:24:06 ubuntu laptop-net: Deselecting network profile "offline" 
Mar  4 15:24:07 ubuntu laptop-net: Selecting network profile "zzz-default"

Create Personal laptop-net Profiles

Once you have confirmed laptop-net is functioning, it's time to create specific profiles for networks you commonly connect to. Let's assume you have two networks, one for home and one for work. To make things simple, you can just copy over the zzz-default profile and use it as a base template for new configurations:

[email protected]:~$ cd /etc/laptop-net/profiles/
[email protected]:/etc/laptop-net/profiles/$ sudo cp -a zzz-default home
[email protected]:/etc/laptop-net/profiles/$ sudo cp -a zzz-default work
            

Configure the patterns file

The first thing you will need to change is the patterns file for each of these new profiles. Let's assume your home network assigns you an IP address somewhere in the 192.168.0.1 network, no matter whether it is on your Ethernet or wireless card, and your work network always assigns you 10.1.1.50 on your Ethernet card and 10.1.1.100 on your wireless card. You would then edit the patterns file in your home profile and make it contain only the following line:

192.168.0.*

This pattern will match any IP from 192.168.0.1 to 192.168.0.255. Next, you edit the patterns file in your work profile and make it contain two lines:

10.1.1.50
10.1.1.100

When you can, try to be as explicit as possible when defining patterns. That 192.168.0.* pattern will match a great number of home networks, so you might end up setting your home profile when you connect at a friend's house. Ideally, you will try to set up your networks so that you get assigned one or two static IP addresses.

Configure the after-select script

Once the pattern file is set up, edit the after-select script in both the home and work profiles. Change the diagnostic line in that script from:

echo "zzz-default" > /tmp/laptop-net-state

to:

echo "home" > /tmp/laptop-net-state

and:

echo "work" > /tmp/laptop-net-state

in your home and work scripts, respectively. Now add any particular programs you want to run or other things you would like to do in these after-select files.

For instance, if you want your default printer to be set to your home printer when you are at home and your work printer when you are at work, configure your printer settings the way you want them for home, and then copy the /etc/cups/printers.conf file to /etc/laptop-net/profiles/home/files.d/etc/cups/printers.conf (you will need to create the etc and etc/cups directories within the files.d directory). Next, configure the printer settings to suit your work environment and copy the /etc/cups/printers.conf to /etc/laptop-net/profiles/work/files.d/etc/cups/printers.conf. Finally, in the after-select scripts for both home and work profiles, add the line:

/etc/init.d/cupsys restart

to refresh the settings when that profile is selected.

Repeat this process for any other settings you want to change. Copy the relevant settings to the corresponding directory under files.d, and if a change requires the service to be restarted, add the corresponding command to the end of your after-select script.

For more information about configuration options in laptop-net, check out the documentation that was installed under /usr/share/doc/laptop-net-doc/.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows