Most cloud server providers let you add your SSH keys during server creation. On Debian servers, this usually means the SSH key gets added for the root user.
However, since it’s best practice not to use root for daily administration, you’ll likely create a separate sudo user. To avoid having to manually set up new SSH keys, it’s useful to copy the root user’s ~/.ssh/authorized_keys to the new sudo user.
This is especially necessary on providers like UpCloud, which don’t allow password logins on Debian servers by default. Without SSH key access set up for your new user, you won’t be able to log in unless you:
- Temporarily enable password login by editing
/etc/ssh/sshd_config - Restart SSH
- Log in as the new user with a password (if you know it)
- Copy your public SSH key to the user
- Then disable password login again in
/etc/ssh/sshd_config
That’s a lot of steps. An easier, safer method is to copy root’s SSH authorized_keys directly to your new sudo user using a script.
How to Do It
Create a file (e.g. root-ssh-to-user.sh) and paste the following script:
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# Replace this with your actual username
TARGET_USER="yourusername"
TARGET_HOME="/home/$TARGET_USER"
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Create the user if they don't exist
if ! id "$TARGET_USER" &>/dev/null; then
echo "Creating user $TARGET_USER..."
adduser --disabled-password --gecos "" "$TARGET_USER"
usermod -aG sudo "$TARGET_USER"
fi
# Copy SSH keys
mkdir -p "$TARGET_HOME/.ssh"
chmod 700 "$TARGET_HOME/.ssh"
cp /root/.ssh/authorized_keys "$TARGET_HOME/.ssh/"
chmod 600 "$TARGET_HOME/.ssh/authorized_keys"
chown -R "$TARGET_USER:$TARGET_USER" "$TARGET_HOME/.ssh"
echo "SSH keys copied. You can now SSH as $TARGET_USER."
Steps to Run It
nano root-ssh-to-user.sh
Then paste the script content into the file, replacing yourusername with your actual sudo username.
# Make the script executable
chmod +x root-ssh-to-user.sh
# Run the script as root (or with sudo)
sudo ./root-ssh-to-user.sh