Why does echoing these parameters with sudo not work? [duplicate]

Your command doesn't work because the redirection > file is done by the current shell prior to the execution of the command, so before the sudo is in effect.

There is a command, called tee, that writes to a file and to stdout what it receives on its stdin: this is handy to write something to a file without redirection. If the file can be modified only by root, it is sufficient to prepend sudo to tee.

echo 1500 | sudo tee /proc/sys/vm/dirty_writeback_centisecs

Another way to obtain the desired result, mantaining the redirection, is to move the redirection to a subshell executed by root, through sudo:

sudo sh -c 'echo 1500 > /proc/sys/vm/dirty_writeback_centisecs'

Lastly, you can enter a root shell in various ways, and you remain root until you exit explicitly that shell:

sudo su
sudo -s
sudo bash

For more information see the manuale pages of sudo, su and of course of bash.


Instead of creating a cronjob for setting values as described by @Seppo Erviälä, you can make use of the configuration files of procps and sysfsutils.

/proc/sys

Values for /proc/sys can be set in the file /etc/sysctl.conf. To make echo 1500 > /proc/sys/vm/dirty_writeback_centisecs persistent, add a line with:

vm.dirty_writeback_centisecs = 1500

To load the changes in /etc/sysctl.conf to the current session, run:

sudo sysctl -p

/sys

For setting values in /sys the package sysfsutils needs to be installed. The configuration file is located at /etc/sysfs.conf. To make echo 1 > /sys/module/snd_hda_intel/parameters/power_save persistent, add a line with:

module/snd_hda_intel/parameters/power_save = 1

To apply the values in /etc/sysfs.conf to the current session, run:

sudo /etc/init.d/sysfsutils restart

Do a sudo -s or a sudo su first:

enter image description here

From a script:

# more powersave
echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
echo 1 > /sys/module/snd_hda_intel/parameters/power_save
# ./powersave 
# 

Best practice would be to ussue the sudo -s before activating the script (hence the #).


EDIT: See answer by @Lekensteyn for more proper way to edit /proc/sys and /sys defaults.

Running these suggestions from command line will only enable them for your current session and they will reset to defaults after a reboot. If you want to enable these suggestions each time your system starts you should make them into a script:

#!/bin/dash
echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
echo 1 > /sys/module/snd_hda_intel/parameters/power_save

You can place this script somewhere convinient eg. /root/power_save.sh.

Then you want to make sure it runs with root privileges each time your system starts. This can be done with sudo crontab -e which opens list of time based tasks for root. Add line:

@reboot /root/power_save.sh

Don't forget to make your script executable:

sudo chmod u+x /root/power_save.sh

This way these power saving options will be enabled for all users, even before login, and no password is needed to authorize them each time.