How to create Swap file Rhel 8 / Centos 8









Creating a swap file in Rhel 8 /
Centos 8

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 our
only option is to create a swap file.

inspect active swap space:

·       
cat /proc/swaps

·       
free -h

Free -h this
command will show you all the used and free space of RAM and Swap area.

Creating
a swap file

Create an empty file:

·       
# dd if=/dev/zero of=/swapfile bs=1M count=1024

·       
Syntax: dd if=<source file name> of=<target
file name> [Options]

·       
bs: Block Size describes how much data will be read
and written at one time.

·       
if: The Input File is the source of the data.

·       
of: The Output File is the destination for all the
data being read in from the input source.

Note: Replace count with the value equal to the desired
block size



Change the permission:

·       
# chmod 0600  
/swapfile


Set up the swap file with the command:

·       
# mkswap /swapfile

Now add to the system in real-time using the swapon
utility:

·       
# swapon /swapfile

Verify that the swap is active by using either the swapon
or the free -h command:

·       
# swapon

To enable the swap file at boot time, edit /etc/fstab
as root to include the following entry:


·       
/swapfile swap swap defaults 0 0

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



Removing a swap file


execute the following command to disable the swap
file.

·       
# swapoff -v /swapfile


Remove its entry from the /etc/fstab file
accordingly.


Regenerate mount units so that your system registers
the new configuration:


·       
# systemctl daemon-reload


Remove the actual file:



·       
# rm /swapfile


Dome :)

Comments

Popular posts from this blog

How to install AnyDesk on Rhel 8 / Centos 8

Huawei OLT how to Add into U2000 NMS

How to install caching only dns server on Rhel 8 / Centos 8