# For laptop users: different network configurations

## xkl

I think this is one of the main problems for laptop users. With this hack you will be able to switch between as many network configurations as you want. E.g. your home network, your work network, your university network and all networks of all your friends.

You are only three changes away from this. The first thing is a change deep in the startup files of the system.

Get the file /sbin/runscript.sh, change the line

```
[ "${NETSERVICE}" = "yes" ] && source "$(add_suffix /etc/conf.d/net)"
```

to

```
[ "${NETSERVICE}" = "yes" ] && source "$(add_suffix tc/conf.d/${myservice%.*})"
```

Now you are able to have more than one network configuration in /etc/conf.d. This is done in the following way:

Make your different config files named liked this:

```
/etc/conf.d/net.home

/etc/conf.d/net.work

/etc/conf.d/net.uni

/etc/conf.d/net.dhcp

```

Make some hard links to /etc/init.d/net.eth0:

```
/etc/init.d/net.home.eth0

/etc/init.d/net.work.eth0

/etc/init.d/net.uni.eth0

/etc/init.d/net.dhcp.eth0
```

Finally create some new runlevels in /etc/runlevels:

```
/etc/runlevels/home

/etc/runlevels/work

/etc/runlevels/uni

/etc/runlevels/dhcp
```

(these names do not need to be like those of the net config)

and add your init scripts to them:

```
rc-update add tri home

rc-update add tra home

rc-update add tru home

rc-update add lala home

rc-update add net.home.eth0 home
```

do the same for work/uni/dhcp

basically, you want to have the same scripts in it as in your default runlevel, except the net.eth0.

you can change your virtual runlevel with

```
rc home

rc work

rc dhcp
```

and so on....

Ok, the second and the third step is needed to boot into a plenty of runlevels.

Check out one unused "real" runlevel from inittab, I use 4 for this. And edit the according line to

```
l4:4:wait:/sbin/rc $PROFILE
```

so that the variable $PROFILE controls the runlevels. To choose the runlevel before bootup, add the assignment to $PROFILE to your bootmanager. I use grub and my grub.conf looks like this:

```

title=home

kernel /vmlinuz 4 root=xxx PROFILE=home

title=work

kernel /vmlinuz 4 root=xxx PROFILE=work

title=uni

kernel /vmlinuz 4 root=xxx PROFILE=uni

title=dhcp

kernel /vmlinuz 4 root=xxx PROFILE=dhcp
```

Please try this, test it and tease it in every way. Post your results.

KaiLast edited by xkl on Wed Oct 15, 2003 7:29 am; edited 2 times in total

----------

## hulk2nd

weee!

looks really great, gonna test it immediately. but one question:

```
Make some hard links to /etc/init.d/net.eth0:

/etc/conf.d/net.home.eth0

/etc/conf.d/net.work.eth0

/etc/conf.d/net.uni.eth0

/etc/conf.d/net.dhcp.eth0 
```

not to "/etc/init.d/net.home.eth0" etc???

and shouldnt it be

```
[ "${NETSERVICE}" = "yes" ] && source "$(add_suffix /etc/conf.d/${myservice%.*})"

```

instead of

```
[ "${NETSERVICE}" = "yes" ] && source"$(add_suffix tc/conf.d/${myservice%.*})"

```

greets,

hulk

----------

## xianx

Yes Yes Yes! I just got my Wireless working last night (It was a long shot, ended up using acx100 and the Windows drivers that came on the CD   :Shocked:  [USR2210 PCMCIA if anyone is interested])

And i'd like to know how to use multiple networks. I havent tried this yet, (i'm still at work) but i have a few questions already:

What do you do with resolv.conf? Obviously each network will have different DNS.

I actually will use eth0 for work, and wlan0 for home/coffee shops/etc. How does this change things?

thanks!  :Smile: 

----------

## al

Looks good.I'll have to try that sometime!

 :Smile: 

Posted using links.

----------

## nacul

Hi,

I solved the same problem in a different way:

```
 emerge quickswitch
```

This is a simple script that can set many different parameters in the system. I use it only to configure the network, but it seems very powerful. By the way, no need to create new runlevels, links, and so on. I got it up and running in 5 minutes.

All you have to do is edit /etc/quickswith/switchto.conf.

Then, in the command prompt:

```
 switchto home 

switchto office

switchto girl

switchto whatever_profile_you_have

```

My 2 cents

Andre

----------

## xkl

 *hulk2nd wrote:*   

