Create Linux Swap space using a file | 1 Easy guide

Introduction

In today’s guide, let see how to create a Linux swap space using a file. This guide may help for newbies to Linux those who are looking to create a temporary swap space while running out of swap.

Creating a swap space using a file will not give a good performance. However, it will be helpful for a quick resolution when your server runs out of swap.

Managing a swap which created on top of a physical disk has some risk on a busy production server. If we don’t have any additional disks and you need to allocate more swap, then you can create a file somewhere on our filesystem and use that file for swap space.

Creating a Swap space on a raw disk and Logical volume are done in the same procedure. Now tet’s see how to create the Linux swap on a file.

Creating a file with DD

The following dd command example creates a swap file with the name “swapfile” under /root directory with a size of 2GB.

# dd if=/dev/zero of=/root/swapfile bs=1M count=2048
[root@gateway ~]# dd if=/dev/zero of=/root/swapfile bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 146.886 s, 14.6 MB/s
[root@gateway ~]#

List the created Linux Swap file

# ls -l /root/swapfile

Changing Permission

The Linux Swap file should be accessed only by root user for security reason. So, let’s change the permission for the created swap file.

# chmod 0600 /root/swapfile

Making Linux Swap

The actual swap creation starts here, Let’s use mkswap command to create the Swap space by marking on the file.

# mkswap /root/swapfile

Note down the UUID, later we could use it.

[root@gateway ~]# mkswap /root/swapfile 
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=f4cbc10e-edec-448d-937f-00bbfff2422c
[root@gateway ~]# 

Activating new Swap

Right after creating the swap, enable it.

# swapon /root/swapfile

Making Persistent

To make this swap file available as a swap area during reboots, add the following line to the /etc/fstab entry.

# vim /etc/fstab

Append the swap file location and type as shown below

/root/swapfile swap swap defaults 0 0

As a good practice, always use the UUID for FSTAB entries.

Checking Status

Verify whether the newly created Linux swap area is available for your use by running the status option.

# swapon -s

To see the Swap free size

# free -k

In the output of swapon -s command, the type column will say “file” if the swap space is created from a swap file.

[root@gateway ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	2097148	136960	-2
/root/swapfile                         	file    	2097148	0	-3
[root@gateway ~]#
[root@gateway ~]# cat /proc/meminfo | grep "^Swap"
 SwapCached:        10760 kB
 SwapTotal:       4194296 kB
 SwapFree:        4057336 kB
[root@gateway ~]#

Managing Linux Swap

To disable and enable all swap space use the below commands.

# swapon -a
# swapoff -a

We have done with creating a Linux Swap space using the file.

Conclusion

Just using a file we can quickly create the Linux swap space in Linux operating systems. To manage the swap this guide may help you. Subscribe to our newsletter for a future post. Your feedbacks are most welcome through below comment section.

Exit mobile version