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!

  • 1
    Are you running this as root or are you using sudo? – terdon Jan 15 '14 at 19:52
  • 2
    I am running this as root. But sudo also fails. – BrainStone Jan 15 '14 at 19:53
  • Does not work either. Already tried it. Same error message. – BrainStone Jan 15 '14 at 19:55
  • See the slm's A on this question, it might help. – Risto Salminen Jan 15 '14 at 19:58
  • @terdon : Whoops. Retracted. – goldilocks Jan 15 '14 at 20:00
up vote 21 down vote accepted

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.

  • 1
    There is like no way??? – BrainStone Jan 15 '14 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 '14 at 20:11
  • 2
    @BrainStone - I use OpenVZ, it's not possible! – slm Jan 15 '14 at 21:42

sudo has to cover whole redirection in order it can be completely executed under root:

$ sudo sh -c “/usr/bin/echo 3 > /proc/sys/vm/drop_caches”
  • 5
    this actually worked for me (with the right binary location for echo) – orm Nov 5 '14 at 19:59
  • in OpenVZ not work – shilovk Mar 3 '15 at 16:03
  • 1
    alternatively, you could use tee instead: echo 3 | sudo tee /proc/sys/vm/drop_caches – mchid Dec 23 '15 at 0:09
  • Working in an Azure VM. Thanks! – lordcheeto Feb 9 '16 at 6:52
  • 1
    @mchid, consider adding your comment as an answer. It also worked for me on Azure VM. – Erik Shilts Jan 26 '17 at 22:04

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.

  • 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 '14 at 20:15
  • Yes, you may not come around this... – chaos Jan 15 '14 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 '14 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 '14 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 '14 at 11:49

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.

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'

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:

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.