Before You Start
Keep your current root or admin SSH session open for the whole procedure. You close it only after the new account has logged in from a second session and run a command with sudo. These steps are written for the current Ubuntu LTS releases and work the same on Debian, where the group is also called sudo. On a minimal Debian install, sudo may be missing; install it first with apt install sudo as root. Since Ubuntu 25.10, sudo is provided by sudo-rs; the commands below stay the same.
Step 1: Create the User
sudo adduser deploy
adduser creates the home folder, copies the default shell files, and asks for a password and some optional details such as the full name. Press Enter to skip the details. Choose a strong password even if you plan to log in with a key: sudo asks for it.
Step 2: Add the User to the sudo Group
Write -aG, not -G: without -a the user would be removed from all other supplementary groups.
sudo usermod -aG sudo deploy
groups deploy
The second command should list sudo. Members of this group may run any command as root, because the default /etc/sudoers on Debian and Ubuntu contains a rule for %sudo.
Step 3: Give the User Your SSH Key
If you log in with an SSH key, copy it to the new account. As root:
mkdir -p /home/deploy/.ssh
cp /root/.ssh/authorized_keys /home/deploy/.ssh/
chown -R deploy:deploy /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys
SSH ignores keys in folders with loose permissions, so the last three lines matter. Open the copied file: if a key line starts with options such as a command= entry that tells you to log in as another user, delete those options in the new copy.
Step 4: Test in a Second Session
Open a new terminal on your computer, leave the first one as it is, and log in as the new account (the command shows the account as user; type deploy):
ssh user@your_server_ip
Then check sudo:
sudo -v
sudo whoami
sudo -v asks for the password and returns quietly if the account may use sudo. sudo whoami should print root. If either fails, fix it from the first session, which is still open. Only then close it.
AlmaLinux and Rocky Linux
On these systems the admin group is wheel, and useradd does not ask for a password:
sudo useradd -m deploy
sudo passwd deploy
sudo usermod -aG wheel deploy
The default sudoers file already allows the %wheel group. Test in a second session in the same way. The difference between the two account tools is in useradd vs adduser.
Edit sudoers Only with visudo
A syntax error in the sudoers file can disable sudo for everyone. visudo checks the file before saving it and refuses a broken one. For your own rules, use a separate file in /etc/sudoers.d:
sudo visudo -f /etc/sudoers.d/deploy
For example, to let deploy restart one service without a password:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx
File names in that folder must not contain a dot or end with a tilde, or sudo ignores them. sudo visudo -c checks the configuration for syntax errors without changing it. Avoid a general NOPASSWD: ALL rule: anyone who gets into the account then has root without a password.
After It Works
- Stop logging in as root: once the sudo account works, you can disable direct root login over SSH. See how to change the root password for the root account itself.
- Reduce noise: moving SSH to another port is covered in how to change the SSH port.
- Review admins:
getent group sudolists every member of the group.
Troubleshooting
- Not in the sudoers file, or not allowed: the exact wording depends on the sudo version. The account is not in the group, or the session started before it was added. Log out and in again, then check with
groups. - Permission denied (publickey): the key was not copied, or the ownership or modes of the .ssh folder are wrong.