Last active
June 4, 2025 08:27
-
-
Save NotYusta/bd638015af9a1cfa3ede720e29adefd3 to your computer and use it in GitHub Desktop.
Linux Automatic Network Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if all arguments are provided | |
if [ $# -ne 6 ]; then | |
echo "Usage: $0 <ipv4> <cidr/subnet-mask> <gateway> <dns1> <dns2> <mac-address>" | |
exit 1 | |
fi | |
IPV4=$1 | |
CIDR=$2 | |
GATEWAY=$3 | |
DNS1=$4 | |
DNS2=$5 | |
MACADDR=$6 | |
# Check if apt (Debian-based) or dnf (RHEL-based) exists | |
if command -v apt >/dev/null 2>&1; then | |
# Detect interface name by MAC address (Debian-based systems) | |
INTERFACE=$(ip -o link | grep -i "$MACADDR" | awk -F':' '{print $2}' | tr -d ' ') | |
if [ -z "$INTERFACE" ]; then | |
echo "Error: No interface found with MAC address $MACADDR" | |
exit 1 | |
fi | |
echo "Detected Debian-based system (apt exists), using interface: $INTERFACE" | |
# Create the Netplan configuration | |
cat <<EOF > /etc/netplan/01-netcfg.yaml | |
network: | |
version: 2 | |
ethernets: | |
$INTERFACE: | |
dhcp4: no | |
addresses: [$IPV4/$CIDR] | |
gateway4: $GATEWAY | |
nameservers: | |
addresses: | |
- $DNS1 | |
- $DNS2 | |
match: | |
macaddress: $MACADDR | |
set-name: $INTERFACE | |
EOF | |
# Apply the network configuration | |
netplan apply | |
echo "Network and DNS configuration applied permanently on Debian-based system." | |
elif command -v dnf >/dev/null 2>&1; then | |
# Detect interface name by MAC address (RHEL-based systems) | |
INTERFACE=$(ip -o link | grep -i "$MACADDR" | awk -F':' '{print $2}' | tr -d ' ') | |
if [ -z "$INTERFACE" ]; then | |
echo "Error: No interface found with MAC address $MACADDR" | |
exit 1 | |
fi | |
echo "Detected RHEL-based system (dnf exists), using interface: $INTERFACE" | |
# Determine subnet mask from CIDR | |
SUBNET_MASK=$(ipcalc -m $IPV4/$CIDR | grep -w 'NETMASK' | cut -d '=' -f 2) | |
# Backup current configuration if it exists | |
if [ -f /etc/sysconfig/network-scripts/ifcfg-$INTERFACE ]; then | |
cp /etc/sysconfig/network-scripts/ifcfg-$INTERFACE /etc/sysconfig/ifcfg-$INTERFACE.bak | |
echo "Backup of existing configuration created." | |
fi | |
# Create static IP config | |
cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE | |
DEVICE=$INTERFACE | |
NAME=$INTERFACE | |
BOOTPROTO=none | |
ONBOOT=yes | |
IPADDR=$IPV4 | |
NETMASK=$SUBNET_MASK | |
GATEWAY=$GATEWAY | |
DNS1=$DNS1 | |
DNS2=$DNS2 | |
HWADDR=$MACADDR | |
EOF | |
# Restart network | |
nmcli connection reload | |
nmcli connection down $INTERFACE && nmcli connection up $INTERFACE | |
echo "Network and DNS configuration applied permanently on RHEL-based system." | |
else | |
echo "Unsupported package manager. This script supports only apt (Debian-based) or dnf (RHEL-based) systems." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment