How to create a user with SSH access and sudo

On Ubuntu or Debian, run sudo adduser deploy and sudo usermod -aG sudo deploy. On RHEL or Fedora, run sudo useradd -m -G wheel deploy and sudo passwd deploy. Then put the public key in /home/deploy/.ssh/authorized_keys, owned by that user, with the folder at mode 700 and the file at 600. Test before closing your session.

Updated . Run on Ubuntu 24.04 and Debian 13 in throwaway containers, including key login and sudo.

Create the user

On Ubuntu and Debian, adduser asks for a password and some optional details, which you can skip with Enter:

sudo adduser deploy
sudo usermod -aG sudo deploy

The -a matters: without it, -G replaces the user's other groups. A minimal Debian may not have sudo at all, so run apt install sudo as root first.

On RHEL, Rocky, AlmaLinux and Fedora the admin group is wheel:

sudo useradd -m -G wheel deploy
sudo passwd deploy

Check the result:

id deploy
sudo -l -U deploy

Install the SSH key

Run this on the server, with the user's public key in place of the example. It sets the owner and modes in one step:

sudo install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
echo 'ssh-ed25519 AAAA... you@laptop' | sudo install -m 600 -o deploy -g deploy /dev/stdin /home/deploy/.ssh/authorized_keys

This writes a new file, replacing any existing one, so use it only for a new account. If the account has a password and password login is on, you can instead do this from your own machine, which appends the key:

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

On systems with SELinux enforcing, fix the labels too:

sudo restorecon -R /home/deploy/.ssh

The permissions that matter

PathOwnerMode
/home/deploydeploynot writable by others, for example 750
/home/deploy/.sshdeploy700
/home/deploy/.ssh/authorized_keysdeploy600

The classic mistake is creating these files as root and forgetting the owner. In our test, a file owned by root with mode 600 gave this, since the user cannot read it:

deploy@server: Permission denied (publickey,password).

Check with this, and see Permission denied (publickey) if the owners and modes are right but it still fails:

sudo stat -c '%a %U %n' /home/deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys

Debian and Ubuntu are more relaxed about group-writable folders when the group is the user's private one: our login still worked with .ssh at 775. Do not count on it, since other systems refuse this. Stick to the table.

Test it before you close your session

Open a new terminal and leave the old one logged in:

ssh deploy@server
sudo whoami

The second command should print root after asking for the password of deploy. Only then is it safe to disable root login or password login.

A user with a key and no password

For an account created from a script:

sudo adduser --disabled-password --comment "" deploy

On older releases, such as Ubuntu 22.04, the option is --gecos "". Key login still worked in our test on Ubuntu 24.04, but sudo cannot ask for a password that does not exist:

sudo: a password is required

Either set a password, or allow sudo without one for this user. Always create that file with visudo, which refuses a broken file:

sudo visudo -f /etc/sudoers.d/90-deploy

with this line inside:

deploy ALL=(ALL) NOPASSWD:ALL

Understand the cost: anyone who gets this key, or a shell as this user, immediately has root. Automation sometimes needs it, and a person logging in by hand rarely does. See using sudo over SSH safely.

Questions

How do I give an existing user sudo?

Run sudo usermod -aG sudo username on Debian and Ubuntu, or sudo usermod -aG wheel username on RHEL and Fedora. The user gets it at the next login.

Why is sudo asking for a password when I log in with a key?

They are separate. The key proves who you are to SSH. sudo asks for the account's own password, unless the sudoers file says otherwise.

Do I need to log out after adding the group?

Yes. Group membership is read at login, so a session opened before usermod does not have it.

Sources

More in SSH on each system

All guides