| <-- Previous | Contents Page |
This page explains how to configure a fixed ("static") IP address.
>Every computer on the Internet has an IP (Internet Protocol) address. This is a series of four numbers separated by dots, for example 192.168.0.47. The numbers range from 0 to 255, so the list of possible addresses ranges from 0.0.0.0 to 255.255.255.255.
By default your Pi gets its IP Addresses using the Dynamic Host Control Protocol (DHCP). The first time it starts up it asks the network which address it should use. Next time it starts up, it asks to use that address again. The nework can say no and supply another address instead in which case the Pi uses that.
Strictly, every route to the Internet on a computer has a unique IP address. If you fitted a network cable to your Pi, it has two routes, a wired connection and a WiFi connection, so it requested two addresses when you switched it on. You may use either or both of them.
When a computer switches on and asks to use the network address it used last time, the network can say no, and supply a different one, so the address can change without warning each time you switch on the Pi, or it may stay the same for a while and then change. This is OK if you want to use the Pi as a desktop computer. If you want to run it "headless" (without a keyboard, mouse and monitor), you need to be certain what address it's using. Do that by configuring a fixed ("static") address.
If you're in an office and somebody else controls the network you need to ask them to assign a static IP address. They will give you all the information you need to configure it -a address, subnet mask, gateway address etc.
If you're at home, connecting to the Internet via a broadband modem, your network probably uses the address range 192.168.0.1, 192.168/0.2 and so on up to 192.168.0.255, 256 addresses in all. Address 192.168.0.1 is used by the modem itself. 192.168.0.0 and 192.168.0.255 are reserved for other purposes, so the available addresses are 192.168.0.2 up to 192.167.0.244. When asked for an address via DHCP it supplies 192.169.0.2, then 192.168.0.3 and so on.
For example, if you connect your mobile phone to your WiFi at home, your broadband modem assigns an IP address to it, asy 192.168.0.2, and reserves it for that purpose. If you then connect a laptiop to the WiFi, the modem assigns 192.168.0.3, and so on, slowly working its way through the list, starting at the bottom.
First of all, let's find out what addresses the Pi is using.
On the Raspberry Pi, click on the wireless network button (top right of the screen) and follow through the menus to see how the wireless network is currently configured:
The wireless config screen
The IP address of my Raspberry Pi's WiFi connection is 192.168.0.46.
When I configured my Pi's operating system, I told it to create a user called "gps". I've installed Git Bash on my laptop and I can connect to the Pi like so:
ssh gps@192.168.0.46
which connects to the Pi via its Wifi connection as that user.
That should just work on a Mac or a Linux machine. The ssh command should pick up the key files that you set up and use them to authenticate the connection. Under Windows you may have to specify which key file to use, for example:
ssh -i ~/.ssh/id_rsa gps@192.168.0.46
which means that the ssh command should use the key file id_rsa
which is in the directory .ssh
in your home directory.
That gives you a means to control the Pi that doesn't need a keyboard, mouse and monitor. The only problem is, that only works if you know what IP address the Pi is using and at some point in the future, the network may tell the Pi to use a different IP address whenm it starts up and you will have no easy way to find out what it is. So we set a fixed address now to avoid that.
The wireless config screen has some other useful information as well. The default route is 192.168.0.1. That's the address of my broadband modem, which is the route to the Internet. When my Pi issues a request to access an address on the Internet, the Pi sends it to the broadband modem, which sends it on.
The primary DNS is 192.168.4.100 and the secondary DNS is 192.168.8.100. The Domain Name Server (DNS) system is used by the Pi to translate a network name such as www.bbc.co.uk to the IP address of that computer, in this case, the BBC's web server. The Pi will attempt to do that translation using the primary DNS address and if that fails it will use the secondary address. I get my broadband service from Virgin Media and these are Virgin's DNS servers.
There are other DNS servers that you can use. Cloudflare offers one on address 1.1.1.1 and a backup on 1.0.0.1. Google offers one on 8.8.8.8 with a backup on 8.8.4.4. Some people suggest that these DNS servers are faster and more reliable thatn Virgin's
That tells us quite a lot about the WiFi connection. Now that we are connected from a command window, we can find out about the wired connection as well, using the ifconfig command. Type that command in the command window and hit the enter key. This is what I got:
ifconfig -a eth0: flags=4163mtu 1500 inet 192.168.0.47 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::2ecf:67ff:fe54:6f60 prefixlen 64 scopeid 0x20 ether 2c:cf:67:54:6f:60 txqueuelen 1000 (Ethernet) RX packets 517 bytes 205298 (200.4 KiB) RX errors 0 dropped 4 overruns 0 frame 0 TX packets 327 bytes 32609 (31.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 27 bytes 3466 (3.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 27 bytes 3466 (3.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 wlan0: flags=4163 mtu 1500 inet 192.168.0.46 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::2ecf:67ff:fe54:6f62 prefixlen 64 scopeid 0x20 ether 2c:cf:67:54:6f:62 txqueuelen 1000 (Ethernet) RX packets 165 bytes 23063 (22.5 KiB) RX errors 0 dropped 2 overruns 0 frame 0 TX packets 83 bytes 10013 (9.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
The "eth0" entry tells us about the wired ("ethernet") connection - IP address 192.168.0.47, subnet mask 255.255.255.0. The "wlan0" tells us about the WiFi connection, which we already know about.
So now to make the Pi use fixed addresses. Mine is running the Trixie version of Debian so I followed the advice given here.
There's one piece of magic that you just have to know about, which is the number of bits in the subnet mask. There are 24 bits in the mask 255.255.255.0.
First I get the list of network devices in use:
$ nmcli con show
NAME UUID TYPE DEVICE
netplan-eth0 75a1216a-9d1a-30cd-8aca-ace5526ec021 ethernet eth0
netplan-wlan0-VM1234567 b333048f-a11f-35f4-a01a-46ce7de3d8ce wifi wlan0
lo f31e321c-bc0c-4404-aba5-7369ea57c519 loopback lo
I have device "netplan-eth0" which is the device "eth0" in the results
from ifconfig above.
I also have netplan-wlan0-VM1234567.
That's my WiFi connection,
VM1234567 being the name of the WiFi network
that my Virgin Media broadband is offering.
I set the wired connection to use the address 192.168.0.147 as below. In the command that does that I also have to quote the number of bits in the subnet mask - 24. The lines starting '#' are comments. You don't need to type them.
# Set the wired connection to use address 192.168.0.147.
sudo nmcli con mod netplan-eth0 ipv4.addresses "192.168.0.145/24"
# Set the gateway.
sudo nmcli con mod netplan-eth0 ipv4.gateway "192.168.0.1"
# Set the list of domain name servers
sudo nmcli con mod netplan-eth0 ipv4.dns "1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4"
# Don't use DHCP.
sudo nmcli con mod netplan-eth0 ipv4.method manual
# Connect automatically.
sudo nmcli con mod netplan-eth0 connection.autoconnect yes
# Apply the changes.
sudo nmcli c down netplan-eth0 && sudo nmcli c up netplan-eth0
When I ran those commands I was connected across the network to the
address that the Pi got from the broadband modem - 192.168.0.46 in my case.
The last command stopped the machine from using that address,
which disconnected me.
I logged in again by connecting to the address that the Pi was now
responding on - 192.168.0.145.
# Set the wireless connection to use address 192.168.0.146.
sudo nmcli con mod netplan-wlan0-VM1234567 ipv4.addresses "192.168.0.146/24"
sudo nmcli con mod netplan-wlan0-VM1234567 ipv4.gateway "192.168.0.1"
sudo nmcli con mod netplan-wlan0-VM1234567 ipv4.dns "1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4"
sudo nmcli con mod netplan-wlan0-VM1234567 ipv4.method manual
sudo nmcli con mod netplan-wlan0-VM1234567 connection.autoconnect yes
sudo nmcli c down netplan-eth0 && sudo nmcli c up netplan-eth0
Full disclosure: in theory this should all work smoothly. In fact, after I typed the second set of commands the Pi became unreachable. After ten minutes or so it obviously wasn't coming back, so I switched off the power while it was still running (which is generally to be avoided) and then switched it back on again. The Pi came back working as it should, with the wired connection using address 192.168.0.145 and the wifi connection using 192.168.0.146. I have no idea what went wrong there, but all's well that ends well.
If all else fails, you can burn the operating system onto the micro-SD card again and start over.
| <-- Previous | Contents Page |