How to Extend Swap Space using Swap file in Linux

Swap space is a dedicated storage space in the disk either in the form of partition or a file. Swap space comes into the picture when Linux kernel wants to allocate more memory to processes by moving infrequently used processes or tasks into the swap space.

There are some scenarios where our Linux box is running out of swap space, so in that case we can extend the swap space either via swap partition or via a swap file. In this post we will learn how to extend swap space using a swap file in Linux.

Steps to extend Swap Space using Swap File in Linux

Let’s first check the size of existing swap space / partition using the command like ‘free -m‘ and ‘swapon -s


In my case, swap partition is of 2 GB. So we will be extending swap space by 1 GB.

Step 1) Create a swap file of size 1 GB

Run following dd command to create a file of size 1GB,

[root@linuxtechi ~]# 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
[root@linuxtechi ~]#

Replace the value of ‘bs‘ and ‘count‘ according your requirement.

We can also use fallocate command to create a file, example is show below.

$ sudo fallocate -l 1G /swap_file

Step 2) Secure the swap file

Secure the swap file by setting the permissions as ‘600’, run beneath chmod command,

[root@linuxtechi ~]# chmod 600 /swap_file

Step 3) Enable the Swap Area on Swap File

Use mkswap command to enable swap area on the file, run

[root@linuxtechi ~]# mkswap /swap_file
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=f7b3ae59-c09a-4dc2-ba4d-c02abb7db33b
[root@linuxtechi ~]#

Step 4)  Add the swap file entry in fstab file

Add the below entry in the fstab file so that swap file become persistent across every reboot. To add below entry either use echo command or vi editor to edit fstab file.

# echo "swap_file  swap   swap   defaults   0 0" >> /etc/fstab
$ vi /etc/fstab/swap_file  swap  swap  defaults  0 0


Step 5) Extend Swap Space

Run swapon command to enable swap space on the file and which will ultimately extend the swap space,

[root@linuxtechi ~]# swapon /swap_file

Step 6) Now verify the swap space

Run ‘free -m‘ or ‘swapon -s‘ commands to verify swap space.


Note: To disable the swap file for any troubleshooting point of view, use swapoff command as shown below and to re-enable swap file then use swapon command as shown in step 5.

[root@linuxtechi ~]# swapoff /swap_file
[root@linuxtechi ~]#

That’s all from this post, I hope you have found it informative. Please do not hesitate to share your valuable feedback and queries in below comments section.

2 thoughts on “How to Extend Swap Space using Swap file in Linux”

  1. Use fallocate, not dd, for creating /swap_file . It’s faster and doesn’t require an enormous memory buffer.
    sudo fallocate -l 4G /swap_file

  2. The mkswap manpage has a warning NOT to use fallocate on most systems:

    “Note that a swap file must not contain any holes. Using cp(1) to create the file is not acceptable. Neither is use of fallocate(1) on file systems that support preallocated files, such as XFS or ext4, or on copy-on-write filesystems like btrfs. It is recommended to use dd(1) and /dev/zero in these cases. “


Leave a Comment