Debian Setup Script: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „*cat /usr/local/sbin/debian-setup.sh <pre> #!/bin/bash # Standardwerte setzen FQDN="" ADDR="" GW="" NS="" USE_DHCP=0 # Optionen mit getopts parsen while ge…“)
 
 
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
=Das Script=
 
*cat  /usr/local/sbin/debian-setup.sh  
 
*cat  /usr/local/sbin/debian-setup.sh  
 
<pre>
 
<pre>
 
#!/bin/bash
 
#!/bin/bash
 
 
# Standardwerte setzen
 
# Standardwerte setzen
 
FQDN=""
 
FQDN=""
Zeile 8: Zeile 8:
 
GW=""
 
GW=""
 
NS=""
 
NS=""
USE_DHCP=0
+
 
  
 
# Optionen mit getopts parsen
 
# Optionen mit getopts parsen
while getopts "f:a:g:n:d" opt; do
+
while getopts "f:a:g:n:" opt; do
 
   case $opt in
 
   case $opt in
 
     f) FQDN="$OPTARG" ;;
 
     f) FQDN="$OPTARG" ;;
Zeile 17: Zeile 17:
 
     g) GW="$OPTARG" ;;
 
     g) GW="$OPTARG" ;;
 
     n) NS="$OPTARG" ;;
 
     n) NS="$OPTARG" ;;
    d) USE_DHCP=1 ;;
 
 
     *) echo "Ungültige Option" >&2; exit 1 ;;
 
     *) echo "Ungültige Option" >&2; exit 1 ;;
 
   esac
 
   esac
 
done
 
done
 
  
 
# Gültigkeit prüfen
 
# Gültigkeit prüfen
if [[ -z "$FQDN" ]]; then
+
if [[ -z "$FQDN" || -z "$ADDR" || -z "$GW" || -z "$NS" ]]; then
  echo "Fehlender FQDN! Nutzung:"
 
  echo "$0 -f <FQDN> [-d | -a <IP/CIDR> -g <Gateway> -n <Nameserver>]"
 
  exit 1
 
fi
 
 
 
if (( USE_DHCP == 1 )); then
 
  if [[ -n "$ADDR" || -n "$GW" || -n "$NS" ]]; then
 
    echo "Fehler: -d darf nicht mit -a, -g oder -n kombiniert werden." >&2
 
    exit 1
 
  fi
 
else
 
  if [[ -z "$ADDR" || -z "$GW" || -z "$NS" ]]; then
 
 
     echo "Fehlende Argumente für statische Konfiguration! Nutzung:"
 
     echo "Fehlende Argumente für statische Konfiguration! Nutzung:"
     echo "$0 -f <FQDN> [-d | -a <IP/CIDR> -g <Gateway> -n <Nameserver>]"
+
     echo "$0 -f <FQDN> -a <IP/CIDR> -g <Gateway> -n <Nameserver>"
 
     exit 1
 
     exit 1
  fi
 
 
fi
 
fi
  
Zeile 47: Zeile 32:
 
DOM=$(echo "$FQDN" | cut -d'.' -f2-)
 
DOM=$(echo "$FQDN" | cut -d'.' -f2-)
  
# /etc/network/interfaces schreiben
+
echo FQDN  : $FQDN
cat <<HERE > /etc/network/interfaces
+
echo ADDR  : $ADDR
 +
echo GW    : $GW
 +
echo NS    : $NS
 +
echo SHORT : $SHORT
 +
echo DOM  : $DOM
 +
 
 +
hostnamectl set-hostname $FQDN
 +
 
 +
cat<<HERE > /etc/network/interfaces
 
# This file describes the network interfaces available on your system
 
# This file describes the network interfaces available on your system
 
# and how to activate them. For more information, see interfaces(5).
 
# and how to activate them. For more information, see interfaces(5).
Zeile 54: Zeile 47:
 
source /etc/network/interfaces.d/*
 
source /etc/network/interfaces.d/*
  
 +
# The loopback network interface
 
auto lo
 
auto lo
 
iface lo inet loopback
 
iface lo inet loopback
  
 +
# The primary network interface
 
auto enp0s3
 
auto enp0s3
HERE
+
iface enp0s3 inet static  
 
 
if (( USE_DHCP == 1 ))
 
then
 
  echo "iface enp0s3 inet dhcp" >> /etc/network/interfaces
 
else
 
  cat <<HERE >> /etc/network/interfaces
 
iface enp0s3 inet static
 
 
  address $ADDR
 
  address $ADDR
 
  gateway $GW
 
  gateway $GW
Zeile 71: Zeile 59:
 
  dns-search $DOM
 
  dns-search $DOM
 
HERE
 
HERE
fi
+
</pre>
  
# Hostname setzen
+
=Rechte anpassen=
hostnamectl set-hostname "$FQDN"
+
*chmod +x /usr/local/sbin/debian-setup.sh
</pre>
 

Aktuelle Version vom 20. April 2026, 11:57 Uhr

Das Script

  • cat /usr/local/sbin/debian-setup.sh
#!/bin/bash
# Standardwerte setzen
FQDN=""
ADDR=""
GW=""
NS=""


# Optionen mit getopts parsen
while getopts "f:a:g:n:" opt; do
  case $opt in
    f) FQDN="$OPTARG" ;;
    a) ADDR="$OPTARG" ;;
    g) GW="$OPTARG" ;;
    n) NS="$OPTARG" ;;
    *) echo "Ungültige Option" >&2; exit 1 ;;
  esac
done

# Gültigkeit prüfen
if [[ -z "$FQDN" || -z "$ADDR" || -z "$GW" || -z "$NS" ]]; then
    echo "Fehlende Argumente für statische Konfiguration! Nutzung:"
    echo "$0 -f <FQDN> -a <IP/CIDR> -g <Gateway> -n <Nameserver>"
    exit 1
fi

# SHORT und DOM aus FQDN berechnen
SHORT=$(echo "$FQDN" | cut -d'.' -f1)
DOM=$(echo "$FQDN" | cut -d'.' -f2-)

echo FQDN  : $FQDN
echo ADDR  : $ADDR
echo GW    : $GW
echo NS    : $NS
echo SHORT : $SHORT
echo DOM   : $DOM

hostnamectl set-hostname $FQDN

cat<<HERE > /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet static 
 address $ADDR
 gateway $GW
 dns-nameservers $NS
 dns-search $DOM
HERE

Rechte anpassen

  • chmod +x /usr/local/sbin/debian-setup.sh