Switching from a dynamic to a static IP address


Summary

When I make a Debian Linux box, I start with it as a DHCP client and let it get the network settings automatically. This tip is to help me remember where to change them when I want to switch to a static IP.

This is for the Debian Linux (Woody and Sarge).


Detail

/etc/network/interfaces

There will be some stuff about lo (the loopback interface), which should be left alone.

Then there is an ethn section for each network card (i.e. eth0, eth1 and so on). The examples all use eth0.

DHCP network cards will have a line like:

iface eth0 inet dynamic

Statically configured network cards will have a section like:

iface eth0 inet static
	address local address
	netmask subnet mask
	network network address, usually the lowest address on your subnet
	gateway gateway's IP address, usually the one greater than lowest address on your subnet
	broadcast network broadcast address, usually the highest address on your subnet
	dns-nameservers server1 server2
	dns-search defaultdomain

Note that the indent is a single tab, not spaces.

Here is an example:

iface eth0 inet static
	address 192.168.1.101
	netmask 255.255.255.0
	network 192.168.1.0
	gateway 192.168.1.1
	broadcast 192.168.1.255
	dns-nameservers 208.67.222.222 208.67.220.220
	dns-search example.net

After making the change, run ifdown eth0 and then ifup eth0.

To check the current configuration, run ifconfig.

/etc/resolv.conf

Note that in Ubuntu 12.04, this is configured with the dns-nameservers and dns-search lines above. Changes to resolv.conf will be overwritten when you restart.

Statically configured:

search default domain name
nameserver DNS server ip address

The default domain is used when you only type in one word (i.e. when you try to resolve mail and want it to go to mail.example.com, you should use example.com as the default domain.

You can have multiple nameserver lines if you have backup name servers. Only one will be used though, it does not search all of them.

Here is an example (using OpenDNS):

search example.com
nameserver 208.67.222.222
nameserver 208.67.220.220