Wednesday, 30 May 2012

Raspberry Pi - Setting a Static IP Address


Anyway, I needed to change my Raspberry Pi so it had a static IP address.  I've got a typical network at home, wireless router in the cupboard under the stairs, a couple of powerline ethernet plugs and broadband provided by someone who offers little support for anything out of the ordinary and using dhcp to dynamically allocate the IP address to the Pi was causing a few headaches - just because it kept changing!

Setting the Pi to use a static IP address was the way forward.

Modify /etc/network/interfaces
The Raspberry Pi's network configuration is stored in the file /etc/network/interfaces, so this needs to be modified in order to change it from using dhcp to get an IP address to assigning a static one.

sudo nano /etc/network/interfaces

First step is to comment out the line:

iface eth0 inet [dhcp]/[manual]

by placing a # at the start:

# iface eth0 inet [dhcp]/[manual]

Next step is to add the static IP configuration to the end of the file, this will be specific to your network setup, but for most 'home' users it will be pretty similar.

You will need to know the IP address of your router, it is probably 192.168.1.1 or 192.168.2.1, but you can check by logging into your router and checking the configuration.

You will also need to decide on an IP address for your Pi that is within the range of your routers IP address e.g. 192.168.1.99.

So in my setup, where I wanted to give the Raspberry Pi an address of 192.168.1.99 and my router has the address 192.168.1.1; I added the following to the bottom of the /etc/network/interfaces file.

iface eth0 inet static
     address 192.168.1.99
     netmask 255.255.255.0
     network 192.168.1.0
     broadcast 192.168.1.255
     gateway 192.168.1.1

Ctrl X to save the changes

Restart
I found that using the raspbian distro I had to reboot the Pi for the network changes to take effect:

sudo shutdown -r now

Test
I tested the change by making sure I could still ping my router

ping 192.168.1.1


12 comments:

  1. Hi, am new to all this so sorry if this is an obvious question, but how would I also set up the RPi with a host name eg "MyRPi" so i can reference it with something a little less clunky than using the ip address the whole time.

    Many thanks

    ReplyDelete
    Replies
    1. Alex, If you are going across OS (e.g. Windows -> Linux) and you dont have the appetite to setup a DNS service, you might find yourselves in a world of pain (for me it just seemed to much touble!), see the following posts of rPi forum. http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=8548, http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=5607&p=84170#p84170 . Mart

      Delete
  2. How do you also specify a DNS server?

    ReplyDelete
    Replies
    1. Ive never done this before, but 'the internet' tells me that you add dns-nameservers 12.34.56.78 12.34.56.79 after the gateway tag.

      Delete
  3. when i save it says so sutch file or directory what do i do

    ReplyDelete
    Replies
    1. Dont know... Type the name of the file properly!

      Delete
  4. Hi, how to change the static IP by a Python script ? Any help please. Thank you very much.

    ReplyDelete
  5. DNS entry goes in /etc/resolv.conf as:

    nameserver 192.168.1.1

    ReplyDelete
  6. this was completely useless to me, what do you mean within range of my routers ip address? what are those numbers after netmask, network and broadcast?!?!?!?!!?!?!?! can you explain this in idiot to me please

    ReplyDelete
    Replies
    1. Apologies if the post was 'completely useless' and yet you ask for advice?!

      Delete
  7. I understand it, and it helped me. Thanks bro.

    ReplyDelete
  8. I understand it, and it helped me. Thanks bro.

    ReplyDelete

Note: only a member of this blog may post a comment.