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
.