With the release of Raspberry Pi OS Bookworm, networking on the Raspberry Pi was changed to use NetworkManager as the standard controller for networking, replacing the previous dhcpcd system. NetworkManager includes a command line tool called "nmcli," which can control NetworkManager and report on the network status.
Display the list of network interfaces:
sudo nmcli -p connection show
You should see a list like this:
======================================
NetworkManager connection profiles
======================================
NAME UUID TYPE DEVICE
----------------------------------------------------------------------------
Wired connection 1 bd220d18-7d6a-36a5-9820-4f67de2c01fc ethernet eth0
mywifi 2359440b-8991-4c86-a905-b011dced4587 wifi wlan0
lo c29ba7c5-98ff-4fa0-8d8e-06b30b8ec384 loopback lo
Set the Ip address, Gateway and DNS Server (both can be provided by the router and have the same address) for your ethernet or/and wifi connection.
sudo nmcli c mod "Wired connection 1" ipv4.addresses 192.168.1.75/24 ipv4.method manual
sudo nmcli con mod "Wired connection 1" ipv4.gateway 192.168.1.254
sudo nmcli con mod "Wired connection 1" ipv4.dns 192.168.1.254
If you want to use multiple DNS servers, you can add them separated by commas; for example, to use Google's DNS servers, use the following.
sudo nmcli con mod "Wired connection 1" ipv4.dns "8.8.8.8,8.8.4.4"
The same method can be used for adding multiple IP addresses to the same network connection; for example, the following command would assign IP addresses 10.0.0.220, 10.0.0.221 and 10.0.0.222.
sudo nmcli c mod "Wired connection 1" ipv4.addresses "10.0.0.220/24, 10.0.0.221/24, 10.0.0.222/24" ipv4.method manual
When you have finished updating the network settings on your Raspberry Pi, you can restart the network connection with the following command.
sudo nmcli c down "Wired connection 1" && sudo nmcli c up "Wired connection 1"
Note: If you are using SSH to connect to your Raspberry Pi, running the above command will cause the SSH session to end if the IP address changes.
To revert the network connection from static to automatic, run the following commands:
sudo nmcli con modify "Wired connection 1" ipv4.method auto
sudo nmcli c down "Wired connection 1" && sudo nmcli c up "Wired connection 1"