Set a Static IP in Initramfs
Initramfs (wikipedia) is a temporary filesystem that can boot a Linux system. Using initramfs allows you to use complex file partition schemes for your root directory, such as storing your root on a raid array, or on an encrypted partition.
Because it is designed to do such complex tasks, it allows for a number of utilities to be installed, including a basic shell (Busybox), and an ssh server (dropbox).
In some cases, this may mean that you need to be able to set a static IP address for initramfs; for instance this would allow you to remotely decrypt an encrypted root partition.
Doing this is as simple as editing a file, and then running some commands.
Do all of the following with either sudo, or as root.
This is from Debian, but by finding the corresponding file in your distribution, you should be able to replicate the effect.
Editing the Configuration File
First, open the file /etc/initramfs-tools/initramfs.conf
Find the line DEVICE=
Underneath it, add a line in this format
IP=ipaddress::gateway::netmask::hostname:eth#
Where ipaddress is your desired IP address, the gateway is the IP address of your router (Finding the gateway), netmask is your netmask (almost certainly 255.255.255.0), hostname is the name of the computer, and eth# is the name of the ethernet adapter you wish to use.
Example
IP=192.168.0.10:192.168.0.1:255.255.255.0:eugenemdavis.com:eth0
Updating the initramfs Image
In order to make the change in IP address effective, you must update the initramfs image.
To do this, run the following command
update-initramfs -u
Once this command completes, you should be able to reboot, and see that the changes have taken effect. If not, try updating grub. Assuming you are running Grub 2, simply run the command
update-grub2
Comments
I was unable to make the above solution work in Ubuntu Lucid, but passing the ip parameter from the bootloader seems to work: in /etc/default/grub add the following to GRUB_CMDLINE_LINUX: ip=ipaddress::gateway:netmask:hostname:eth#:autoconf Example:
GRUB_CMDLINE_LINUX="ip=10.0.0.100::10.0.0.1:255.255.255.0:hostname:eth0:off"
Before rebooting remember to run:update-grub2
I followed this guide to make a headless server with luks encryption and unlock via ssh: http://forum.ubuntuusers.de/topic/script-verschluesseltes-system-via-ssh-freisch/There is a mistake in the example. Instead of IP=192.168.0.10:192.168.0.1:255.255.255.0:eugenemdavis.com:eth0 you need to write IP=192.168.0.10::192.168.0.1:255.255.255.0:eugenemdavis.com:eth0 – with two colons after the IP address, as you stated in the format line.
Thanks for sharing this usefull information! I am trying to setup dropbear+busybox+initramfs in Ubuntu. This seems the only way to set a static IP in initramfs in Ubuntu You saved my day! Happy Christmas :)