Assign Static IP and Manage Networking in Raspberry PI OS Lite

Last Updated on

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

This guide suppose 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 phisical 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 end of 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 Wifi interface, use same congifurations changing “eth0” in “wlan0”.

DHCP

An interesting option with dhcpcd is using fallback feature. This feature allow you to use your DHCP service. If (and olny 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

First part will define a static profile to be call in specific conditions.

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

Enjoy!

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon Micro SD box