Skip to content

Instantly share code, notes, and snippets.

@Ghepes
Last active February 22, 2025 13:26
Show Gist options
  • Save Ghepes/65e5197326cbd7f9c9a92bf64b815286 to your computer and use it in GitHub Desktop.
Save Ghepes/65e5197326cbd7f9c9a92bf64b815286 to your computer and use it in GitHub Desktop.
Network vm ubuntu 22.04
If you are running **Ubuntu 22.04.5 LTS** in a virtual machine and want to ensure that your IPv4 network settings are not lost across reboots or configuration changes, follow these steps:
---
## **1. Saving Current Configuration**
Before making any changes, save your current network settings:
```bash
mkdir -p ~/network_backup
ip a > ~/network_backup/current_ip.txt
ip route > ~/network_backup/current_routes.txt
nmcli con show > ~/network_backup/nmcli_connections.txt
nmcli con show --active > ~/network_backup/active_connections.txt
```
If you are using **Netplan**:
```bash
sudo cp /etc/netplan/*.yaml ~/network_backup/
```
If you are using **NetworkManager**:
```bash
nmcli connection export > ~/network_backup/nmcli_export.nm
```
---
## **2. Checking Current Configuration**
To see details about the active connection:
```bash
nmcli device show
nmcli connection show
````
If you are using **Netplan**:
```bash
cat /etc/netplan/*.yaml
```
If you are using `ifconfig` (install it if not):
```bash
sudo apt install net-tools -y
ifconfig -a
```
---
## **3. Manually Configuring Static IP (If Necessary)**
If the IP changes after reboot, set it manually in a Netplan file.
1. **Edit the Netplan file**:
```bash
sudo nano /etc/netplan/00-installer-config.yaml
```
2. **Configure a static IP** (replace with the desired IP):
```yaml
network:
ethernets:
ens33: # Change with your interface (ex: `ens160`, `eth0`)
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
version: 2
```
3. **Apply the changes**:
```bash
sudo netplan apply
```
4. **Verify the new configuration**:
```bash
ip a
ip route
```
---
## **4. Ensuring Configuration Persistence**
If settings are still lost on reboot, follow these steps:
1. **Make sure the `systemd-networkd` service is started correctly**:
```bash
sudo systemctl enable systemd-networkd
sudo systemctl restart systemd-networkd
```
2. **If you are using NetworkManager, check that the interface is configured correctly**:
```bash
nmcli con show
```
If it shows as **dhcp active**, manually change it to static:
```bash
nmcli con mod "Ethernet connection 1" ipv4.method manual ipv4.addresses 192.168.1.100/24 ​​ipv4.gateway 192.168.1.1 ipv4.dns "8.8.8.8 8.8.4.4"
nmcli con up "Ethernet connection 1"
```
3. **Make sure Netplan is applied to each boot**:
```bash
sudo crontab -e
```
Add this line at the end:
```
@reboot sudo netplan apply
```
---
## **5. Restoring the Configuration in Case of Problems**
If after an update or reboot the settings were lost, run:
```bash
sudo cp ~/network_backup/*.yaml /etc/netplan/
sudo netplan apply
```
If you use NetworkManager:
```bash
nmcli connection import type ethernet file ~/network_backup/nmcli_export.nm
nmcli con up "Ethernet connection 1"
```
---
Now your network settings will remain persistent regardless of updates or reboots. 🚀
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment