Set a static IP address in Linux

There are various reasons you may want to set your IP address to be static, one of these being to reduce boot time by removing DHCP discovery. Doing this in Linux is quite straight forward.

If you want to make your current IP address your static address, you can find the details in a few ways (if you already have the address details you can skip this part). First, run ifconfig. The line you’re looking for will look something like this:

inet addr:192.168.0.5 Bcast:192.168.0.255 Mask:255.255.255.0

The first address is your IP address, the second is your default gateway and the third is your subnet mask. Your address should be for the eth0 (ethernet) or ath0 (wireless) interface, or something similar to these. DON’T use the address for lo (loopback) – 127.0.0.1 – as this address is the address used by the computer to talk to itself.

So, once you have your networking details, you’ll need to edit the following file as root:

sudo vi /etc/network/interfaces

From here, add the following code (replace eth0 with the label from ifconfig):

auto eth0
iface eth0 inet static
address
netmask
gateway

If there are already references to eth0 in the file, you can remove them (or comment them out by adding # to the beginning of the line). You may also need to change the settings on your router to remove this address from the DHCP address pool. Unfortunately I can’t give instructions for this as this will be different for different routers.

From here, restart the computer and voilà, you’re now using a static IP address.

Leave a Reply

Your email address will not be published.