Network configuration, wi-fi, ethernet with linux debian HOWTO
by Rémy Malgouyres

Cette page existe en Français

Introduction
This page explains how to configure the linux debian system to be
able to conveniently use an ethernet network card and a wi-fi card,
and to swich easily form one mode of connexion to the other,
and from one network to another.
Typically, you have a router at home which handles both wifi and ethernet,
and you also would like to be able to connect at work.

Prerequisite : the drivers of your wi-fi card must be installed and tou should obtain
a nonzero acces-point by typing iwconfig.

Contents
1. The file resolv.conf
2. The file interfaces
    2.A. For wi-fi
    2.B. For ethernet DHCP
    2.C. For ethernet static
3. Obtaining the current configuration of the interfaces
    3.A. For wi-fi
    3.B. For ethernet
4. Reinitialization of networking
5. Shell scripts for automatization
    5.A. For wi-fi
    5.B. For ethernet DHCP
    5.C. For ethernet static


1. The file resolv.conf

To configure networking under debian, it's simplest to configure
the files /etc/network/interfaces and /etc/resolv.conf

Below appears my file /etc/resolv.conf (for my internet provider wanadoo.
  The IP addresses here are the addresses of the primary and secundary name server (DNS).)

search
nameserver 193.252.19.3
nameserver 193.252.96.4


2. The file interfaces

2.A. For wi-fi

Here is my file /etc/network/interfaces used for wi-fi.
It is configured as dhcp, that is, the IP address
is not fixed in the debian system, but is obtained from
the router via DHCPREQUEST

I named this file /etc/network/interfaces.wifi
When I want to use wifi, I create a symbolic link

ln -s /etc/network/interfaces.wifi /etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo

iface lo inet loopback

#the wifi network interface
# my wifi interface is called rausb0,
# I knew that by typing ifconfig and iwconfig (see below)
auto rausb0
iface rausb0 inet dhcp
      wireless-essid my_essid
      wireless-mode managed
      wireless-key AF32852BE7A39B522BG60C4353
# The wep key which appear here is just an example. This is not
# really my web key, which is secret


The ESSID and the WEP key must be correctly configured on the modem-router.

2.B. For ethernet

Here is my file /etc/network/interfaces for the ethernet card.
It is configured as dhcp, that is the IP address
isn't fixed in the debian system, but is obtained from
the router via DHCPREQUEST

I called this file /etc/network/interfaces.home
(I also have such a file when I want to connect at work)
When I want to use the ethernet network, I create a symbolic link

ln -s /etc/network/interfaces.home /etc/network/interfaces

Note : The network 192.168.0 corresponds to my NETGEAR router.
You have to change these values according to your router's configuration


# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo eth0

iface lo inet loopback


# The primary network interface
# my ethernet interface is called eth0,
# I knew that by typing ifconfig (see below)
iface eth0 inet dhcp



2.C. For ethernet static

In my office, we have an ethernet local network that use static IP addreses.
My network administrator gave me a fixed IP address to use
(for me it is the 192.168.151.102).
(I can also configure my home router to use static addresses
but it doesn't suit well wi-fi networking).

Here is the file /etc/network/interfaces I use for static addressing.
I named it /etc/network/interfaces.work

When I get work and want to use static ethernet networking,
I create a symbolic link:

ln -s /etc/network/interfaces.work /etc/network/interfaces



# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
# The primary network interface
# my ethernet interface is called eth0,
# I knew that by typing ifconfig (see below)
iface eth0 inet static
        address 192.168.151.102
        netmask 255.255.255.0
        broadcast 191.168.151.255
        gateway 192.168.151.1


3. Obtaining current interfaces configuration

3.A. For the wi-fi

By typing ifconfig one can see one's network interfaces
(eth0, eth1 or wlan0 or rausb0, wifi0 or other)
If your wireless card is recognized, it should appear
with an access-point (or MAC address) when you type iwconfig.

Example
Here is what I see when I type ifconfig on my laptop
(here, my wifi interface (or wlan interface) is configured and I obtained
the IP address 192.168.0.3 by dhcp and typing /etc/init.d/networking restart
This corresponds to the network 192.168.0 of my NETGEAR modem-router)

(In this instance, my wifi interface is rausb0
because I use as wi-fi card the USB adapter with chipset RALINK
"
Asus Spacelink WL-167g USB2 adapter" which works with the
rt2570 driver, the sources of which can be downloaded on the RALINK website.)


ifconfig

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:92 errors:0 dropped:0 overruns:0 frame:0
          TX packets:92 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:6040 (5.8 KiB)  TX bytes:6040 (5.8 KiB)

rausb0    Link encap:Ethernet  HWaddr 00:24:F2:C1:AB:23
          inet addr:192.168.0.3  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::213:d4ff:fef4:cd49/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:421 errors:0 dropped:0 overruns:0 frame:0
          TX packets:460 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4823848 (4.6 MiB)  TX bytes:79806 (77.9 KiB)

iwconfig

lo        no wireless extensions.

eth0      no wireless extensions.

sit0      no wireless extensions.

rausb0    RT2500USB WLAN  ESSID:"my_essid" 
          Mode:Managed  Frequency=24.37 MHz  Access Point: 00:F0:C3:A4:C1:41 
          Bit Rate=54 Mb/s  
          RTS thr:off   Fragment thr:off
          Encryption key:AF32-852B-E7A3-9B52-2BG6-0C43-53
          Link Quality=84/70  Signal level:-62 dBm  Noise level:-87 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0


3.B. For ethernet

Here is the output of ifconfig when I'm configured for ethernet.
(I obtained the IP address 192.168.0.2 by dhcp
on my ethernet interface eth0
by typing /etc/init.d/networking restart)



eth0      Link encap:Ethernet  HWaddr 00:B2:3A:24:F3:C4
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::2c0:9fff:fef9:95b0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:5
          collisions:0 txqueuelen:1000
          RX bytes:1520 (1.4 KiB)  TX bytes:2024 (1.9 KiB)
          Interrupt:10

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:92 errors:0 dropped:0 overruns:0 frame:0
          TX packets:92 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:6040 (5.8 KiB)  TX bytes:6040 (5.8 KiB)



4. Reinitialization of networking

When the files /etc/network/interfaces and /etc/resolv.conf
are correctly configured (either for wi-fi, or for ethernet),
and the corresponding interface appears by typing ifconfig,
in order to initialise networking and get an IP address, you have to type

/etc/init.d/networking restart

Remark.
With my wifi card, a repeated reinitialization of networking following
a loss of connection between the router and the computer
(as sometimes happens with wifi) made the system unstable
(system freeze which forced reboot).
To avoid this, before restarting networking,
I kill the process dhclient

killall -9 dhclient

To switch networking (from wifi to ethernet and converse), after having
killed the dhclient process, I use ifconfig. For example with my
interfaces rausb0 for wi-fi and eth0 for ethernet (just adapt to your
interface names obtained by typing ifconfig)

i) To switch from wifi to ethernet :

ifconfig rausb0 down
ifconfig eth0 up

then I change the link on the interfaces and resolv.conf files
and re-initialise networking

ii) To switch from ethernet to wifi

ifconfig eth0 down
ifconfig rausb0 up

then I change the link on the interfaces and resolv.conf files
and re-initialise networking


5. Shell scripts for automatization

Warning: the following scripts brutally delete the files
/etc/network/interfaces and /etc/resolv.conf

You can copy these files before launching the scripts:

cd /etc/
cp resolv.conf resolv.conf.old
cd network
cp interfaces interfaces.old

For the scripts to work, you have to slightly modify them
to put the names of your wifi and ethernet interfaces
obtained by ifconfig and iwconfig (see comments below).
You also have to change the IP addresses to
the ones corresponding to your router configuration.

To simplify the scripts, I configured my router (configured as DHCP server)
so that it reserves fixed IP addresses corresponding to the MAC addresses
(access point of iwconfig for wi-fi and HWaddr of ifconfig for ethernet).
This enables us to always obtain the same IP addresses by dhcp,
which simplifies tests.


5.A. For wi-fi

# Script to launch wi-fi connection.  Put in a file with execution permission.
# Execute as root
# or as a simple user using sudo
# By using visudo, root can enable a user
# to launch this script without typing root password
# and to start wi-fi networking

rm /etc/network/interfaces
ln -s /etc/network/interfaces.wifi /etc/network/interfaces
rm /etc/resolv.conf
ln -s /etc/resolv.conf.maison /etc/resolv.conf

# change here to set your reserved dhcp address
WIFI_ADDR=192.168.0.3
# change here to set your configred router address
ROUTER_ADDR=192.168.0.1
# change here if your wifi interface is not rausb0
WIFI_IFACE=rausb0
#change here if your ethernet interface is not eth0
ETHERNET_IFACE=eth0

e=$(ifconfig|grep "$ETHERNET_IFACE")
if [ -n "$e" ]
then
    echo "ifconfig $ETHERNET_IFACE down"
    ifconfig $ETHERNET_IFACE down
fi

if [ "$1" == "--force" -o -z "$(ifconfig|grep "$WIFI_IFACE")" ]
then
    echo "I\'m removing the IP adress link (kill dhclient)"
    killall -9 dhclient
    u=
else
    u=$(iwconfig 2>/dev/null|grep "$WIFI_IFACE")
fi
if [ -z "$u" ]
then
    echo "ifconfig $WIFI_IFACE up"
    ifconfig $WIFI_IFACE up
    a=$(ifconfig|grep "$WIFI_ADDR")
else
    a=$(ifconfig|grep "$WIFI_ADDR")
    if [ "$1" == "--force" ]
        then
            a=
        fi
fi

if [ -z "$(iwconfig 2>/dev/null | grep "$WIFI_IFACE")" ]
then
    echo "Impossible to configure $WIFI_IFACE. Material problem ?"
    exit 0
fi

i=0
if [ -z "$a" ]
then
    echo "Please wait, requesting IP address"
else
    echo IP address OK
fi
while [ -z "$a" -a $i -lt 4 ]
do
    echo "restarting network"
    /etc/init.d/networking restart
    i=`expr $i + 1`
    a=$(ifconfig|grep "$WIFI_ADDR")
done

if [ -z "$a" ]
then
    echo "no IP address."
    echo "Try to reboot the router or relaunch the script"
    exit 0
fi

echo "Please wait, test by ping..."
b=$(ping -w 2 -c 1 $ROUTER_ADDR | grep "64 bytes from")
# ping example.com
c=$(ping -w 2 -c 1 google.com | grep "64 bytes from")
if [ -z "$b" ]
     then
        echo "ping failure, but we have an IP address."
        if [ "$1" == "--force" ]
            then
                echo "Be patient patient and relaunch the script (sometimes wait 1mn)"
            else
                echo "I'm killing the process"
                $0 --force
            fi
    else
        echo "succes of the ping on the router."
        if [ -z "$c" ]
        then
            echo "No internet access"
            echo "Try and reboot the router"
        else
            echo "Internet access O.K. Have fun !"
        fi
     fi


5.B. For ethernet DHCP

# Script to launch ethernet connection.  Put in a file with execution permission.
# Execute as root
# or as a simple user using sudo
# By using visudo, root can enable a user
# to launch this script without typing root password
# and to start ethernet networking

rm /etc/network/interfaces
ln -s /etc/network/interfaces.home /etc/network/interfaces
rm /etc/resolv.conf
ln -s /etc/resolv.conf.home /etc/resolv.conf

# change here to set your reserved dhcp address
ETHERNET_ADDR=192.168.0.2
# change here to set your configred router address
ROUTER_ADDR=192.168.0.1
# change here if your wifi interface is not rausb0
WIFI_IFACE=rausb0
#change here if your ethernet interface is not eth0
ETHERNET_IFACE=eth0

e=$(iwconfig 2>/dev/null|grep "$WIFI_IFACE")
if [ -n "$e" ]
then
    echo "I'm supressing the IP address link (kill dhclient)"
    killall -9 dhclient
        echo "ifconfig $WIFI_IFACE down"
    ifconfig $WIFI_IFACE down
fi



u=$(ifconfig|grep "$ETHERNET_IFACE")
if [ -z "$u" ]
then
    killall -9 dhclient
    echo "ifconfig $ETHERNET_IFACE up"
    ifconfig $ETHERNET_IFACE up
fi

a=$(ifconfig|grep "$ETHERNET_ADDR")
if [ -z "$a" -o "$1" == "--force" ]
then
    echo "Requesting IP address."
    /etc/init.d/networking restart
fi

echo "Please wait, test by ping..."
b=$(ping -w 2 -c 1 $ROUTER_ADDR | grep "64 bytes from")
# ping example.com
c=$(ping -w 2 -c 1 google.com | grep "64 bytes from")
if [ -z "$b" ]
     then
        echo "ping failure, but we have an IP address."
        if [ "$1" == "--force" ]
            then
                echo "Be patient and relaunch the script"
            else
                echo "starting all over again..."
                $0 --force
            fi
    else
        echo "succes of the ping on the router."
        if [ -z "$c" ]
        then
            echo "no internet access"
            echo "try and reboot the router"
        else
            echo "Internet access O.K. Have fun !"
        fi
     fi


5.C. For ethernet static

# Script to launch ethernet connection.  Put in a file with execution permission.
# Execute as root
# or as a simple user using sudo
# By using visudo, root can enable a user
# to launch this script without typing root password
# and to start ethernet networking

killall -9 dhclient
rm /etc/network/interfaces
ln -s /etc/network/interfaces.work /etc/network/interfaces
rm /etc/resolv.conf
ln -s /etc/resolv.conf.work /etc/resolv.conf
/etc/init.d/networking restart