> weee!
> 
> looks really great, gonna test it immediately. but one question:
> 
> ```
> ...

 

Yes.

 *hulk2nd wrote:*   

> 
> 
> and shouldnt it be
> 
> ```
> ...

 

yes, I thought it was cut and pasted....

xkl

----------

## xkl

 *xianx wrote:*   

> 
> 
> And i'd like to know how to use multiple networks. I havent tried this yet, (i'm still at work) but i have a few questions already:
> 
> What do you do with resolv.conf? Obviously each network will have different DNS.
> ...

 

this is quite easy, I have multiple resolv.conf/hosts/... files, and let them copy to /etc/resolv.conf on each change of the runlevel.

 *xianx wrote:*   

> 
> 
> I actually will use eth0 for work, and wlan0 for home/coffee shops/etc. How does this change things?
> 
> 

 

It shouldn't, but try it.

xkl

----------

## optilude

For a slightly more generic solution, you could try my hprofile set of scripts:

https://forums.gentoo.org/viewtopic.php?t=46180

This integrates with the Gentoo runlevel model to let you arbitrarily replace configuration files and change runlevels based on an arbitrary condition. Simply put, this means you can use hprofile to automatically select the appropriate version of your configuration files (and individual users can change configuration files in their home directories), and your runlevel at boot, depending on your hardware configuration. 

I use this so I can boot gentoo both within vmware and on its own, changing runlevels and files such as modules.autoload and XF86Config to files appropriate for the hardware configuration. 

One important advantage over the original post here, besides being general purpose, is that it requires no manual editing of any gentoo rc scripts, which means that your changes won't be overwritten when you update baselayout etc.  :Smile: 

Best wishes,

Martin

----------

## xentric

After following xkl's little howto I just want to add a couple of changes that I needed

to make in order to get this to work...

1. Remove pcmcia from all runlevels:

```
$ rc-update del pcmcia
```

2. Add the following to my /etc/init.d/net.*.eth0 files so pcmcia is started when you start

one of the net.*.eth0 scripts:

```
depend() {

        need pcmcia

}
```

3. Run depscan:

```
$ depscan.sh
```

4. Add net.*.eth0 script to the matching runlevel:

```
$ rc-update add net.home.eth0 home

$ rc-update add net.work.eth0 work

$ rc-update add net.dhcp.eth0 dhcp
```

5. I use LILO so I had to add PROFILE to the append line:

```
image=/boot/bzImage-2.4.20-r7

        label=dhcp

        root=/dev/hda5

        append="PROFILE=dhcp idebus=33"

image=/boot/bzImage-2.4.20-r7

        label=home

        root=/dev/hda5

        append="PROFILE=home idebus=33"

image=/boot/bzImage-2.4.20-r7

        label=work

        root=/dev/hda5

        append="PROFILE=work idebus=33"
```

Now I can boot into the desired runlevel from the LILO bootmenu   :Very Happy: 

Thanks guys!

----------

## tomapd

Thanks for a nice tip!!!

It's much better than my way.   :Razz: 

----------

## xmoy

