Getting your Raspberry Pi To run on a static IP with internet

Hi

I have been working on a series of Raspberry Pi projects, some trivial and some, less so. Recently at the Raspberry Pi non-event, I attempted to remote desktop into my Raspberry Pi, and had some success doing so, but after a request to show of the Pi Store, my little dalliance was over, and I felt like a complete idiot not being able to view the internet from the Pi.

The problem was that I had set up a static IP address for my Pi, and forgot how to tell the Pi to talk to anything else. The solution is a rather simple one, but I now have, at least what I would consider to be, a better way to do this in the future.

Problem

I wanted to be able to switch in and out of dynamic and static IP address systems and retain all of the functionality of a dynamic IP address

Solution

  1. Firstly I fired up the Pi, when I got home...
  2. After logging in, I switched to administrative privileges console mode by using the "
    sudo -i
    " command NOTE: This works right out of the box using every Raspberry Pi and Most *NIX systems, but please be careful in the sudo environment and remember to use the exit command when you are done!
  3. type "
    ifconfig eth0 && netstat -r
    " This will show some information below
    eth0	Link encap: Ethernet	HWaddr aa:bb:cc:dd:ee:ff
    	inet addr:192.168.0.2	Bcast:192.168.0.255	Mask:255.255.255.0
    	UP BROADCAST RUNNING MULTICAST	MTU:1500	Metric:1
    	..... (Next few lines are un-important)
    
    Kernel IP routing table
    Destination		Gateway		Genmask		Flags	MSS	Window	irtt	Iface
    default			192.168.0.1	255.255.255.0	UG	0	0	0	eth0
    192.168.0.0		*		255.255.255.0	U	0	0	0	eth0
    

  4. Make a log of the following:-
    • The Gateway (In our case 192.168.0.1)
    • The Current Address (In our case 192.168.0.2)
    • The Mask (In our case 255.255.255.0)
    • The Network Address (In our case 192.168.0.0)
    • The broadcast Address (In our case 192.168.0.255)
    • The Gateway (In our Case 192.168.0.1)
  5. I then used the command
    nano /etc/network/interfaces
  6. Once in this file I added a # at the beginning of the line for dhcp, so that rather than
    iface eth0 inet dhcp

    it read
    #iface eth0 inet dhcp

  7. After completing this step add a newline by navigating to the end of the line just edited and pressing the key
  8. on the new line, begin to write the following
    iface eth0 inet static
    address 192.168.0.125
    netmask 255.255.255.0
    network 192.168.0.1
    broadcast 192.168.0.255
    gateway 192.168.0.1
    nameserver 192.168.0.1
    

    These lines should basically mirror the numbers we took in step 3, with the nameserver generally being the same as the gateway...

  9. Use +X, followed by Y+ to save the file
  10. Issue a reboot command

  11. Now at this point you are done, the static IP address should be set-up, but the next few steps will allow you to chop and change more simply

  12. Now we need to use the "
    sudo -i
    " command again to get to the admin console
  13. We then need to use the copy command to clone our config
    cd /etc/network && cp interfaces interfaces.static && cp interfaces interfaces.dynamic
  14. We then need to edit the files using nano through the following command
    nano interfaces.dynamic

  15. All you need to do in this file is remove the "#" character and remove the 7 lines following that line (leave the line you have just un-commented about)
  16. Now using +X, followed by Y+, you can save this file
  17. Lastly use the following command(s) to delete the interfaces file and replace it with a link to the static or dynamic using the following commands
    For Dynamic IP Address
    rm interfaces && ln interfaces.dynamic interfaces

    For Static IP Address
    rm interfaces && ln interfaces.static interfaces


This concludes our tutorial, I hope you enjoyed and look forward to hearing any critique of the techniques from any readers attending the next LUG Meeting Monday 11th February 2013.