RPI Computers

Assign Static IP and Manage Networking in Raspberry PI OS Lite

Many projects involving desktop-less installations require making it available always with the same IP address. This condition can be achieved by managing networking in Raspberry PI OS Lite settings from the terminal console.

This guide supposes that you are starting from Raspberry PI OS Lite installation.

Default Behavior

Raspberry Pi uses dhcpcd daemon to configure TCP/IP across all of its network interfaces. This includes assigning each interface an IP address, setting netmasks, and configuring DNS resolution nameservers.

By default, Raspberry Pi OS attempts to automatically configure all network interfaces by DHCP, falling back to automatic private addresses in the range 169.254.0.0/16 if DHCP fails.

Previous versions of Raspberry Pi OS used the file /etc/network/interfaces for network interface configuration. If an interface is listed in this file, any settings there will take precedence over what is in /etc/dhcpcd.conf.

On Raspberry Pi OS versions with desktop environment installed, a GUI tool called “lxplug-network” is used to allow the user to make changes to the configuration of dhcpcd.

Raspberry PI OS usually lists 2 interfaces (for models WITHOUT ethernet physical interface) or 3 interfaces (all other models):

  • wlan0: for WiFi network connection
  • eth0: for Ethernet cabled networking connection
  • lo: loopback interface used by OS to identify itself (with ip address 127.0.0.1)

You can list all these interfaces and their status with following terminal command:

ifconfig

or with the command:

ip link

Correctly Managing Networking Configurations

The correct way to manage networking config is by editing dhcpcd.conf file:

sudo nano /etc/dhcpcd.conf

Default configuration (with no interface settings) uses DHCP to configure both eth0 and wlan0 interfaces.

To make your ethernet interface IP address static, please append these lines to the end of the file, changing Ip addresses with your ones:

interface eth0
static ip_address=192.168.0.10/24
static ip6_address=fd51:42f8:caae:d92e::ff/64
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

IPv6 info can be omitted, as they are rarely used.

To set static configuration for the WiFi interface, use the same configuration changing “eth0” in “wlan0”.

DHCP

An interesting option with dhcpcd is using the fallback feature. This feature allows you to use your DHCP service. If (and only if) it fails, then preset a static IP configuration will be used.

For this purpose, we need to add in dhcpcd.conf file following lines (again, use your IP addresses instead of showed ones):

profile static_eth0
static ip_address=192.168.1.23/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

#fallback to static profile on eth0
interface eth0
fallback static_eth0

The first part will define a static profile to be called in specific conditions.

The second part is where the job is done: interface eth0 is defined with the only fallback statement to be used when the DHCP service fails. In this condition, the static_eth0 profile is applied.

Enjoy!

peppe8o

Open source and Raspberry PI lover

Published by
peppe8o

Recent Posts

Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

SPI communication between two Arduinos

In this tutorial, we will use two Arduino Uno to demonstrate the Serial Peripheral Interface…

2 weeks ago

Automatic irrigation system with Arduino and Sensors

In this tutorial, we will be making an automatic irrigation system (AIS) with Arduino and…

3 weeks ago

Beginner’s Guide to Use Python Virtual Environment with Raspberry PI (venv)

This tutorial will show you how to use Python Virtual Environment with Raspberry PI computer…

4 weeks ago

Get Betting Odds with Raspberry PI and Odds-API (free)

This tutorial will show you how to get betting odds with Raspberry PI by using…

1 month ago

Backup Raspberry PI (computer) Data with Rsync to Remote NAS

This tutorial will show you how to perform the backup of Raspberry PI (computer board)…

1 month ago

Honeygain and Raspberry PI: Earn by Sharing Internet Connection

This tutorial will show you how to install Honeygain on a Raspberry PI computer board…

1 month ago