Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

this is the first occurrence where su was required for me.

I read an article about changing the value in /sys/devices/virtual/backlight/acpi_video0/brightness to alter my laptop's screen brightness.

I first noticed that when I would $ sudo echo 10 > brightness I would get permission denied.

I switched to root using su and # echo 10 > brightness changed my brightness almost instantly.

The last weird thing to me happened when I tried # echo 20 > brightness (maxbrightness file holds the value 15) and I got a write error

Could someone explain this difference between sudo and su to me? Understanding the write error would be an added bonus. Any help, pointers, and/or links would be much appreciated.

share|improve this question
1  
Related unix.stackexchange.com/q/1416/250 –  phunehehe Jan 31 '11 at 17:48

4 Answers 4

up vote 14 down vote accepted

Redirection does not work that way. Appending > to a command will run that redirection as the invoking user (you) and not as root. Do it with tee:

echo 20 | sudo tee /sys/devices/virtual/backlight/acpi_video0/brightness

or by invoking the command in a separate privileged shell:

sudo bash -c "echo 20 > /sys/devices/virtual/backlight/acpi_video0/brightness"
share|improve this answer
    
Like the tee tip. It had not occurred to me during my 15 years of unix sysadmining :-) Obvious when seen. –  Mr Shark Feb 11 '11 at 12:15

This isn't because of sudo, it's because of the way your command is processed; I actually explained it in this question. When you do

$ sudo echo 10 > brightness

the shell runs the command sudo echo 10, which runs echo 10 as root. The shell then tries to open brightness so it can redirect the output from echo 10 into it, but it can't -- your shell is running with your permissions, not root. There are workarounds posted as answers on the question I linked to; a good one is:

$ echo 10 | sudo tee brightness

Now tee is the one to open brightness, and since it's running as root it succeeds

share|improve this answer

You might want to take a look at /etc/sudoers file. If your a/c is not listed there then you will not be able to sudo.

share|improve this answer
    
True, but you'd get a specific error message in that case ("username is not in the sudoers file") –  Michael Mrozek Feb 1 '11 at 5:54

su = s witch u ser: Use the shell as a different user

sudo = s uper u ser do: execute following command as the root user.

In your case the following command is only echo 20, hence only echo 20 gets executed as root (as said before). The redirect is executed as you again.

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.