46

I am currently having some issues with the cache. It is a little too much right now so I wanted to clear it. I googled and found this little command: sync && echo 3 > /proc/sys/vm/drop_caches.
I am logged in as root over SSH (not using sudo). These are the attempts I made:

root@server: ~ # ll /proc/sys/vm/drop_caches
-rw-r--r-- 1 root root 0 15. Jan 20:21 /proc/sys/vm/drop_caches

root@server: ~ # echo 3 > /proc/sys/vm/drop_caches
-bash: /proc/sys/vm/drop_caches: Permission denied

root@server: ~ # sudo su -c "echo 3 > /proc/sys/vm/drop_caches"
bash: /proc/sys/vm/drop_caches: Permission denied

root@server: ~ # echo 3 | sudo tee /proc/sys/vm/drop_caches
tee: /proc/sys/vm/drop_caches: Permission denied
3

It is a remote machine running Debian. As far as I know there are some vCores in this machine and it uses Virtuozzo for the virtualization.
I really just want to clear the cache (So I can only access it using SSH).
I also tried registering this as a cronjob. But it simply fails too!

9
  • 2
    Are you running this as root or are you using sudo?
    – terdon
    Jan 15, 2014 at 19:52
  • 3
    I am running this as root. But sudo also fails.
    – BrainStone
    Jan 15, 2014 at 19:53
  • Does not work either. Already tried it. Same error message.
    – BrainStone
    Jan 15, 2014 at 19:55
  • See the slm's A on this question, it might help.
    – user37607
    Jan 15, 2014 at 19:58
  • This is extremely unlikely, but you never know, does /proc/sys/vm/drop_caches actually exist?
    – terdon
    Jan 15, 2014 at 20:02

7 Answers 7

64

sudo has to cover whole the redirection so it can be completely executed by root:

$ sudo sh -c "/usr/bin/echo 3 > /proc/sys/vm/drop_caches"
6
  • 7
    this actually worked for me (with the right binary location for echo)
    – orm
    Nov 5, 2014 at 19:59
  • in OpenVZ not work Mar 3, 2015 at 16:03
  • 4
    alternatively, you could use tee instead: echo 3 | sudo tee /proc/sys/vm/drop_caches
    – mchid
    Dec 23, 2015 at 0:09
  • 1
    @mchid, consider adding your comment as an answer. It also worked for me on Azure VM. Jan 26, 2017 at 22:04
  • It's root already, see # at the beginning of line. Issue with sudo is worth noting, but for another question. Mar 18, 2018 at 7:31
32

I am logged in as root over SSH...It is a remote machine running Debian.

Is it actually a remote machine, or a just a remote system? If this is a VPS slice somewhere, (at least some forms of) OS virtualization (e.g. openVZ) won't permit this from within the container. You don't run the machine, you just run your slice.

3
  • 2
    There is like no way???
    – BrainStone
    Jan 15, 2014 at 20:09
  • 1
    Probably not, since there wouldn't be much point in not permitting it if there were alternate methods that worked.
    – goldilocks
    Jan 15, 2014 at 20:11
  • 2
    @BrainStone - I use OpenVZ, it's not possible!
    – slm
    Jan 15, 2014 at 21:42
14

This is normal behavior under OS-level virtualization. This can only be executed by someone with root access to the hardware node.

With for example OpenVZ, you don't get your own kernel instance and as such, are restricted from performing commands like this.

All share the same page cache, so to drop caches of only your instance, the kernel must check if the page belongs to you and if the other instances aren't using this page too.

With another virtualization technique like KVM or Xen this might be working.

7
  • 1
    So I could/should/must contact my hoster to clear the cache for me? And how do I find out what "virtualisation technique" my system is on?
    – BrainStone
    Jan 15, 2014 at 20:15
  • Yes, you may not come around this...
    – chaos
    Jan 15, 2014 at 20:18
  • I am not shure if this is right. I am running some vm's at aws and Xen and i am able to drop_caches.
    – user55518
    Jan 15, 2014 at 20:51
  • 2
    The difference is between OS virtualization (OpenVZ, LXC) and platform virtualization (QEMU, Xen), both of which have advantages and disavantages vs. the other.
    – goldilocks
    Jan 15, 2014 at 21:52
  • 1
    @bersch Xen uses an own kernel for VMs. OpenVZ does not. You can think of OpenVZ like a better "chroot".
    – Nils
    Jan 20, 2014 at 11:49
12

You can use echo piped to sudo tee to allow the proper permission needed when you need to echo as root.

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

Use tee --help to list more options.

1

sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"

This command work for me without /usr/bin/echo in mchid answer. It was giving error sh: 1: /usr/bin/echo: not found . So used only 'echo'

0

I had the same problem when I tried to use sudo like this:

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

My solution was to temporarily switch to root. Obiously, that permission has to be enabled on your system:

sudo su -  #temporarily switch to root user
echo 1 > /proc/sys/vm/overcommit_memory
exit # Exit as root.  
-1

Using a smilar VM settings with OpenStack and this works (running Debian):

sync && sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .