Linux and servers · Beginner · 9 min
How to Create a Sudo User on Ubuntu 26.04
By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS
You will create a user that can run administrative commands with sudo, test it, limit it to specific commands, and take the access away again.
On this page
sudo lets a normal user run one command as root, after confirming who they are. Compared with logging in as root, it limits the damage a mistake can do, and it writes a log entry for each use. In this tutorial you will create a user, put it in the sudo group, check that it works, then write a narrower rule for a second user who may run only one command.
Ubuntu 26.04 provides sudo through sudo-rs, a Rust rewrite of the same tool. The commands and the /etc/sudoers format in this tutorial are unchanged. The wording of some messages may differ from older Ubuntu releases.
Step 1 · Create the user
Log in as root, or as a user that already has sudo. Create the account. adduser asks for a password and a few optional details.
sudo adduser deployChoose a long password. sudo asks for it each time the user runs an administrative command.
Check it worked
id deployThe output shows the new user and its own group:
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy),100(users)Step 2 · Add the user to the sudo group
On Ubuntu, /etc/sudoers contains the line %sudo ALL=(ALL:ALL) ALL. It gives every member of the group sudo the right to run any command as any user. Adding the user to that group is all that is needed.
sudo usermod -aG sudo deployAlways include -a. Without it, -G replaces all of the user's other groups.
Check it worked
id deploy
sudo grep -n '^%sudo' /etc/sudoersThe group list now includes 27(sudo), and the second command prints %sudo ALL=(ALL:ALL) ALL with its line number.
You can also read the rules the system will apply to the user:
sudo -l -U deployYou should see something like this:
User deploy may run the following commands on myserver:
(ALL : ALL) ALLThe host name is your server's. (ALL : ALL) ALL means any command, as any user and any group.
Step 3 · Test it as the new user
A group change does not reach sessions that are already open. Log in again as the new user. From your own computer:
ssh deploy@203.0.113.10Or, from the root session on the server, switch to it:
su - deployRun a command that needs root:
sudo whoamisudo asks for the password of deploy, not the root password. The output is root.
Troubleshooting
deploy is not in the sudoers file(orSorry, user deploy is not allowed to execute): the user is not in thesudogroup in the session you are using. Runid. Ifsudois missing, repeat theusermodcommand, then log out and in.sudo: command not found: a minimal image has nosudo. As root, runapt install sudo.- You added the group but
idstill lackssudo: the session predates the change. Log out and in.
Step 4 · Give a second user one command only
Full sudo is more than many accounts need. A rule in /etc/sudoers.d/ can name the exact command. This example lets a user called ops refresh the package index, without a password, and nothing else.
Create the user, and give it a password when asked. It is not added to the sudo group.
sudo adduser opsWrite the rule to a file in /etc/sudoers.d/. The safest way to edit it is sudo visudo -f /etc/sudoers.d/90-ops: visudo checks the syntax before it saves, and a broken sudoers file can lock you out of sudo. The commands below write the same line without an editor.
echo 'ops ALL=(root) NOPASSWD: /usr/bin/apt-get update' | sudo tee /etc/sudoers.d/90-ops
sudo chmod 0440 /etc/sudoers.d/90-ops
sudo visudo -cf /etc/sudoers.d/90-opsThe last command prints /etc/sudoers.d/90-ops: parsed OK. The mode 0440 is required: sudo ignores rule files that other users can write.
Check it worked
Switch to ops, and run the one command it is allowed:
su - ops
sudo -n /usr/bin/apt-get updateThe index refreshes with no password prompt. Now ask for a command the rule does not name:
sudo -n whoamiIt is refused, with a message such as sudo: I'm sorry ops. I'm afraid I can't do that.
The -n flag tells sudo never to ask for a password, so a command that is not allowed fails at once instead of waiting.
Step 5 · Take sudo access away
To remove a user from the sudo group, and leave the account itself in place:
sudo deluser deploy sudoTo delete the rule file for ops:
sudo rm /etc/sudoers.d/90-opsTo delete a user and the home directory with it:
sudo deluser --remove-home opsCheck it worked
id deploy
sudo -n /usr/bin/apt-get updateRun the second command as ops. The group list for deploy has no sudo, and ops can no longer run the command. Existing sessions of deploy keep their old rights until they end.
Next steps
- Add SSH key login for the new user in
/tutorials/log-in-to-a-server-with-ssh-keys. - Stop
rootand password logins over SSH in/tutorials/harden-ssh-on-ubuntu-26-04. - For the full rule syntax, read
man sudoerson the server.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials