Linux (28) Python (13) Raspberry Pi (5) Bugs (3) Install (3) C (2) Brainf**k (1) HTML (1) Maths (1) Sorts (1)

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.

1 comment:

  1. You dont need to reboot. From putty you may get away with "sudo ifdown eth0;sudo ifup eth0". If the connection drops from putty you will have to do the ifup on the console directly.