KB-630107 | How to create a Linux swap file

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 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:

  1. Check the current status of swap using any/all of the following commands:

    swapon -s
    free -m
    cat /proc/swaps
  2. 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
  3. Format the swap file by entering the following command:

    sudo mkswap /mnt/1GB.swap
  4. 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
  5. Add the following line to the end of /etc/fstab to make the change permanent:

    /mnt/1GB.swap none swap sw 0 0
  6. Validate your fstab entry by instructing swapon to use fstab to mount all swap definitions by entering the following command:

    sudo swapon -a
  7. Check for the correct and expected amount of swap by using commands listed in step 1

  8. 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.

  9. Optional: Reboot the server to ensure that the changes survive.

References 



      



    • 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 ...
    • Popular Articles

    • CP Plus Camera and NVR Configuration

      NVR Configuration The CP Plus Pro Series of NVRs have been meticulously designed for providing you with upgraded performance and higher recording quality in your IP video surveillance solution. The robust processor that has been inculcated in this ...
    • KB 775235 - Kerberos Authentication – Overview

      What is Kerberos? Kerberos is a secure authentication method used in our Active Directory (AD) environment (mbuzztech.com). It allows users to: Access multiple systems without re-entering passwords (Single Sign-On – SSO) Log in once Where We Use It ...
    • How to Remove and Reinstall NVIDIA Drivers on Ubuntu

      This article provides step by step guide to completely remove existing NVIDIA drivers and reinstall specific version of the NVIDIA driver on the Ubuntu system Prerequisites Administrative (sudo) access to the Ubuntu system. Internet access to ...
    • KB 692001 - Personal Computers and Servers - Classification and Point of Contact

      We can classify the computers that MBUZZ handles based on their form-factor as below: Tower Workstations, Desktops, Gaming PCs and SFF (Small form factor) PCs fall under this category. These are computers people would use on a desk and rarely move. ...
    • KB 298031 - M.2 SSD Tier List

      The sequential read and write speeds, which are usually the most advertised number, are not a proper benchmark of real-world performance or the quality of an SSD. This article categorizes and tiers SSDs based on factors like the type of NAND flash, ...