#!/usr/bin/env sh# Script Name: switch_network_management.sh# Author: Aina# Date Created: 2025-08-13 | Date Modified: 2025-08-13# Exit immediately if a command exits with a non-zero status# Treat unset variables as an error and exit immediatelyset-eu
# Check the permissionif[$(id-u)-ne0];thenecho'This script must be run with `sudo`.'exit1fi# Define variables# Please change the following values according to your network environmentnm_conf="/etc/NetworkManager/NetworkManager.conf"ip_addresses="192.168.1.2/24"gateway="192.168.1.1"dns="192.168.1.1"interface_name="ens33"connection_name="connection-${interface_name}"# Install and enable NetworkManagerapt-getupdateapt-getinstallnetwork-manager-y
systemctlenableNetworkManager.service
# Configure NetworkManagersed-i.bak-e's/^\(managed=\)false/\1true/'"${nm_conf}"# Check for connection naming conflictsfornamein$(nmcliconshow|awk'NF=1 {print}');doif[${name}=${connection_name}];thenecho"There is the connection name conflict."exit1fidone# Add an Ethernet connection configurationnmcliconadd\typeethernet\con-name"${connection_name}"\ifname"${interface_name}"\ipv4.addresses"${ip_addresses}"\ipv4.gateway"${gateway}"\ipv4.dns"${dns}"\ipv4.method"manual"\connection.autoconnect-priority100# Purge ifupdownapt-getpurgeifupdown-y
# Log the reboot actionecho"All actions done, system will now reboot to apply change in 10s."sleep10systemctlreboot