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.
Trending Topics
| OpenLDAP Everywhere Reloaded, Part I | May 23, 2012 |
| Chemistry the Gromacs Way | May 21, 2012 |
| Make TV Awesome with Bluecop | May 16, 2012 |
| Hack and / - Password Cracking with GPUs, Part I: the Setup | May 15, 2012 |
| An Introduction to Application Development with Catalyst and Perl | May 14, 2012 |
| Cryptocurrency: Your Total Cost Is 01001010010 | May 09, 2012 |
- OpenLDAP Everywhere Reloaded, Part I
- Strip DRM from WMV File
- Validate an E-Mail Address with PHP, the Right Way
- Boot with GRUB
- Why Python?
- A Statistical Approach to the Spam Problem
- Chapter 16: Ubuntu and Your iPod
- Why Hulu Plus Sucks, and Why You Should Use It Anyway
- Building an Ultra-Low-Power File Server with the Trim-Slice
- Science the GNU Way, Part I
- Editorial Standards?
4 hours 3 min ago - Great one
5 hours 38 min ago - Common form in many
5 hours 59 min ago - Awsome
11 hours 2 min ago - Euro 2012 Coupon Codes - Get 20% Off Pavtube TiVo Converter
3 days 9 hours ago - Euro 2012 Big Sale: 20% Off Instant Savings on TiVo Converter
3 days 9 hours ago - MakeMKV works as well, though
3 days 9 hours ago - Euro 2012 Big Sale: 20% Off Instant Savings on TiVo Converter
3 days 10 hours ago - Awesome
4 days 8 hours ago - Who worries approx the
4 days 10 hours ago





Comments
Why? The /bin/echo program
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:
boediger
A whole article decribing a
A whole article decribing a single command line method? Surely more thought could have gone into this.
A whole article decribing a
A whole article decribing a single command line method? Surely more thought could have gone into this.
tee shirts?
man 1 tee
just as the above post explains, you can use tee. It is easier to visualise what's happening.
Proto's way is better.
Sorry, bad cut and paste. Proto's method shows:
Apr 3 12:00:09 xxxxxxxx sudo: xxxxxxx : TTY=pts/22 ; PWD=/tmp ; USER=root ; COMMAND=/bin/sh -c echo 0 >/proc/sys/net/ipv4/ip_forward
First set of xxxxxxxx is machine name, second is userid.
Proto's way is better
It gives better logging.
Using proto's method, auth.log shows:
Apr 3 12:00:12 xxxxxxxxx sudo: xxxxxxxx : TTY=pts/22 ; PWD=/tmp ; USER=root ; COMMAND=/usr/bin/tail /var/log/auth.log
Don's way just shows:
Apr 3 11:58:02 xxxxxxxx sudo: xxxxxxxx : TTY=pts/22 ; PWD=/tmp ; USER=root ; COMMAND=/bin/sh
sudo: cd: command not found
The one that drives me crazy is when I'm trying to change to a directory with only root access:
I always just give up at that point and change to the root user account with:
but I guess I could stack up the commands as shown above.
I do the same with: sudo sh
I do the same with:
sudo sh -c "echo 12000 > /proc/sys/vm/dirty_writeback_centisecs"
I use the same one: sudo -c
I use the same one: sudo -c ''. It is the more direct approach available, than using echo with sudo.
how about 'sudo -i' first?
how about 'sudo -i' first?
I am totally agree with you,
I am totally agree with you, It's really great.
another way
I like to use tee for this. Using your example, you could use
$ echo 12000 | sudo tee /proc/sys/vm/dirty_writeback_centisecsThis works for the same reason, it runs tee with sudo. You can also use
tee -ato append to file instead of overwriting it.