Skip to main content

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.

Bash
sudo adduser deploy

Choose a long password. sudo asks for it each time the user runs an administrative command.

Check it worked

Bash
id deploy

The output shows the new user and its own group:

Text
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.

Bash
sudo usermod -aG sudo deploy

Always include -a. Without it, -G replaces all of the user's other groups.

Check it worked

Bash
id deploy
sudo grep -n '^%sudo' /etc/sudoers

The 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:

Bash
sudo -l -U deploy

You should see something like this:

Text
User deploy may run the following commands on myserver:
    (ALL : ALL) ALL

The 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:

Bash
ssh deploy@203.0.113.10

Or, from the root session on the server, switch to it:

Bash
su - deploy

Run a command that needs root:

Bash
sudo whoami

sudo asks for the password of deploy, not the root password. The output is root.

Troubleshooting

  • deploy is not in the sudoers file (or Sorry, user deploy is not allowed to execute): the user is not in the sudo group in the session you are using. Run id. If sudo is missing, repeat the usermod command, then log out and in.
  • sudo: command not found: a minimal image has no sudo. As root, run apt install sudo.
  • You added the group but id still lacks sudo: 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.

Bash
sudo adduser ops

Write 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.

Bash
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-ops

The 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:

Bash
su - ops
sudo -n /usr/bin/apt-get update

The index refreshes with no password prompt. Now ask for a command the rule does not name:

Bash
sudo -n whoami

It 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:

Bash
sudo deluser deploy sudo

To delete the rule file for ops:

Bash
sudo rm /etc/sudoers.d/90-ops

To delete a user and the home directory with it:

Bash
sudo deluser --remove-home ops

Check it worked

Bash
id deploy
sudo -n /usr/bin/apt-get update

Run 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 root and password logins over SSH in /tutorials/harden-ssh-on-ubuntu-26-04.
  • For the full rule syntax, read man sudoers on the server.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.