Table of Contents

Howto map network interface to defined configuration

Before UDEV (e.g kernel < 2.6)

This example below shows how to configure the /etc/network/interfaces file in order to map network interface card to a specific configuration helped by the hardware MAC address.

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
 
mapping eth2
	script /etc/network/get-mac-address.sh
	map 00:14:5E:D8:43:92 dmz 
	map 00:14:5E:D8:43:94 lan 
mapping eth3
	script /etc/network/get-mac-address.sh
	map 00:14:5E:D8:43:92 dmz
	map 00:14:5E:D8:43:94 lan 
 
 
# The primary network interface (eth2)
#iface eth2 inet dhcp
auto eth2
iface lan inet static
     hostname proxy_lan
     name Ethernet LAN card
     address 192.168.1.101
     dns-nameserver 192.168.1.21
     #dns-search myDNS
     network 192.168.1.0
     gateway 192.168.1.1
     broadcast 192.168.1.255
     netmask 255.255.255.0
 
# The secondary network interface (eth3)
#iface eth3 inet dhcp
auto eth3
iface dmz inet static
     hostname proxy_dmz
     name Ethernet LAN card
     address 88.88.88.88 
     dns-nameserver 88.88.88.11 88.88.88.110
     network 88.88.88.0
     gateway 88.88.88.1
     broadcast 88.88.88.255
     netmask 255.255.255.0

The script /etc/network/get-mac-address.sh below is called by /etc/network/interfaces and return the right interface name depending on the mac address and create a log file in the same directory.

get-mac-address.sh

#!/bin/sh
 
log="get-mac-address.log"
date > $log
 
set -e
 
export LANG=C
 
iface="$1"
echo "iface: $1" >> $log
mac=$(/sbin/ifconfig "$iface" | sed -n -e '/^.*HWaddr \([:[:xdigit:]]*\).*/{s//\1/;y/ABCDEF/abcdef/;p;q;}')
echo "mac: $mac" >> $log
which=""
 
while read testmac scheme;
do
	echo "testmac: $testmac" >> $log
	echo "scheme: $scheme" >> $log
	echo "which: $which" >> $log
	if [ "$which" ]; then
		echo "continue..." >> $log
		continue;
	fi
 
	if [ "$mac" = "$(echo "$testmac" | sed -e 'y/ABCDEF/abcdef/')" ]; then
		which="$scheme";
		echo "which=$scheme (scheme)" >> $log
	fi
done
 
 
if [ "$which" ]; then
	echo "$which";
	echo "which: $which -> exit!" >> $log
	exit 0;
fi
 
exit 1

With UDEV (e.g kernel >= 2.6)

You can add a rule into udev to link network card MAC address to a name as described into debianhelp. Create into /etc/udev/rules.d a file like 010_netinterfaces.rules and add such line:

KERNEL=="eth*", SYSFS{address}=="00:1a:64:94:2d:54", NAME="eth0"
KERNEL=="eth*", SYSFS{address}=="00:1a:64:94:2d:56", NAME="eth1"
KERNEL=="eth*", SYSFS{address}=="00:10:18:2b:e4:60", NAME="eth2"
KERNEL=="eth*", SYSFS{address}=="00:10:18:2b:e4:61", NAME="eth3"