How To Configure A Static Ip Address
Back to current versionRestore this version

Every time you reboot your Raspberry Pi it contends on the WiFi network for a new IP ("internet protocol") address. It may get the same address as last time, it may not. There is a way to have it always get the same address, called a static IP address.

You don't need to use a static IP address (the router automatically assigns addresses when asked to, and this all works fine) but it's handy if you want to remotely connect to your robot using ssh so that you won't have to somehow find out which IP address the robot is currently using.

The idea here is to find out what address your robot is currently using, assume that address is generally available, and then set it so the robot always asks for the same address. The only downside is that if another device shows up on your network and dynamically obtains that address, your robot won't be able to connect to the network because the address is in use — every device on a network must use a unique address.

How to Find Out What Address Your Robot Is Using#

First, let's see what address it currently has. Type the ifconfig command and then hit Enter:

  % ifconfig
The result will be a lot of stuff, most of which you can avoid. It comes in sections, one of which will be named wlan0 or something similar (with "wlan" meaning "wireless local area network").

In that section there's one string of characters you're looking for, what follows "inet" on the second line of that section:

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.74 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::6849:3b23:1365:745 prefixlen 64 scopeid 0x20<link> inet6 fd48:3c0c:41d9:3100:b1b7:99fc:7136:b0f3 prefixlen 64 scopeid 0x0<global> ether 16:8b:34:0f:08:6f txqueuelen 1000 (Ethernet) RX packets 14174 bytes 1782568 (1.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8959 bytes 2536988 (2.4 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

The "192.168.1.74" above (four decimal numbers separated by dots) is the ipv4 IP address of the robot. Yours will likely start with the same first three "192.168.1" (this is common on local area networks at home but not always true), with the last number "74" being unique to your robot.

If you don't see a traditional ipv4 IP address then you're likely seeing an ipv6 address. These look something like "inet6 fe80::7d5c:3d4a:cd06:bdc2" or "inet6 fd48::3e0c:41c9:3100:700:33ac:dda7:6045", etc., basically colon-delimited groups of hexadecimal characters. If that's the case you can disable ipv6 and use only ipv4. To do that, see the next section.

How to Disable ipv6 on a Pi#

I won't get into the history of ipv6 or the reasons for or against using it here. If you want to search the Web about ipv6, please do... I'm just showing you how to disable it on your robot.

When viewing the results from typing ifconfig you see only "inet6" output and no "inet" then ipv6 has been enabled. If you want to use ipv4 style IP addresses (e.g., 192.168.1.85) then you'll need to disable ipv6.

If you're using Raspbian (i.e., earlier versions of the Raspberry Pi operating system), add the following to the end of the single line of text in the file /boot/cmdline.txt:

ipv6.disable=1
This has the advantage that you can edit it by mounting it to your desktop before you boot the Pi, since the boot partition is readable under desktop operating systems. (source)

That on its own might work. Reboot to see. If you still see 'inet6' then continue with the following steps.

If you're using the current "Raspberry Pi OS" then edit/create the file /etc/modprobe.d/ipv6.conf to either contain the following single line, otherwise add to the existing file the line :

blacklist ipv6
Then reboot.

If this fails to disable ipv6 (and on later Raspberry Pi OS it seems to be true), edit “/etc/sysctl.conf”:

sudo nano /etc/sysctl.conf

Add this to the end:

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1
net.ipv6.conf.eth0.disable_ipv6 = 1

Save and close the file, then reboot.

If you still have ipv6 active I'd suggest just giving up. For some reason ipv6 is like a virus: once there, hard to get rid of. This has to be one of the more stubborn "features" that developers have been telling us is going to happen, never has happened, and has polluted our user space now for years (*grumble*).

One thing that has caught me out a few times: If you don't see an "inet6" line you have successfully managed to disable ipv6. That doesn't mean you will see an "inet" line. If your local router has previously allocated an IP address then your attempt to assign your Pi that address will fail. You need to choose an address the router will permit you to use. The best way to do this is choose the address that your router has dynamically assigned that particular device, then make that address static (as you know it can be assigned to that device).

How To Set a Static IP Address#

NOTE: this currently doesn't seem to work on a Raspberry Pi 4 due to ipv6 (?), but has worked reliably for the Raspberry Pi 3 B+.

The DHCP service must be running on your Pi. To check, try this:

sudo service dhcpcd status

If it's not running you can start it (and with the second line, set it to always start) with:

sudo service dhcpcd start
sudo systemctl enable dhcpcd

Then, edit the DHCP configuration file located at /etc/dhcpcd.conf. You'll need to do this as a superuser (because you need special permissions to edit system configuration files), so use "sudo" and the "nano" editor like this:

sudo nano /etc/dhcpcd.conf

...and add these lines at the bottom of the file (subsituting your chosen IP address for the "192.168.1.74"):

# set a static IP address for this Pi interface wlan0 static ip_address=192.168.1.74/24 static routers=192.168.1.254 static domain_name_servers=192.168.1.254

References#

This information is derived from from pages like:

...where there's a lot more detail than here if you get stuck. If you mess things up just comment out the lines you added by putting a "#" at the beginning of those lines.


Tags:  Recipe