Static IP (All Raspbian versions)Next I gave the Pi a static IP address. There are two ways to do this, the best way is to use address reservation in your router, doing it this way makes things easier if you have to change the IP address or router in the future, but if that's not possible then you can set one in the Pi itself.The process differs depending on which version of Raspbian you're using, but first you'll need some info about your network...
pi@Home:/$ ifconfig eth0 Link encap:Ethernet HWaddr b8:27:eb:c8:0f:7d inet addr:192.168.0.23 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9724220 errors:0 dropped:2465 overruns:0 frame:0 TX packets:9430543 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:852424363 (812.9 MiB) TX bytes:1721816933 (1.6 GiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:640 errors:0 dropped:0 overruns:0 frame:0 TX packets:640 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:50792 (49.6 KiB) TX bytes:50792 (49.6 KiB)
The important values from this command are inet addr (your current IP, mine is 192.168.0.23) and Mask (the subnet, again mine is 255.255.255.0), Note your values down and then type...
pi@Home:/$ netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
Note down the Gateway, which is 192.168.0.1 on mine. Now will be a good time to log into your routers web configuration page, so open a web browser and type the Gateway address into the address field, you will probably need to log in. You should know what the username and password are, if not they may be written on the base of the router, or just google them.Within the routers web configuration you're looking for DHCP settings. You'll need to reduce the amount of IP addresses available to the dhcp server, on some routers you can only adjust the lower value, or vice-versa, and some you can adjust both. I normally have around 100 addresses available for static IP's by either changing the lower value to 100 (192.168.0.100) or the higher value to 155 (192.168.0.155), choose one depending on what your router allows.Don't worry if any existing devices have IP's in the static range, they'll still work and will be allocated new IP's when renewing their leases. Static IP (Raspbian Wheezy only) Enter this into the terminal...
pi@Home:/$ sudo nano /etc/network/interfaces
Which opens the nano text editor with this information.Use the cursor keys to move around.
auto lo iface lo inet loopback iface eth0 inet dhcp address 192.168.0.23 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 allow-hotplug wlan0 iface wlan0 inet manual wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp
Make your settings look like mine below, but substitute your values for address, netmask, network, broadcast and gateway. You'll probably find that as you've already got a connection through ethernet, the values will already be correct. All you will have to do is change dhcp for static and choose an IP address, as I can only change the lower value for the DHCP server I chose 192.168.0.20 for the static address.
auto lo iface lo inet loopback iface eth0 inet static address 192.168.0.20 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 allow-hotplug wlan0 iface wlan0 inet manual wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp
Once you're happy with what you've set up, save the file with CTRL + O then press enter, then exit with CTRL + X, then reboot to start using the new IP address.
pi@Home:/$ sudo reboot
Static IP (Raspbian Jessie only)Enter this into the terminal...
pi@Home:/$ sudo nano /etc/dhcpcd.conf
Which opens the nano editor with this text...
# A sample configuration for dhcpcd. # See dhcpcd.conf(5) for details. # Allow users of this group to interact with dhcpcd via the control socket. #controlgroup wheel # Inform the DHCP server of our hostname for DDNS. hostname # Use the hardware address of the interface for the Client ID. clientid # or # Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
Add this to the top using your values, but beware, on some systems copy and pasting gives strange results... FYI static ip_address= is the IP you want to use, static routers= is the Gateway value you found earlier and static domain_name_servers= is usually the same as Gateway unless you have a separate DNS server on your network, if you do then why are you reading this guide?
interface eth0 static ip_address=192.168.0.20 static routers=192.168.0.1 static domain_name_servers=192.168.0.1
Once you're happy with what you've set up, save the file with CTRL + O then press enter, then exit with CTRL + X, then reboot to start using the IP address.
pi@Home:/$ sudo reboot