menu.jpg  ::  Home ::  Computing ::  Downloads ::  Scooter ::  Links ::  Music ::  Nonsense ::  Mail :: 

PC: Mehrere Netzwerk-Profile on-the-fly unter Debian verwenden

Gerade ein Notebook möchte man in verschiedenen Netzwerkumgebungen benutzen können, ohne mit 'ifconfig' und 'route' manuell herum hantieren zu müssen. Debian bietet von Haus aus über die Mechanismen von 'ifup' und 'ifdown' dazu alle Möglichkeiten, bestimmte vordefinierte Schemata zu laden.

1. Netzwerkkonfiguration unter Debian

Unter Debian werden die verschiedenen Netzwerk-Interfaces in der Datei '/etc/network/interfaces' konfiguriert. Beim Booten verwendet 'ifup' die für einzelne Devices hier hinterlegten Einstellungen und führt ggfs noch hier hinterlegte Anweisungen vor und nach Aktivierung oder Deaktivierung des Interfaces aus. Die Interfaces können mittels 'map'-Anweisungen für verschiedene Schemata konfiguriert werden.

Die Manpage zu interfaces(5) sowie die Dokumentation zum Paket 'ifupdown' nebst Beispielen ist übrigens absolut lesenswert.

Als Beispiel folgt hier die - leicht unkenntlich gemachte - '/etc/network/interfaces' meines Laptops.

   # /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
   #
   # $Id$

   # The loopback interface is started by '/etc/init.d/networking'
   #
   auto lo
   iface lo inet loopback

   # The first network card is started by '/etc/init.d/networking'
   #
   auto eth0
   mapping eth0
           script /etc/network/scheme-test.sh
           map AtHomeLAN eth0-AtHomeLAN
           map OnTheRoad eth0-OnTheRoad
           map WithDHCP eth0-WithDHCP

   iface eth0-AtHomeLAN inet static
           address nn.nn.nn.nn
           netmask nn.nn.nn.nn
           network nn.nn.nn.nn
           broadcast nn.nn.nn.nn
           gateway nn.nn.nn.nn
           up [ -x /usr/sbin/rdate ] && ping -c 1 nn.nn.nn.nn > /dev/null && \
              /usr/sbin/rdate -s nn.nn.nn.nn && /sbin/hwclock --systohc --utc
           up cd /etc/exim && ln -sf exim.conf-AtHomeLAN exim.conf
           down route del default && \
              [ `ls -l /var/spool/exim/input | wc -l` -gt 1 ] && \
              ping -c 1 nn.nn.nn.nn > /dev/null && /usr/sbin/exim -qf

   iface eth0-OnTheRoad inet static
           address nn.nn.nn.nn
           netmask nn.nn.nn.nn
           network nn.nn.nn.nn
           broadcast nn.nn.nn.nn
           up [ -x /usr/local/sbin/fw-up ] && /usr/local/sbin/fw-up
           up cd /etc/exim && ln -sf exim.conf-OnTheRoad exim.conf
           down [ -x /usr/local/sbin/fw-down ] && /usr/local/sbin/fw-down

   iface eth0-WithDHCP inet dhcp
           up [ -x /usr/local/sbin/fw-up ] && /usr/local/sbin/fw-up
           up cd /etc/exim && ln -sf exim.conf-OnTheRoad exim.conf
           down [ -x /usr/local/sbin/fw-down ] && /usr/local/sbin/fw-down

   # The second network card is started by 'cardctl/pcmcia'
   #
   mapping eth1
           script /etc/network/scheme-test.sh
           map AtHomeLAN eth1-AtHomeLAN
           map OnTheRoad eth1-OnTheRoad
           map WithDHCP eth1-WithDHCP

   iface eth1-AtHomeLAN inet static
           address nn.nn.nn.nn
           netmask nn.nn.nn.nn
           network nn.nn.nn.nn
           broadcast nn.nn.nn.nn
           wireless_essid "WLAN AtHome"
           wireless_rate auto
           wireless_mode Ad-Hoc
           up iwconfig eth1 power min period 2 power max period 4
           up iwconfig eth1 key restricted nnnn-nnnn-nnnn-nnnn-nnnn-nnnn-nn && \
              iwconfig eth1 key on
           down iwconfig eth1 key off

   iface eth1-OnTheRoad inet static
           address nn.nn.nn.nn
           netmask nn.nn.nn.nn
           network nn.nn.nn.nn
           broadcast nn.nn.nn.nn
           wireless_essid "WLAN OnTheRoad"
           wireless_rate auto
           wireless_mode Ad-Hoc
           up iwconfig eth1 power min period 2 power max period 4
           up iwconfig eth1 key restricted nnnn-nnnn-nnnn-nnnn-nnnn-nnnn-nn && \
              iwconfig eth1 key on
           down iwconfig eth1 key off

   iface eth1-WithDHCP inet dhcp
           wireless_essid "WLAN WithDHCP"
           wireless_rate auto
           wireless_mode Managed
           up iwconfig eth1 power min period 2 power max period 4
           up iwconfig eth1 key restricted nnnn-nnnn-nnnn-nnnn-nnnn-nnnn-nn && \
              iwconfig eth1 key on
           down iwconfig eth1 key off

Diese enthält folgende Schemata für 'eth0' (LAN) und 'eth1' (WLAN):

Die Mappings findet der Kleine mit folgendem Skript, das 'ifup' über die Anweisung 'script' jedes Mail ausführt:

   #!/bin/sh
   #
   # $Id$
   #
   # /etc/network/scheme-test.sh
   #
   # This script is called via '/etc/init.d/networking', '/etc/init.d/pcmcia' and
   # './interfaces'. It tries to read the file '${SCHEME_FILE}' or the kernel's
   # append line to find the network environment. So it is usable with switches
   # passed to the kernel from boot menu, when '${SCHEME_FILE}' is not present yet
   # (on Debian you have to delete it by yourself on shutdown/reboot) or from the
   # content of the '${SCHEME_FILE}', which may be tweeked.
   #
   # (see interfaces(5) and '/usr/share/doc/ifupdown/examples')
   #
   # Author: Martin Bock
   # Date:   07-Oct-2002

   # environment
   #
   IF=${1:-"eth0"}
   PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
   SCHEME_FILE=/var/lib/misc/pcmcia-scheme
   
   # what are we looking for
   #
   if [ -f ${SCHEME_FILE} ]
   then
       SCHEME=`cat ${SCHEME_FILE}`
   else
       SCHEME=`cat /proc/cmdline | sed -n 's/.*SCHEME=/\1/p'`
   fi

   # tell '/etc/init.d/networking' and '/etc/init.d/pcmcia' what we've found
   #
   case ${SCHEME} in
       AtHomeLAN | OnTheRoad | WithDHCP )
           echo "${IF}-${SCHEME}"
           ;;
       * )
           echo "${IF}-OnTheRoad"
           exit 1
           ;;
   esac

   exit 0

In simplen Worten ausgedrückt, geschieht hier folgendes:

Im Umkehrschluss bedeutet das, das ich lediglich die beim Start des Init-Skriptes 'pcmcia' neu angelegte Datei '/var/lib/misc/pcmcia-schemes' zu manipulieren brauche, um im laufenden Betrieb das Netzwerk-Schema zu wechseln.

2. Ein Schalter für die Auswahl bestimmter Schemata

Diese Manipulation erledigt vollkommen komfortabel das folgende Skript, das mich ausserdem vor Tippfehlern oder anderen Dummheiten bewahrt:

   #!/bin/sh
   #
   # $Id$
   #
   # /usr/local/sbin/net-scheme
   #
   # Script, that uses Debian's ifupdown mechanism to reconfigure a laptop's
   # network settings on-the-fly with schemes defined in /etc/network/interfaces
   #
   # Author: Martin Bock
   # Date:   27-Dec-2003

   # variables
   #
   APP=${0##*/}
   SCHEME_FILE=/var/lib/misc/pcmcia-scheme
   CUR_SCHEME=`cat ${SCHEME_FILE}`

   #functions
   #
   hilfe() {
       echo "Usage: ${APP} { AtHomeLAN | OnTheRoad | WithDHCP }"
   }

   # the script
   #
   if [ $# = 0 ]
   then
       hilfe
       exit 1
   elif [ $# = 1 ]
   then
       case ${1} in
           AtHomeLAN | OnTheRoad | WithDHCP )
               if [ "${1}" == "${CUR_SCHEME}" ]
               then
                   echo "Network is still configured with scheme ${1}."
                   hilfe
                   exit 1
               else
                   echo "${1}" > ${SCHEME_FILE}
                   echo "Changed network scheme from ${CUR_SCHEME} to ${1}."
                   echo "You need to restart your network."
               fi
               ;;
           -h | --help )
               hilfe
               ;;
           * )
               echo "Unknown network scheme: ${1}!"
               hilfe
               exit 1
               ;;
       esac
   else
       hilfe
       exit 1
   fi

   exit 0

Nach Aufrufen dieses Skriptes mit dem erwünschten Schema als Parameter brauche ich nur noch /etc/init.d/networking restart' aufrufen und habe die Netzwerkeinstellungen meines Laptops im relativ bequem laufenden Betrieb umkonfiguriert.

Das ganze funktioniert nicht gewissermassen an Debian vorbei, wie das viele andere derartige Lösungen machen, sondern bedient sich der von Debian bereitgestellten Mechanismen. Es setzt lediglich voraus, dass beim Reboot oder Shutdown die Datei '/var/lib/misc/pcmcia-scheme' gelöscht wird, was standardmässiges Debian-System nicht macht.

Verschiedene individuelle und systemweite Konfigurationsdateien werden - je nach ausgewähltem Schema - beim Booten von 'cron' mit dem Zeitfeld '@reboot' umkopiert oder verlinkt. Diese bleiben bei der o. a. Lösung unberücksichtigt, aber das ist eine andere Baustelle.


Valid XHTML 1.0

Zurück zur Main-Site
Impressum
Created with GNU-Emacs on Sat Dec 27 15:45:22 CET 2003

Valid CSS