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
| Flag | Meaning |
|---|---|
| -u | Run as another user instead of root |
| -i | Start a login shell with that user environment |
| -s | Start a shell but keep your own environment |
| -E | Keep your environment variables, if the policy allows it |
| -k | Forget the cached password right now |
| -l | List what you are allowed to run |
| -v | Refresh the timestamp without running anything |
Examples
sudo -u postgres psqlRuns the client as the postgres user.
sudo -iA root login shell, with root PATH and environment.
echo text | sudo tee -a /etc/hostsRedirection 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