Setting a static IP on the Raspberry Pi using Wi-Fi and Raspbian

Setting a static IP on the Raspberry Pi using Wi-Fi and Raspbain
Published: by
Last updated:
Categories
Raspbian

Setting a static (vs. dynamic) IP address is sometimes desirable for standalone wireless Raspberry Pis. Whereas dynamic (dhcp) IPs might change from time to time, a static IP will not and will simplify and standardise network connections to the Raspberry Pi.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

After a fresh install, the Raspberry Pi will be configured to connect to a network using a dynamic IP address. This post will show how to set up a static IP address for the Raspberry Pi using Wi-Fi and Raspbian. To make it accessible on all distributions of Raspbian, the process will mainly focus on using terminal commands. Initially, a keyboard and screen will be required, but after a (dynamic) IP address is obtained, the terminal commands can also be done by using PuTTY.

Before setting up a static IP address on a local network, it is strongly advised to read my experience on the Raspberry Pi & local home networking. Using an Wi-Fi connection is somewhat different from setting a static IP using an Ethernet connection.

Raspbian Wheezy, Raspbian Jessie Lite and Raspbian Stretch Lite will boot straight into the terminal immediately asking for a username and password. The default username:password for Raspbian is pi:raspberry.

Raspbian Jessie and Raspbian Stretch will boot into the GUI. To access the terminal from Raspbian Jessie and Raspbian Stretch, click on the terminal button.

Raspbian GUI terminal icon
Raspbain Jessie GUI interface. To access the terminal, use the terminal icon on the top of the screen.

Using an USB Wi-Fi dongle

Whereas the Raspberry Pi 3 has on-board Wi-Fi, earlier models (e.g. model 2) can use an USB Wi-Fi dongle to connect wirelessly.

Edimax USB Wi-Fi dongle
Edimax has a nice range of USB Wi-Fi dongles, but any general Wi-Fi dongle can be used.

Either way, all the versions of Raspbian come pre-equipped to automatically detect and use the hardware. To ensure the best functionality, an update/upgrade will suffice in most cases:

sudo apt-get update
sudo apt-get upgrade

After booting with the USB Wi-Fi dongle attached, its detection can be confirmed using the Linux kernel command, dmesg. The output of this command typically contains the messages produced by the device drivers:

dmesg | more

The spacebar key can be used to scroll down one page at a time. Close to the end, something similar to the following lines will show:
Set Up a Static IP Address on the Raspberry Pi using Wi-Fi

The lines above indicates that a high-speed USB device was found. It also shows that it is a wireless LAN (802.11n WLAN) adapter. This means that the USB Wi-Fi dongle is identified and ready to use. Setting a static IP address for an USB Wi-Fi dongle is similar to doing it for on-board Wi-Fi.

Setting a static IP on the Raspberry Pi using Wi-Fi and Raspbian

When using a Wi-Fi to connect to a network, the network information need to be supplied first. Also read 3 ways to connect a Raspberry Pi 3 to a network using Wi-Fi. After the network name (SSID) and the password (also called the passphrase or pre-shared key) has been entered, the current IP address of the Raspberry Pi can be obtained using the following terminal command:

hostname -I

The IP address can also be obtained, together with other network information, by using ifconfig. If the network settings hasn’t been changed, this will be a dynamic/random IP address assigned by the router. This IP will likely change from time to time.

To be able to change a dynamic IP address to a static one, certain network information needs to be collected and entered into the network configuration file.

Step 1 Collect the information needed for a static IP

In addition to the network name and password, the following network information will be needed:

  1. Current IP address (dynamic/dhcp)
  2. Desired IP address
  3. Netmask address
  4. Broadcast address
  5. Network address
  6. Gateway address

The Desired IP address is the address to be used as a static IP. Any unused IP on the network can be used.

Go the the Netmask address (Mask) and the Broadcast address (Bcast), use ifconfig.

Set Up A Static IP Address On The Raspberry Pi

For the Network address, replace the last number of the Current IP address with 0 – i.e. 192.168.1.81’s Network address is 192.168.1.0

For the Network address, replace the last number of the Current IP address with 0 – e.g. 192.168.1.81‘s Network address is 192.168.1.0.

Lastly, use netstat -nr or ip route | grep default | awk '{print $3}' to get the Gateway address.

Set Up A Static IP Address On The Raspberry Pi

The following network information will be used during this post:

  1. Current IP address 192.168.1.81
  2. Desired IP address 192.168.1.16
  3. Netmask address 255.255.255.0
  4. Broadcast address 192.168.1.255
  5. Network address 192.168.1.0
  6. Gateway address 192.168.1.254

Step 2 Enter the information gathered

After obtaining the desired network information, it needs to be added to Raspbian’s network configuration file. The network configuration files and the way the information is added are different for Raspbain Wheezy (2a) and Raspbian Jessie/Stretch (2b).

Step 2a Edit the interfaces file (Raspbian Wheezy)

For Raspbian Wheezy, the interface file is used to store the network configuration. Start off by making a backup of the original interfaces file:

sudo cp -p /etc/network/interfaces /etc/network/interfaces.original

Open the interfaces file for editing:

sudo nano /etc/network/interfaces

The original interfaces file for Raspbian Wheezy will look something like this:

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

It is always a good idea to comment out the original lines with # (comment sign) to allow a simple reversal.

Replace everything with the following lines (copy/paste):

# NOTES:
# These lines are a summary for switching between a static and dynamic IP on Raspbian Wheezy
# (The process for Raspbian Jessie/Stretch is different!)
# Last updated 06.07.2018

# For static IPs, there are 3 important sections in the /etc/network/interfaces file
# 1) General section
# 2) Wired settings section (eth0)
# 3) Wireless (WiFi) settings section (wlan0 & wlan1)
# 3a) Dynamic (dhcp) WiFi settings
# 3b) Static WiFi settings

# 1) ##########################################################

auto lo
iface lo inet loopback

# 2) ##########################################################

auto eth0
allow-hotplug eth0
iface eth0 inet manual

# 3) ##########################################################
# 3a) For a dynamic WiFi connection un-comment this section

#auto wlan0
#allow-hotplug wlan0
#iface wlan0 inet manual
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

#auto wlan1
#allow-hotplug wlan1
#iface wlan1 inet manual
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

# 3b) For a static WiFi connection un-comment this section

#allow-hotplug wlan0
#iface wlan0 inet static
#address 192.168.1.16
#netmask 255.255.255.0
#broadcast 192.168.1.255
#network 192.168.1.0
#gateway 192.168.1.254
#     wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

#auto wlan1
#allow-hotplug wlan1
#iface wlan1 inet manual
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

###############################################################

This will allow to simply un-comment the appropriate section for the settings needed. For the purpose of this post, un-comment section 3b and supply the network information as per Step 1. Exit and save with Ctrl+X and then Y.

For Wi-Fi,  no eth0 settings are needed. Changing the file above is mainly aimed at setting up the wlan0 to static (as suppose to dynamic). This will set a dynamic Ethernet connection and static Wi-Fi connection.

To check if Raspbian accepted the new network settings, use:

sudo /etc/init.d/networking restart

If there are no errors, release the potential lease and reboot:

