Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»How to Create Swap in Linux

    How to Create Swap in Linux

    RahulBy RahulJanuary 22, 20203 Mins ReadUpdated:September 15, 2020

    Swap memory is a part of system permanent storage (harddisk). When the system found the physical memory of the system is full, then it automatically starts the utilization of Swap memory available on the system. In simple terms, swap memory is a part of the hard disk used as RAM on the system.

    If your system is facing issues like system running out of memory frequently and you don’t want to increase physical memory. Then this is the best idea to add Swap memory on your system. Remember, swap memory is good but much slower than physical memory. This tutorial will help you to create a swap file on the Linux system and use it as swap memory.

    How to Create Swap in Linux

    Here is the step by step tutorial to create a swap file on the Linux machine and configure in the system.

    1. Check System Swap

      Before start working, check if the system has already swap enabled. If there is no swap, you will get an output header only.

      sudo swapon -s
      

      Alternatively use free or top command to view swap memory status.

    2. Create Swap File

      Let’s create a file to use for swap in the system of the required size. Before making a file make sure you have enough free space on the disk. Generally, it recommends that swap should be equal to double of installed physical memory.

      My system have 2 GB physical memory installed. So I am creating a swap file of 4 GB using following command. Then set the proper permission on file:

      sudo fallocate -l 4G /swapfile
      chmod 600 /swapfile
      
    3. Make It Swap

      Now make this file as swap usable file using mkswap command.

      sudo mkswap /swapfile
      
    4. Enable Swap

      After that, enable the swap memory on your system using swapon command.

      sudo swapon /swapfile
      

      Now, again check that swap is enabled or not. You will see results something like below.

      sudo swapon -s
      
      Filename                Type        Size    Used    Priority
      /swapfile               file        4194300 0       -1
      
    5. Setup Swap Permanent

      Append the following entry in /etc/fstab file to enable swap on system reboot. Its a good idea to make a copy of this file before changes:

      cp /etc/fstab /etc/fstab.backup
      vim /etc/fstab
      
      /swapfile   none    swap    sw    0   0
      
    6. Setup Kernel Parameter

      Now change the swappiness kernel parameter as per your requirement. It tells the system how often the system utilizes this swap area.

      Edit /etc/sysctl.conf file and append following configuration in file.

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

      Now reload the sysctl configuration file

      sudo sysctl -p
      

    Disable Swap

    If you don’t need a swap file or need to increase swap file. You can disable an already active swap file on the system using the following command.

    swapoff /swapfile
    

    You can create a new larger swap file using the above steps or to disable permanently remove the entry from /etc/fstab file.

    Conclusion

    You have successfully created a swap file on your Linux machine and configured it as swap memory.

    swap swap memory swapfile
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Gradle on CentOS 7
    Next Article How To Install Mattermost Client on Ubuntu 18.04

    Related Posts

    (Resolved) userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms

    Updated:May 10, 20221 Min Read

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How To Enable SSH Server on Ubuntu 22.04

    Updated:April 22, 20222 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    10 Best Linux Video Players in 2022

    Updated:February 18, 20226 Mins Read

    How to Switch Python Version in Ubuntu & Debian

    Updated:April 22, 20223 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.