How to create Swap file Rhel 8 / Centos 8
Creating a swap file in Rhel 8 /
Centos 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.
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.
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
cat /proc/swaps
·
free -h
free -h
Free -h this
command will show you all the used and free space of RAM and Swap area.
command will show you all the used and free space of RAM and Swap area.
Creating
a swap file
a swap file
Create an empty file:
·
# dd if=/dev/zero of=/swapfile bs=1M count=1024
# dd if=/dev/zero of=/swapfile bs=1M count=1024
·
Syntax: dd if=<source file name> of=<target
file name> [Options]
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.
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.
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.
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
block size
Change the permission:
·
# chmod 0600
/swapfile
# chmod 0600
/swapfile
Set up the swap file with the command:
·
# mkswap /swapfile
# mkswap /swapfile
Now add to the system in real-time using the swapon
utility:
utility:
·
# swapon /swapfile
# swapon /swapfile
Verify that the swap is active by using either the swapon
or the free -h command:
or the free -h command:
·
# swapon
# swapon
To enable the swap file at boot time, edit /etc/fstab
as root to include the following entry:
as root to include the following entry:
·
/swapfile swap swap defaults 0 0
/swapfile swap swap defaults 0 0
The next
time the system boots, it activates the new swap file.
time the system boots, it activates the new swap file.
Removing a swap file
execute the following command to disable the swap
file.
file.
·
# swapoff -v /swapfile
# swapoff -v /swapfile
Remove its entry from the /etc/fstab file
accordingly.
accordingly.
Regenerate mount units so that your system registers
the new configuration:
the new configuration:
·
# systemctl daemon-reload
# systemctl daemon-reload
Remove the actual file:
·
# rm /swapfile
# rm /swapfile
Dome :)
Comments
Post a Comment