Showing posts from September, 2010

using tee to echo to a system file with sudo privileges

It is always a good practice not to execute privileged by logging as root. We can avoid
that by executing with sudo privileges. Many times , we need to change kernel parameter
for changing the behaviour of a Linux system . Like recently there was a need for me to
change the CPU governor from 'userspace' to 'ondemand' on a Linux system.

I have to use the following command to complete the task .

$ sudo echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

We have to execute the command as priviliged user , so I was trying to do it using sudo . But it was failing with the following error. But if I do by logging with uid=0 , then it succeeds . But I want to su - to root.

$ sudo echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
-bash: /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor: Permission denied

This was because the above command has two parts and we are using sudo on the first part (sudo echo userspace) , which itself d…