sudo rm /var/lib/dhcp/*
sudo reboot

STEP 2b EDIT THE dhcpcd.conf FILE (RASPBIAN Jessie/Stretch)

For Raspbian Jessie and Raspbian Stretch, the dhcpcd.conf file is used to store the network configuration. Start off by making a backup of the original dhcpcd.conf file:

sudo cp -p /etc/dhcpcd.conf /etc/dhcpcd.conf.original

Open the dhcpcd.conf file for editing:

sudo nano /etc/dhcpcd.conf

The original dhcpcd.conf file for Raspbian Jessie/Stretch will look something like this:

# 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.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Most distributions have NTP support.
option ntp_servers
# Respect the network MTU.
# Some interface drivers reset when changing the MTU so disabled by default.
#option interface_mtu

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate Stable Private IPv6 Addresses instead of hardware based ones
slaac private

# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname

Scroll all the way to the bottom of the file and add the following:

# NOTES:
# These lines are a summary for switching between a static and dynamic IP on Raspbian Jessie & Raspbian Stretch
# (The process for Raspbian Wheezy is different!)
# Last updated 06.07.2018

# For static IPs, there are 2 important sections to be added to the end of the /etc/dhcpcd.conf file
# 1) Wired settings section (eth0)
# 2) Wireless (WiFi) settings section (wlan0)

# 1) ##########################################################

#interface eth0
#static ip_address=192.168.0.16/24  # note the /24 at the end
#static routers=192.168.1.254
#static domain_name_servers=192.168.1.254

# 2) ##########################################################

#interface wlan0
#static ip_address=192.168.0.16/24
#static routers=192.168.1.254
#static domain_name_servers=192.168.1.254

###############################################################

This allows to simply un-comment the appropriate section for the settings needed. For the purpose of this post, un-comment section 2 and supply the network information as per Step 1 (routers & domain_name_servers are both = Gateway address). Leave the /24 at the end of the static IPs address. Exit and save with Ctrl+X and then Y.

Release the potential lease and reboot:

sudo rm /var/lib/dhcp/*
sudo reboot

Step 3 Check & test the new IP

After the Raspberry Pi has booted, get the newest IP address again by using:

hostname -I

There might be more than one IP shown, but the static IP just configured should also be on the list. The other IP addresses can be ignored. While the other addresses might change, the static IP address will remain the same until the network configuration file has been changed again.

Step 4 Disable power management (optional)

Without disabling power management, the Wi-Fi tend to disconnect if not used for a certain period of time. The current power management settings can be seen with the iwconfig command:

iwconfig
iwconfig with power management on
Note Power Management

In the sample above, Power Management is on, which can cause disconnection problems if the Raspberry Pi is idling. Power management can be disabled with the following command:

sudo iwconfig wlan0 power off

To make this change permanent, the command can be added to either Crontab or the rc.local file. In crontab (crontab -e) add:

@reboot sleep 10 && sudo iwconfig wlan0 power off

or in rc.local (sudo nano /etc/rc.local) add:

# Disable power management
sleep 10
sudo iwconfig wlan0 power off

at the bottom. In either case, exit and save with Ctrl+X and then Y before rebooting (sudo reboot).

Step 5 Keep the network alive (optional if required)

When still experiencing disconnection problems after Step 4, the following Bash script added to Cron can help. It basically checks if the router is still available and, if not, then resets wlan0.

To create a bash file in the /home/pi directory, use:

sudo nano /home/pi/ping.sh

Add the following to it:

#!/bin/bash

ping -c4 192.168.1.xx > /dev/null # the router's IP
if [ $? != 0 ] # if the IP can't be pinged
then
  echo "No network connection, restarting wlan0..."
  /sbin/ifdown 'wlan0'
  sleep 5
  /sbin/ifup --force 'wlan0'
fi

Exit and save with Ctrl+X and then Y.

Add the following line to the bottom of Crontab (crontab -e):

*/5 * * * * sudo /home/pi/ping.sh > /dev/null

Exit and save with Ctrl+X and then Y and reboot (sudo reboot).

This will run ping.sh every 5 minutes to keep the network alive.

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause.
Donate via PayPal

Save, share & discuss

Your comment is important, but don't be a knob. Keep it constructive and polite.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Raspbian related posts

Using the Raspberry Pi as a home web server
Using the Raspberry Pi as a home web server
18 January 2016 | Updated 19 November 2017
A web server is a computer that can send and receive data via a web browser. It uses, the very popular, Hypertext Transfer Protocol (HTTP), the basic network protocol used to distribute information on the internet. More…
Categories
Raspbian