From 58179ea548571a1769f652215103dd26084972d0 Mon Sep 17 00:00:00 2001 From: Ezri Zhu Date: Fri, 22 Mar 2024 02:43:40 -0400 Subject: [PATCH] swap article --- content/blog/20240315-swap.md | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 content/blog/20240315-swap.md diff --git a/content/blog/20240315-swap.md b/content/blog/20240315-swap.md new file mode 100644 index 0000000..5b2964a --- /dev/null +++ b/content/blog/20240315-swap.md @@ -0,0 +1,47 @@ +--- +Title: Adding SWAP to a Debian Machine +Description: Guide to remind myself how to add swap to Debian machines. +Tags: + - sysadmin + +--- + +So for SWAP, we usually have two choices. A swapfile, and a swap partition. + +A swapfile is a lot more flexible since it lives on your filesystem. However, it +does have an added overhead of the filesystem. + +Another reason why you may want to use a swap partition is that in case you have +more than one linux distro installed, they can share the same swap partition. + +People also [encrypts their swap +partition](https://askubuntu.com/questions/313564/why-encrypt-the-swap-partition) + +1. First we create the swapfile with 4G \ +`fallocate -l 4G /swapfile` + +2. Then, we set the correct permission for the swapfile \ +`chmod 600 /swapfile` + +3. We now run this to setup the swapfile with the correct headers. \ +`mkswap /swapfile` + +4. We will now run this to enable the swap partition, please note that this is a +transient operation and will not persist if you reboot the system. \ +`swapon /swapfile` + +5. Lastly, we will add the swapfile to our fstab so it will automatically enable +the swapfile on boot. \ +`echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab` + +There are also two sysctl options that you can tweak, either via the sysctl +command (transient), or `/etc/sysctl.conf` (persistent). + +`vm.swappiness` \ +`vm.vfs_cache_pressure` + +Further reading: [kernel Memory Management +docs](https://docs.kernel.org/admin-guide/mm/index.html) + +Adapted from the [DigitalOcean SWAP space +guide](https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04)