Running Complex Commands with sudo

 in

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.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Why? The /bin/echo program

boediger's picture

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

Anonymous's picture

A whole article decribing a single command line method? Surely more thought could have gone into this.

A whole article decribing a

K31th's picture

A whole article decribing a single command line method? Surely more thought could have gone into this.

tee shirts?

felipe's picture

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.

jpenny's picture

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

jpenny's picture

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

Anonymous's picture

The one that drives me crazy is when I'm trying to change to a directory with only root access:

john@p490:~$ cd /root
john@p490:/root$ cd .gconf
bash: cd: .gconf: Permission denied
john@p490:/root$ sudo cd .gconf 
sudo: cd: command not found

I always just give up at that point and change to the root user account with:

$ sudo su -

but I guess I could stack up the commands as shown above.

I do the same with: sudo sh

Proto's picture

I do the same with:

sudo sh -c "echo 12000 > /proc/sys/vm/dirty_writeback_centisecs"

I use the same one: sudo -c

Barun's picture

I use the same one: sudo -c ''. It is the more direct approach available, than using echo with sudo.

how about 'sudo -i' first?

Gerald Marewo's picture

how about 'sudo -i' first?

I am totally agree with you,

day spa gold coast's picture

I am totally agree with you, It's really great.

another way

Joe A's picture

I like to use tee for this. Using your example, you could use
$ echo 12000 | sudo tee /proc/sys/vm/dirty_writeback_centisecs

This works for the same reason, it runs tee with sudo. You can also use
tee -a
to append to file instead of overwriting it.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix