How to Create Swap Memory in Linux
To create a swap file in Linux, prepare a file on your storage, initialize it with mkswap, and then activate it with swapon. Swap helps reduce memory pressure, but it is slower than RAM and does not solve all memory shortage issues or guarantee the normal operation of programs.
What is Swap?
Swap is a space that allows part of the memory pages in use to be placed on storage. You can use a disk file or a separate partition. This article explains how to add a file, and the 2GiB in the example is for practice purposes. The required size should be determined considering RAM, workload, available space, and whether hibernation is used.
The system may already be using swap. Check the currently activated items and memory status with the following command.
swapon --show
free -h
Check the name, size, and usage in swapon --show, and look at the total usage in the Swap row of free -h. Even if you increase the swap, performance can decrease if there is frequent storage access. If memory is constantly insufficient, also consider the workload and RAM expansion.
Things to check before starting
The following procedure is an example for Linux using GNU tools and util-linux, where the root file system is the common ext4 and has sufficient free space. Administrator privileges are required. Containers or managed servers may restrict swap activation, so check the operating policies first.
findmnt -T / -o TARGET,FSTYPE,SOURCE,OPTIONS
df -h /
Check the file system type and available space. In addition to the 2 GiB swap file, leave enough free space for normal operation. Do not use the example path /swap-manualfactory if a file or symbolic link already exists there. Reinitializing or overwriting an existing swap file can cause data loss and problems on a running system.
Btrfs has separate restrictions related to Copy-on-Write and snapshots, so do not directly apply the ext4 procedure below. For Btrfs-specific creation methods and conditions, check the Btrfs swap file document . Network file systems or other file systems may also have different support conditions.
Creating and Activating a Swap File
The following is an example of creating a 2GiB file when /swap-manualfactory does not exist. The conv=excl in GNU dd will fail if the target already exists, preventing overwriting. If it fails or is interrupted, do not proceed to the next step; check the cause and the generated file.
sudo dd if=/dev/zero of=/swap-manualfactory bs=1M count=2048 conv=excl status=progress
If the command succeeds, check the file size and path, set it so that only the administrator can read and write it, and then initialize it as a swap area. mkswap modifies the specified file, so be sure to verify that the path is the file you just created.
sudo ls -lh /swap-manualfactory
sudo chmod 600 /swap-manualfactory
sudo mkswap /swap-manualfactory
Only execute the next step if each previous step succeeds. After activating with the command below, verify that the new path appears in the list.
sudo swapon /swap-manualfactory
swapon --show
free -h
The displayed file size may not exactly match the size of the created file due to the swap header and other factors. If swapon fails, check the error and do not add it to the boot settings. A swap file must not have holes, and depending on the file system, a file created with fallocate may not be suitable. The util-linux swapon manual explains the constraints for each file system.
Activating it after boot
Only add items to /etc/fstab after successfully activating it. Since this file also contains other mount settings, back it up before editing and ensure that the paths do not duplicate existing swap entries. If the backup path below already exists, choose a different new name.
sudo cp -a -n /etc/fstab /etc/fstab.before-swap
sudo nano /etc/fstab
Keep the existing content in the editor and add the line below, then save. If you created an example path under a different name, use the actual path.
/swap-manualfactory none swap defaults 0 0
After saving, check the file contents again, and on systems where available, review the settings with the following command. This check does not guarantee successful reboot.
sudo findmnt --verify --verbose
sudo systemctl daemon-reload
daemon-reload causes systems using systemd to reload the changed settings. If there are validation errors, correct them and ensure you did not accidentally change other entries. Reboot at an appropriate maintenance time and then confirm with swapon --show that the file has been reactivated. You can also check the registration and validation procedures in Red Hat Swap Management Documentation.
Deactivate and revert
Before turning off the swap, check the usage and available memory with swapon --show and free -h. If there is not enough space to move the pages in use to other memory or swap, disabling may fail or put a strain on the system. On a system in operation, proceed after securing available capacity and maintenance time.
sudo swapoff /swap-manualfactory
swapon --show
Make sure the target item is no longer in the active list. To prevent it from being reactivated at boot, only remove the line added by this operation in /etc/fstab, and in a systemd environment, run sudo systemctl daemon-reload. To delete the swap file, first confirm that deactivation succeeded and the added configuration line was removed, then delete only the file you created. Leave other swap items or existing settings as they are.