I added this to my net.eth0 (after start() { )

```
        0<&2 2<&-

        default="192.168.0.2"

        echo -n "ip [$default] "

        read ip

        if [[ $ip = "" ]]; then

                ip="$default"

        fi

        if [[ $ip = "dhcp" ]]; then

                iface_eth0="$ip"

        else

                default="192.168.0.255"

                echo -n "broadcast [$default] "

                read bc

                if [[ $bc = "" ]]; then

                        bc="$default"

                fi

                default="255.255.255.0"

                echo -n "netmask [$default] "

                read nm

                if [[ $nm = "" ]]; then

                        nm="$default"

                fi

                iface_eth0="$ip broadcast $bc netmask $nm"

        fi
```

Now, I can enter an IP address when net.eth0 is started (if I just press enter, the default value is used). If I enter 'dhcp', dhcp is used

----------

## Massimo B.

Right now this is the part of my /sbin/runscipt.sh with the modified line:

```
# Source configuration files.

# (1) Source /etc/conf.d/${myservice} to get initscript-specific

#     configuration (if it exists).

# (2) Source /etc/conf.d/net if it is a net.* service

# (3) Source /etc/rc.conf to pick up potentially overriding

#     configuration, if the system administrator chose to put it

#     there (if it exists).

[[ -e $(add_suffix /etc/conf.d/${myservice}) ]] && source "$(add_suffix /etc/conf.d/${myservice})"

[[ -e $(add_suffix /etc/conf.d/net) ]]          && \

[[ ${NETSERVICE} == "yes" ]]                    && source "$(add_suffix /etc/conf.d/${myservice%.*})"

[[ -e $(add_suffix /etc/rc.conf) ]]             && source "$(add_suffix /etc/rc.conf)"
```

But I don't understand, why the first line with ${myservice} is not sufficient to do the job.

For example, my service is called net.lan-fh.eth0 and my conf.d file is called net.lan-fh.

So {myservice%.*} should look for a lan-fh.eth0%.*,  but it works. Where is my error in reasoning?

----------

## UberLord

OK - this is vastly out of date. Here's what works with >=baselayout-1.11.12

if /etc/conf.d/${configfile}.${runlevel} exists then use that

else

if /etc/conf.d/${configfile} exists then use that

${configfile} is any file in /etc/conf.d which corresponds to a matching /etc/init.d/${script}

(exception - all net.xxx scripts load conf.d/net)

${runlevel} is the current runlevel

So in the runlevel foobar

/etc/init.d/vsftpd

load /etc/conf.d/vsftpd.foobar if it exists, otherwise /etc/conf.d/vsftpd

/etc/init.d/vsftpd.virtual -> /etc/init.d/vsftpd (symbolic link)

load /etc/conf.d/vsftpd.virtual.foobar if it exists, otherwise /etc/conf.d/vsftpd.virtual

/etc/init.d/net.eth0 -> /etc/init.d/net.lo (symbolic link)

load /etc/conf.d/net.foobar if it exists, otherwise /etc/conf.d/net

To change runlevel at boot, append "softlevel=foobar" to your kernel line. This would change you to the foobar runlevel.

You can use "rc foobar" to do the same thing at the command prompt.

----------

## househead

Uberlord > how does this behaviour work with the /etc/conf.d/wireless file? or should I put my runlevel-specific wireless settings in /etc/conf.d/net?

----------

## jamapii

I prefer (and use) a much simpler solution. I don't change any baselayout files, I don't make a lot of copies of config files, I don't create additional links in /etc/init.d/... and (optionally) I try to trim down the number of runlevels.

My first approach is to create runlevels if necessary. The "softlevel" variable contains the runlevel name. Now I take advantage of the fact that (almost all) /etc/conf.d/... files are just shell scripts sourced in, so I make the variables depend on $softlevel.

This may be doomed if the config files aren't really meant to be shell scripts, but I still prefer it over maintaining a baselayout patch, initscript links and multiple copies of nearly identical conf.d files. With my solution, the complex and ugly stuff is contained in a few config files.

Depending on the location, I put preup() / postup() functions in the environment that fix the config files for dependent services, e.g. sendmail and ntpd.

My favourite ultimate solution would be to detect the location automatically at boot, possibly by switching on all network cards and trying to find out what MAC addresses are on the net, using $softlevel as a hint. That's what I did on the old laptop. Using dhcp as much as possible is also a good thing for laptops. 

The order in which config files are sourced is important, up to recently it was just simply backwards - generic overrides specific. But the changes mentioned by UberLord seem to make it much more complex, so it should be used with caution.

I won't use separate config files for runlevels if it prevents the generic file from being sourced, because it probably involves a lot of duplication. But if the config files someday aren't treated as scripts anymore, I'll have to find a new solution.

----------

## househead

I actually think the baselayout method is far simpler. I just need to figure out a method (without using dhcp) that I can use different DNS servers depending whether I am on the wired work network, or my wirleless home (vpn'd) network

----------

## jamapii

 *househead wrote:*   

> I just need to figure out a method (without using dhcp) that I can use different DNS servers depending whether I am on the wired work network, or my wirleless home (vpn'd) network

 

You can use separate runlevels and change or switch /etc/resolv.conf depending on the runlevel in a preup() or postup() shell function. But as long as you use different network adapters in different locations, you can take a shortcut.

In /etc/conf.d/net.eth0 write

```
postup() { cp /etc/resolv.conf.work /etc/resolv.conf; }
```

 and in /etc/conf.d/net.wlan0 (assuming it's called wlan0)

```
postup() { cp /etc/resolv.conf.home /etc/resolv.conf; }
```

 or add the line to your existing postup() function. It is important to use postup() because this is only executed if the network starts successfully. Also, there must be no postup() in /etc/conf.d/net because it would take precedence. If eth0 starts even at home, ping a known ip address before copying, like "ping -c 2 -w 2 10.23.45.67 && cp ..."

----------

## Massimo B.

So finally I have to correct my (still working) net configs to the new baselayout.

That's the interesting point:

 *UberLord wrote:*   

> 
> 
> /etc/init.d/net.eth0 -> /etc/init.d/net.lo (symbolic link)
> 
> load /etc/conf.d/net.foobar if it exists, otherwise /etc/conf.d/net
> ...

 

So I don't need anymore in /etc/init.d

```
net.wlan-dhcp.eth1 -> net.lo*

net.wlan-mops.eth1 -> net.lo*

net.wlan-other.eth1 -> net.lo*
```

?

Just net.eth0 and net.eth1, with the apropriate conf.d/net.wlan-mops, net.wlan.other according to the runlevel?

Disadvantage would be, that I can't start anymore a specific network by    /etc/init.d/net.wlan-other.eth1 start    don't care which runlevel is up.

Would be fine if I get the large list of networks working with the runlevel method. I still don't have the courage to use ifplugd.

----------

## Massimo B.

It doesn't work the way you've told:

```
# ls -al /etc/init.d/net*

/etc/init.d/net.eth0 -> net.lo*

/etc/init.d/net.eth1 -> net.lo*

/etc/init.d/net.lo*

# rc-status wlan-schanz

Runlevel: wlan-schanz

 airport                                                                                                            [ started ]

 net.eth1                                                                                                             [   off ]

# cat /etc/conf.d/net.wlan-schanz |grep -v '#'

config_eth1=( "192.168.10.22/24" )

routes_eth1=( "default via 192.168.10.200" )

# /etc/init.d/net.eth1 restart

 * Starting eth1

 *   Bringing up eth1

 *     Configuration not set for eth1 - assuming dhcp
```

So it doesn't look for my net.[runlevel]. I had a look into /sbin/runscript.sh:

```
#line61:

[[ -e $(add_suffix /etc/conf.d/${myservice}) ]] && source "$(add_suffix /etc/conf.d/${myservice})"

[[ -e $(add_suffix /etc/conf.d/net) ]]          && \

[[ ${NETSERVICE} == "yes" ]]                    && source "$(add_suffix /etc/conf.d/net)"
```

Finally it only loads net? My baselayout is 1.11.13.

Someone has started a  bugreport  for me.

By the way, if I start the runlevel: 

```
rc wlan-schanz

 * Lade Airport-Modul ...                                                                                                [ ok ]

 * WARNING:  "net.eth1" has already been started.
```

..net.eth1 doesn't start, because net.lo is in the boot runlevel. Before I have changed my hardlinks to net.lo into softlinks (as the doc explains), this wasn't the case.

----------

## dirtyd

Check this out:

http://www.gentoo.org/doc/en/handbook/2005.1/handbook-x86.xml?full=1#book_part4

specifically:

```

# We can define nameservers and other things too

# NOTE: DHCP will override these unless it's told not too

dns_servers_ESSID1=( "192.168.0.1" "192.168.0.2" )

dns_domain_ESSID1="some.domain"

dns_search_domains_ESSID1="search.this.domain search.that.domain"

# You override by the MAC address of the Access Point

# This handy if you goto different locations that have the same ESSID

config_001122334455=( "dhcp" )

dhcpcd_001122334455="-t 10"

dns_servers_001122334455=( "192.168.0.1" "192.168.0.2" )

```

You can also use those same variables with ethx suffixes like this for eth0:

```

dns_servers_eth0=("192.168.0.1" "192.168.0.2" )

dns_search_domains_eth0="my.local.domain.name my.private.domain.name my.providers.domain.name"

```

etc.

You may want to just use the default, dhcp, and set just what you need based on interface.  Like so:

```

# If picking your own ip while getting everything else from dhcp

# was your only goal then this line is all you need

dhcpcd_eth0="-t 5 -s 192.168.0.100"

# Set your own ip and gateway and inform the dhcp server about your ip

dhcpcd_eth1="-t 5 -s 192.168.2.100 -G 192.168.0.1"

# Don't use the dns servers from your dhcp server.  Maybe for security reasons they are bogus  ;)

dhcp_eth1="nodns"

dns_servers_eth1=("192.168.2.1" "192.168.2.2" )

```

And lastly in that same document is the big scary ifplugd in simplified cut and paste format.

It's at the bottom of the document in section 6.b.

http://www.gentoo.org/doc/en/handbook/2005.1/handbook-x86.xml?full=1#book_part4_chap6

This all works with the new RC_USE_CONFIG_PROFILE variable in /etc/conf.d/rc.  Meaning you can

add a softlevel=home t your kernel parameters to make a /etc/conf.d/net.home override /etc/conf.d/net.

And you can also execute "rc home" to get the same effect.

----------

