Thursday, April 18, 2019

How to add more swap space as a swapfile on RHEL or CentOS 7 ?

How to add more swap space as a swapfile on RHEL or CentOS 7

What is swap?

Swap space on a disk is used when the amount of physical RAM is full in the Linux system. When the system runs out of RAM, inactive pages are moved from the RAM to the swap space. Swap space can be either a dedicated swap partition or a swap file. So here we will see how you can add more swap space as a swap file.

In this article we will see how we can add more swap space using a swapfile.

Steps :

Let's check how much swap is available on the system currently:

[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.9G   0B   -2


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        414M        1.1G         14M        2.2G        3.0G
Swap:          3.9G          0B        3.9G


Step 1 : Create a swapfile, which can be used as swap memory:

[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1G count=2
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB) copied, 56.732 s, 37.9 MB/s


OR you can use "fallocate" command to create a new swapfile:


[root@localhost ~]# fallocate -l 2G /swapfile


[root@localhost ~]# ls -lkh /swapfile
-rw-r--r--. 1 root root 2.0G Apr 18 17:22 /swapfile



Step 2 : Ensure that only the root user have read and write permissions on the /swapfile:

[root@localhost ~]# chmod 600 /swapfile


Step 3 : Create swap on /swapfile :

[root@localhost ~]# mkswap /swapfile
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=97900239-4711-4cfd-90a2-e46be60a34e4


Step 4 : Activate swap on the file :

[root@localhost ~]# swapon /swapfile


Step 5 : To make this swap file persistent across reboot, add an fstab entry:

# vi /etc/fstab

/swapfile swap swap defaults 0 0

save and exit.


Verify if this new swap file is active:

[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.9G   0B   -2
/swapfile file        2G   0B   -3


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        418M        1.1G         14M        2.2G        3.0G
Swap:          5.9G          0B        5.9G

The new swapfile is now added as a swap memory.

Removing swapfile gracefully:


Step 1 : Deactivate swap on /swapfile:

[root@localhost ~]# swapoff -v /swapfile
swapoff /swapfile

Step 2 : Remove swap entry from /etc/fstab file:


Step 3 : Delete /swapfile :

[root@localhost ~]# rm -rf /swapfile


Verify if the /swapfile is deactivated and removed:

[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO

/dev/dm-1 partition 3.9G   0B   -2


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        415M        1.1G         14M        2.2G        3.0G
Swap:          3.9G          0B        3.9G


No comments:

Post a Comment