# [solved] Switching between network configurations?

## dr.nil

Hi,

I usually use DHCP on my laptop. But sometimes I visit a client where I need a special network config (more than one IP addres on the eth0 interface, different routes). To switch network configs I edit /etc/conf.d/net commenting out DHCP and enabling the settings for that customer. Back in the hotel I need DHCP again so the whole config editing begins again.

Is there a better way to switch network configs via a simple command or even a GUI for that?

-dirkLast edited by dr.nil on Fri May 24, 2013 12:22 am; edited 1 time in total

----------

## BillWho

dr.nil,

Try using two files, net.dhcp and net.home. Then it's just a matter of setting a symlink to the proper file.

----------

## syn0ptik

or change the MAC-address on client side, for proper configuration.

----------

## dr.nil

 *syn0ptik wrote:*   

> or change the MAC-address on client side, for proper configuration.

 

I'm not sure I understand how this is supposed to help?

----------

## khayyam

dr.nl ...

This is really a task for runlevels.

Firstly, remove all the network related services you have currently added to the 'default' runlevel (net.eth0, sshd, ntp-client, etc), then create a runlevel for this specific configuration:

```
# mkdir /etc/runlevels/test

# rc-update -s add default test
```

Now we have replicated 'default' into 'test', all the services set to start in 'default' will also be set to run in 'test'. Here we used '-s, --stack', which is not mentioned in the above linked docs, however, its the best method of replicating a particular runlevel, and saves having to sym-link each file individually (see man rc-update for more details).

Now, add whatever services should start in runlevel 'test':

```
# rc-update add net.eth0 test

# rc-config list test

Init scripts to be started by runlevel test

   net.eth0
```

Note that this doesn't list all the services in 'defualt' (those we '--stacked'), only those explicitly added to 'test'.

Next create specific a 'net.$RC_RUNLEVEL' configuration file for the runlevel, in the above example that would be /etc/conf.d/net.test, so one configuration for each network setup to match your runlevel(s). You should then remove/empty 'net' as each network setup is configured seperately.

This done, you should be able to call "rc test" and the network, and services set to run in that runlevel, should start/restart. Calling "rc default" should switch back to the "default" runlevel (ie, sans networking).

You can add as many runlevels as you wish, each with a specific network configuration and/or services. You can also configure 'softlevel=runlevel' to boot to a specific configuration (see the docs above).

best ... khay

----------

## kimmie

Perhaps you could try the ARPING module? It looks to me like this allows you to probe for specific gateways using arp (well, /usr/share/doc/openrc*/net.example.bz2 talks about gateways, but any known host on the network should do) and then apply config based on what's found. You could have an overall fallback to dhcp.

I also have various alternate configs /etc/conf.d/net for the same reason as you, so I'm going to give this a try myself....

----------

## khayyam

 *kimmie wrote:*   

> Perhaps you could try the ARPING module? It looks to me like this allows you to probe for specific gateways using arp (well, /usr/share/doc/openrc*/net.example.bz2 talks about gateways, but any known host on the network should do) and then apply config based on what's found. You could have an overall fallback to dhcp.

 

kimmie ... that would be one solution, but you loose some flexibility, for example, having a specific postup() function for that network, or configuring seperate iptables rulesets, or using ${SVCNAME} in general. I guess that really depends on whats needed, but runlevels can make doing complicated setups fairly easy. 

 *kimmie wrote:*   

> I also have various alternate configs /etc/conf.d/net for the same reason as you, so I'm going to give this a try myself....

 

OK, you should see bug #458090 so that you don't fall foul of that. I'd be interested in hearing how you get along.

best ... khay

----------

## dr.nil

 *khayyam wrote:*   

> 
> 
> This is really a task for runlevels.
> 
> 

 

khay, this looks like a very elegant solution. I have set it up and it works so far. The only thing that's a bit annoying is that I still manually have to stop the net.eth0 service before switching runlevels. Am I still missing something to fix this?

-dirk

----------

## khayyam

 *dr.nil wrote:*   

> this looks like a very elegant solution. I have set it up and it works so far. The only thing that's a bit annoying is that I still manually have to stop the net.eth0 service before switching runlevels. Am I still missing something to fix this?

 

EDIT2: So scratch ALL that (previous content removed). I should really never take reports at face value. After spending sometime trying to figure out why exactly my (now removed) example didn't work as I expected (or why it worked, and then subsequently didn't) I thought to reproduce the issue as it was presented, and lo and behold the interfaces are treated uniquely for each runlevel and restarted.

I created two runlevels using two seperate network configurations, with different preup() nad postup() functions, services, and two seperate AP's to authenticate to ... and it works just as stated in the above post.

```
[root@host: ~]# rc test1

 * Caching service dependencies ...

 * Bringing up interface wlan0

 *   Running preup ...

 *     Reloading postfix  ...                                              [ ok ]

 *   Starting wpa_supplicant on wlan0 ...

 Successfully initialized wpa_supplicant                                   [ ok ]

 *   Starting wpa_cli on wlan0 ...                                         [ ok ]

 *   Backgrounding ... ...

 * WARNING: net.wlan0 has started, but is inactive

 * WARNING: pdnsd is scheduled to start when net.wlan0 has started

 * WARNING: ntp-client is scheduled to start when net.wlan0 has started

[root@host: ~]# rc test2

 * Stopping pdnsd ...                                                      [ ok ]

 * Bringing down interface wlan0

 *   Stopping dhcpcd on wlan0 ...                                          [ ok ]

 *   Stopping wpa_cli on wlan0 ...                                         [ ok ]

 *   Stopping wpa_supplicant on wlan0 ...                                  [ ok ]

 *   Running postdown ...

 *     Reloading postfix  ...                                              [ ok ]

 * Bringing up interface wlan0

 *   Running preup ...

 *   Starting wpa_supplicant on wlan0 ...

 Successfully initialized wpa_supplicant

 *   Starting wpa_cli on wlan0 ...                                         [ ok ]

 *   Backgrounding ... ...

 * WARNING: net.wlan0 has started, but is inactive
```

Both connections were established as configured, so what is happening your end I can only guess. My first guess would be that you still have net.eth0, or some service that 'need[s] net', in the 'default' runlevel but that wouldn't explain why the interface isn't restarted. Anyhow, without further info I can only speculate.

best ... khay

----------

