Automatic Network Profiles with Gentoo

Introduction

If you own a laptop and use it in different locations you may know this problem: Each location has its own settings for network access, gateways and dns-servers. After some research about gentoos networking-scripts and their advanced configuration options I found the solution documented here. It offers fully automatic configuration for wired and wireless interfaces with combined static and dynamic IP-Addresses. So lets see how to set this up!

Install necessary programs

The first step is to install the necessary programs. Here is a list:

  • wpa_supplicant
  • dhcpcd
  • ifplugd
  • arping
  • iputils

Since they are all in gentoos portage, you can just emerge them..

Configuring WLAN

Configuring wpa_supplicant

I wont go into much detail here since there are many howtos about configuring wpa_supplicant. Lets asume we have two Wireless Networks we want to connect to: WLAN1 and WLAN2 both using WPA-encryption. The according wpa_supplicant.conf may look like this:

  network={ 
    ssid="WLAN1"
    psk="somesupersecretpassword1"
    priority=20
  } 

  network={
    ssid="WLAN2"
    psk="anothersecretpassword"
    priority=19
  }

  network={
    key_mgmt=NONE
    priority=-9999999
  }

Note: The last network block will connect to any open network aviable. The priority tells which network to prefer if more than one are aviable at the same time. Higher number means higher priority.

Configuring gentoos networking-scripts

Now that we have set up wpa_supplicant lets procceed with gentoos /etc/conf.d/net. For our example we asume WLAN1 uses a static configuration and WLAN2 uses dhcp. Here is the according /etc/conf.d/net:

  modules_wlan0=( "wpa_supplicant" ) # tell wlan0 to use wpa_supplicant
  wpa_supplicant_wlan0="-Dwext" # additional options for wpa_supplicant
  associate_timeout_wlan0="15" # how long to wait for association

  config_WLAN1=( "192.168.178.5/24" ) # if we are connected to WLAN1, use this IP
  routes_WLAN1=( "default gw 192.168.178.1" ) # default gateway for WLAN1 
  dns_servers_WLAN1=( "192.168.178.1" ) # dns-server for WLAN1 

Notes: with the config_SSID, routes_SSID and dns_servers_SSID tokens you can set the static configuration for specific SSIDs. For all networks without a matching config, routes and dns_servers dhcp is used. You can get more info about this in /etc/conf.d/net.example Thats it for WLAN-config, now on to the wired interface.

Configuring LAN

For the wired interface we use three programs: ifplugd detects if a cable is plugged in and starts/stops the interface accordingly. arping handles the static configuration and dhcpcd is used for dynamic configuration. So lets see how to set this up in /etc/conf.d/net :

  config_eth0=( "arping" ) # use arping by default
  fallback_eth0=( "dhcp" ) # if no arping config matches use dhcp

  # this line sets up all known gateway+MAC compinations which have static
  # configuration, seperated by space
  # the format is "IP1,MAC1 IP2,MAC2 IP3,MAC3"...
  gateways_eth0=( "192.168.0.1,00:AA:BB:CC:DD:EE" )

  # setup the static config for the first network
  # format for the identifier:
  # assume IP AAA.BBB.CCC.DDD and MAC 00:11:22:33:44:55 leads to identifier
  # AAABBBCCCDDD_001122334455, note that every part of the IP-Address must have
  # three digits! The identifier has to use a defined IP+MAC pair from
  # gateways_eth0 
  config_192168000001_00AABBCCDDEE=( "192.168.0.10/24" )
  routes_192168000001_00AABBCCDDEE=( "default gw 192.168.0.1" )
  dns_servers_192168000001_00AABBCCDDEE=( "192.168.0.1" )

Notes: The IP and MAC for gateways_eth0 and the identifier should be a device on the network that is allways aviable. In most networks this is the gateway or router. The MAC-Address of the gateway can be found by using

arping <ip of gateway>

For more info on the arping config see again /etc/conf.d/net.example

Finalizing the Config

The last thing to do for a fully working config is to set up which interface to prefer if LAN and WLAN are up and connected at the same time. Normally this will be the LAN because it is faster than WLAN. To achive this we need two little lines in /etc/conf.d/net :

metric_eth0=10
metric_wlan0=20

Notes: Lower metric means higher priority, in this case all traffic will use eth0 (the wired interface) if both are aviable.

The complete /etc/conf.d/net

Here is the whole /etc/conf.d/net for your reference:

  ## LAN

  config_eth0=( "arping" ) # use arping by default
  fallback_eth0=( "dhcp" ) # if no arping config matches use dhcp

  # this line sets up all known gateway+MAC compinations which have static 
  # configuration, seperated by space
  # the format is "IP1,MAC1 IP2,MAC2 IP3,MAC3"...
  gateways_eth0=( "192.168.0.1,00:AA:BB:CC:DD:EE" )

  # setup the static config for the first network
  # format for the identifier:
  # assume IP AAA.BBB.CCC.DDD and MAC 00:11:22:33:44:55 leads to identifier
  # AAABBBCCCDDD_001122334455, note that every part of the IP-Address must have
  # three digits! The identifier has to use a defined IP+MAC pair from 
  # gateways_eth0
  config_192168000001_00AABBCCDDEE=( "192.168.0.10/24" )
  routes_192168000001_00AABBCCDDEE=( "default gw 192.168.0.1" )
  dns_servers_192168000001_00AABBCCDDEE=( "192.168.0.1" )

  ## WLAN

  modules_wlan0=( "wpa_supplicant" ) # tell wlan0 to use wpa_supplicant
  wpa_supplicant_wlan0="-Dwext"      # additional options for wpa_supplicant
  associate_timeout_wlan0="15"       # how long to wait for association

  config_WLAN1=( "192.168.178.5/24" )          # if we are connected to WLAN1, use this IP
  routes_WLAN1=( "default gw 192.168.178.1" )  # default gateway for WLAN1
  dns_servers_WLAN1=( "192.168.178.1" )        # dns-server for WLAN1

  ## Prefer LAN over WLAN

  metric_eth0=10
  metric_wlan0=20

I hope this makes sense to you and helps to make your life in multiple networks easier ^_^

linux/gentooautonetprofiles.txt · Last modified: 28.09.2010 16:14 by Seiichiro
CC Attribution-Share Alike 3.0 Unported
Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0