Skip to content

Instantly share code, notes, and snippets.

@yssk22r
Created November 26, 2011 10:19
Show Gist options
  • Select an option

  • Save yssk22r/1395420 to your computer and use it in GitHub Desktop.

Select an option

Save yssk22r/1395420 to your computer and use it in GitHub Desktop.
Automatic Network Configuration based on MAC address.
#!/bin/bash
hwaddr=$(ifconfig -a | grep HWaddr | awk '{print $5}')
echo "Update /etc/udev/rules.d/70-persistent-net.rules"
cat <<-EOS > /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${hwaddr}", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
EOS
echo "Update hostname and ipaddress"
host=$(echo $hwaddr | awk -F: '{print $6}' | tr '[a-z]' '[A-Z]')
ip=$(echo 16i $host p | dc)
hostname="vm-$ip"
echo "$hostname" > /etc/hostname
cat <<-EOS > /etc/hosts
127.0.0.1 localhost ${hostname}
127.0.1.1 ${hostname}
EOS
cat <<-EOS > /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.${ip}
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 192.168.1.32 8.8.8.8
broadcast 192.168.1.255
EOS
echo "Update finished. Please reboot the server"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment