#!/bin/bash # # Interactive Static IP Configuration Script using nmcli # Usage: sudo bash set_static_ip_nmcli.sh # set -euo pipefail log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"; } if [ "$(id -u)" -ne 0 ]; then log "ERROR: This script must be run as root (use sudo)." exit 1 fi if ! command -v nmcli &> /dev/null; then log "ERROR: nmcli is not installed. This system may not be using NetworkManager." exit 1 fi echo "=========================================" echo " NetworkManager Static IP Setup" echo "=========================================" echo "" log "Available Network Connections:" mapfile -t CONNECTIONS < <(nmcli -t -f NAME connection show) if [ ${#CONNECTIONS[@]} -eq 0 ]; then log "ERROR: No network connections found." exit 1 fi for i in "${!CONNECTIONS[@]}"; do echo " $((i+1)). ${CONNECTIONS[$i]}" done echo "" read -p "Enter the number of the connection to configure: " CONN_CHOICE if ! [[ "$CONN_CHOICE" =~ ^[0-9]+$ ]] || [ "$CONN_CHOICE" -lt 1 ] || [ "$CONN_CHOICE" -gt "${#CONNECTIONS[@]}" ]; then log "ERROR: Invalid selection. Please enter a valid number from the list." exit 1 fi CONN_NAME="${CONNECTIONS[$((CONN_CHOICE-1))]}" echo "" log "Current settings for '$CONN_NAME':" CUR_METHOD=$(nmcli -g ipv4.method connection show "$CONN_NAME") CUR_IP=$(nmcli -g IP4.ADDRESS connection show "$CONN_NAME") if [ -z "$CUR_IP" ]; then CUR_IP=$(nmcli -g ipv4.addresses connection show "$CONN_NAME") fi CUR_GW=$(nmcli -g ipv4.gateway connection show "$CONN_NAME") CUR_DNS=$(nmcli -g ipv4.dns connection show "$CONN_NAME") echo " - Method: ${CUR_METHOD:-}" echo " - IP Addr (CIDR): ${CUR_IP:-}" echo " - Gateway: ${CUR_GW:-}" echo " - DNS: ${CUR_DNS:-}" echo "" IPV4_CIDR_REGEX="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/([0-9]|[1-2][0-9]|3[0-2])$" IPV4_REGEX="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" DNS_REGEX="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(,[ ]?((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$" read -p "Enter static IP address with CIDR (e.g., 192.168.1.100/24): " IP_ADDR if [ -z "$IP_ADDR" ]; then log "ERROR: IP address cannot be empty." exit 1 fi if ! [[ "$IP_ADDR" =~ $IPV4_CIDR_REGEX ]]; then log "ERROR: Invalid IP address format. Must be a valid IPv4 CIDR notation (e.g., 192.168.1.100/24)." exit 1 fi read -p "Enter Gateway IP address (e.g., 192.168.1.1): " GATEWAY if [ -z "$GATEWAY" ]; then log "ERROR: Gateway cannot be empty." exit 1 fi if ! [[ "$GATEWAY" =~ $IPV4_REGEX ]]; then log "ERROR: Invalid Gateway IP format (e.g., 192.168.1.1)." exit 1 fi read -p "Enter DNS server(s) separated by commas (e.g., 8.8.8.8,1.1.1.1): " DNS_SERVERS if [ -z "$DNS_SERVERS" ]; then log "ERROR: DNS servers cannot be empty." exit 1 fi if ! [[ "$DNS_SERVERS" =~ $DNS_REGEX ]]; then log "ERROR: Invalid DNS server format. Must be comma-separated IPv4 addresses (e.g., 8.8.8.8,1.1.1.1)." exit 1 fi log "Applying static IP configuration to '$CONN_NAME'..." nmcli connection modify "$CONN_NAME" ipv4.method manual ipv4.addresses "$IP_ADDR" ipv4.gateway "$GATEWAY" ipv4.dns "$DNS_SERVERS" log "Restarting connection to apply changes (you may lose SSH briefly if connected remotely)..." nmcli connection up "$CONN_NAME" log "=== Configuration Complete ===" log "Verify your new IP address using 'ip addr show' or 'nmcli device show'."