How to create a Linux Swap File
Introduction swap
Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition or a swap file. In most cases, when running Linux on a virtual machine, a swap partition is not present, so the only option is to create a swap file.
Check swap
Before creating swap we need to see if our server has swap space available. We can have multiple swap partitions. By running the following command:
swapon -s
If the results return no information, it means that our system does not have a swap partition. We can also check the swap partition with the free utility that shows us the overall memory usage of the system.:
free -h
Check disk space
Before creating swap we should know about current disk usage. Using lathe little df to check:
df -h
How to add Swap File
Follow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 1G with 2G
Step 1: First, create a file that will be used as the swap space. How to do to create a swap file we use the fallocate command. This command creates a file of preallocated size. Create a 1G file by doing the following:
fallocate -l 1G /swapfile
If faillocate is not installed or if you get an error message saying fallocate failed: Operation not supported then you can use the following command to create the swap file:
dd if=/dev/zero of=/swapfile bs=1024 count=1024
You can change count=1024 with another value to create a swap file with the size you want.
After creating the swap file we need to check the swap file size with the ls command:
ls -lh /swapfile
Step 2: Make sure that only the root user can read and write the swap file. Do the following:
chmod 600 /swapfile
ls -lh /swapfile
Step 3: Use the mkswap utility to set up the file as Linux swap area:
mkswap /swapfile
Step 4: Enable the swap with the following command
swapon /swapfile
To make the change permanent open the /etc/fstab
file and append the following line:
vi
/etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Apr 11 21:36:46 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=8c1d2c4f-13d0-4ec9-ae3d-f706311400ad /boot xfs defaults 0 0
/swapfile swap swap defaults 0 0
Step 5: To verify that the swap is active, use either the swapon or the free command as shown below:
swapon --show
free -h
Create swap on partition
Step 1: We can create a new partition /dev/sdb1 with a capacity of 2.9GB. Use the command fdisk -l to check if the partition we created is there:
fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000acf99
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1025b50f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6097151 3047552 82 Linux swap / Solaris
Step 2: Use the partprobe command to have the kernel re-read the hard drive partition:
partprobe /dev/sdb1
Replace /dev/sdb1 with your disk name.
Step 3: Set up a swap partition on the file. Use the following command:
mkswap /dev/sdb1
Step 4: Set automatic swap to be activated every reboot
We need to set up swap to be automatically enabled every reboot by using vi editor adding the file /etc/fstab. We need to enable the following:
vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Apr 11 21:36:46 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=8c1d2c4f-13d0-4ec9-ae3d-f706311400ad /boot xfs defaults 0 0
UUID=961ec0db-ff53-4f23-8b55-c52f9e48e3d0 swap swap defaults 0 0
To get the UUID of the partitions we do the following:
blkid /dev/sdb1
/dev/sdb1: UUID="961ec0db-ff53-4f23-8b55-c52f9e48e3d0" TYPE="swap"
Step 5: Our swap file is now ready to be used as a swap partition. We can start using it using the following command:
free -m
swapon -a
After creating the swap partition we need to check if our system reports the swap partition:
swapon -s
Filename Type Size Used Priority
/swapfile file 4044 0 -2
/dev/sdb1 partition 3047548 0 -3
[root@greencloudvps ~]# free -m
total used free shared buff/cache available
Mem: 972 127 700 7 143 682
Swap: 2980 0 2980
How to adjust the swappiness value
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60. You can check the current swappiness value by typing the following command:
cat /proc/sys/vm/swappiness
While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.
For example, to set the swappiness value to 10, you would run the following sysctl command:
sysctl vm.swappiness=10
To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
vi /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
vm.swappiness=10
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
How to remove Swap File
If for any reason you want to deactivate and remove the swap file, follow these steps:
Step 1: To deactivate swap we do the following syntax:
sudo swapoff -v /swapfile
Step 2: Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
Step 3: Finally, delete the actual swapfile file using the rm command:
rm /swapfile