Linux Administrator

How to Create a Linux Swap File

Written by Santosh Prasad

By default swap partition is not present on Cloud Servers, but you can add the swap to server by allocating a swap file. The performance of a swap file is similar to that of a swap partition while making it easier to control the swap size without re-partitioning a volume.

Swap space is a portion of a hard disk drive (HDD) that is used for virtual memory. Virtual memory is the use of space on a HDD to simulate additional main memory. Memory is used to hold portions of the operating system, programs and data that are currently in use or that are frequently used.

Create a Linux Swap File

Follow the below steps to create a swap file on Linux.

1. Login your server with root privileges and type the below command with count being equal to the desired block size. Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.

# dd if=/dev/zero of=/swap_file bs=1024 count=65536

2. Format the swap created swap file using below command:

# mkswap /swap_file

3. Follow the below command to enable the swap file immediately but not automatically at boot time.

# swapon /swap_file

4. Add this line to the end of /etc/fstab to To enable it at boot time.

# vim /etc/fstab
/swap_file swap swap defaults 0 0

The next time the system boots, it enables the new swap file.

Change Swappiness Value

To change the swappiness value edit /etc/sysctl.conf and add the following line.


Start with a value of 10 and increase if needed. A typical default value for swappiness is 60. The higher the number (up to 100), the more often swap is utilized.

5. Reboot the server to ensure that the changes go into effect.

In the above instructions creating swap file is world-readable. You should setup the correct permission to prevent the swap file from being world-readable. Follow the below command to change the permission.
# chmod 600 /swap_file

In most cases, the only user that needs access to the swap partition is the root user.

Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author


Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment