Set up a static IP-address on the Raspberry Pi
By default, the Raspberry Pi will get a dynamically allocated IP-address, meaning it changes as you restart it or potentially when new devices are added to the network. To make it easier to connect and have a more stable connection I recommend to set up a static IP address.
Table of contents
- Get a static IP-address
- Setting-up using the Desktop
- Setting-up with the Terminal
- Prioritising internet interface
- Disabling static IP-address
Get a static IP-address
To get a static IP-address that works, it will need to be within the range provided by the router. We will therefore first need to find the router’s ip address. This tends to be written on the bottom of the router. If not, simply open a terminal window and type in netstat -nr
. Now look under Gateway
:
You can also use the command ip route | grep default | awk '{print $3}'
.
In this example it is 192.168.0.1
. Using the router’s ip address we can choose a static ip address in the range between 1 and 255, which will become the last number of your ip-address, e.g. 192.168.0.40
.
Determine if you want a static ip address over WiFi or Ethernet. The interfaces are called respectively wlan0
and eth0
.
Setting-up using the Desktop
It is very simple to set up your static ethernet address. Simply right-click on the Wi-Fi icon in the menu bar (top-right on the left of the speaker icon) and select the Wireless & Wired Network Settings
.
Now click the empty dropdown menu and select the network interface you want to configure. Now for IPv4 Address
enter your chosen ip address, for Router
the IP address of the router. AS DNS Servers add 8.8.8.8
. When wanting to add multiple DNSs make sure to add them one after another separate by a space. Finally, click the Disable IPv6
option.
Setting-up with the Terminal
One can also set up a static IP-address via the terminal. For this we need to change the dhcpcd.conf
file:
sudo nano /etc/dhcpcd.conf
Now scroll to the bottom, and add the following text:
interface INTERFACE
static ip_address=YOURSTATICIP/24
static routers=YOURGATEWAYIP
static domain_name_servers= YOURGATEWAYIP
replacing the words in capital by what is desired. Now save the file by pressing ctrl+x
then y
to exit.
Prioritising internet interface
When you are using multiple internet interfaces, such as Ethernet over Wifi, it is important to make sure the internet interface has priority over the other such that you get a working internet connection. To do so, we need to add a metric
number to each, with the higher metric being prioritised first. Open the dhcpcd.conf
file:
sudo nano /etc/dhcpcd.conf
And add the metrics. For example:
interface eth0
metric 300
static ip_address=192.168.0.40/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
metric 200
Now finally reboot your Raspberry Pi for the changes to be incorporated:
sudo reboot
Once your raspberry pi has finished restarting, connect to it locally to verify the static IP address hostname -I
or ping from it on a networked computer ping YOURSTATICIP
.
Disabling static IP-address
In many cases you may not want your Raspberry Pi set to use a static IP address. You can change the network configuration back by editing dhcpcd.conf
again (sudo nano /etc/dhcpcd.conf
and removing all the lines you added in the previous steps.