Operating System Linux

Set Up SSH Key Login for a Non-Root User on Ubuntu 26.04 and Debian 13

Create a non-root user, install your SSH public key for it and test the login on Ubuntu 26.04, Debian 13 and AlmaLinux 9.

A server that was installed with your SSH key usually accepts that key for root only. A user you create afterwards has no authorized_keys file, so the same key is refused. This guide creates a non-root user called deploy, gives it sudo rights, installs your public key for it and tests the login. The commands apply to Ubuntu 26.04 and 24.04, Debian 13 and 12, and, where noted, AlmaLinux 9 and Rocky Linux 9.

The guide stops once key login works for the new user. Turning off root login and password authentication are separate steps that you should only take after the test at the end succeeds.

What you need

  • A working login on the server as root or as a user with sudo. Keep that session open until the new login has been tested.
  • An SSH key pair on your own computer. The server only ever receives the public half, a file ending in .pub.

If you do not have a key pair yet, create one on your own computer, not on the server:

ssh-keygen -t ed25519

Accept the default location and set a passphrase. This writes the private key to ~/.ssh/id_ed25519 and the public key to ~/.ssh/id_ed25519.pub.

Step 1: Create the user

On Ubuntu and Debian, adduser creates the account and its home directory and asks for a password. Then add the user to the sudo group:

sudo adduser deploy
sudo usermod -aG sudo deploy

If you are logged in as root on a Debian system where sudo is not installed, run the commands without sudo and install it first with apt install sudo.

On AlmaLinux 9 and Rocky Linux 9 the administrators' group is called wheel:

sudo useradd -m deploy
sudo passwd deploy
sudo usermod -aG wheel deploy

Choose a real password even though you will log in with a key: sudo asks for it.

Step 2: Install your public key

The key has to end up as one line in /home/deploy/.ssh/authorized_keys. Pick the method that matches your server.

Method A: ssh-copy-id from your computer

Use this when the server still accepts password logins. Run it on your own computer:

ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10

You are asked for the password of deploy once. The script appends the key to .ssh/authorized_keys on the server and creates the file and directory if they are missing. Useful options:

  • -n is a dry run: it prints the keys that would be installed and changes nothing.
  • -p 2222 connects to a non-standard SSH port.
  • Without -i, the script installs every key your SSH agent holds, or else the most recently modified ~/.ssh/id*.pub file. Naming the file with -i avoids surprises.

If the command ends with Permission denied (publickey), the server does not accept passwords. Use method B or C from your existing session instead.

Method B: reuse the key that root already has

If you log in as root with the key you want to use, copy root's authorized_keys file to the new user. Run this on the server:

sudo install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
sudo install -m 600 -o deploy -g deploy /root/.ssh/authorized_keys /home/deploy/.ssh/authorized_keys

install copies the file and sets owner and mode in one step. Look at the result before you rely on it:

sudo cat /home/deploy/.ssh/authorized_keys

Each line should start with a key type such as ssh-ed25519 or ssh-rsa. If a line starts with options such as command="...", your image restricts root's key to printing a message; delete everything before the key type on that line, or use method C. Remove any keys that should not be able to log in as deploy.

Copy only this one file. Older tutorials copy the whole of /root/.ssh/, which also hands the new user any private keys stored there.

Method C: paste the public key by hand

Print the public key on your own computer and copy the whole line:

cat ~/.ssh/id_ed25519.pub

On the server, create the directory and add the line. Replace the example key with yours and keep the single quotes:

sudo install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
echo 'ssh-ed25519 AAAAC3...yourkey... you@laptop' | sudo tee -a /home/deploy/.ssh/authorized_keys
sudo chown deploy:deploy /home/deploy/.ssh/authorized_keys
sudo chmod 600 /home/deploy/.ssh/authorized_keys

Step 3: Check ownership and permissions

With the default StrictModes yes, sshd ignores an authorized_keys file if the file, the .ssh directory or the home directory can be written by anyone other than the user. This is the most common reason a correctly installed key is refused.

PathOwnerMode
/home/deploydeploynot writable by group or others (for example 750 or 755)
/home/deploy/.sshdeploy700
/home/deploy/.ssh/authorized_keysdeploy600

Check all three at once:

sudo ls -ld /home/deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys

Fix them if needed:

sudo chown -R deploy:deploy /home/deploy/.ssh
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys
sudo chmod go-w /home/deploy

On AlmaLinux 9 and Rocky Linux 9, also restore the SELinux labels, because files created or copied by hand can carry the wrong context:

sudo restorecon -R -v /home/deploy/.ssh

Step 4: Test the login

Leave your current session open. In a new terminal on your own computer, connect as the new user:

ssh deploy@203.0.113.10

You should get a shell without being asked for the account password (a prompt for the key's passphrase is expected and comes from your own computer). If your key is not in a default location, name it with ssh -i ~/.ssh/mykey deploy@203.0.113.10. Then confirm that sudo works:

sudo whoami

The answer should be root.

Troubleshooting

  • Still asked for a password, or "Permission denied (publickey)": run the client in verbose mode with ssh -v deploy@203.0.113.10 and look for the line that says which key is offered. Then read the server log from your open session: sudo journalctl -u ssh -n 30 on Ubuntu and Debian, sudo journalctl -u sshd -n 30 on the RHEL family.
  • The log says "Authentication refused: bad ownership or modes": repeat step 3. The message names the file or directory at fault.
  • The key is in the file but ignored: make sure it is on a single line with no line breaks added by your terminal, and that the server reads the usual file: sudo sshd -T | grep -i authorizedkeysfile should show .ssh/authorized_keys.
  • "deploy is not in the sudoers file": group membership is read at login. Log out and back in after the usermod command.

Frequently asked questions

Can I use the same key for root and for the new user?

Yes. A public key can be listed in any number of authorized_keys files, on any number of servers. What matters is that the private key never leaves your computer.

How do I give a second person access to the same account?

Add their public key as another line in the same authorized_keys file. To revoke access later, delete that line. A separate user account per person is cleaner, because logs and sudo records then show who did what.

Does the user still need a password?

Not for logging in, but sudo asks for it by default. A user without a usable password can log in with the key and then cannot run sudo unless you configure a passwordless rule for it.

Why does ssh-copy-id say the key already exists?

Before installing, the script tries to log in with each key and skips the ones that already work. Nothing needs to be done; go straight to the test.

Share story:

Continue reading other guides on How VPS.