From 52f932b26298a31f2c9664bc39a616ce86e2c672 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Sat, 23 Mar 2024 00:47:24 +0000 Subject: [PATCH] Switch to root user if non-root user is used for nixos installer --- src/nixos-anywhere.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/nixos-anywhere.sh b/src/nixos-anywhere.sh index d5adca2..70fe0c8 100755 --- a/src/nixos-anywhere.sh +++ b/src/nixos-anywhere.sh @@ -282,6 +282,7 @@ if [[ -n ${ssh_private_key_file-} ]]; then fi ssh_settings=$(ssh "${ssh_args[@]}" -G "${ssh_connection}") +ssh_user=$(echo "$ssh_settings" | awk '/^user / { print $2 }') ssh_host=$(echo "$ssh_settings" | awk '/^hostname / { print $2 }') ssh_port=$(echo "$ssh_settings" | awk '/^port / { print $2 }') @@ -411,6 +412,14 @@ SSH # waiting for machine to become available again until ssh_ -o ConnectTimeout=10 -- exit 0; do sleep 5; done fi + +# Installation will fail if non-root user is used for installer. +# Switch to root user by copying authorized_keys. +if [[ ${is_installer-n} == "y" ]] && [[ ${ssh_user} != "root" ]]; then + ssh_ "${maybe_sudo} mkdir -p /root/.ssh; ${maybe_sudo} cp ~/.ssh/authorized_keys /root/.ssh" + ssh_connection="root@${ssh_host}" +fi + for path in "${!disk_encryption_keys[@]}"; do step "Uploading ${disk_encryption_keys[$path]} to $path" ssh_ "umask 077; cat > $path" <"${disk_encryption_keys[$path]}"