Purpose
To provide Field Engineers and Linux administrators with a standardized procedure for creating, configuring, and validating a Linux swap file. This procedure helps ensure sufficient virtual memory is available when physical RAM is exhausted and provides guidance on configuring the system’s swappiness parameter.
Scope
This procedure applies to Linux servers, including cloud servers, where swap space is not configured by default or where additional swap space is required
What is Linux swap file
Swap space is disk space reserved to act as virtual memory. When a Linux server exhausts its available physical memory (RAM), the kernel can move inactive memory pages to the swap space, freeing RAM for active processes and helping maintain system stability.
Cloud Servers do not include a swap partition by default. However, you can easily configure swap by creating a swap file. A swap file offers performance comparable to a swap partition while providing greater flexibility, as its size can be adjusted without repartitioning the disk.
You can also tune how frequently the system uses swap by modifying the swappiness parameter. A lower swappiness value encourages the kernel to retain processes in RAM for as long as possible, while a higher value causes the system to use swap space more aggressively.
The following steps explain how to create and enable a swap file on a Linux system, as well as how to configure the system's swappiness value.
How do I add a swap file?
The following steps show how to add 1GB of swap to your server:
Check the current status of swap using any/all of the following commands:
swapon -s
free -m
cat /proc/swaps
Create the file that you want to use for swap using either of the following commands:
sudo fallocate -l 1G /mnt/1GB.swap
# or
sudo dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
Format the swap file by entering the following command:
sudo mkswap /mnt/1GB.swap
Ensure permissions on the file is readable, writeable, and owned by root:root only. If not, use:
sudo chown root:root /mn/t1GB.swap
sudo chmod 600 /mnt/1GB.swap
Add the following line to the end of /etc/fstab to make the change permanent:
/mnt/1GB.swap none swap sw 0 0
Validate your fstab entry by instructing swapon to use fstab to mount all swap definitions by entering the following command:
sudo swapon -a
Check for the correct and expected amount of swap by using commands listed in step 1
To change the swappiness value, add the following line to the file at /etc/sysctl.conf:
vm.swappiness=10
Start with a value of 10 and increase if it as necessary. A typical default value for swappiness is 60. The higher the number (up to 100), the more often the system uses swap.
The degree to which swappiness affects performance depends on how your memory is currently used. We recommend that you experiment to find an optimal value. At 0, the system only uses the swap file when it runs entirely out of memory. Higher values enable the system to swap idle processes out in order to free memory for disk caching, potentially improving overall system performance.
Optional: Reboot the server to ensure that the changes survive.
Related Articles
KB 054003 - How to create Virtual Machine in VMWare
What is Virtualisation Virtualization is technology that lets you create useful IT services using resources that are traditionally bound to hardware. It allows you to use a physical machine’s full capacity by distributing its capabilities among many ...
KB 258332 - Backup, Snapshot, and Restore Linux installations using Timeshift
If you mess up (or are going to mess up) some config files or drivers, it is always good to have a fallback point. The de-facto standard for doing this on Linux is Timeshift. Timeshift for Linux is an application that provides functionality similar ...
KB 547003 - Permanent Mount Devices/File System in Linux
How to mount Devices/File System in Linux This guide only covers NTFS partitions, for other file systems you will need to change the commands or types as needed. Mounting makes file systems, files, directories, devices, and special files available ...
How to Partition, Format, and Mount Secondary Drives in Linux - Manual Drive Configuration (MDC)
Introduction This guide provides detailed instructions on how to manually configure secondary drives in a Linux system, including partitioning, formatting, and mounting the drives. Warning Performing these steps on an existing primary drive or a ...
KB 361001 - What is Boot Process in Linux OS.
Boot Process in Linux OS. Have you ever wondered what happens behind the scenes from the time you press the power button until the Linux login prompt appears? Press the power button on your system, and after few moments you see the Linux login ...
Recent Articles
KB-630208 | How to use Find and Locate to search for files in Linux
Purpose To provide Linux administrators and Field Engineers with guidance on using the find and locate utilities to efficiently search for files and directories within a Linux filesystem based on criteria such as name, type, size, timestamps, ...
KB-630164 | How to use rsync to Synchronize Files
Purpose To provide a clear and practical procedure for using the rsync utility to synchronize files and directories between local systems and remote hosts, including push and pull operations, while preserving file attributes and optimizing data ...
KB-630107 | How to create a Linux swap file
Purpose To provide Field Engineers and Linux administrators with a standardized procedure for creating, configuring, and validating a Linux swap file. This procedure helps ensure sufficient virtual memory is available when physical RAM is exhausted ...
MSI All-in-One (AIO) PC – Physical Damage Policy for Warranty Claims
MSI All-in-One (AIO) PC – Physical Damage Policy for Warranty Claims MSI's standard warranty does not cover physical or accidental damage to an All-in-One (AIO) PC. If the damage is determined to be caused by external factors rather than a ...
KB 134833 - Troubleshooting NVSM Alert NV-CPU-XX – Unrecoverable CPU Internal Error
Purpose This document provides a general troubleshooting procedure for the NVIDIA System Management (NVSM) alert NV-CPU-XX, which indicates that a CPU has reported an internal error. The article outlines how to verify whether the alert represents an ...