Server management training Learn how to create a swap space on a Linux operating system

22.03.2021

Swap space on Linux systems: A primer



Swap space in Linux is like virtual memory in Windows, whose job is to help improve the performance of the operating system in times of need. When a computer's RAM is fully occupied, swap memory acts as an auxiliary RAM that uses hard disk space. It should be noted that the swap space or swap memory can not have the same speed and function as RAM, but in emergencies when RAM is under pressure can prevent the operating system to hang. Accordingly, the existence of swap space in Linux is a requirement and we must allocate some hard disk space to it.

How much to allocate to Swap memory?

This is completely optional and usually the more the better. For example, if our computer or server RAM is 16 GB, if the hard disk space is not limited, it is better to allocate about the same amount or half of it to the swap space. SSD / NVMe drives have now replaced older hard drives, which are much faster but still slower than RAM, so it can be concluded that even with high-speed drives, some of it is not dedicated to swap memory. It had the same performance power as RAM.

Actions before starting work

Due to the fact that in the Linux operating system we use a series of commands to create a swap space, it is recommended that you back up your important data before any action to access them in case of problems. It should also be noted that to do this we need root access, which is the highest level of access in the Linux operating system.

Create Swap space on CentOS operating system

First, log in to Linux with the root user. If you are using a server and your access is in SSH, you can use the SSH login tutorial.

Then, with the following command, remove a certain amount of hard disk space, which is considered below the amount of 8 GB. Note that the last number is the amount of space and change the amount according to your needs.

dd if = / dev / zero of = / swapspace bs = 1024 count = 8388608

Which is calculated as follows:

  • 8 GB equals 8192 MB based on: 1024 × 8
  • 8192 If multiplied by 1024, the value is based on KB, which is the size of each block of space.
  • So the number 8388608 KB is equal to 8192 MB and 8 GB.

Next, we have to give the user ownership of this space to the root user with the following command.

chown root: root / swapspace

With the following command, we determine the appropriate access level of this space.

chmod 0600 / swapspace

And we enter the following command to determine the structure of this space and convert it to Swap:

mkswap / swapspace

And to activate it, enter the following command.

swapon / swapspace

Finally, with the following command, edit the fstab file in the etc path

nano / etc / fstabIf you encounter an error, enter the command yum install nano and continue the steps after installing nano.

And add the following value to the content of the fstab file in edit mode, which is actually the introduction of Swap memory to the system.

/ swapspace none swap sw 0 0

So far we have been able to do the job successfully and you only need to reboot the system once.

After booting the operating system, you can see the active memories of the system with the following command:

free -m

Which refers to the main RAM memory and Swap memory.

In the line above, each field means:

  • / mnt / swapfile - device / file name
  • swap - defines device mount point
  • swap - specifies the file-system type
  • defaults - describes the mount options
  • 0 - specifies the option to be used by the dump program
  • 0 - specifies the fsck command option

6. To set how often the swap file can be used by the kernel, open the /etc/sysctl.conf file and add the line below.

Note that the default value of how frequent swap space can be used is 60 (maximum value is 100 ). The higher the number, the more frequent swap space utilization by the kernel. When the value is set to 0 , the swap file will only be used if the operating system has fully utilized memory here.


© 2021 Antonín Jelen. Všechna práva vyhrazena.
Vytvořeno službou Webnode
Vytvořte si webové stránky zdarma! Tento web je vytvořený pomocí Webnode. Vytvořte si vlastní stránky zdarma ještě dnes! Vytvořit stránky