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!

tshepang
  • 64,472
  • 86
  • 223
  • 290
BrainStone
  • 3,534
  • 12
  • 32
  • 53

7 Answers7

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"
Wrzlprmft
  • 103
  • 5
rasty.g
  • 768
  • 5
  • 6
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.

goldilocks
  • 86,451
  • 30
  • 200
  • 258
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.

terdon
  • 234,489
  • 66
  • 447
  • 667
chaos
  • 47,463
  • 11
  • 118
  • 144
  • 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. –  Jan 15 '14 at 20:51
  • That's why I wrote that under some virtualization techniques it works, for example Xen. – chaos Jan 15 '14 at 20:58
  • It works also unter qemu and vmware. I'd propose to write 'under some virtualization techniques it doesn't work', because OpenVZ provides virtualization at kernel level, so it is a special case. And i am unsure about if it could be modified to do so. –  Jan 15 '14 at 21:18
  • 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
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.

mchid
  • 1,421
  • 2
  • 15
  • 21
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'

iamabhaykmr
  • 119
  • 4
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.  
muru
  • 69,900
  • 13
  • 192
  • 292
-1

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

sync && sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"
Secko
  • 276
  • 1
  • 8