270 lines
7.4 KiB
Bash
270 lines
7.4 KiB
Bash
#!/bin/bash
|
|
|
|
# =============================================
|
|
# Static IP Configuration Script
|
|
# Configures static IP for active interface (eth0 or wlan0)
|
|
# Must be run with sudo!
|
|
#
|
|
# Usage:
|
|
# IP_ADDRESS=192.168.1.100 GATEWAY=192.168.1.1 ./set_static_ip.sh
|
|
# =============================================
|
|
|
|
# --- Configuration ---
|
|
DEFAULT_GATEWAY="${GATEWAY_ENV:-10.10.0.1}" # Use GATEWAY_ENV if provided, otherwise default
|
|
DNS_SERVERS="8.8.8.8 8.8.4.4"
|
|
LOG_FILE="/var/log/set_static_ip.log"
|
|
NETPLAN_DIR="/etc/netplan"
|
|
|
|
# --- Functions ---
|
|
|
|
# Function to log messages
|
|
log() {
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to fix netplan permissions
|
|
fix_netplan_permissions() {
|
|
log "Fixing netplan permissions..."
|
|
|
|
# Fix permissions for all netplan files
|
|
find "$NETPLAN_DIR" -type f -name "*.yaml" -exec chmod 600 {} \;
|
|
find /lib/netplan -type f -name "*.yaml" -exec chmod 600 {} \;
|
|
find /run/netplan -type f -name "*.yaml" -exec chmod 600 {} \;
|
|
|
|
# Fix ownership
|
|
chown root:root "$NETPLAN_DIR"/*.yaml 2>/dev/null
|
|
chown root:root /lib/netplan/*.yaml 2>/dev/null
|
|
chown root:root /run/netplan/*.yaml 2>/dev/null
|
|
|
|
log "Netplan permissions fixed"
|
|
}
|
|
|
|
# Function to check if interface is up
|
|
is_interface_up() {
|
|
local interface="$1"
|
|
if ip link show "$interface" | grep -q "state UP"; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to get active interface
|
|
get_active_interface() {
|
|
# Check eth0 first
|
|
if is_interface_up "eth0"; then
|
|
echo "eth0"
|
|
return 0
|
|
fi
|
|
|
|
# Then check wlan0
|
|
if is_interface_up "wlan0"; then
|
|
echo "wlan0"
|
|
return 0
|
|
fi
|
|
|
|
# No active interface found
|
|
return 1
|
|
}
|
|
|
|
# Function to check if system uses dhcpcd
|
|
uses_dhcpcd() {
|
|
if [ -f /etc/dhcpcd.conf ] && systemctl list-unit-files | grep -q dhcpcd; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to check if system uses netplan
|
|
uses_netplan() {
|
|
if [ -d "$NETPLAN_DIR" ] && command -v netplan >/dev/null; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to check if system uses NetworkManager
|
|
uses_network_manager() {
|
|
if systemctl list-unit-files | grep -q NetworkManager && command -v nmcli >/dev/null; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to configure static IP using dhcpcd
|
|
configure_static_ip_dhcpcd() {
|
|
local interface="$1"
|
|
local ip_address="$2"
|
|
local gateway="$3"
|
|
local dns="$4"
|
|
|
|
log "Configuring static IP for $interface using dhcpcd..."
|
|
|
|
# Backup original dhcpcd.conf
|
|
if [ ! -f /etc/dhcpcd.conf.bak ]; then
|
|
cp /etc/dhcpcd.conf /etc/dhcpcd.conf.bak
|
|
log "Created backup of /etc/dhcpcd.conf"
|
|
fi
|
|
|
|
# Remove any existing configuration for this interface
|
|
sed -i "/^interface $interface$/,/^$/d" /etc/dhcpcd.conf
|
|
|
|
# Add new configuration
|
|
{
|
|
echo ""
|
|
echo "# Static IP configuration for $interface"
|
|
echo "interface $interface"
|
|
echo "static ip_address=$ip_address"
|
|
echo "static routers=$gateway"
|
|
echo "static domain_name_servers=$dns"
|
|
} >> /etc/dhcpcd.conf
|
|
|
|
log "Static IP configuration added to /etc/dhcpcd.conf"
|
|
log "Restarting networking..."
|
|
|
|
# Try different methods to restart networking
|
|
if systemctl restart dhcpcd 2>/dev/null; then
|
|
log "dhcpcd service restarted successfully"
|
|
return 0
|
|
elif systemctl restart networking 2>/dev/null; then
|
|
log "networking service restarted successfully"
|
|
return 0
|
|
elif systemctl restart systemd-networkd 2>/dev/null; then
|
|
log "systemd-networkd service restarted successfully"
|
|
return 0
|
|
else
|
|
log "Could not restart networking service automatically"
|
|
log "Please reboot the system for changes to take effect"
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
# Function to configure static IP using netplan
|
|
configure_static_ip_netplan() {
|
|
local interface="$1"
|
|
local ip_address="$2"
|
|
local gateway="$3"
|
|
local dns="$4"
|
|
|
|
log "Configuring static IP for $interface using netplan..."
|
|
|
|
# Fix netplan permissions first
|
|
fix_netplan_permissions
|
|
|
|
# Find netplan config file
|
|
local netplan_file=$(ls "$NETPLAN_DIR"/*.yaml 2>/dev/null | head -n 1)
|
|
|
|
if [ -z "$netplan_file" ]; then
|
|
# Create default netplan file if none exists
|
|
netplan_file="$NETPLAN_DIR/01-static-ip.yaml"
|
|
log "Creating new netplan configuration file: $netplan_file"
|
|
else
|
|
# Backup original netplan config
|
|
if [ ! -f "$netplan_file.bak" ]; then
|
|
cp "$netplan_file" "$netplan_file.bak"
|
|
log "Created backup of $netplan_file"
|
|
fi
|
|
fi
|
|
|
|
# Create new netplan configuration
|
|
cat > "$netplan_file" <<EOF
|
|
network:
|
|
version: 2
|
|
renderer: networkd
|
|
ethernets:
|
|
$interface:
|
|
addresses: [$ip_address]
|
|
routes:
|
|
- to: default
|
|
via: $gateway
|
|
nameservers:
|
|
addresses: [$(echo $dns | sed 's/ /, /g')]
|
|
EOF
|
|
|
|
# Set correct permissions
|
|
chmod 600 "$netplan_file"
|
|
chown root:root "$netplan_file"
|
|
|
|
log "Static IP configuration added to $netplan_file"
|
|
log "Applying netplan configuration..."
|
|
|
|
# Apply netplan configuration
|
|
if netplan apply; then
|
|
log "Netplan configuration applied successfully"
|
|
return 0
|
|
else
|
|
log "Failed to apply netplan configuration"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to configure static IP using NetworkManager
|
|
configure_static_ip_network_manager() {
|
|
local interface="$1"
|
|
local ip_address="$2"
|
|
local gateway="$3"
|
|
local dns="$4"
|
|
|
|
log "Configuring static IP for $interface using NetworkManager..."
|
|
|
|
# Set static IP
|
|
if nmcli con show "$interface" >/dev/null 2>&1; then
|
|
nmcli con mod "$interface" ipv4.method manual ipv4.addresses "$ip_address" ipv4.gateway "$gateway" ipv4.dns "$dns"
|
|
else
|
|
nmcli con add type ethernet ifname "$interface" con-name "$interface" ipv4.method manual ipv4.addresses "$ip_address" ipv4.gateway "$gateway" ipv4.dns "$dns"
|
|
fi
|
|
|
|
# Restart connection
|
|
nmcli con down "$interface" && nmcli con up "$interface"
|
|
|
|
log "NetworkManager configuration applied successfully"
|
|
return 0
|
|
}
|
|
|
|
# Function to configure static IP
|
|
configure_static_ip() {
|
|
local ip_address="${IP_ADDRESS_ENV:-$1}"
|
|
local gateway="${GATEWAY_ENV:-$DEFAULT_GATEWAY}"
|
|
local interface
|
|
local dns="$DNS_SERVERS"
|
|
|
|
# Create log directory if it doesn't exist
|
|
mkdir -p /var/log
|
|
|
|
# Get active interface
|
|
if ! interface=$(get_active_interface); then
|
|
log "No active network interface found (eth0 or wlan0)"
|
|
return 1
|
|
fi
|
|
|
|
log "Active interface: $interface"
|
|
log "Configuring static IP: $ip_address"
|
|
log "Gateway: $gateway"
|
|
log "DNS Servers: $dns"
|
|
|
|
# Check which network management system is in use
|
|
if uses_dhcpcd; then
|
|
configure_static_ip_dhcpcd "$interface" "$ip_address" "$gateway" "$dns"
|
|
elif uses_netplan; then
|
|
configure_static_ip_netplan "$interface" "$ip_address" "$gateway" "$dns"
|
|
elif uses_network_manager; then
|
|
configure_static_ip_network_manager "$interface" "$ip_address" "$gateway" "$dns"
|
|
else
|
|
log "Could not determine network configuration method"
|
|
log "Please configure your network manually:"
|
|
log "Interface: $interface"
|
|
log "IP Address: $ip_address"
|
|
log "Gateway: $gateway"
|
|
log "DNS Servers: $dns"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# --- Main Execution ---
|
|
|
|
# Configure static IP
|
|
configure_static_ip
|