Short answer

Log in as root and run adduser deploy, then usermod -aG sudo deploy. Copy your SSH key to the new user with cp -r ~/.ssh /home/deploy/ and chown -R deploy:deploy /home/deploy/.ssh. Then open a new terminal, run ssh deploy@your-server-ip, and test with sudo whoami. It asks for deploy’s password and prints root. On Ubuntu 26.04, sudo is sudo-rs, a rewrite in Rust: same commands, different messages.

Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04? 4 sections differ, each step marked “On Ubuntu 24.04” right under it: Step 1: Create the user, Step 4: Log in as the new user and test sudo, If sudo says “I’m sorry deploy” or “not in the sudoers file”, Undo. Everything that differed

Before you start

  • A new Ubuntu 26.04 or 24.04 server, and the root login your provider gave you. Here root logs in with an SSH key, as most providers set it up.
  • Your server’s IP address. The commands use 203.0.113.10, an address kept for examples. Type yours instead.
  • A user name. This answer uses deploy. Pick any short lowercase name.

Log in as root. On a fresh server, root is the only user:

Server, as root
$ whoami && id deployrootid: 'deploy': no such user
Output hidden (2 lines)

Step 1: Create the user

Run adduser and type a password twice. It is not shown as you type. This is the password sudo will ask for later, so pick one you can type. Press Enter at each question about the name and phone numbers, then type Y:

Server, as root
$ adduser deployNew password:Retype new password:passwd: password updated successfullyChanging the user information for deployEnter the new value, or press ENTER for the default	Full Name []:	Room Number []:	Work Phone []:	Home Phone []:	Other []:Is the information correct? [Y/n] Y
Output hidden (11 lines)
On Ubuntu 24.04

adduser on 24.04 also prints each thing it does before it asks for the password:

Server, as root
$ adduser deployinfo: Adding user `deploy' ...info: Selecting UID/GID from range 1000 to 59999 ...info: Adding new group `deploy' (1000) ...info: Adding new user `deploy' (1000) with group `deploy (1000)' ...info: Creating home directory `/home/deploy' ...info: Copying files from `/etc/skel' ...New password:Retype new password:passwd: password updated successfullyChanging the user information for deployEnter the new value, or press ENTER for the default	Full Name []:	Room Number []:	Work Phone []:	Home Phone []:	Other []:Is the information correct? [Y/n] Yinfo: Adding new user `deploy' to supplemental / extra groups `users' ...info: Adding user `deploy' to group `users' ...
Output hidden (19 lines)

On both releases adduser made a group with the same name, a home folder with the default files from /etc/skel, and set bash as the shell:

Server, as root
$ getent passwd deploydeploy:x:1000:1000:,,,:/home/deploy:/bin/bash
Output hidden (1 line)

Step 2: Add the user to the sudo group

On Ubuntu, a member of the sudo group may run any command as root. -aG means “add to this group”. The command prints nothing when it works:

Server, as root
$ usermod -aG sudo deploy
Server, as root
$ groups deploydeploy : deploy sudo users
Output hidden (1 line)

Step 3: Copy your SSH key to the new user

Your provider put your key in root’s ~/.ssh/authorized_keys. The new user has no key yet, so you cannot log in as it with your key. Copy root’s .ssh folder and give it to deploy:

Server, as root
$ cp -r ~/.ssh /home/deploy/ && chown -R deploy:deploy /home/deploy/.ssh

Check the owner and the permissions. Both must belong to deploy, and nobody else may write to them: sshd refuses a key file that another user can write to. The folder should be drwx------ and the file -rw-------:

Server, as root
$ ls -la /home/deploy/.sshtotal 12drwx------ 2 deploy deploy 4096 Nov 18 22:11 .drwxr-x--- 3 deploy deploy 4096 Nov 18 22:11 ..-rw------- 1 deploy deploy  108 Nov 18 22:11 authorized_keys
Output hidden (4 lines)

If your provider set a root password instead of a key, skip this step and use SSH key login for deploy after step 4.

Step 4: Log in as the new user and test sudo

Keep the root session open. In a NEW terminal on your computer, run ssh deploy@203.0.113.10. The copied key logs you in with no password. Check who you are (on 24.04, Ubuntu first prints a short note on how to use sudo; 26.04 does not):

Server, as deploy
$ whoami && groupsdeploydeploy sudo users
Output hidden (2 lines)

Now run a command with sudo. sudo asks for deploy’s OWN password (the one from step 1), not root’s:

Server, as deploy
$ sudo whoami[sudo: authenticate] Password:root
Output hidden (2 lines)
On Ubuntu 24.04

The classic sudo asks with a different prompt, after the first-sudo note:

Server, as deploy
$ sudo whoamiTo run a command as administrator (user "root"), use "sudo <command>".See "man sudo_root" for details. [sudo] password for deploy:root
Output hidden (5 lines)

The prompt [sudo: authenticate] Password: is from sudo-rs, which Ubuntu 26.04 uses as sudo. To see which sudo you have:

Server, as deploy
$ sudo --versionsudo-rs 0.2.13-0ubuntu1.2
Output hidden (1 line)
On Ubuntu 24.04
Server, as deploy
$ sudo --versionSudo version 1.9.15p5Sudoers policy plugin version 1.9.15p5Sudoers file grammar version 50Sudoers I/O plugin version 1.9.15p5Sudoers audit plugin version 1.9.15p5
Output hidden (5 lines)

root means it works. sudo -l lists what deploy may run: (ALL : ALL) ALL is every command, as any user:

Server, as deploy
$ sudo -l[sudo: authenticate] Password:User deploy may run the following commands on web-server:    (ALL : ALL) ALL
Output hidden (3 lines)
On Ubuntu 24.04

The classic sudo also lists its default settings first:

Server, as deploy
$ sudo -l[sudo] password for deploy:Matching Defaults entries for deploy on web-server:    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,    use_pty User deploy may run the following commands on web-server:    (ALL : ALL) ALL
Output hidden (7 lines)

Why the sudo group gives admin rights

There is no setting on the user. Ubuntu’s /etc/sudoers has one line for the whole group, and % means “group”:

Server, as deploy
$ sudo grep -n "^%sudo" /etc/sudoers[sudo: authenticate] Password:53:%sudo	ALL=(ALL:ALL) ALL
Output hidden (2 lines)

So you never need to edit /etc/sudoers to make an admin. Adding the user to the group is enough, and removing them takes the rights away. If you ever write your own rule, put it in a file under /etc/sudoers.d/ and edit it with sudo visudo -f, which refuses to save a file with a mistake in it.

adduser or useradd?

Use adduser. useradd is the low-level tool, and with no options it makes a user with no home folder and /bin/sh as its shell:

Server, as root
$ useradd spare && getent passwd spare && ls -d /home/sparespare:x:1001:1001::/home/spare:/bin/shls: cannot access '/home/spare': No such file or directory
Output hidden (2 lines)

/home/spare is written in the user’s entry, but the folder was never made, so there is nowhere to put an SSH key. useradd also sets no password: the account is locked (L) until you run passwd. To get what adduser gives you, you would need useradd -m -s /bin/bash and then passwd.

What -G without -a does

The spare user above was added to the adm group, which can read log files:

Server, as root
$ groups sparespare : spare adm
Output hidden (1 line)

Now the mistake: -G without -a. The user gets sudo, but adm is gone:

Server, as root
$ usermod -G sudo spare && groups sparespare : spare sudo
Output hidden (1 line)

The test user was then removed:

Server, as root
$ userdel spare && id spareid: 'spare': no such user
Output hidden (1 line)

If sudo says “I’m sorry deploy” or “not in the sudoers file”

This is what a user who is not in the sudo group sees. It was captured after step 2 had been undone. sudo-rs on 26.04 refuses at once, without asking for a password:

Server, as deploy
$ sudo whoamisudo: I'm sorry deploy. I'm afraid I can't do that
Output hidden (1 line)
On Ubuntu 24.04

The classic sudo asks for the password first, then refuses:

Server, as deploy
$ sudo whoami[sudo] password for deploy:deploy is not in the sudoers file.
Output hidden (2 lines)
  • Check the group from root: groups deploy must list sudo. If not, run step 2.
  • If it does list sudo, log out and log in again. A login keeps the groups it had when it started: in the test, a deploy session opened before step 2 still showed only deploy users after it.
  • Older guides quote a second line, “This incident will be reported”. Neither release printed it: sudo-rs does not have it, and the classic sudo prints a line like that only when it has mailed the admin, and it mails only when the server has a mail program. A fresh server has none (see the sources in the Tested on box):
Server, as root
$ ls /usr/sbin/sendmaills: cannot access '/usr/sbin/sendmail': No such file or directory
Output hidden (1 line)

What a security audit and the server say about it

Both runs audited the whole server with Lynis before the first step and after the last (see the Tested on box below). Lynis noted one new suggestion on both releases: AUTH-9282, set an expiry date for password-protected accounts, because deploy now has a password. The same box lists what changed on the server: only the user and group files in /etc (passwd, shadow, group, gshadow, subuid, subgid, each with its - backup).

Ubuntu 26.04 vs 24.04: what changed

WhatUbuntu 26.04Ubuntu 24.04
SystemUbuntu 26.04.1 LTS7.0.0-43-genericUbuntu 24.04.5 LTS6.8.0-147-generic
adduser3.153ubuntu13.137ubuntu1
passwd1:4.17.4-2ubuntu31:4.13+dfsg1-4ubuntu3.2
sudo1.9.17p2-1ubuntu3.21.9.15p5-3ubuntu5.24.04.4
sudo-rs0.2.13-0ubuntu1.2not installed
openssh-server1:10.2p1-2ubuntu3.81:9.6p1-3ubuntu13.20
The program behind sudosudo-rs 0.2.13 (Rust)sudo 1.9.15p5 (C)
sudo’s password prompt[sudo: authenticate] Password:[sudo] password for deploy:
A user outside the sudo grouprefused at once: “I’m sorry deploy. I’m afraid I can’t do that”asked for the password, then “deploy is not in the sudoers file.”
sudo -l lists the default settings firstNoYes
adduser and deluser print each stepNoYes
First-sudo note at the new user’s first loginNoYes
ProofRun log, 28 checks passedRun log, 28 checks passed

Undo

To take sudo away but keep the user, remove them from the group. The change applies to their next login:

Server, as root
$ deluser deploy sudo
On Ubuntu 24.04

On 24.04 deluser says what it did (on 26.04 it prints nothing):

Server, as root
$ deluser deploy sudoinfo: Removing user `deploy' from group `sudo' ...
Output hidden (1 line)

To remove the user and their home folder, with every file in it, first make sure deploy is not logged in anywhere. While it is, even in the second terminal from step 4, userdel refuses:

Server, as root
$ userdel -r deployuserdel: user deploy is currently used by process 62669
Output hidden (1 line)

End every deploy login. loginctl returns at once, and the logins close a second or two later (about 2 seconds in the test), so wait a moment before the next command. If userdel still says “currently used by process”, run it again:

Server, as root
$ loginctl terminate-user deploy
Server, as root
$ userdel -r deploy; id deploy; ls -d /home/deployuserdel: deploy mail spool (/var/mail/deploy) not foundid: 'deploy': no such userls: cannot access '/home/deploy': No such file or directory
Output hidden (3 lines)

Before you remove the last sudo user, make sure you can still log in as root. Otherwise you can only get back in through your provider’s web console.