Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When using sudo to allow edits to files, I regularly get 'permission denied'.

For example, my mouse is jittery and sluggish, so I want to disable polling:

sudo echo "options drm_kms_helper poll=N">/etc/modprobe.d/local.conf

I'm prompted for a password, and then get:

bash: /etc/modprobe.d/local.conf: Permission denied

So I tried to do a temporary change to disable polling by using:

sudo echo N> /sys/module/drm_kms_helper/parameters/poll

Yet again the system responded with:

bash: /sys/module/drm_kms_helper/parameters/poll: Permission denied

Any ideas?

share|improve this question

5 Answers 5

up vote 33 down vote accepted

Output redirection (via the > operator) is done by the shell, not by echo. You have to login as root

sudo -i

Then you can use redirection

echo N> /sys/module/drm_kms_helper/parameters/poll

Otherwise you can run bash string with sudo

sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll"
share|improve this answer
1  
You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi –  saji89 Dec 19 '12 at 4:41
    
you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem. –  shantanu Dec 19 '12 at 4:55
4  
Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only. –  saji89 Dec 19 '12 at 4:59
    
You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands. –  kojiro Dec 19 '12 at 13:48

The output redirection is done by the shell from which the command has been invoked. So, breaking everything into bits, here what is happening:

  • shell invokes sudo echo "options drm_kms_helper poll=N", which executes sudo command with echo "options drm_kms_helper poll=N" command line

  • sudo asks for a password, opens superuser shell and invokes echo "options drm_kms_helper poll=N", which runs echo command passing it "options drm_kms_helper poll=N"

  • echo, running with root privileges, prints the string to its standard output.

  • echo command terminates, superuser shell exits, sudo terminates

  • the shell from which the command has been invoked collects the output and tries to redirect it to /etc/modprobe.d/local.conf, which is writeable only by root. It gets "permission denied" error.

For the ways to fix this see @shantanu answer.

share|improve this answer
7  
+1 Great explanation! Thanks Sergey :-) –  JacobusR Dec 19 '12 at 5:26

Adding to Shantanu's answer:

... Or you could use a tee command like this:

sudo tee /sys/module/drm_kms_helper/parameters/poll <<<10

or if its a command's output:

echo 10 | sudo tee /sys/module/drm_kms_helper/parameters/poll
share|improve this answer
    
+1 Thanks for the alternative. –  JacobusR Dec 19 '12 at 9:25
2  
+1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks. –  l0b0 Dec 19 '12 at 16:41

An approach I haven't seen mentioned here is to simply execute the entire commandline in its own shell. The sudo manpage itself gives an example of this approach:

To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work.

$ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"
share|improve this answer

Another option is to use a temporary file. This is useful in a bash script.

temp=$(mktemp)
echo "Hello, world!" > $temp
sudo cp $temp /etc/wherever
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.