Tutorial - How to give your Raspberry Pi a Static IP Address

July 19, 2013

Tutorial - How to Give your Raspberry Pi a Static IP Address
 
To log in to your Raspberry Pi remotely, you'll need the IP of the Raspberry Pi – this is basically like your house address and tells the host computer where to look for it on the network. By default, the Raspberry Pi will be given an IP automatically by the router (called Dynamic IP and denoted by DHCP) when you connect to a network. However, this can change whenever you remove the Pi from the network e.g. turn it off.
 
Having a static IP isn't essential, however it will make repeated access to the Raspberry Pi via SSH much simpler, as you'll always know that the Raspberry Pi has the same address. Imagine how much trouble your postman would have if your house constantly changed location :)
 
This task assumes that you have the official Raspian OS release installed. This is available in the NOOBS distribution and can be downloaded from http://www.raspberrypi.org/downloads. This guide also assumes that you've connected your Pi to a network via Ethernet. If you're going to be logging into your Pi remotely for most tasks, then I recommend it's easiest and fastest to plonk it next to your router, and use ethernet to access the internet anway!
 
A. Checking Set Up
 
Boot into Raspian and log in (Username. pi, Password. raspberry), this will all be command line stuff, so no need to log in to the GUI.
 
First, we need to list the network interface we currently have available:
 
cat /etc/network/interfaces
 
 
The line . . . . 
 
iface eth0 inet dhcp
 
Implies that we're currently getting out IP address via DHCP, meaning it's being dynamically registered by the router. This is what we want to change!
 
B. Gathering Information
 
Fist of all we need to grab some information from our router and Pi. There's a couple of command we need to run to get this info. Have a pen and paper handy! . . . 
 
ifconfig
 
 
This reveals your router information, the bit you want is after eth0 (the ethernet connection). . . .
 
eth0      Link encap:Ethernet  HWaddr b8:27:eb:b3:fc:2c
               inet addr:192.168.1.81  Bcast:192.168.1.255  Mask:255.255.255.0
 
Write down the following information. . .
 
inet addr – 192.168.1.81 (Pi's Current IP Address)
Bcast –  192.168.1.255 (The Broadcast IP Range)
Mask –  255.255.255.0 (Subnet Mask Address)
 
We need a little more information before we proceed. Use the command. . . 
 
netstat -nr
(route -n will give you the same info.)
 
 
We need:
 
'Gateway' Address – 192.168.1.254
'Destination' Address – 192.168.1.0
 
C. Editing Network Configuration
 
We now need to plug this information into the Pi's network configuration file using a text editor. I always use nano text editor. . . 
 
sudo nano /etc/network/interfaces
 
 
Simply change the line that reads:
 
iface eth0 inet dhcp
 
to
 
iface eth0 inet static
 
Then directly below this line enter the following (Please Note. You will need your own addresses we gathered in Part B, more details below). . . . 
 
address 192.168.1.81
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
 
To clarify what each part means. . . . 
 
address – The address you want to give your Pi, this can be any IP in the network range, but it's usually advisable to go higher rather than lower, or you could end up logging different devices to the same IP! I've selected 192.168.1.81, as we're already registered to that address (denoted by 'inet addr'), but this can be any IP address from the range192.168.1.1 to 192.168.1.255.
 
netmask – The 'Mask' address we wrote down earlier.
 
network – The router IP address, this is the 'Destination' Address was found earlier. You can also grab this off your router, it will say on the side somewhere.
 
broadcast – The 'Bcast' address we wrote down earlier. 
 
gateway – This is the 'Gateway' address we found earlier.
 
 
So, it should look something like the above, but with your values! Remember to save before exit, CTRL+X (exit) then yes to save changes!
 
D. Re-check Static IP Configuration
 
Then we'll need to reboot and check your changes. . . 
 
sudo reboot
 
Log back in and run 
 
ifconfig
 
Which should reveal your new settings. . 
 
 
To double checks all is working as it should, ping your 'Gateway' Address. . . 
 
ping 192.168.1.254 -c 10
 
(the -c 10 command simply denotes that you want to ping it 10 times, if you forget to add this, it will ping the address continuosly. To stop it press CTRL+C)
 
 
This should ping successfully and all packets should be recieved. If something's not right double check through all your IP addresses, and make sure you're pinging the right address too. Remember you can always revert back to DHCP by reversing the steps. The 'network' router IP address is sometimes a little fiddly, so check that if you're still having issues!
 
Hopefully however, your Raspberry Pi is now set up with a static IP address!

 

출처 : https://www.modmypi.com/blog/tutorial-how-to-give-your-raspberry-pi-a-static-ip-address 

A big part of Raspberry Pi's usefulness comes from its size. A lot of those benefits get lost when you need an external display and keyboard (and mouse maybe) to actually do anything with it. In this post I'll quickly cover how you can set up your Raspberry Pi (A, but B would work too, it'd actually be a little bit easier) to automatically connect to your wireless network and obtain a static IP. All you need is a WiFi-dongle.

Because the Raspberry Pi A only has one USB-port, there'll be a lot of USB switching.

Setting up WiFi connection

Start by booting the Raspberry Pi, connected to a display and a keyboard. Open up the terminal and edit the network interfaces file:

$ sudo nano /etc/network/interfaces

This file contains all known network interfaces, it'll probably have a line or two in there already.

Change the first line (or add it if it's not there) to:

auto wlan0

Then at the bottom of the file, add these lines telling the Raspberry Pi to allow wlan as a network connection method and use the/etc/wpa_supplicant/wpa_supplicant.conf as your configuration file.

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

(ctrl-X, then type Y to quit and save)

The next step is to create this configuration file.

Configuring WiFi connection

Open up the wpa_supplicant.conf file in the editor.

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Again, some lines might already be present, just add the following.

network={
ssid="YOUR_NETWORK_NAME"
psk="YOUR_NETWORK_PASSWORD"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}

The other parameters are network specific, I can't tell you what you need. If you boot Raspbian to desktop, you can launc the wpa_gui (WiFi config) application and click 'Scan'. You'll find a list that has your network too with all flags you need. To do this on a RPi A you'll have to disconnect your keyboard and connect your dongle once the scanning list is open.

  • proto could be either RSN (WPA2) or WPA (WPA1).
  • key_mgmt could be either WPA-PSK (most probably) or WPA-EAP(enterprise networks)
  • pairwise could be either CCMP (WPA2) or TKIP (WPA1)
  • auth_alg is most probably OPEN, other options are LEAP and SHARED

Make sure it works

Reboot the Raspberry Pi and it should connect to the wireless network. If it doesn't, repeat above steps or get help from an adult.

A static IP

Since the goal of this tutorial is to be able to work with the RPi without external keyboard or display, you want to be ssh into it. The best way is to make sure it'll always have a static IP on your network.

Doing so is simple. Open the /etc/network/interfaces file again and add the following changes:

Change iface wlan0 inet dhcp into iface wlan0 inet static. This changes the wlan0 interface from DHCP to static.

Add the following lines before the wpa-conf line:

address 192.168.1.155 # Static IP you want 
netmask 255.255.255.0
gateway 192.168.1.1 # IP of your router

The Raspberry Pi will still be able to connect to the internet.

Wrapping up

With these changes you'll be able to always connect to your Raspberry Pi over your wireless network via ssh at the same, static IP. This means you can disconnect keyboard, mouse and display and have it plugged in a wall socket, anywhere, taking almost no space.

As an overview, my interfaces- and wpa_supplicant-files:

1234567891011121314
# /etc/network/interfaces
 
auto wlan0
 
iface lo inet loopback
iface eth0 inet dhcp
 
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.155
netmask 255.255.255.0
gateway 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
12345678910111213
# /etc/wpa_supplicant/wpa_supplicant.conf
 
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
 
network={
ssid="NYO_WWWP"
psk="topsecret"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}
Written by Pieter Beulque


+ Recent posts