Keegan Turner
  • πŸ‘‹Hello World.
  • πŸ“šHow-To Guides
    • Integrating Git on your local host
    • βš–οΈLoad Balancing with HAProxy
    • πŸ‘¨β€πŸ’»Code Server - Self Hosted VSCode alternative
    • πŸ•ΈοΈWordPress + LAMP - Self Hosted CMS
    • ☁️Nextcloud - Self Hosted file sync & sharing solution
    • ⏳Grafana Cloud - Cloud Hosted Monitoring
    • βŒ›Prometheus + Grafana - Self Hosted monitoring stack solution
    • πŸ›³οΈPortainer + Docker - Self Hosted container management UI
    • 🐬MariaDB - Self Hosted database server
    • πŸ€–Let's Encrypt + Certbot - Secure your web services with SSL certificates
    • ◀️Nginx Proxy Manager - Self Hosted domain controller with LE SSL certificates
    • πŸ–₯️LXD - Linux OS containers
    • 🐧Linux basics: commands
    • 🐧Linux basics: file paths
    • 🐧Linux basics: SWAP file
  • 🚫Bytes of Caution
Powered by GitBook
On this page
  1. How-To Guides

Linux basics: SWAP file

19-05-2024

PreviousLinux basics: file pathsNextBytes of Caution

Last updated 1 year ago

Swap is a portion of the servers hard drive which is allocated to temporarily hold data that is not currently used by the server RAM. It acts as additional virtual memory for when the physical RAM is full.

I will be adding 4GB swap to my Ubuntu instance with 8GB RAM.

  1. Check if any swap space is currently enabled on the server

sudo swapon --show

If there's no output on your terminal, it means no swap space is active on the server.

  1. Decide on the size of the swap file you want to create

sudo fallocate -l 4G /swapfile
  1. Secure the swap file

sudo chmod 600 /swapfile
  1. Set the swap area

sudo mkswap /swapfile
  1. Enable the swap file

sudo swapon /swapfile
  1. Verify that the swap is active

sudo swapon --show

To ensure the swap file is used on boot, you need to add it to /etc/fstab.

  1. Backup the current file

sudo cp /etc/fstab /etc/fstab.bak
  1. Add the swap file to the /etc/fstab file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
  1. Set the swappiness value (Optional)

Swappiness controls how much your server makes use of the allocated swap space. Lower values results in the server using swap less often, which can make it faster. Higher values make the system use swap more often, which can keep more RAM free.

sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Check the server RAM and SWAP

free -h

Enabling swap allows you to fine-tune how your system balances performance and processes that require memory in either form of RAM or Swap. Having swap space is a practical strategy for maintaining smooth and efficient operations on your Linux server.

πŸ“š
🐧
Page cover image