diff --git a/README.md b/README.md index 86de3da..20c9243 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ It uses a two‑stage approach: - Prompts for root if needed - Downloads and runs the latest interactive wizard 2. **Remote setup wizard** (pulled from Git) - - Menu‑driven (`whiptail`) configuration: updates, sudo, hostname, SSH keys, aliases, neofetch, CIFS mounts, etc. + - Menu‑driven (`whiptail`) configuration: updates, sudo, hostname, SSH keys, aliases, fastfetch, CIFS mounts, etc. --- diff --git a/setup.sh b/setup.sh index 887fdf8..a8e7e80 100644 --- a/setup.sh +++ b/setup.sh @@ -1,7 +1,7 @@ #!/bin/bash ############################################################################### # setup.sh - Interactive Debian VM bootstrap wizard # -# Jordan-friendly edition (whiptail UI, aliases, neofetch, CIFS, etc.) # +# Jordan-friendly edition (whiptail UI, aliases, fastfetch, CIFS, etc.) # ############################################################################### set -euo pipefail @@ -63,7 +63,7 @@ fi # Welcome # ############################################################################### whiptail --title "VM Setup Wizard" --yesno "\ -Welcome!\n\nThis wizard will update the system, configure sudo, hostname,\naliases, neofetch, optional tools, CIFS mounts, and SSH keys.\n\nContinue?" 12 70 || exit 0 +Welcome!\n\nThis wizard will update the system, configure sudo, hostname,\naliases, fastfetch, optional tools, CIFS mounts, and SSH keys.\n\nContinue?" 12 70 || exit 0 ############################################################################### # Core packages & sudo # @@ -100,44 +100,44 @@ hostnamectl set-hostname "$NEW_HOST" msg "Hostname set to $NEW_HOST" ############################################################################### -# SSH keys for root # +# SSH keys for root (idempotent overwrite per run) # ############################################################################### install -d -m 700 /root/.ssh chmod 700 /root/.ssh -test -n "$DEFAULT_SSH_KEY" && { - grep -qxF "$DEFAULT_SSH_KEY" /root/.ssh/authorized_keys 2>/dev/null || \ - echo "$DEFAULT_SSH_KEY" >> /root/.ssh/authorized_keys - chmod 600 /root/.ssh/authorized_keys - msg "Default SSH key added for root" -} +TMP_KEYS_ROOT=$(mktemp) +if [[ -n "$DEFAULT_SSH_KEY" ]]; then + printf '%s\n' "$DEFAULT_SSH_KEY" >>"$TMP_KEYS_ROOT" +fi if confirm "Add additional SSH public key for root?"; then KEY=$(whiptail --title "SSH Key (root)" --inputbox "Paste your public key for root:" 12 70 3>&1 1>&2 2>&3) || die "Cancelled" - grep -qxF "$KEY" /root/.ssh/authorized_keys 2>/dev/null || echo "$KEY" >> /root/.ssh/authorized_keys - chmod 600 /root/.ssh/authorized_keys - msg "Additional SSH key added for root" + [[ -n "$KEY" ]] && printf '%s\n' "$KEY" >>"$TMP_KEYS_ROOT" fi +# de-duplicate and overwrite authorized_keys +awk '!seen[$0]++' "$TMP_KEYS_ROOT" > /root/.ssh/authorized_keys +chmod 600 /root/.ssh/authorized_keys +msg "Root authorized_keys updated (overwritten this run)" +rm -f "$TMP_KEYS_ROOT" ############################################################################### -# SSH keys for user jordanwages # +# SSH keys for user jordanwages (idempotent overwrite per run) # ############################################################################### USER_NAME="jordanwages" USER_HOME="/home/$USER_NAME" install -d -m 700 "$USER_HOME/.ssh" chown $USER_NAME:$USER_NAME "$USER_HOME/.ssh" -test -n "$DEFAULT_SSH_KEY" && { - grep -qxF "$DEFAULT_SSH_KEY" "$USER_HOME/.ssh/authorized_keys" 2>/dev/null || \ - echo "$DEFAULT_SSH_KEY" >> "$USER_HOME/.ssh/authorized_keys" - chown $USER_NAME:$USER_NAME "$USER_HOME/.ssh/authorized_keys" - chmod 600 "$USER_HOME/.ssh/authorized_keys" - msg "Default SSH key added for $USER_NAME" -} +TMP_KEYS_USER=$(mktemp) +if [[ -n "$DEFAULT_SSH_KEY" ]]; then + printf '%s\n' "$DEFAULT_SSH_KEY" >>"$TMP_KEYS_USER" +fi if confirm "Add additional SSH public key for $USER_NAME?"; then KEY=$(whiptail --title "SSH Key ($USER_NAME)" --inputbox "Paste public key for $USER_NAME:" 12 70 3>&1 1>&2 2>&3) || die "Cancelled" - grep -qxF "$KEY" "$USER_HOME/.ssh/authorized_keys" 2>/dev/null || echo "$KEY" >> "$USER_HOME/.ssh/authorized_keys" - chown $USER_NAME:$USER_NAME "$USER_HOME/.ssh/authorized_keys" - chmod 600 "$USER_HOME/.ssh/authorized_keys" - msg "Additional SSH key added for $USER_NAME" + [[ -n "$KEY" ]] && printf '%s\n' "$KEY" >>"$TMP_KEYS_USER" fi +awk '!seen[$0]++' "$TMP_KEYS_USER" > "$USER_HOME/.ssh/authorized_keys" +chown $USER_NAME:$USER_NAME "$USER_HOME/.ssh/authorized_keys" +chmod 600 "$USER_HOME/.ssh/authorized_keys" +msg "$USER_NAME authorized_keys updated (overwritten this run)" +rm -f "$TMP_KEYS_USER" ############################################################################### # Optional utilities # @@ -148,16 +148,18 @@ SEL_RAW=$(whiptail --title "Extra Utilities" --checklist \ "Select additional packages to install:" 15 60 8 \ "${OPTS[@]}" 3>&1 1>&2 2>&3) || true IFS=' ' read -r -a SEL_PKGS <<< "${SEL_RAW//\"/}" -ensure_pkgs bat ncdu neofetch cifs-utils "${SEL_PKGS[@]}" +ensure_pkgs bat ncdu fastfetch cifs-utils "${SEL_PKGS[@]}" # Aliases echo "alias cat='batcat --paging=never'" >/etc/profile.d/10-bat_alias.sh echo "alias du='ncdu'" >/etc/profile.d/10-ncdu_alias.sh chmod +x /etc/profile.d/10-*alias.sh -echo 'neofetch' >/etc/profile.d/90-neofetch.sh -chmod +x /etc/profile.d/90-neofetch.sh +# Ensure only fastfetch runs on login +rm -f /etc/profile.d/90-neofetch.sh +echo 'fastfetch' >/etc/profile.d/90-fastfetch.sh +chmod +x /etc/profile.d/90-fastfetch.sh ############################################################################### -# NFS mounts # +# NFS mounts (managed block, idempotent) # ############################################################################### NFS_HOSTS=(jimmu keiko keitai) OPTS=(); for h in "${NFS_HOSTS[@]}"; do OPTS+=("$h" "" OFF); done @@ -167,17 +169,33 @@ SEL_HOSTS=$(whiptail --title "NFS Mounts" --checklist \ mkdir -p /media for host in "${NFS_HOSTS[@]}"; do - TEMPLATE="${host}.wageshouse:/Data /media/${host} nfs4 _netdev,x-systemd.automount,noatime 0 0" - if [[ $SEL_HOSTS == *\"$host\"* ]]; then - mkdir -p "/media/$host" - grep -q "${host}.wageshouse" /etc/fstab && \ - sed -i "#${host}\.wageshouse#d" /etc/fstab - echo "$TEMPLATE" >> /etc/fstab - else - grep -q "${host}.wageshouse" /etc/fstab || echo "# $TEMPLATE" >> /etc/fstab - fi + [[ $SEL_HOSTS == *\"$host\"* ]] && mkdir -p "/media/$host" done +# Clean up legacy lines from prior runs (pre-managed-block versions) +for host in "${NFS_HOSTS[@]}"; do + TEMPLATE="${host}.wageshouse:/Data /media/${host} nfs4 _netdev,x-systemd.automount,noatime 0 0" + sed -i "#^${host}\.wageshouse:/Data /media/${host} nfs4 _netdev,x-systemd.automount,noatime 0 0$#d" /etc/fstab || true + sed -i "#^# ${host}\.wageshouse:/Data /media/${host} nfs4 _netdev,x-systemd.automount,noatime 0 0$#d" /etc/fstab || true +done + +# Replace the managed block with current selections +BLOCK_START="# BEGIN setup.sh managed NFS" +BLOCK_END="# END setup.sh managed NFS" +sed -i "/^$BLOCK_START$/,/^$BLOCK_END$/d" /etc/fstab || true +{ + echo "$BLOCK_START" + for host in "${NFS_HOSTS[@]}"; do + TEMPLATE="${host}.wageshouse:/Data /media/${host} nfs4 _netdev,x-systemd.automount,noatime 0 0" + if [[ $SEL_HOSTS == *\"$host\"* ]]; then + echo "$TEMPLATE" + else + echo "# $TEMPLATE" + fi + done + echo "$BLOCK_END" +} >> /etc/fstab + if ! mount -a 2>>"$LOGFILE"; then msg "⚠️ Some NFS mounts failed. Re-run the script to check configuration." fi @@ -186,4 +204,4 @@ fi # Summary # ############################################################################### msg "\ -Setup complete!\n\n• Hostname: $NEW_HOST\n• Sudoers: ${SEL_SUDO//\"/ }\n• Packages: bat ncdu neofetch ${SEL_PKGS[*]}\n• CIFS: ${SEL_HOSTS//\"/ }\n\nLog saved to $LOGFILE\n\nEnjoy your new Debian VM!" +Setup complete!\n\n• Hostname: $NEW_HOST\n• Sudoers: ${SEL_SUDO//\"/ }\n• Packages: bat ncdu fastfetch ${SEL_PKGS[*]}\n• CIFS: ${SEL_HOSTS//\"/ }\n\nLog saved to $LOGFILE\n\nEnjoy your new Debian VM!"