5 Sudo Command Tricks in Linux: Safe and Practical
The sudo command runs a single command as another user, usually root, if the rules in /etc/sudoers allow it. These five sudo command tricks save typing and close a few gaps that bite people later. Everything was run on Debian 13 with sudo 1.9.16p2, with a user admin in the sudo group and a plain user dev. All accounts and passwords are throwaway lab values.
Contents
- Before the tricks: which sudo command may you run?
- 1. Write to root files with tee, not a redirect
- 2. Repeat the last line with sudo !!
- 3. Edit protected files with sudoedit
- 4. Grant one sudo command with a checked drop-in
- A file name with a dot is silently ignored
- Wildcards are wider than they look
- 5. Control the user, environment and cached password
- Sudo command habits worth keeping
Before the tricks: which sudo command may you run?
sudo -l lists every sudo command your account may run. A user without any rule gets a clear refusal, and -n makes sudo fail instead of waiting for a password, which is what you want in scripts:
admin$ sudo -n id
sudo: a password is required
[exit 1]
dev$ echo Lab-pass-2 | sudo -S id
[sudo] password for dev: dev is not in the sudoers file.
[exit 1]
admin$ echo Lab-pass-1 | sudo -S -l
[sudo] password for admin: Matching Defaults entries for admin on lab:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin,
use_pty
User admin may run the following commands on lab:
(ALL : ALL) ALL
[exit 0]
1. Write to root files with tee, not a redirect
The shell opens the redirect target before sudo starts, and the shell still runs as you. That is why the first line fails although it begins with sudo:
admin$ sudo echo "x=1" > /etc/demo-redirect.conf
/tmp/s.sh: line 2: /etc/demo-redirect.conf: Permission denied
[exit 1]
admin$ echo "x=1" | sudo tee /etc/demo-redirect.conf
x=1
[exit 0]
In this form tee is the program that opens the file, so it needs the privilege. Use -a to append, and send its copy to /dev/null if you do not want to see it. A quoted sh -c string works too, because the redirect then happens inside the root shell:
admin$ echo "y=2" | sudo tee -a /etc/demo-redirect.conf >/dev/null
[exit 0]
admin$ cat /etc/demo-redirect.conf; ls -l /etc/demo-redirect.conf
x=1
y=2
-rw-r--r--. 1 root root 8 Nov 28 12:31 /etc/demo-redirect.conf
[exit 0]
admin$ sudo sh -c 'echo z=3 >> /etc/demo-redirect.conf' && wc -l /etc/demo-redirect.conf
3 /etc/demo-redirect.conf
[exit 0]
Shell built-ins are a related surprise for the sudo command. There is no cd program for sudo to start:
admin$ sudo cd /root
sudo: cd: command not found
sudo: "cd" is a shell built-in command, it cannot be run directly.
sudo: the -s option may be used to run a privileged shell.
sudo: the -D option may be used to run a command in a specific directory.
[exit 1]
2. Repeat the last line with sudo !!
In an interactive Bash session, !! expands to the previous command. Bash prints the expanded line before running it, so you can check it:
admin$ head -1 /etc/shadow
head: cannot open '/etc/shadow' for reading: Permission denied
admin$ sudo !!
sudo head -1 /etc/shadow
[sudo] password for admin:
root:<hash>:20761:0:99999:7:::
admin$ history | tail -3
2 head -1 /etc/shadow
3 sudo head -1 /etc/shadow
4 history | tail -3
Read the expanded sudo command before you press Enter on anything destructive. History expansion works in interactive shells only, and the root hash above is masked. If you prefer to see the line first, press the Up arrow, then Ctrl+A and type sudo.
3. Edit protected files with sudoedit
sudo vim /etc/file runs the whole editor as root, including plugins and any shell you open from it. sudoedit copies the file to a temporary one, runs your editor as you, and copies the result back. The test used a tiny script as the editor so the identity is visible:
$ ls -l /etc/demo.conf; cat /usr/local/bin/lab-editor
-rw-r--r--. 1 root root 8 Nov 28 12:31 /etc/demo.conf
#!/bin/sh
{ echo "editor runs as: $(id -un)"; echo "editor got: $*" ; } >&2
sed -i 's/Port 80/Port 8080/' "$1"
[exit 0]
admin$ SUDO_EDITOR=/usr/local/bin/lab-editor sudoedit /etc/demo.conf
editor runs as: admin
editor got: /var/tmp/demol7ODwkU1.conf
[exit 0]
admin$ cat /etc/demo.conf; ls -l /etc/demo.conf
Port 8080
-rw-r--r--. 1 root root 10 Nov 28 12:31 /etc/demo.conf
[exit 0]
The editor ran as admin on a file in /var/tmp, and the target stayed owned by root. Set SUDO_EDITOR, VISUAL or EDITOR to choose the editor. If nothing changes, sudoedit says so. It also refuses to edit a file in a directory you can write to:
admin$ SUDO_EDITOR=true sudoedit /etc/demo.conf
sudoedit: /etc/demo.conf unchanged
[exit 0]
admin$ touch ~/mine.txt; SUDO_EDITOR=true sudoedit ~/mine.txt
sudoedit: /home/admin/mine.txt: editing files in a writable directory is not permitted
[exit 1]
4. Grant one sudo command with a checked drop-in
To allow one sudo command, do not edit /etc/sudoers with a normal editor. Write a small file, validate it with visudo -cf, then install it with mode 440 in /etc/sudoers.d. A missing colon is caught before it can lock you out:
$ echo "dev ALL=(root) NOPASSWD /usr/local/sbin/restart-demo" > /tmp/dev-rule; visudo -cf /tmp/dev-rule
/tmp/dev-rule:1:53: syntax error
dev ALL=(root) NOPASSWD /usr/local/sbin/restart-demo
^
[exit 1]
$ echo "dev ALL=(root) NOPASSWD: /usr/local/sbin/restart-demo" > /tmp/dev-rule; visudo -cf /tmp/dev-rule
/tmp/dev-rule: parsed OK
[exit 0]
$ install -m 440 /tmp/dev-rule /etc/sudoers.d/dev-restart && visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/README: parsed OK
/etc/sudoers.d/dev-restart: parsed OK
[exit 0]
dev$ sudo -n -l
Matching Defaults entries for dev on lab:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin,
use_pty
User dev may run the following commands on lab:
(root) NOPASSWD: /usr/local/sbin/restart-demo
[exit 0]
dev$ sudo -n /usr/local/sbin/restart-demo
restarted by root for dev
[exit 0]
dev$ sudo -n /usr/bin/id
sudo: a password is required
[exit 1]
A file name with a dot is silently ignored
Files in the include directory whose names contain a dot or end in ~ are skipped. visudo -c did not even list dev.id, and the rule had no effect until the file was renamed:
$ echo "dev ALL=(root) NOPASSWD: /usr/bin/id" > /etc/sudoers.d/dev.id; chmod 440 /etc/sudoers.d/dev.id; visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/README: parsed OK
/etc/sudoers.d/dev-restart: parsed OK
[exit 0]
dev$ sudo -n /usr/bin/id
sudo: a password is required
[exit 1]
$ mv /etc/sudoers.d/dev.id /etc/sudoers.d/dev-id; visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/README: parsed OK
/etc/sudoers.d/dev-id: parsed OK
/etc/sudoers.d/dev-restart: parsed OK
[exit 0]
dev$ sudo -n /usr/bin/id
uid=0(root) gid=0(root) groups=0(root)
[exit 0]
Wildcards are wider than they look
This sudo command rule was meant to let dev read logs. The wildcard matches .. and extra arguments, so it also hands out /etc/shadow:
$ echo "dev ALL=(root) NOPASSWD: /usr/bin/cat /var/log/*" > /etc/sudoers.d/dev-cat; chmod 440 /etc/sudoers.d/dev-cat; visudo -c | tail -1
/etc/sudoers.d/dev-restart: parsed OK
[exit 0]
dev$ sudo -n /usr/bin/cat /var/log/../../etc/shadow | head -1 | cut -d: -f1
root
[exit 0]
dev$ sudo -n /usr/bin/cat /var/log/dpkg.log /etc/shadow | grep -c "^root:"
1
[exit 0]
List exact commands and arguments, or write a root-owned script that validates its input and allow only that script. The sudoers manual has a section on wildcards with the same warning.
5. Control the user, environment and cached password
-u picks another user. -i starts a login shell, which changes the directory to the target user's home; a plain sudo command keeps your current directory:
admin$ sudo -n id -un
root
[exit 0]
admin$ sudo -u dev id -un
dev
[exit 0]
admin$ sudo sh -c 'echo $HOME; pwd'
/root
/home/admin
[exit 0]
admin$ sudo -i sh -c 'echo $HOME; pwd'
/root
/root
[exit 0]
Variables you export do not reach the sudo command, because env_reset is on. Pass one variable on the command line or keep it by name. Both depend on the policy; a restricted rule can refuse them:
admin$ sudo sh -c 'echo FOO=${FOO-unset}'
FOO=unset
[exit 0]
admin$ sudo FOO=bar sh -c 'echo FOO=${FOO-unset}'
FOO=bar
[exit 0]
admin$ sudo --preserve-env=FOO sh -c 'echo FOO=${FOO-unset}'
FOO=bar
[exit 0]
After a successful sudo command, the password is remembered for a while. sudo -k forgets it, which is worth doing before you walk away from a shared terminal:
admin$ sudo -k
[exit 0]
admin$ sudo -n true
sudo: a password is required
[exit 1]
Sudo command habits worth keeping
- Run
sudo -lbefore assuming which sudo command an account can use. - Use
sudoeditfor configuration files. - Check every rule with
visudoand keep one root session open while you change sudoers. - Treat editors, pagers and interpreters in a rule as full root access.
- Review
/etc/sudoers.dfor rules nobody remembers.
If you need the sudo command on a remote host from a script, quoting and password handling add their own problems; see running sudo commands remotely over SSH.
Comments
Post a Comment