12

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.

Anthon
  • 79,293
flumpb
  • 551
  • 2
  • 5
  • 14

4 Answers4

18

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"
wag
  • 35,944
  • 12
  • 67
  • 51
  • 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
9

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

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
3

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.

2

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.

yasouser
  • 416