Arduino (1) Bugs (3) C (1) Install (3) Linux (25) Maths (1) Python (11) Raspberry Pi (4)

Friday, 24 July 2015

How to configure a static IP on linux

Normally your Linux setup will come with come with a DHCP setup which requests a IP address and gets given one. The Linux box has no control over the IP address that its given this is fine if you are just browsing the web but if you want to ssh into the box or set it up as a web server it can get quite annoying if the IP just changes. A static IP address means that it will always be the same which makes it easy to access.

First we need to find your gateway address this is the address your box goes through to get to the Internet.

route -n
You should get something that looks like this:
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric         UG    0         UG    303   U     303    

My gateway address is

Now we need to open /etc/network/interfaces but before we do that make a backup as from previous experiences if you mess this up its hard to fix.
sudo cp /etc/network/interfaces /etc/network/interfaces.backup
Now open the file with a editor of your choice.
sudo vi /etc/network/interfaces
And edit the line that looks like
iface eth0 inet dhcp
iface eth0 inet static
Now add these three lines underneath that
address #This is the static ip
netmask #This is the netmask which is a 24 bit netmask.
gateway #This is the gateway address we found earlier

Edit: Better way instead of rebooting
ifconfig eth0 down;ifconfig eth0 up

Now we need to restart for the effects to take place.
sudo reboot

If it all works you may want to remove the backup file to save space.