swap
Create a swap file if the server has no swap, and set vm.swappiness to match the amount of RAM
- Command:
/go/swap - Category: System
- Required option: None
- Output: Plain text (
mkswapoutput when a swap file is created) - Last updated: 2026-11-20
On this page: Overview · Quick start · Syntax · Options · Output · Examples · Troubleshooting · Details · See also
Overview
Swap is disk space the system uses when it runs out of RAM. /go/swap sets it up for you. It:
- reads the server's total RAM
- creates a swap file at
/swapfileif the server has no swap at all - makes the swap file permanent by adding it to
/etc/fstab - sets
vm.swappiness, which controls how readily the system moves memory to swap, and saves it so it survives a reboot
The swap size and the swappiness value are chosen automatically from the amount of RAM (see the size table). You can't pass your own values.
The command is safe to run again. If the server already has swap, it leaves it alone and only refreshes the swappiness setting.
Quick start
/go/swap
Syntax
/go/swap
The command takes no arguments. Any arguments you pass are logged but ignored.
Options
None.
Output
When it creates a swap file, the command prints the output of mkswap:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=3f1b6c1e-0c2e-4a3e-9d52-5b1f1c4f7a10
When the server already has swap, the command prints nothing.
Every run is logged to /var/log/go/swap.log with a timestamp and the arguments used.
To check the result:
swapon --show # the active swap file
free -h # RAM and swap totals
cat /proc/sys/vm/swappiness # the current swappiness value
Examples
# Create swap (if missing) and set swappiness
/go/swap
# Check the result
swapon --show
cat /proc/sys/vm/swappiness
# Replace the swap file with a new one of the size the command would choose
sudo swapoff /swapfile
sudo rm /swapfile
/go/swap
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The command prints nothing and no swap file appears | The server already has swap. The command only creates a file when total swap is 0. |
Check with swapon --show. To change the size, remove the old swap first (see Examples). |
fallocate failed: No space left on device |
There isn't enough free disk space. | Free up space (see clean), then run the command again. |
swapon: ... skipping - it appears to have holes |
The filesystem doesn't support swap files made with fallocate. |
Create the file with dd instead, for example sudo dd if=/dev/zero of=/swapfile bs=1M count=2048, then run chmod 600, mkswap, and swapon on it. |
swapon failed: Operation not permitted |
The server is a container (such as OpenVZ or LXC) that can't manage swap. | Set the swap in the host or in your provider's panel. |
| Swap disappears after a reboot | The /etc/fstab entry is missing. The command skips it if any line in fstab contains swap, even a comment. |
Add /swapfile none swap sw 0 0 to /etc/fstab. |
/etc/fstab has a /swapfile line but the file is missing |
The file couldn't be created, but the fstab line was still added. |
Fix the cause (usually disk space) and run the command again. |
| Swappiness isn't the value in the table | Another setting overrides it, such as vm.swappiness in /etc/sysctl.conf. |
Remove the other setting and run sudo sysctl --system. |
| A large server got only 2 GB of swap | RAM above 256 GB falls outside the sizing rule. | See Sizes and swappiness. |
Details
Sizes and swappiness
The RAM is rounded up to whole gigabytes (a server with 3.8 GB counts as 4 GB). A server with 1 GB or less counts as 1 GB.
| RAM | Swap file size | vm.swappiness |
|---|---|---|
| 1 GB or less | 2 GB | 60 |
| 2 to 3 GB | 2 GB | 30 |
| 4 to 5 GB | 2 GB | 20 |
| 6 to 7 GB | RAM ÷ 2, rounded down (3 GB) | 15 |
| 8 to 256 GB | RAM ÷ 2, rounded down (for example 4 GB at 8 GB, 8 GB at 16 GB) | 10 |
| More than 256 GB | 2 GB | 10 |
A lower swappiness value makes the system keep more data in RAM and use swap later.
What it does
flowchart TD
A["/go/swap"] --> B["Read total RAM, work out swap size and swappiness"]
B --> C{"Total swap is 0?"}
C -- "No" --> F
C -- "Yes" --> D["fallocate /swapfile, chmod 600, mkswap, swapon"]
D --> E["Add /swapfile to /etc/fstab if there is no swap line"]
E --> F["Write /etc/sysctl.d/99-swappiness.conf"]
F --> G["sysctl --system"]
- Reads the total RAM from
free. - Reads the total active swap. If it is not
0, skips to step 5. - Creates
/swapfilewithfallocate, sets its permissions to600(readable only by root), formats it withmkswap, and turns it on withswapon. - Adds
/swapfile none swap sw 0 0to/etc/fstabunless a line containingswapalready exists. - Writes
vm.swappiness=<value>to/etc/sysctl.d/99-swappiness.conf, replacing the file if it exists. - Applies it with
sysctl --system, which reloads all sysctl settings.
Limits
- The command does not check free disk space and stops at no error. If a step fails, the later steps still run.
- It never resizes or removes existing swap, and it doesn't touch a swap partition.
- It doesn't change the size of an existing
/swapfilethat is already in use.
See also
| Command | Description |
|---|---|
mem |
Check the top ten memory-consuming commands or services |
memoryavg |
Check the weekly server memory average |
heal |
Check GoAdmio service status and fix issues |
slice |
Apply server-wide CPU and memory limits to user-.slice |
clean |
Clean and remove unnecessary files to free up space |
my |
Optimize the my.cnf configuration for MySQL/MariaDB |