Main menu

Creating and managing swap file in Linux

swap is a virtual memory inside your Disk which holds inactive memory pages from RAM to improve system performance.
There are many reasons why you want to add swap space in Linux. You have just upgraded your Systems RAM and want to adjust the swap file size according it or you have not created any swap space during installation or simply you don’t have any free space in your system to create a swap file system.
Creating a swap file is as simple as creating a swap partition. Rather then using a dedicated partition we can use a file which act as a swap file system.

Step1: crate a file with dd command

# dd if=/dev/zero of=/.swapfile bs=1M count=2048
  • if=/dev/zero: /dev/zero device file provides null characters to build file.
  • of=/.swapfile: Read from /dev/zero and write to .swapfile. it will crate a hidden .swapfile in root of filesystem.
  • bs=1M: Block size of 1MB, it will read and write 1MB at a time.
  • count=2014: 1MB size block will be crated 2048 time means 2GB.

Step2: Convert file to a swap space

Type the following command to set up a Linux swap area in a file:

# mkswap /.swapfile01

Step3: Enabling or activating swap space

# swapon /.swapfile01
swapon: /.swapfile01: insecure permissions 0644, 0600 suggested.

Step4: Securing swap file

# chmod 0600 /.swapfile01

Step5: Update /etc/fstab for boot time

To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file. Open this file using a text editor such as vi:

# vi /etc/fstab

Append the following line:

/.swapfile01 none swap sw 0 0

Save and close the file. Next time Linux comes up after reboot, it enables the new swap file for you automatically.

Verify and check swap usage

# free -m
              total        used        free      shared  buff/cache   available
Mem:           7822        1878        1670         130        4273        5467
Swap:          8191

swap file priority

In Linux system there can be multiple swap file and partitions. Might be you have an SSD partition for swap and another swap file in Magnetic disk. You want to prioritize that SSD swap must be use before Magnetic disks swap. You can prioritize swap with priority values between 0 to 32767.

Checking the swap files status

# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 1011460 -1
/.swapfile01                            file    6291452 0       -2

Changing the priority to 100, so it’s priority will be higher then other swap partition.

# swapon -p 100 /.swapfile01

Verify that priority has changed

# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 1011460 -1
/.swapfile01                            file    6291452 176     100

Configuring fstab for priority information

/swapfile none swap sw,pri=60 0 0

It’s Done !!

FacebookTwitterGoogle+RSS