Echo to pipe sudo tee Instead of Switching to root

By | 2016/05/14

Trying to change a value in proc or write a root owned file without switching to root is possible via sudo and tee. This is very useful in automation or scripts. Here’s the scoop!

Changing values in proc

In this example, I am disabling one led on a Raspberry Pi. Attempting as a normal user gives permission denied.

pi@mahler:~ $ echo 0 >/sys/class/leds/led0/brightness
-bash: /sys/class/leds/led0/brightness: Permission denied

At this point, most just sudo -i or -s and run the same command as root. However, there is a better way!

Check this out:

pi@mahler:~ $ echo 0 | sudo tee /sys/class/leds/led0/brightness

Writing files owned by root

For another example see this app:

The sources list file is written via echo ‘blah’ | sudo tee < file >

Debian Jessie

wget -O - | sudo apt-key add -
echo 'deb jessie main' | sudo tee /etc/apt/sources.list.d/rocket-depot.list
sudo apt-get update && sudo apt-get install rocket-depot

Rock on,