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:
$ whoami && id deployrootid: 'deploy': no such userStep 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:
$ 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] YOn Ubuntu 24.04
adduser on 24.04 also prints each thing it does before it asks for the password:
$ 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' ...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:
$ getent passwd deploydeploy:x:1000:1000:,,,:/home/deploy:/bin/bashStep 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:
$ usermod -aG sudo deploy$ groups deploydeploy : deploy sudo usersStep 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:
$ cp -r ~/.ssh /home/deploy/ && chown -R deploy:deploy /home/deploy/.sshCheck 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-------:
$ 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_keysIf 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):
$ whoami && groupsdeploydeploy sudo usersNow run a command with sudo. sudo asks for deploy’s OWN password (the one from step 1), not root’s:
$ sudo whoami[sudo: authenticate] Password:rootOn Ubuntu 24.04
The classic sudo asks with a different prompt, after the first-sudo note:
$ sudo whoamiTo run a command as administrator (user "root"), use "sudo <command>".See "man sudo_root" for details. [sudo] password for deploy:rootThe prompt [sudo: authenticate] Password: is from sudo-rs, which Ubuntu 26.04 uses as sudo. To see which sudo you have:
$ sudo --versionsudo-rs 0.2.13-0ubuntu1.2On Ubuntu 24.04
$ 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.15p5root means it works. sudo -l lists what deploy may run: (ALL : ALL) ALL is every command, as any user:
$ sudo -l[sudo: authenticate] Password:User deploy may run the following commands on web-server: (ALL : ALL) ALLOn Ubuntu 24.04
The classic sudo also lists its default settings first:
$ 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) ALLWhy 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”:
$ sudo grep -n "^%sudo" /etc/sudoers[sudo: authenticate] Password:53:%sudo ALL=(ALL:ALL) ALLSo 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:
$ 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/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:
$ groups sparespare : spare admNow the mistake: -G without -a. The user gets sudo, but adm is gone:
$ usermod -G sudo spare && groups sparespare : spare sudoThe test user was then removed:
$ userdel spare && id spareid: 'spare': no such userIf 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:
$ sudo whoamisudo: I'm sorry deploy. I'm afraid I can't do thatOn Ubuntu 24.04
The classic sudo asks for the password first, then refuses:
$ sudo whoami[sudo] password for deploy:deploy is not in the sudoers file.- Check the group from root:
groups deploymust listsudo. 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 usersafter 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):
$ ls /usr/sbin/sendmaills: cannot access '/usr/sbin/sendmail': No such file or directoryWhat 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
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| adduser | 3.153ubuntu1 | 3.137ubuntu1 |
| passwd | 1:4.17.4-2ubuntu3 | 1:4.13+dfsg1-4ubuntu3.2 |
| sudo | 1.9.17p2-1ubuntu3.2 | 1.9.15p5-3ubuntu5.24.04.4 |
| sudo-rs | 0.2.13-0ubuntu1.2 | not installed |
| openssh-server | 1:10.2p1-2ubuntu3.8 | 1:9.6p1-3ubuntu13.20 |
The program behind sudo | sudo-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 group | refused 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 first | No | Yes |
| adduser and deluser print each step | No | Yes |
| First-sudo note at the new user’s first login | No | Yes |
| Proof | Run log, 28 checks passed | Run 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:
$ deluser deploy sudoOn Ubuntu 24.04
On 24.04 deluser says what it did (on 26.04 it prints nothing):
$ deluser deploy sudoinfo: Removing user `deploy' from group `sudo' ...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:
$ userdel -r deployuserdel: user deploy is currently used by process 62669End 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:
$ loginctl terminate-user deploy$ 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 directoryBefore 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.
