Imagine kernel as the engine of your computer.
Because the Linux OS might be installed on a different medium (laptop, database server, network router etc.).
Each medium needs different configurations.
These adjustable configurations are Kernel Tunables.
In Linux, everything is treated as a file.
User can edit kernel tunables through:
/proc/sys/ for temporary changes.
usually is /etc/sysctl.conf for permanent changes.
Imagine physical RAM as your working desk. Your desk holds two distinct categories of materials:
File Caches (Page Cache): Reference papers or books pulled from a bookshelf. If your desk gets messy, you can put these books back on the shelf without losing any written work. If you need them again, you simply reach back to the shelf.
Application Data (Anonymous Memory): Notes you are actively writing. You cannot just discard them. To free up space on your desk, you must carefully pack them into a box, carry them to a storage room (the swap space on your disk), and retrieve them later when needed.
When RAM starts running low, the operating system must free up desk space.
When the swappiness value is higher, the kernel is more willing to move Application Data (Anonymous Memory) out of RAM and into swap space.
Change Swappiness Temporary
Check current Swappiness number
cat /proc/sys/vm/swappiness
Edit Swappiness number temporary
sudo bash -c 'echo [Swappiness number] > /proc/sys/vm/swappiness'
Example: sudo bash -c 'echo 40 > /proc/sys/vm/swappiness'
Change Swappiness Permanently
Direct to sysctl.d and list all configuration files
cd /etc/sysctl.d/
Decide naming of new configuration file
[Index]-[File Name].conf. Example: 10-swappiness.conf
Linux reads configuration files in alphabetical order. Numbers are read before letters.
10-swappiness.conf (read first)
99-sysctl.conf (read last, overriding anything set before it, default there's no configuration inside.)
Check current Swappiness number via sysctl
sysctl vm.swappiness
Create new configuration file in /etc/sysctl.d/
echo "vm.swappiness=[Swappiness number]" | sudo tee -a /etc/sysctl.d/[Index]-[File Name].conf
Example: echo "vm.swappiness=40" | sudo tee -a /etc/sysctl.d/10-swappiness.conf
Verify configuration
cat -b [Index]-[File Name].conf
Example: cat -b 10-swappiness.conf
Apply configuration
sudo sysctl --system
Cleanup
Delete configuration file
sudo rm /etc/sysctl.d/[Index]-[File Name].conf
Example: sudo rm /etc/sysctl.d/10-swappiness.conf
Apply configuration
sudo sysctl --system
Result
Change Swappiness Temporary
student@localhost:/etc/sysctl.d$ cat /proc/sys/vm/swappiness
60
student@localhost:/etc/sysctl.d$ sudo bash -c 'echo 40 > /proc/sys/vm/swappiness'
[sudo] password for student:
Change Swappiness Permanently
student@localhost:/etc/sysctl.d$ cd /etc/sysctl.d/
student@localhost:/etc/sysctl.d$ sysctl vm.swappiness
vm.swappiness = 40
student@localhost:/etc/sysctl.d$ echo "vm.swappiness=40" | sudo tee -a /etc/sysctl.d/10-swappiness.conf
vm.swappiness=40
student@localhost:/etc/sysctl.d$ cat -b 10-swappiness.conf
1 vm.swappiness=40
student@localhost:/etc/sysctl.d$ sudo sysctl --system
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
* Applying /usr/lib/sysctl.d/10-map-count.conf ...
* Applying /etc/sysctl.d/10-swappiness.conf ...
* Applying /usr/lib/sysctl.d/50-coredump.conf ...
* Applying /usr/lib/sysctl.d/50-default.conf ...
* Applying /usr/lib/sysctl.d/50-libkcapi-optmem_max.conf ...
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
* Applying /usr/lib/sysctl.d/50-redhat.conf ...
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.conf ...
kernel.yama.ptrace_scope = 0
vm.max_map_count = 1048576
vm.swappiness = 40
kernel.core_pattern = |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %d %F
kernel.core_pipe_limit = 16
fs.suid_dumpable = 2
kernel.sysrq = 16
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.enp0s3.rp_filter = 2
net.ipv4.conf.lo.rp_filter = 2
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.enp0s3.accept_source_route = 0
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.enp0s3.promote_secondaries = 1
net.ipv4.conf.lo.promote_secondaries = 1
net.ipv4.ping_group_range = 0 2147483647
net.core.default_qdisc = fq_codel
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.protected_regular = 1
fs.protected_fifos = 1
net.core.optmem_max = 81920
kernel.pid_max = 4194304
kernel.kptr_restrict = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.enp0s3.rp_filter = 1
net.ipv4.conf.lo.rp_filter = 1
Cleanup
student@localhost:/etc/sysctl.d$ sudo rm /etc/sysctl.d/10-swappiness.conf
student@localhost:/etc/sysctl.d$ ls
99-sysctl.conf
student@localhost:/etc/sysctl.d$ sudo sysctl --system
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
* Applying /usr/lib/sysctl.d/10-map-count.conf ...
* Applying /usr/lib/sysctl.d/50-coredump.conf ...
* Applying /usr/lib/sysctl.d/50-default.conf ...
* Applying /usr/lib/sysctl.d/50-libkcapi-optmem_max.conf ...
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
* Applying /usr/lib/sysctl.d/50-redhat.conf ...
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.conf ...
kernel.yama.ptrace_scope = 0
vm.max_map_count = 1048576
kernel.core_pattern = |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %d %F
kernel.core_pipe_limit = 16
fs.suid_dumpable = 2
kernel.sysrq = 16
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.enp0s3.rp_filter = 2
net.ipv4.conf.lo.rp_filter = 2
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.enp0s3.accept_source_route = 0
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.enp0s3.promote_secondaries = 1
net.ipv4.conf.lo.promote_secondaries = 1
net.ipv4.ping_group_range = 0 2147483647
net.core.default_qdisc = fq_codel
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.protected_regular = 1
fs.protected_fifos = 1
net.core.optmem_max = 81920
kernel.pid_max = 4194304
kernel.kptr_restrict = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.enp0s3.rp_filter = 1
net.ipv4.conf.lo.rp_filter = 1