Skip to content

Commit 52f932b

Browse files
committed
Switch to root user if non-root user is used for nixos installer
1 parent c34fd21 commit 52f932b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/nixos-anywhere.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ if [[ -n ${ssh_private_key_file-} ]]; then
282282
fi
283283

284284
ssh_settings=$(ssh "${ssh_args[@]}" -G "${ssh_connection}")
285+
ssh_user=$(echo "$ssh_settings" | awk '/^user / { print $2 }')
285286
ssh_host=$(echo "$ssh_settings" | awk '/^hostname / { print $2 }')
286287
ssh_port=$(echo "$ssh_settings" | awk '/^port / { print $2 }')
287288

@@ -411,6 +412,14 @@ SSH
411412
# waiting for machine to become available again
412413
until ssh_ -o ConnectTimeout=10 -- exit 0; do sleep 5; done
413414
fi
415+
416+
# Installation will fail if non-root user is used for installer.
417+
# Switch to root user by copying authorized_keys.
418+
if [[ ${is_installer-n} == "y" ]] && [[ ${ssh_user} != "root" ]]; then
419+
ssh_ "${maybe_sudo} mkdir -p /root/.ssh; ${maybe_sudo} cp ~/.ssh/authorized_keys /root/.ssh"
420+
ssh_connection="root@${ssh_host}"
421+
fi
422+
414423
for path in "${!disk_encryption_keys[@]}"; do
415424
step "Uploading ${disk_encryption_keys[$path]} to $path"
416425
ssh_ "umask 077; cat > $path" <"${disk_encryption_keys[$path]}"

0 commit comments

Comments
 (0)