Extend Swap Space using Swap file in Linux

We may face an issue with insufficient swap partition space on our Linux servers and we aren't able to extend it using swap partition due to unavailability of free partitions on the disk. In such cases we can extend or increase swap space using a swap file created manually by using a few commands:

Here are the steps to extend Swap Space using a swap file in Linux:

First, we need to check the size of existing swap space/partition using the command like 'free -m' or 'swapon -s'

  1. Create a swap file with a size of 1 GB using 'dd' command:
    # dd if=/dev/zero of=/swap_file bs=1G count=1
    1+0 records in
    1+0 records out
    1073741824 bytes (1.1 GB) copied, 414.898 s, 2.6 MB/s

    Here you can replace the values of ‘bs‘ and ‘count‘ fields according to your requirement.

  2.  We need to secure the newly created swap file with the permission: 644.
    # chmod 600 /swap_file

  3. Make the swap area on the file using 'mkswap' command like the following:
    # mkswap /swap_file
    Setting up swapspace version 1, size = 1048572 KiB
    no label, UUID=e8b4ac59-c90a-4fk3-ja9d-d92dbb7sd3f3fb

  4. Then we need to add the following entry to the 'fstab' file so that the swap file will become persistent across every reboot.
    /swap_file  swap  swap  defaults  0 0

  5. Enable the swap file using ‘swapon’ command:
    # swapon /swap_file

  6. Now we can check if the swap space is increased or not: 
    # free -m
  •  
  • 205 Users Found This Useful
Was this answer helpful?

Related Articles

How To Install and Use Docker on Ubuntu 16.04

Introduction Docker is an application that makes it simple and easy to run application processes...

Backup and Restore cPanel Accounts via SSH

a. How to create a backup of a cPanel Account via SSH?   1. Log-in to the SSH as the Root...

Change Main IP of the server :: Vesta Panel

If we are changing the main IP of a server installed with Vesta panel, we can use the following...

Change permissions using find command

On a Linux server, if you are in need of changing the permissions of a bulk amount of files or...

Change the time of your vps/dedi

It's very easy, you first need to remove the current time file rm /etc/localtime and replace it...