Ubuntu – Changing swappiness in sysctl.conf doesn’t work for me


I'm using 12.04 LTS and can sudo sysctl to set swappiness to 10, but adding vm.swappiness=10 to sysctl.conf doesn't work for me – after I reboot, swappiness still reports 60 (default)

I'd like to be able to reduce swaps to my SSD, but can't find a way to do so except manually per session. Modifying sysctl.conf seems to work for most – can anyone advise what I need to check / change to make it work for me too, please?

Best Answer

What I usually do is create a custom rule in /etc/sysctl.d/ instead of editing the main sysctl file. Change directory to sysctl.d and create your file:

cd /etc/sysctl.d/ 
sudo touch 60-my-swappiness.conf

Then run your favourite text editor:

sudo nano 60-my-swappiness.conf

Enter your value at the top of the file, for example, vm.swappiness = 10 (there must be spaces between swappiness and the value as shown).

As it recommends in the readme in /etc/sysctl.d/, run

sudo service procps start

for the system to read the new values and then reboot.

The reason why we use 60 at the start of the my-swappiness.conf is so that your custom rule overrides any other rules in the directory.

Check that you have the correct swappiness with

cat /proc/sys/vm/swappiness

This works for me, but there is more guidance on swap and swappiness in the Ubuntu guide if you need any more detailed information.