Add Sudo User Ubuntu: Guide for 24.04 and Debian 12

· 0x.cloud Team

Add Sudo User Ubuntu: Guide for 24.04 and Debian 12

To add sudo user Ubuntu style, run two commands as root: adduser deploy creates the account and usermod -aG sudo deploy adds it to the sudo group. The new user can then run admin commands with sudo and their own password. Copy your SSH key over, test the login, and you are done in about five minutes.

Working as root every day is risky: one typo or a buggy script and the system is broken. It matters even more when an AI agent (Claude Code, Codex, OpenClaw) runs on the server. Run the agent as a regular user, never as root. Every command below is checked against the Ubuntu 24.04 LTS and Debian 12 documentation.

Before You Start

  • A server running Ubuntu 24.04 or Debian 12 and a root shell: over SSH, or on 0x.cloud in the web console in your dashboard. If you have not changed the root password yet, start with change the root password.
  • A username. The examples use deploy; pick your own: lowercase letters, no spaces.

Add Sudo User Ubuntu: Step by Step

Step 1. Create the user with adduser

# creates the user and /home/deploy, then asks for a password
adduser deploy

You will type the password twice and get optional prompts for full name, phone and so on. Press Enter to skip them. Choose a strong password: sudo asks for it.

Minimal Debian 12 images sometimes ship without the sudo package. Check and install it:

apt update && apt install -y sudo

Step 2. Add the user to the sudo group

# -a appends, -G names the group; without -a the user loses other groups
usermod -aG sudo deploy

On Ubuntu and Debian, every member of the sudo group can run any command as root. That rule already lives in /etc/sudoers, so you do not need to edit it. All options are documented in man adduser.

Confirm the membership:

groups deploy
# deploy : deploy sudo

Step 3. Copy your SSH key

If you log in with an SSH key (you should), give the new user the same key:

# copies root's ~/.ssh, including authorized_keys, and fixes ownership
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy

No key yet? Create one with our guide on how to add an SSH key.

Step 4. Test login and sudo

Keep your root session open. Over SSH, log in as the new user from a new terminal window on your computer (ssh deploy@SERVER_IP); in the 0x.cloud web console, switch to it with su - deploy. Then check sudo:

sudo whoami
# root

If it prints root, you are set. Next, turn off root and password logins as shown in disable SSH password login, and enable the firewall with UFW on Ubuntu.

Passwordless Sudo (add user to sudoers)

Typing a password for every sudo gets in the way of deploy scripts and unattended jobs. Never edit /etc/sudoers directly. Use a drop-in file and visudo, which checks the syntax before saving:

sudo visudo -f /etc/sudoers.d/deploy

Add one line:

deploy ALL=(ALL) NOPASSWD:ALL

That gives the user full root with no password. Convenient, and risky if an AI agent runs under that account.

A User for an AI Agent: Sudo for Specific Commands Only

An agent usually needs root for one or two actions, such as restarting its own service. Allow exactly that:

sudo adduser --disabled-password --comment "" agent
sudo visudo -f /etc/sudoers.d/agent
# the agent may only restart its own service, no password
agent ALL=(root) NOPASSWD: /usr/bin/systemctl restart mybot.service

The agent does not need sudo to read logs. Add it to the journal group with sudo usermod -aG systemd-journal agent and journalctl -u mybot.service works without root. Never allow commands through sudo that open a pager or editor (journalctl, systemctl status, less, vim): they let a user escape to a root shell.

The agent user has no password (key login only) and is not in the sudo group, so any other sudo command is refused. Cloud permissions (creating and deleting servers) are a separate layer: API key scopes and a spend cap, covered in how to limit an AI agent.

Prompt for an AI agent

Connect to my server as root over SSH or through the 0x.cloud MCP. Create a user named deploy with key-only login, add it to the sudo group, copy my key from /root/.ssh, test the login as deploy, and only then report back. Do not disable root access until I confirm.

Common Errors and Fixes

Error Cause Fix
deploy is not in the sudoers file user not in sudo group or old session usermod -aG sudo deploy, then log in again
sudo: command not found Debian 12 without the sudo package as root: apt install -y sudo
Permission denied (publickey) key not copied or wrong owner rerun rsync, check ls -la /home/deploy/.ssh
syntax error in sudoers file edited without visudo as root: visudo -f /etc/sudoers.d/name and fix the line
user lost other groups usermod -G without -a add them back: usermod -aG group deploy

For a server that runs an AI agent under its own user, s2-4 (2 vCPU, 4 GB RAM, $15.99 per month) fits, since Claude Code needs 4 GB+ RAM. A bot deployed as the deploy user runs fine on s1-2 at $7.99. Practice user setup on an hourly server (s1-2 is about $0.054725 an hour), and if you break something, an OS reinstall costs $0. See all plans on VPS pricing.

FAQ

What is the difference between adduser and useradd? adduser is the friendly Debian and Ubuntu wrapper: it creates the home directory and prompts for a password. useradd is the low-level tool; without -m it skips the home directory, which makes it better suited to scripts.

How do I check that a user has sudo? Run groups username and look for sudo in the output. Or log in as that user and run sudo whoami: if it prints root, sudo works.

Why doesn't sudo work right after usermod? Group membership is applied at login. Log out and back in, or run su - username to start a fresh session.

How do I give a user passwordless sudo? Run sudo visudo -f /etc/sudoers.d/username and add the line username ALL=(ALL) NOPASSWD:ALL. For an AI agent, allow only the specific commands it needs instead.