Skip to content

Increasing Swap Space Using a Swap File on Linux

Follow these steps to increase the swap space on your Linux system by creating a swap file.

Step 1: Turn Off the Existing Swap

First, you need to turn off the existing swap. This can be done with the following command:

sudo swapoff -a

Step 2: Create a New Swap File

Next, create a new swap file. For example, to create an 8GB swap file, use:

sudo dd if=/dev/zero of=/swapfile bs=1G count=8

Adjust the count value to change the size of the swap file.

Step 3: Set the Correct Permissions

Set the correct permissions for the swap file to ensure that only the root user can read and write to it:

sudo chmod 0600 /swapfile

Step 4: Set Up the Swap Area

Prepare the file to be used as swap:

sudo mkswap /swapfile

Step 5: Enable the Swap File

Turn the swap on:

sudo swapon /swapfile

Step 6: Make the Swap File Permanent

To ensure the swap file is used after a reboot, add the following line to /etc/fstab:

/swapfile none swap defaults 0 0

Comments