kmail.at
contact

elsewhere

EnglishEN
← learning

sysadmin · difficulty ◆◆

sudo — run one command as another user

Root for one command — and the policy behind the yes or no.

`sudo echo hello > /etc/motd` still says Permission denied, and the reason has nothing to do with sudo — it is the shell, and it catches every sysadmin once.

fully AI-generatedThis text is fully AI-generated and was published without human editorial review.

2026-10-26 · 5 min read

$ sudo
1980
First release

SUNY Buffalo, a decade before Linux

15 min
Credential cache

default timestamp_timeout, per terminal

1.9.15p5
This box

Ubuntu 24.04 package

It is a policy lookup, not a root switch

`sudo` authenticates you — your password, not root's — then looks in `/etc/sudoers` and every file under `/etc/sudoers.d/` for a rule matching three things: this user, this host, this command. Only then does it fork a child as the target user and exec the binary. Nothing gets raised in place; your shell stays exactly as privileged as it was.

The canonical Ubuntu line is `%sudo ALL=(ALL:ALL) ALL` — anyone in group `sudo` may run anything as anyone on any host, password required. That broad rule is why `sudo -l` usually prints a single `ALL` and why people assume sudo means root. On a tighter box the same output lists command paths one per line, some tagged `NOPASSWD:`, and those are the ones automation actually depends on. `sudo -l` is the resolved policy, after includes and group expansion — the only answer worth trusting. The build in front of you is 1.9.15p5 (`sudo -V` prints it).

The redirection happens before sudo runs

`sudo echo something > /etc/environment` fails, and the error comes from bash, not from sudo: the shell opens `/etc/environment` for writing with your uid while it sets up the pipeline, before it ever execs `sudo`. Root is granted to `echo`, which writes to a file that is already locked shut. This is the single most reported 'sudo is broken' moment on Linux.

Two fixes, and you should know both. `| sudo tee -a file` makes `tee` the command sudo runs, so the write is root's — and `tee` echoes the line to stdout, which doubles as confirmation that it landed; that is why you see the line printed again. For several redirected commands at once, `sudo sh -c 'cmd1 > a && cmd2 >> b'` puts the shell itself under root so the opens happen there too. Related trap: `env_reset` (on by default) wipes most of your environment, so a `PATH` or `LD_LIBRARY_PATH` you exported is gone inside the command unless a `env_keep` rule names it — `-E` only helps when the policy grants `SETENV`.

No terminal means no password prompt

Sudo needs a TTY to ask for a password. In cron, in a CI runner, in a systemd unit with no `tty` path, there is nothing to prompt on and it fails immediately with `sudo: a password is required` and exit code 1. That is the good failure. The bad one is a script run under `ssh -t` or an interactive wrapper, where sudo asks a question nobody is watching and the job sits there until your deploy window closes.

So put `-n` in every unattended call: it forbids the prompt and turns the ambiguity into an instant error you can branch on. `sudo -n true || exit 1` at the top of a playbook is a one-second preflight that tells you whether the NOPASSWD rules actually landed. If you are doing a burst of privileged commands by hand, `sudo -v` refreshes the cached credential up front so only the first asks; `sudo -k` throws the cache away when you are done.

Example

$ sudo -l
Matching Defaults entries for kmail on web01:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
    use_pty

User kmail may run the following commands on web01:
    (ALL : ALL) ALL

The `(ALL : ALL) ALL` line means any user, any group, any command, password required. Anything reading NOPASSWD is a lane your automation can use without a terminal — audit those first when you inherit a box.

$ echo 'export PATH=/opt/app/bin:$PATH' >> /etc/environment
-bash: /etc/environment: Permission denied

The classic. Nothing was written; bash refused to open the file for appending because the open happened as kmail. Add sudo to the front and you get the same error, which is what sends people to Stack Overflow.

$ echo 'export PATH=/opt/app/bin:$PATH' | sudo tee -a /etc/environment
export PATH=/opt/app/bin:$PATH

Now the write is root's, because tee is the process sudo execs. The echoed line is tee's normal stdout — proof the append happened. Drop -a and you truncate the file instead, so this flag is the one to double-check.

$ sudo -u postgres psql -Atc 'select count(*) from pg_stat_activity;'
4

`-u` runs the command as that account without giving you a shell or a new session. peer authentication on the socket then sees postgres, which is why this works when a plain psql asks for a password you do not have.

$ apt install nginx
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

Real output from Ubuntu 24.04 as a non-root user. Follow it with `sudo !!` and bash re-runs the whole line as root — the fastest fix when you forget the prefix, and safer than retyping a long apt command from memory.

$ sudo -n true || echo "no passwordless sudo here"
sudo: a password is required
no passwordless sudo here

Verified on this host, exit 1. This exact two-second check belongs at the top of any script that expects NOPASSWD rights: it fails loudly instead of hanging on a prompt in CI.

Common flags

-l, --list
Print the resolved policy for you: Defaults lines plus the commands you may run, with NOPASSWD and run-as user spelled out. `-U other` to inspect someone else, if allowed.
-u USER
Run as that user instead of root. The right way to poke a service account — postgres, www-data, deploy — without `su -` and without a login shell.
-i, --login
Interactive login shell as the target user: their HOME, their PATH, their profile files sourced. Feels like a real root login, which is also why it should be a last resort.
-s, --shell
Shell as the target user but keeps your environment and working directory. Handy when you want root's rights with your current `cd`.
-n, --non-interactive
Never prompt. Fails with `sudo: a password is required` and exit 1. Mandatory in cron, CI and anything without a TTY.
-k, --reset-timestamp
Invalidate your cached credential now (`-K` clears them for every target user). Use it before handing a laptop over or after a policy change.
-e, --edit / sudoedit
Edit a file as root through your own editor on a temp copy, then write it back with privilege. Keeps your vimrc and avoids a root-owned shell.

History

1980, a shared VAX, and a name that stuck

Sudo started around 1980 at the State University of New York at Buffalo, where Bob Coggeshall wrote a tool so operators could run a few privileged commands without handing out the root password. Cliff Spencer maintained it next, and since 1994 Todd C. Miller has been the maintainer — which is why every sudoers file opens with a comment naming him and the project still lives at sudo.ws. It predates Linux by a decade, and it began as a way to stop sharing one password, not as a security product.

Baron Samedit, and why `sudo -V` matters

In January 2021 Qualys disclosed CVE-2021-3156: a heap-based buffer overflow in sudo's argument parsing, reachable through `sudoedit -s` by any local user — including users with no sudo rule at all. It had been in the code for roughly ten years and was fixed in 1.9.5p2. The nickname, Baron Samedit, came from the researcher's write-up. That bug is the reason `sudo --version` is worth knowing by heart: on a box you inherited, the version number is a security fact, not trivia.

Fun facts

Pros & cons

pros

  • + Per-command policy with an audit trail: auth.log or journald records who ran what, so it is never just 'root did it'.
  • + Authenticates you with your own password — no shared root credential to rotate, and removing access is one group change.
  • + NOPASSWD plus exact command paths gives automation a narrow, reviewable lane instead of a root shell.

cons

  • − Password prompts are the default, so cron jobs and CI runners hang or fail unless you add -n or a NOPASSWD rule.
  • − Redirection and env_reset break the 'just put sudo in front' mental model in ways that read as bugs.
  • − One bad line in sudoers can lock every admin out of escalation, and the fix needs out-of-band access.

Takeaways

  1. 1Run `sudo -l` first: it prints the commands you may run and which of them skip the password.
  2. 2Any redirection needs the tee form — `sudo cmd > file` fails because your shell opens the file, not root.
  3. 3Use `sudo -n` in scripts and cron so a missing NOPASSWD rule fails in a second instead of hanging.
  4. 4`sudo !!` re-runs the last command as root; `sudo -k` drops the cached credential when you are finished.
  5. 5Prefer `sudo -u user` over `su - user`: the action stays logged under your name and leaves no root-owned shell behind.

Related commands

archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →

← all learning