17

If I configure the swappiness value to another, from ex.: 60 to 0, then I always need to reboot the machine to the changes to take effect? Even when modifying with:

sysctl -w vm.swappiness=0
evachristine
  • 2,613

6 Answers6

29

Everything is well explained in the Wikipedia page you gave.

# Set the swappiness value as root
echo 10 > /proc/sys/vm/swappiness

# Alternatively, run this as a non-root user
# This does the same as the previous command
sudo sysctl -w vm.swappiness=10

# Verify the change
cat /proc/sys/vm/swappiness
10

At this point, the system will manage the swap like you just configured it, BUT if you reboot NOW, your change will be forgotten and the system will work with the default value (assuming 60, meaning than it will start to swap at 40% occupation of RAM).

You have to add the line below in /etc/sysctl.conf to keep your change permanently:

vm.swappiness = 10

Hope it’s more clear for you now!

TRiG
  • 331
tisc0
  • 466
  • 4
  • 5
  • It's interesting that the -w switch is supposed to write the change. One would think that after writing that, adding the default line in sysctl.conf would not be necessary. – WEBjuju Jan 25 '17 at 12:55
  • 1
    Hi WEBjuju, You're right, the switch still doesn't work for me (fedora 25 or centos 7). It's not needed then, since changing live value in /proc file is done without anyway. – tisc0 Feb 12 '17 at 11:31
8

need to reboot the machine to the changes to take effect?

The opposite, in fact -- rebooting will reset the swappiness to its default value. To make it persist across reboots, you need to include a directive in a boot script or use the method recommended in the wikipedia article by adding:

vm.swappiness = ??

To /etc/sysctl.conf (or an /etc/sysctl.d file), where ?? is the value you want to use. Note that just adding this will not cause any change at the time.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
5

The accepted answer is correct, but it's recommended to use a separate "sysctl" config file so that you don't accidentally overwrite other settings (which might happen if you overwrite the global "sysctl.conf").

# echo 'vm.swappiness=10' >/etc/sysctl.d/swappiness.conf

Those who've already modified the global config file "/etc/sysctl.conf" might want to remove lines defining this "swappiness" value from that file. You could use this command to remove these lines from the global config file (keeping comments):

# sed -i '/^vm.swappiness=/d' /etc/sysctl.conf

Now, to apply the new value, tell sysctl to use it:

# sysctl -p /etc/sysctl.d/swappiness.conf

Or apply all settings, including swappiness:

# sysctl -p /etc/sysctl.d/*

Verify it:

# sysctl vm.swappiness
vm.swappiness = 10
basic6
  • 6,255
  • Agreed, it's a best practice, making things clearer and safer. A good habit too when using a configuration management tool. – tisc0 Oct 30 '19 at 21:52
1

I found that on a CentOS machine, the vm.swappiness was being set in /usr/lib/tuned/virtual-guest/tuned.conf.

Determine where the default is being set by looking through tuned configuration files:

sudo grep -R swap /usr/lib/tuned | grep swappiness

After updating the default, current swappiness can still be set without rebooting:

sudo sysctl vm.swappiness=10

and tested by:

cat /proc/sys/vm/swappiness
muru
  • 72,889
WEBjuju
  • 506
  • 2
    /etc/tuned/tuned-main.conf (RHEL7) has a setting that allows /etc/sysctl.conf, /etc/sysctl.d, etc., to override settings applied by tuned (i.e. reapply_sysctl = 1). At least on my install, this is the default. – kbulgrien Nov 02 '18 at 23:01
1

User changes to sysctl by convention go into files with the prefix 60- so the filename should be /etc/sysctl.d/60-swappiness.conf. Execute service procps start as root to force a reread of configuration values (no reboot needed).

0

We can change values in cache using sysctl -w vm.swappiness=xx but this cache will get flush after server restart thus we need to vm.swappiness=xx in /etc/sysctl.conf for kernel will pick values during server boot from /etc/sysctl.conf.