ServerHandbook live check: run log ------------------------------------------------------------------------------ Article https://serverhandbook.com/kb/create-sudo-user-ubuntu-24-04 Check scripts/live/checks/create-sudo-user-ubuntu-24-04.mjs (sha256 32926881d5be4ae576cd50b658e241656ed170d98896d456ecd52b900ed16b26) Ran at 2026-11-11T15:52:00Z (48 s) Result PASS, all 26 claims held Tested on System Ubuntu 24.04.5 LTS Kernel 6.8.0-147-generic Package adduser 3.137ubuntu1 Package passwd 1:4.13+dfsg1-4ubuntu3.2 Package sudo 1.9.15p5-3ubuntu5.24.04.4 Package openssh-server 1:9.6p1-3ubuntu13.20 Setup A fresh Ubuntu 24.04 server as a provider hands it over: root logs in with your SSH key, no other user. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ Claims (26), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok the provider logs you in as root, and there is no deploy user yet | root | id: ‘deploy’: no such user 3. ok adduser creates the user, its group and /home/deploy, and sets the password | info: 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 successfully | Changing the user information for deploy | Enter 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 | info: Adding new user `deploy' to supplemental / extra groups `users' ... | info: Adding user `deploy' to group `users' ... 4. ok deploy gets /home/deploy and /bin/bash | deploy:x:1000:1000:,,,:/home/deploy:/bin/bash 5. ok usermod -aG sudo prints nothing and succeeds | 0 6. ok groups deploy now lists sudo | deploy : deploy sudo users 7. ok a login started before usermod still has no sudo group (groups are read at login) | deploy users 8. ok cp + chown copy root’s .ssh folder to deploy and print nothing | 0 9. ok the copy is owned by deploy: .ssh is drwx------ and authorized_keys -rw------- | total 12 | drwx------ 2 deploy deploy 4096 Nov 11 15:52 . | drwxr-x--- 3 deploy deploy 4096 Nov 11 15:52 .. | -rw------- 1 deploy deploy 108 Nov 11 15:52 authorized_keys 10. ok the key logs in as deploy, and the new login has the sudo group | To run a command as administrator (user "root"), use "sudo ". | See "man sudo_root" for details. | | deploy | deploy sudo users 11. ok sudo asks for deploy’s own password, then runs as root | To run a command as administrator (user "root"), use "sudo ". | See "man sudo_root" for details. | | [sudo] password for deploy: | root 12. ok sudo -l shows (ALL : ALL) ALL for deploy | [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 13. ok /etc/sudoers gives the sudo group (ALL:ALL) ALL | [sudo] password for deploy: | 53:%sudo ALL=(ALL:ALL) ALL 14. ok useradd sets no password: passwd -S shows L (locked) | spare L 2026-11-11 0 99999 7 -1 15. ok useradd with no options gives /bin/sh and makes no home folder | spare:x:1001:1001::/home/spare:/bin/sh | ls: cannot access '/home/spare': No such file or directory 16. ok the spare user is in adm | spare : spare adm 17. ok usermod -G sudo WITHOUT -a drops adm: the list is replaced, not added to | spare : spare sudo 18. ok userdel removes the spare user | id: ‘spare’: no such user 19. ok deluser deploy sudo removes deploy from the group | info: Removing user `deploy' from group `sudo' ... 20. ok a new login then gets "deploy is not in the sudoers file" | [sudo] password for deploy: | deploy is not in the sudoers file. 21. ok no mail program on a fresh server, so sudo prints no "This incident has been reported" line | ls: cannot access '/usr/sbin/sendmail': No such file or directory 22. ok (tested on) the box reports its OS, kernel and the version of 4 package(s) | PRETTY_NAME="Ubuntu 24.04.5 LTS" | VIRT=none | KERNEL=6.8.0-147-generic | PKG adduser 3.137ubuntu1 | PKG openssh-server 1:9.6p1-3ubuntu13.20 | PKG passwd 1:4.13+dfsg1-4ubuntu3.2 | PKG sudo 1.9.15p5-3ubuntu5.24.04.4 23. ok userdel refuses while deploy is still logged in: "currently used by process" | userdel: user deploy is currently used by process 1804 24. ok loginctl terminate-user ends every deploy login and prints nothing | 0 25. ok a few seconds after terminate-user, deploy has no process left | gone after 1 s 26. ok userdel -r removes deploy and /home/deploy | userdel: deploy mail spool (/var/mail/deploy) not found | id: ‘deploy’: no such user | ls: cannot access '/home/deploy': No such file or directory ------------------------------------------------------------------------------ Commands and what they printed (21) ------------------------------------------------------------------------------ $ whoami && id deploy root id: ‘deploy’: no such user $ adduser deploy info: 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 successfully Changing the user information for deploy Enter 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 info: Adding new user `deploy' to supplemental / extra groups `users' ... info: Adding user `deploy' to group `users' ... $ getent passwd deploy deploy:x:1000:1000:,,,:/home/deploy:/bin/bash $ usermod -aG sudo deploy $ groups deploy deploy : deploy sudo users $ cp -r ~/.ssh /home/deploy/ && chown -R deploy:deploy /home/deploy/.ssh $ ls -la /home/deploy/.ssh total 12 drwx------ 2 deploy deploy 4096 Nov 11 15:52 . drwxr-x--- 3 deploy deploy 4096 Nov 11 15:52 .. -rw------- 1 deploy deploy 108 Nov 11 15:52 authorized_keys $ whoami && groups To run a command as administrator (user "root"), use "sudo ". See "man sudo_root" for details. deploy deploy sudo users $ sudo whoami To run a command as administrator (user "root"), use "sudo ". See "man sudo_root" for details. [sudo] password for deploy: root $ 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 $ sudo grep -n "^%sudo" /etc/sudoers [sudo] password for deploy: 53:%sudo ALL=(ALL:ALL) ALL $ useradd spare && getent passwd spare && ls -d /home/spare spare:x:1001:1001::/home/spare:/bin/sh ls: cannot access '/home/spare': No such file or directory $ groups spare spare : spare adm $ usermod -G sudo spare && groups spare spare : spare sudo $ userdel spare && id spare id: ‘spare’: no such user $ deluser deploy sudo info: Removing user `deploy' from group `sudo' ... $ sudo whoami [sudo] password for deploy: deploy is not in the sudoers file. $ ls /usr/sbin/sendmail ls: cannot access '/usr/sbin/sendmail': No such file or directory $ userdel -r deploy userdel: user deploy is currently used by process 1804 $ loginctl terminate-user deploy $ userdel -r deploy; id deploy; ls -d /home/deploy userdel: deploy mail spool (/var/mail/deploy) not found id: ‘deploy’: no such user ls: cannot access '/home/deploy': No such file or directory ------------------------------------------------------------------------------ The check's source (scripts/live/checks/create-sudo-user-ubuntu-24-04.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB: Create a sudo user on Ubuntu 24.04 (and log in as it with the key you already use for root). * * The server is a fresh VM box as a provider hands it over: root with the customer's SSH key, no other user. Every * step the reader types runs as root (`root`), then as the new user (`deploy`) over a REAL key login: the key works * only because the article's rsync step copied root's authorized_keys. Unlike the other checks, this one never calls * `sudoUser()`: that adds a NOPASSWD sudoers line for scripted runs, which would make "sudo asks for deploy's * password" untestable. sudo prompts are answered on a pty instead. Every claim below is one the article makes. */ import { spawn } from 'node:child_process' import { asRoot, sshOpts } from '../box.mjs' import { reportBox, bareBox, shell } from '../lib.mjs' export const meta = {} // throwaway test values for a box deleted after the run; shown as [password] in the published log const DEPLOY_PASSWORD = '[password]' const SPARE_PASSWORD = '[password]' const sleep = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms) const check = (ctx) => bareBox(ctx, async ({ root }) => { const pw = ctx.secret(DEPLOY_PASSWORD) const sudoAnswer = [['password for deploy', pw]] const quick = { timeout: 90 } // ── the starting point: root with a key, no other user ─────────────────────────────────── const start = root.run('whoami && id deploy', { id: 'start' }) ctx.check('the provider logs you in as root, and there is no deploy user yet', /^root$/m.test(start.out) && /no such user/.test(start.out), start.out) // ── step 1: adduser ────────────────────────────────────────────────────────────────────── const add = root.ask('adduser deploy', [ ['New password:', pw], ['Retype new password:', pw], ['Full Name', ''], ['Room Number', ''], ['Work Phone', ''], ['Home Phone', ''], ['Other', ''], ['correct\\?', 'Y'], ]) ctx.check('adduser creates the user, its group and /home/deploy, and sets the password', add.exit === 0 && /deploy/.test(add.out) && /home\/deploy/.test(add.out) && /password updated successfully/.test(add.out), add.out) const pwent = root.run('getent passwd deploy') ctx.check('deploy gets /home/deploy and /bin/bash', /:\/home\/deploy:\/bin\/bash$/m.test(pwent.out), pwent.out) // a login that starts BEFORE the group change (the article's "log in again" advice) L0(`rm -f /tmp/old-session; nohup su - deploy -c 'sleep 8; id -nG' > /tmp/old-session 2>&1 < /dev/null &`) sleep(2000) // ── step 2: the sudo group ─────────────────────────────────────────────────────────────── const um = root.run('usermod -aG sudo deploy') ctx.check('usermod -aG sudo prints nothing and succeeds', um.exit === 0 && um.out === '', `${um.exit} ${um.out}`) const gr = root.run('groups deploy') ctx.check('groups deploy now lists sudo', /^deploy : .*\bsudo\b/m.test(gr.out), gr.out) sleep(9000) const old = L0('cat /tmp/old-session') ctx.check('a login started before usermod still has no sudo group (groups are read at login)', /^deploy\b/m.test(old) && !/\bsudo\b/.test(old), old) // ── step 3: copy root's key ────────────────────────────────────────────────────────────── const cp = root.run('cp -r ~/.ssh /home/deploy/ && chown -R deploy:deploy /home/deploy/.ssh') ctx.check('cp + chown copy root’s .ssh folder to deploy and print nothing', cp.exit === 0 && cp.out === '', `${cp.exit} ${cp.out}`) const ls = root.run('ls -la /home/deploy/.ssh') ctx.check('the copy is owned by deploy: .ssh is drwx------ and authorized_keys -rw-------', /^drwx------ \d+ deploy deploy .* \.\n?$/m.test(ls.out) && /-rw------- 1 deploy deploy .* authorized_keys/.test(ls.out), ls.out) // ── step 4: log in as deploy (the copied key) and use sudo ──────────────────────────────── const deploy = shell(ctx, 'deploy') const who = deploy.run('whoami && groups') if (!ctx.check('the key logs in as deploy, and the new login has the sudo group', /^deploy$/m.test(who.out) && /\bsudo\b/.test(who.out), who.out)) return const su = deploy.ask('sudo whoami', sudoAnswer, quick) ctx.check('sudo asks for deploy’s own password, then runs as root', /\[sudo\] password for deploy:/.test(su.out) && /^root$/m.test(su.out), su.out) const sl = deploy.ask('sudo -l', sudoAnswer, { id: 'sudo-l', ...quick }) ctx.check('sudo -l shows (ALL : ALL) ALL for deploy', /\(ALL : ALL\) ALL/.test(sl.out), sl.out) // ── why: the rule that makes the sudo group admins ─────────────────────────────────────── const rule = deploy.ask('sudo grep -n "^%sudo" /etc/sudoers', sudoAnswer, { id: 'rule', ...quick }) ctx.check('/etc/sudoers gives the sudo group (ALL:ALL) ALL', /^\d+:%sudo\s+ALL=\(ALL:ALL\) ALL$/m.test(rule.out), rule.out) // ── adduser vs useradd, and -G without -a, on a spare user ─────────────────────────────── const ua = root.run('useradd spare && getent passwd spare && ls -d /home/spare', { id: 'useradd' }) const locked = L0('passwd -S spare') ctx.check('useradd sets no password: passwd -S shows L (locked)', /^spare L /m.test(locked), locked) ctx.check('useradd with no options gives /bin/sh and makes no home folder', /:\/home\/spare:\/bin\/sh$/m.test(ua.out) && /No such file or directory/.test(ua.out), ua.out) L0(`echo 'spare:${ctx.secret(SPARE_PASSWORD)}' | chpasswd && usermod -aG adm spare`) const before = root.run('groups spare', { id: 'spare-before' }) ctx.check('the spare user is in adm', /\badm\b/.test(before.out), before.out) const noA = root.run('usermod -G sudo spare && groups spare', { id: 'no-a' }) ctx.check('usermod -G sudo WITHOUT -a drops adm: the list is replaced, not added to', /\bsudo\b/.test(noA.out) && !/\badm\b/.test(noA.out), noA.out) const ud = root.run('userdel spare && id spare', { id: 'userdel-spare' }) ctx.check('userdel removes the spare user', /no such user/.test(ud.out), ud.out) // ── undo, part 1: take sudo away ───────────────────────────────────────────────────────── const del = root.run('deluser deploy sudo', { id: 'deluser-sudo' }) ctx.check('deluser deploy sudo removes deploy from the group', del.exit === 0 && /sudo/.test(del.out), del.out) const ns = deploy.ask('sudo whoami', sudoAnswer, { id: 'not-in-sudoers', ...quick }) ctx.check('a new login then gets "deploy is not in the sudoers file"', /deploy is not in the sudoers file/.test(ns.out) && !/^root$/m.test(ns.out), ns.out) const mail = root.run('ls /usr/sbin/sendmail', { id: 'no-mailer' }) ctx.check('no mail program on a fresh server, so sudo prints no "This incident has been reported" line', /No such file or directory/.test(mail.out) && !/incident/.test(ns.out), mail.out) reportBox(ctx, root.L, 'A fresh Ubuntu 24.04 server as a provider hands it over: root logs in with your SSH key, no other user.', ['adduser', 'passwd', 'sudo', 'openssh-server']) // ── undo, part 2: remove the user and its home ─────────────────────────────────────────── // deploy still logged in elsewhere (the reader's second terminal): userdel refuses while it has a process // a REAL ssh login as deploy (a `su -` started by root registers as a root session, which terminate-user skips) spawn('ssh', [...sshOpts(), 'deploy@127.0.0.1', 'sleep 300'], { detached: true, stdio: 'ignore' }).unref() sleep(4000) const busy = root.run('userdel -r deploy', { id: 'userdel-busy' }) ctx.check('userdel refuses while deploy is still logged in: "currently used by process"', busy.exit !== 0 && /user deploy is currently used by process/.test(busy.out), busy.out) const term = root.run('loginctl terminate-user deploy') ctx.check('loginctl terminate-user ends every deploy login and prints nothing', term.exit === 0 && term.out === '', `${term.exit} ${term.out}`) // terminate-user returns at once; the processes end a moment later (about 2 s on the test VM) const gone = L0('for i in $(seq 1 20); do pgrep -u deploy >/dev/null || { echo "gone after $i s"; exit 0; }; sleep 1; done; echo still running') ctx.check('a few seconds after terminate-user, deploy has no process left', /^gone after \d+ s$/.test(gone), gone) const rm = root.run('userdel -r deploy; id deploy; ls -d /home/deploy', { id: 'userdel' }) ctx.check('userdel -r removes deploy and /home/deploy', /no such user/.test(rm.out) && /No such file or directory/.test(rm.out), rm.out) }) /** Root on the box, never shown: a probe or a setup step the reader does not type. */ function L0(script) { const r = asRoot(script) return `${r.stdout ?? ''}${r.stderr ?? ''}`.replace(/\s+$/, '') } export default check