November 3, 2023

RaspberryPi Ubuntu 22.04 sets the static IP address

Environment


Steps

Check IP (net-tools need to be installed first)

1
2
3
4
5
6
7
# If you do not have net-tools installed, use command below to install
sudo apt install net-tools

# Check IP
ifconfig
# Or
ip addr

Check Gateway (查看网关)

1
route -n

Check DNS

1
2
3
nslookup hcos
# Or
sudo cat /etc/resolv.conf

Modify netplan YAML file to set static IP address

1
sudo vim /etc/netplan/50-cloud.init.yaml

YAML file under netplan will vary based on your situation which means on my machine it is called 50-cloud.init.yaml does not mean it will have the same name on your machine.

Original netplan yaml file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
version: 2
wifis:
renderer: networkd
wlan0:
access-points:
FBI:
password: Encrypt Wifi Password
dhcp4: true
optional: true

Updated netplan yaml file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
version: 2
wifis:
renderer: networkd
wlan0:
access-points:
FBI:
password: Encrypt Wifi Password
addresses:
- 192.168.31.47/24
routes:
- to: default
via: 192.168.31.1
nameservers:
addresses: [192.168.31.1,8.8.8.8]
dhcp4: false
optional: true

Nameservers addresses

You can use the following 2 free DNS server IP addresses or whatever you like.

  • 8.8.8.8:Google free DNS server IP address

  • 144.144.144.144: China Telecom free DNS server IP address

192.168.31.1: My router IP address

Apply Changes for the new net plan

1
sudo netplan apply

Check the updated interface IP address

1
ip addr show wlan0

Check default route

1
ip route show

If everything works fine, reboot the system.

1
sudo reboot


Error Solution

** (generate:155256): WARNING **: 00:51:57.891: Permissions for /etc/netplan/00-installer-config.yaml are too open. Netplan configuration should NOT be accessible by others.

Change the netplan yaml file privilege.

1
sudo chmod 600 /etc/netplan/your_config_file.yaml

WARNING:root: Cannot call Open vSwitch: ovsdb-server.service is not running.

Since netplan apply says ovsdb-server.service is not running, then I installed this openvswitch

Also, I am running a Ubuntu server in Raspberry Pi, so I need to install extra libs.

1
2
3
4
# run this first
$ sudo apt-get install linux-modules-extra-raspi
# run this then
$ sudo apt-get install openvswitch-switch-dpdk

After the installation is complete, no annoying WARNING shows again:

1
2
$ sudo netplan try
$ sudo netplan apply

Till now it works fine for me.



Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#Ubuntu#RaspberryPi