Running Complex Commands with sudo
If you use sudo to run commands as root, you've probably run into “permission denied” problems when only part of a pipeline or part of a command is running with root permissions.
This fails with “permission denied” because the file is writable only by root:
$ echo 12000 > /proc/sys/vm/dirty_writeback_centisecs
But, this fails too:
$ sudo echo 12000 > /proc/sys/vm/dirty_writeback_centisecs
Why? The /bin/echo program is running as root, because of sudo, but the shell that's redirecting echo's output to the root-only file is still running as you. Your current shell does the redirection before sudo starts.
The solution is to run the whole pipeline under sudo. There are a couple ways to do it, but I prefer:
echo "echo 12000 > /proc/sys/vm/dirty_writeback_centisecs" | sudo sh
That way, I can type everything before the pipe character, and see what I'm about to run as root, then press the up arrow and add the | sudo sh to do it for real. This is not a big deal for short, obvious pipelines, but when you're building up a more complicated command as root, it's safer to look at it first before you run it.
- Readers' Choice Awards 2013
- A Plexible Pi
- Swap Your Laptop for an iPad + Linode
- RSS Feeds
- Sublime Text: One Editor to Rule Them All?
- Linux Kernel News - November 2013
- Mars Needs Women
- Advanced Hard Drive Caching Techniques
- Raspberry Pi: the Perfect Home Server
- December 2013 Issue of Linux Journal: Readers' Choice
20 min 31 sec ago
20 min 31 sec ago
- Nice but....
43 min 14 sec ago
- great specs
7 hours 12 min ago
- Reply to comment | Linux Journal
8 hours 18 min ago
- rilakkuma onesie
10 hours 54 min ago
- flying squirrel onesie
10 hours 56 min ago
- animal onesies for adults
10 hours 58 min ago
- animal onesies
10 hours 59 min ago
- stitch onesie
11 hours 32 sec ago