Skip to main content
In this guide, you will learn how to create and manage swap space on a Linux system. Swap space is a designated area on disk where Linux temporarily moves data from RAM when physical memory is fully utilized. This process helps ensure that active applications have sufficient RAM to operate efficiently. Consider the following scenario that demonstrates how swap space is used:
  • A computer has 4 GB of RAM.
  • A video editor uses 2 GB of RAM.
  • An audio editor uses another 2 GB of RAM.
  • With no free RAM available, a 2-GB swap partition is activated.
  • When launching an additional application like Chrome, Linux shifts inactive data (for example, from the video editor) into swap space to free up RAM.
The image illustrates the concept of managing swap space, showing a 4GB RAM with applications like Chrome and an audio editor using 2GB, and a swap partition with a video editor using 2GB.
Below are the detailed steps and commands to set up and manage swap space on your Linux system.

Checking Active Swap Space

Before making changes, you can verify your current swap configuration. To display active swap areas, run:
You can also review all disk partitions using the lsblk command:
In this output, the swap partition on /dev/dm-1 is active (2 GB), while /dev/vdb3 is present but not yet enabled for swap usage.

Preparing and Enabling a Swap Partition

Before you can activate a partition as swap space, it must be formatted accordingly. Use the mkswap command to initialize the partition:
Then, enable the swap partition with the swapon command in verbose mode to display details:
Reconfirm the swap status:
Linux will utilize all configured swap areas if multiple swap partitions are added.

Making the Swap Configuration Persistent

By default, swap space is not activated after a reboot. To ensure that the swap partition is enabled automatically at startup, add an appropriate entry in the /etc/fstab file. This will also help maintain system stability by ensuring that swap is consistently available. For more details on achieving persistent mounts, refer to the file system documentation.

Disabling Swap Space

There might be times when you need to disable a swap partition—such as for system maintenance or configuration changes. To disable swap temporarily, execute:

Creating and Using a Swap File

In addition to swap partitions, Linux supports swap files. Swap files can be a flexible alternative when dedicated partitions are not available.

Step 1: Creating the Swap File

First, allocate space for the swap file using the dd command. For example, to create a 128 MB swap file:
To create a larger 2 GB swap file with progress updates, run:
Here:
  • if=/dev/zero reads zeros.
  • of=/swap specifies the output file location.
  • bs=1M sets the block size to 1 megabyte.
  • count determines how many blocks are allocated.
  • status=progress displays the progress.

Step 2: Setting Secure Permissions

Ensure that your swap file is secure by adjusting the permissions so that only the root user can access it:
Always set the correct permissions on swap files. Improper permissions can expose sensitive data.

Step 3: Formatting the File as Swap

Initialize the file as swap space with the mkswap command:

Step 4: Enabling the Swap File

Activate the swap file using the swapon command:
Verify that your swap space is active by running:

Conclusion

This article has covered both methods for implementing swap space on Linux: using a dedicated swap partition and creating a swap file. In both cases, the steps involve initializing with mkswap, activating with swapon, and verifying the swap status. For persistent swap configurations across reboots or more advanced setups, consult additional system administration resources. Now try these steps in your practice environment for enhanced system performance and efficient memory management.

Watch Video

Practice Lab