Create Linux Swapfiles PDQ
swappy is an automated command-line utility and Ansible orchestration tool designed to safely initialize, size, secure, and prioritize swap space on Linux systems in seconds.
Interactive Linux Swap Calculator & Command Generator
Calculate optimal swap space for your server or desktop workstation and get the exact swappy command.
Why Use swappy?
Eliminate repetitive boilerplate and risk of corrupted swap headers across your infrastructure.
Instant Provisioning
No manual calculations of block sizes or memorizing dd, mkswap, or swapon flags. One command does it all.
Hardened Security
Automatically enforces chmod 0600 and root ownership so that memory dumps cannot be snooped by unprivileged local accounts.
Ansible Fleet Ready
Includes an out-of-the-box Ansible playbook and inventory structure. Target hundreds of cloud instances simultaneously with make play.
Installation Guide
Choose between direct bootstrap, standard Makefile install, or manual binary placement.
Option 1: Quick Bootstrap (curl & bash)
Review the bootstrap script before running:
curl https://raw.githubusercontent.com/joshuacox/swappy/master/bootstrap | bashOption 2: Clone and Install via Makefile
The recommended method for production systems:
git clone https://github.com/joshuacox/swappy.git
cd swappy
sudo make installOption 3: Manual PATH Placement
Copy swappy directly to any directory in your system $PATH:
sudo cp swappy /usr/local/bin/swappy
sudo chmod 755 /usr/local/bin/swappyCLI Command Reference
How to use swappy init and inspect active swap files.
1. Default Initialization (1024 MB at /swapfile)
Running swappy init with no arguments provisions a 1 GB swap file at /swapfile with priority 1:
swappy init2. Custom File Path
Specify a dedicated mount point, NVMe disk, or specific file name:
swappy init /mnt/fast-nvme/swapfile.img3. Custom Path and Size (in Megabytes)
Provision 4096 MB (4 GB) or 8192 MB (8 GB) on demand:
# Provision a 4 GB swapfile
swappy init /swapfile 4096
# Provision an 8 GB swapfile
swappy init /swapfile 81924. Custom Path, Size, and Kernel Priority
Control the swap priority (-p $PRIORITY). Higher numerical values are prioritized by the Linux kernel before lower ones:
# Prioritize this fast swap device with priority 10
swappy init /mnt/nvme/swapfile 4096 105. Inspect Active Swap Partitions & Files
List all currently registered swap areas along with size, usage, and priority:
swappy listAnsible Multi-Host Automation
Deploy swappy across fleet nodes, dev clusters, or cloud staging machines simultaneously.
The swappy repository includes an Ansible playbook (swappy.yaml) and Makefile recipe (make play) that installs the script to remote hosts in parallel.
Define your Ansible Inventory (/etc/ansible/hosts):
node1 ansible_ssh_host=192.168.1.101 ansible_ssh_user=root
node2 ansible_ssh_host=192.168.1.102 ansible_ssh_user=root
node3 ansible_ssh_host=192.168.1.103 ansible_ssh_user=root
[swappy]
node1
node2
node3Run the Ansible Playbook:
make playThis runs ansible-playbook -s -v swappy.yaml under the hood to ensure proper sudo escalation and distribution.
Architecture, Safety & Cleanup Mechanism
Understanding how swappy provisions swap files and logs safe deconstruction scripts.
Zero-Fragmentation Allocation
Unlike fallocate, which on some filesystems (such as Btrfs or older ext4 kernels) creates sparse files with holey blocks incompatible with the kernel swap subsystem, swappy uses:
This guarantees contiguous physical disk allocation and eliminates the risk of silent memory corruption under heavy paging.
Automated Cleanup Scripting
Whenever a swapfile is initialized, swappy records its deletion command to:
When decommissioning an ephemeral instance or recycling storage, root administrators have an immediate audit log and destruction script ready.
Frequently Asked Questions
Common questions regarding Linux swap space, performance, and best practices.
Why do I need swap if my server has 32 GB of RAM?
Linux memory management uses swap to page out idle, anonymous memory pages (e.g. startup daemon initialization code that is never run again). Paging out idle allocations frees up precious physical RAM for the Linux Page Cache, speeding up filesystem I/O across your applications.
Does a swapfile persist across reboots?
swappy init provisions the file on disk and activates it immediately for the current boot session with swapon. To make it persistent across system reboots, add an entry to your /etc/fstab:
How do I tune Linux swappiness?
The kernel parameter vm.swappiness (ranging from 0 to 200, default 60) dictates how aggressively the kernel replaces memory pages with swap. For production databases or latency-sensitive microservices, setting it to 10 or 1 is common:
Ready to bulletproof your Linux memory?
Check out the open source repository on GitHub, contribute improvements, or integrate it into your automated provisioning scripts.