Labels

Arduino (1) Brainf**k (1) Bugs (3) C (1) HTML (1) Install (3) Linux (27) Maths (1) Python (12) 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
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      
0.0.0.0         192.168.0.1     0.0.0.0         UG    303   
192.168.0.0     0.0.0.0         255.255.255.0   U     303    

My gateway address is 192.168.0.1

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
to
iface eth0 inet static
Now add these three lines underneath that
address 192.168.0.100 #This is the static ip
netmask 255.255.255.0 #This is the netmask which is a 24 bit netmask.
gateway 192.168.0.1 #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.