Home·Terminal commands

sudo

Permissions

Usage

sudo [cmd]

Runs one command as another user, root by default; the redirect in sudo cmd > file is opened by your own shell, so it fails on a root-owned file and tee is the way.

Common flags

FlagMeaning
-uRun as another user instead of root
-iStart a login shell with that user environment
-sStart a shell but keep your own environment
-EKeep your environment variables, if the policy allows it
-kForget the cached password right now
-lList what you are allowed to run
-vRefresh the timestamp without running anything

Examples

sudo -u postgres psql

Runs the client as the postgres user.

sudo -i

A root login shell, with root PATH and environment.

echo text | sudo tee -a /etc/hosts

Redirection needs tee, because > runs as you.

Numeric and symbolic modes name the same thing: 755 and u=rwx,go=rx are the same permission.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does sudo do?

Runs one command as another user, root by default; the redirect in sudo cmd > file is opened by your own shell, so it fails on a root-owned file and tee is the way.

Q. How do I type it?

sudo [cmd] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

7 are listed here; the full set is in man sudo. This command sits under Permissions.

Related commands

man page: man sudo