Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clearing RAM Caches

481 views
Skip to first unread message

Timothy M Butterworth

unread,
Aug 15, 2022, 3:00:05 AM8/15/22
to
When I run `sudo echo 1 > /proc/sys/vm/drop_caches` I receive the following error: bash: /proc/sys/vm/drop_caches: Permission denied

I have `%sudo   ALL=(ALL:ALL) ALL` in my /etc/sudoers file. All commands should be allowed for group sudo.

ls -la /proc/sys/vm/drop_caches
--w------- 1 root root 0 Aug 14 16:28 /proc/sys/vm/drop_caches

I tried adding group permissions with `sudo chmod 440 /proc/sys/vm/drop_caches` and receive the following error message chmod: changing permissions of '/proc/sys/vm/drop_caches': Operation not permitted

If I run `sudo sh -c "/usr/bin/echo 1 > /proc/sys/vm/drop_caches"` it works.  Why does  `sudo echo 1 > /proc/sys/vm/drop_caches` not work, so frustrating.

Tim

--
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org/
⠈⠳⣄⠀⠀

Anssi Saari

unread,
Aug 15, 2022, 4:20:05 AM8/15/22
to
Timothy M Butterworth <timothy.m....@gmail.com> writes:

> When I run `sudo echo 1 > /proc/sys/vm/drop_caches` I receive the following error: bash: /proc/sys/vm/drop_caches: Permission denied

Unfortunately, it's your current shell and not root who does the
redirection in this case so no permissions.

For a longer explanation see
https://www.reddit.com/r/linux4noobs/comments/qq76qo/sudo_echo_vs_echo_from_the_root_shell/

A proposed alternative from there is to use tee, like this:

echo 1 | sudo tee /proc/sys/vm/drop_caches

Tixy

unread,
Aug 15, 2022, 4:20:05 AM8/15/22
to
On Mon, 2022-08-15 at 02:50 -0400, Timothy M Butterworth wrote:
> When I run `sudo echo 1 > /proc/sys/vm/drop_caches` I receive the following
> error: bash: /proc/sys/vm/drop_caches: Permission denied

Because the output redirection occurs as your normal user, all you are
doing is executing the 'echo 1' command as the superuser.

--
Tixy

to...@tuxteam.de

unread,
Aug 15, 2022, 5:20:06 AM8/15/22
to
To add another one, I like dd for that one (it doesn't clutter
stdout unnecessarily).

echo 1 | sudo dd of=/proc/sys/and-so-on

Embarrasment of riches!

Basically, what one is looking for is a cat one can pass an
output file name as a command line argument -- or put another
way, a cat which opens its output itself.

Cheers
--
t
signature.asc

Nicolas George

unread,
Aug 15, 2022, 5:40:05 AM8/15/22
to
to...@tuxteam.de (12022-08-15):
> echo 1 | sudo dd of=/proc/sys/and-so-on

sudo sh -c "echo 1 > /proc/sys/and-so-on"

Or, in this particular case:

sudo systcl -w and-so-on=1

Regards,

--
Nicolas George
signature.asc

Timothy M Butterworth

unread,
Aug 15, 2022, 9:10:06 PM8/15/22
to


---------- Forwarded message ---------
From: Timothy M Butterworth <timothy.m....@gmail.com>
Date: Mon, Aug 15, 2022 at 9:04 PM
Subject: Re: Clearing RAM Caches
To: Tixy <ti...@yxit.co.uk>




Thanks for the clarification. `echo 1 > sudo /proc/sys/vm/drop_caches` seems to work just fine. 
 
--
Tixy



--
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org/
⠈⠳⣄⠀⠀

to...@tuxteam.de

unread,
Aug 16, 2022, 12:40:05 AM8/16/22
to
On Mon, Aug 15, 2022 at 09:05:59PM -0400, Timothy M Butterworth wrote:
> ---------- Forwarded message ---------
> From: Timothy M Butterworth <timothy.m....@gmail.com>
> Date: Mon, Aug 15, 2022 at 9:04 PM
> Subject: Re: Clearing RAM Caches
> To: Tixy <ti...@yxit.co.uk>
>
>
>
>
> On Mon, Aug 15, 2022 at 4:12 AM Tixy <ti...@yxit.co.uk> wrote:
>
> > On Mon, 2022-08-15 at 02:50 -0400, Timothy M Butterworth wrote:
> > > When I run `sudo echo 1 > /proc/sys/vm/drop_caches` I receive the
> > following
> > > error: bash: /proc/sys/vm/drop_caches: Permission denied
> >
> > Because the output redirection occurs as your normal user, all you are
> > doing is executing the 'echo 1' command as the superuser.
> >
> >
> Thanks for the clarification. `echo 1 > sudo /proc/sys/vm/drop_caches`
> seems to work just fine.

This is... surprising. If I were you, I'd look around in the current
directory whether there's a file named `sudo' with an "1" in it.

Cheers
--
t
signature.asc

Tixy

unread,
Aug 16, 2022, 3:30:05 AM8/16/22
to
On Mon, 2022-08-15 at 21:05 -0400, Timothy M Butterworth wrote:
> >
> Thanks for the clarification. `echo 1 > sudo /proc/sys/vm/drop_caches`
> seems to work just fine.

It doesn't, as Tomas pointed out it creates a file called 'sudo' and
puts a '1' in it.

Output redirection done with a single '>' is implemented by the shell
first creating the specified file and then when it forks a process for
the command ('echo' in this case) it makes file descriptor number 1 in
that process (the descriptor used by convention and Standard Output)
refer to the file opened.

The reply you got from Anssi Saari gave an example of a command that
will do what you want.

--
Tixy

Timothy M Butterworth

unread,
Aug 16, 2022, 5:20:05 AM8/16/22
to
You are correct I did have a file named sudo with "1 /proc/sys/vm/drop_caches" in it.

It looks like `sudo sh -c "/usr/bin/echo 1 > /proc/sys/vm/drop_caches"` is the winner.
 
Cheers
--
t

Greg Wooledge

unread,
Aug 16, 2022, 7:30:05 AM8/16/22
to
On Tue, Aug 16, 2022 at 08:25:12AM +0100, Tixy wrote:
> On Mon, 2022-08-15 at 21:05 -0400, Timothy M Butterworth wrote:
> > >
> > Thanks for the clarification. `echo 1 > sudo /proc/sys/vm/drop_caches`
> > seems to work just fine.
>
> It doesn't, as Tomas pointed out it creates a file called 'sudo' and
> puts a '1' in it.

The following two commands are equivalent:

echo 1 > sudo /proc/sys/vm/drop_caches

echo 1 /proc/sys/vm/drop_caches > sudo

The file "sudo" will have "1 /proc/sys/vm/drop_caches" in it, because
echo received two arguments. Redirections may appear anywhere in a
"simple" command. It's conventional to write them at the end of the
command, but the shell permits them to be anywhere.

This is a standard feature of all POSIX shells, by the way. Not a
bash extension.

Redirections must appear at the end of "compound" commands, which are
basically anything with internal shell syntax -- if, case, while, and
so on. It may be desirable to try to write something like:

< inputfile while read -r line; do
...
done

but it's not allowed. You have to write it as:

while read -r line; do
...
done < inputfile

Or, explicitly open the input file on a new FD (file descriptor), and
close it afterward:

exec 3< inputfile
while read -r line <&3; do
...
done
exec 3<&-

Tixy

unread,
Aug 16, 2022, 8:00:06 AM8/16/22
to
On Tue, 2022-08-16 at 07:24 -0400, Greg Wooledge wrote:
> The following two commands are equivalent:
>
> echo 1 > sudo /proc/sys/vm/drop_caches
>
> echo 1 /proc/sys/vm/drop_caches > sudo
>
> The file "sudo" will have "1 /proc/sys/vm/drop_caches" in it, because
> echo received two arguments.  Redirections may appear anywhere in a
> "simple" command.  It's conventional to write them at the end of the
> command, but the shell permits them to be anywhere.
>
> This is a standard feature of all POSIX shells, by the way.  Not a
> bash extension.

I should have remembered this as I learnt it a couple of years ago when
implementing a POSIX shell (subset thereof).

--
Tixy
0 new messages