| <-- 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 - address, subnet mask, gateway address etc.
If you're at home and 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. That's 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 there are 253 available addresses, 192.168.0.2 up to 192.167.0.244.
To make sense of that jumble of numbers, an example may help. If your broadband modem has just been installed and you connect your mobile phone to your WiFi, your modem will tell it to use address 192.168.0.2 and it will reserve that address for that device. Then if you then connect a laptop to the WiFi, the modem will tell it to use 192.168.0.3, and so on, working its way through the list. So, when assigning a static IP address, choose one of the higher numbers.
You can't have two devices on a network with the same IP address, but if there is a clash, the broadband modem can usually sort it out quietly. If you try to assign an address that's being used by another device and it's connected, you will get an error message. If it's assigned to a device which is not connected, that's also not a problem if it got the address via DHCP. Once your Pi starts using the address, the broadband modems notes that and takes it out of the DHCP list. When the other device next connects and asks to use the address, it will be told no, and given another. The main thing to avoid is to configure two computers to use the same static IP address. The second one to switch on will not be able to connect to the network.
You are probaly always either going to use the wired connection or the WiFi, so you only need to fix one address. Here I show how to fix both of them.
To get started, let's find out what addresses the Pi is using. Click on the wireless network button (top right of the Pi's screen) and follow through the menus to see how the wireless network is currently configured. Go to Advanced Options and then Connection Information. If you have connected a network cable, the menu will have two tabs, one for the wired connection and one for the Wifi. This is the data about the WiFi connection.
The IP address of my Raspberry Pi's WiFi connection is currently 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.
Once connected, I can issue UNIX commands through the command window and they run on the Raspberry Pi. That gives me a means to control the Pi that doesn't involve fitting a keyboard, mouse and monitor. The only problem is, I can only connect if I know what IP address the Pi is using and at some point in the future the network may tell it to use a different address and I will have no easy way to find out what it is. So I set a fixed address now to avoid that.
The Connection Information screen has some other useful information as well. The subnet mask is 255.255.255.0. Never mind what that means, we just need to know the value. 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 address is 194.168.4.100 and the secondary DNS address is 194.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 than Virgin's.
The Connection Information screen told me quite a lot about the WiFi connection. Now that I'm connected from a command window, I can find out about the wired connection as well using the ifconfig> command. I type that command into the command window and hit the enter key. I get this response:
ifconfig 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. You only see that entry if you have a network cable plugged in.
The "wlan0" entru tells us about the WiFi connection, which we already know about.
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, which is the number of bits set to one in the subnet mask. Trust me, there are 24 bits set to one in the mask 255.255.255.0.
First I get the list of network devices in use using the nmcli command like so:
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
which is my WiFi connection,
VM1234567 being the name of the WiFi network
that my Virgin Media broadband offers.
I set the wired connection to use the address 192.168.0.145 as below. In the command that does that I also have to supply the number of bits in the subnet mask, which is 24. The lines starting '#' are comments. You don't need to type them.
This a very long and tricky sequence of commands. I show below how to make it easier to run them.
# Set the wired connection to use address 192.168.0.145.
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
You may not want to use address 192.168.0.145.
In fact, if you are in an office,
your network administrator may tell you to user different versions of
other values too.
(If she gives you a different subnet mask, you need to ask
how many bits in it are set to one.)
To make it easier to get the command right,
create some variables in the command window
containing the stuff that can change.
In my example, I would set them like so:
connection=netplan-eth0
address=192.168.0.145
bits=24
gateway=192.168.0.1
dnslist="1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4"
That right-hand side of that last command contains spaces, so you have to wrap
it in doble quotes.)
Now you can copy this text into paste it into the command window:
sudo nmcli con mod ${connection} ipv4.addresses "${address}/${bits}"
sudo nmcli con mod ${connection} ipv4.gateway "${gateway}"
sudo nmcli con mod ${connection} ipv4.dns "${dnslist}"
sudo nmcli con mod ${connection} ipv4.method manual
sudo nmcli con mod ${connection} connection.autoconnect yes
sudo nmcli c down ${connection} && sudo nmcli c up ${connection}
To paste into the command window under Windows, right click,
which produces a dropdown menu.
Choose the Paste option
and press the Enter key.
When the commands run,
"${address}" is replaced by the contents of the address variable
and so on.
On a Mac, click on the Edit menu and choose Paste.
To fix the WiFi address, I need these commands:
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-wlan0-VM1234567 && sudo nmcli c up netplan-wlan0-VM1234567
So I just need to change the variables as necessary:
connection=netplan-wlan0-VM1234567
address=192.168.0.146
And then copy and paste the same commands as before.
When I ran those commands I was connected across the network to the Pi via its WiFi address 192.168.0.46. The last command stopped the machine from using that address, which disconnected my session. Changing the address of the network connection that you are using to connect can easily leave the connection in a bit of a chaotic state. After I typed the second set of commands the Pi became unreachable through that route, but I still had the keyboard, mouse and monitor connected, so I could use the menu on the raspberry logo to reboot. When it came back, everything was fine. 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.
Once you've fixed the addresses, your Pi is ready to use.
| <-- Previous | Contents Page |