How to quickly create swap files on Linux

Swap is the space on the disk that is used when the physical RAM is full. When the Linux system runs out of RAM, the inactive pages are moved from the RAM to switching space.

Swap space can take the form of a private swap partition or swap file. In most cases, when running Linux on a virtual machine, the swap partition is not present, so our only option is to create a swap file.

We hope that you will open the Linux distribution console to follow the guide on this page so that you better understand how to create a Swap file in a Linux terminal.

We also recommend that you test any online tutorials or guides on a virtual machine (vmware or virtualbox) before deploying to a production server, so that you don’t mess with your running system when something goes wrong.

You can learn how to install VMware on Ubuntu, CentOS, and VirtualBox on Ubuntu, Fedora and CentOS.

This tutorial was tested on Linux systems with Ubuntu 18.04 and CentOS 7 but should work with other Linux distributions.

How to create the swap file

Follow these steps to add 1 GB of swap to the server. If you want to add 2 GB, replace it 1G With 2G.

  1. Create a file that we’ll use in Swap
    sudo fallocate -l 1G /swapfile

    if fallocate Not installed or if you receive an error message stating fallocate failed: Operation not supported Then you can use the following command to create a swap file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  2. Set the permissions so that only the root can write and read the swap file. To set the correct permission type, use the command:
    sudo chmod 600 /swapfile
  3. Set up the Linux swap region. Use the utilities mkswap To set the file as Linux swap region
    sudo mkswap /swapfile
  4. Activate the swap file with the following command:
    sudo swapon /swapfile

    To make permanent changes, open the file /etc/fstab And add the following line:

    sudo nano /etc/fstab
    /swapfile    swap    swap   defaults   0   0
  5. Check the status of the swap. To verify that the swap is active, we can use swapon Or orders free As shown below:
    sudo swapon --show
    NAME      TYPE  SIZE   USED PRIO
    /swapfile file 1024M 507.4M   -1
    sudo free -h
                  total        used        free      shared  buff/cache   available
    Mem:           488M        158M         83M        2.3M        246M        217M
    Swap:          1.0G        506M        517M

Adjust the swap value

Swappiness is the Linux kernel property that determines how often the system uses swap space. A swap can have a value between 0 and 100.

A lower value will make the kernel try to avoid swapping whenever possible, while a higher value will make the kernel use the swap space more strongly.

The default swappiness value is 60. You can check the current swappiness value by typing the following command cat

cat /proc/sys/vm/swappiness
60

While a swap value of 60 is sufficient for desktop computers, you may need to set a lower value for production servers.

For example, to set the swap value to 10, type:

sudo sysctl vm.swappiness=10

To make this parameter permanent, add the following line to the file /etc/sysctl.conf:

sudo nano /etc/sysctl.conf
vm.swappiness=10

The optimal swap value depends on system workload and how memory is used. You will need to adjust these parameters little by little to find the optimum value that suits your needs.

How to delete swap files

If, for some reason, you want to disable and delete the swap file, follow these steps:

First, disable swap with the following command:

sudo swapoff -v /swapfile

Delete the swap file entry /swapfile swap swap defaults 0 0 Of files /etc/fstab.

Finally, delete the swapfile with the command rm:

sudo rm /swapfile

Conclusion

You learned how to create a swap file and enable and configure swap space on your Linux system.

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *