Axxellance blog logoAxxellanceBlog
How to Increase or Update a Swap Space on Linux
  • linux
  • server
  • ubuntu

How to Increase or Update a Swap Space on Linux

Michael ChukwuemekaSat, 30 Nov 2024 16:52:01 GMT 0

To update swap space on your Linux system, you can either increase or decrease the amount of swap space. This can involve adjusting the swap file size or switching between swap files and swap partitions. Below are the steps for updating swap space on Linux.

1. Increasing Swap Space (Using a Swap File)

If you want to increase the amount of swap space, you can either:

  • Increase the size of the current swap file
  • Create a new, larger swap file

Here’s how you can do both:

Option 1: Increase the Size of the Existing Swap File

If you want to increase the size of an existing swap file, you can do so with the following steps:

  1. Turn off the current swap: First, you need to disable the current swap file (ensure you're not using swap space while doing this).

    sudo swapoff /swapfile
    
  2. Resize the swap file: Use fallocate to increase the size of the swap file. For example, to increase the swap file by 2GB:

    sudo fallocate -l +2G /swapfile
    
    • Alternatively, if fallocate doesn't work, use dd:
      sudo dd if=/dev/zero of=/swapfile bs=1M seek=4096 count=2048 status=progress
      
      This will increase the swap file size by 2GB.
  3. Make the swap file bigger (for dd users or if needed): If you resized the file using dd, you'll need to tell the system about the updated size.

  4. Set the correct permissions: Ensure the permissions are set correctly for security:

    sudo chmod 600 /swapfile
    
  5. Reformat the swap file: Reinitialize the swap file with the new size:

    sudo mkswap /swapfile
    
  6. Re-enable the swap file: Turn the swap file back on:

    sudo swapon /swapfile
    
  7. Verify the new swap space: Use free -h or swapon --show to verify that the swap space has been increased:

    free -h
    

Option 2: Create a New Swap File

If you want to create a new, larger swap file:

  1. Create a new swap file (e.g., 8GB):

    sudo fallocate -l 8G /newswapfile
    
  2. Set the correct permissions:

    sudo chmod 600 /newswapfile
    
  3. Set up the new swap space:

    sudo mkswap /newswapfile
    
  4. Enable the new swap:

    sudo swapon /newswapfile
    
  5. Remove the old swap file (optional): If you no longer need the old swap file:

    sudo swapoff /swapfile
    sudo rm /swapfile
    
  6. Update /etc/fstab: To make the new swap file permanent, add it to /etc/fstab:

    sudo nano /etc/fstab
    

    Add the following line (for the new swap file):

    /newswapfile none swap sw 0 0
    

2. Decreasing Swap Space (Reducing the Size of a Swap File)

To decrease swap space, you can either shrink the swap file or remove it entirely:

Option 1: Shrinking the Swap File

  1. Turn off the swap:

    sudo swapoff /swapfile
    
  2. Resize the swap file: You can use fallocate or dd to reduce the size of the swap file. For example, to reduce it to 2GB:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
    

    You may also use the fallocate command, though resizing a swap file using fallocate might be tricky. It's typically better to create a new swap file with the desired size.

  3. Reformat and enable the swap file: After resizing, format and re-enable the swap file:

    sudo mkswap /swapfile
    sudo swapon /swapfile
    
  4. Verify: Run free -h or swapon --show to verify the new swap space.

Option 2: Removing the Swap File (Completely Removing Swap)

  1. Turn off swap: Disable the swap file:

    sudo swapoff /swapfile
    
  2. Remove the swap file: Delete the swap file:

    sudo rm /swapfile
    
  3. Update /etc/fstab: If the swap file was mentioned in /etc/fstab, remove the corresponding entry to avoid errors during boot:

    sudo nano /etc/fstab
    

    Remove or comment out the line referring to the swap file.

  4. Verify: Use swapon --show or free -h to ensure that the swap space is no longer active.

3. Adding Swap Partition (If Desired)

If you want to use a dedicated swap partition instead of a swap file, you'll need to create a swap partition. This typically involves:

  1. Creating a partition using fdisk or parted.
  2. Formatting the partition as swap (mkswap).
  3. Enabling the swap partition (swapon).
  4. Adding it to /etc/fstab to make it persistent across reboots.

This is a more complex process and is often not necessary unless you need the performance benefits of a dedicated swap partition.

Conclusion

You can increase swap space by either enlarging the existing swap file or creating a new one. Conversely, you can decrease swap space by resizing or removing swap files. Always remember to update /etc/fstab for persistent changes, and ensure the correct permissions are set for the swap files.