Skip to content

Commit

Permalink
swap article
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrizhu committed Mar 22, 2024
1 parent 19a26f3 commit 58179ea
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions content/blog/20240315-swap.md
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 58179ea

Please sign in to comment.