[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: IP address depending on the MAC



Hello,

2011/10/2 Ireneusz Szcześniak <irek.szczesniak@gmail.com>:
> Hi,
>
> I'm planning to install an image of Debian to a number of computers. Each of
> these computers will have the same configuration except the hostname and the
> IP address.  The IP configuration has to be static. I can't use a DHCP
> server.
>
> QUESTION; Is there a way to assign a hostname and a static IP address at
> boot time depending on the MAC address of the computer's network interface
> card?
If you know which macs you will have, you can make an init script
which generate /etc/network/interface and /etc/hostname and /etc/hosts
the first boot and then remove itself.

Imagine:

$ cat /etc/machines
mac1 ip1 hostname1
....
macN ipN hostname N

$ cat /etc/init.d/10setnetworkonlyfirstboot
#!/bin/bash
mymac=$(ip add show dev eth0 | awk '/ether/ {print $2}')
myhost=$(awk "/$mymac/ {print \$3}")
myip=$(awk "/$mymac/ {print \$2}")

sed -i "s/IP/$myip" /etc/network/interfaces # and probably some
changes in /etc/hosts
sed -i "s/HOSTNAME/$myhost" /etc/hostname # and probably some changes
in /etc/hosts
rm -f /etc/init.d/10setnetworkonlyfirstboot

$ cat /etc/network/interfaces
auto eth0
iface eth0 inet static
          address IP
          network xxx
          gateway yyy

$ cat /etc/hostname
HOSTNAME

Do you think this is a good approach ? It is not very elegant, but
maybe it works.

Regards,


Reply to: