How to Make Swap File on Linux

How to Make Swap File on Linux

This guide is about How to Make Swap File on Linux. So read this free guide, How to Make Swap File on Linux step by step. If you have query related to same article you may contact us.

How to Make Swap File on Linux – Guide

A swap file allows Linux to simulate hard disk space as RAM. If your system runs out of memory, it will use swap memory and swap some memory for disk space. it frees up up working memory to serve more important processes. When main memory is free again, it stores data from disk. I recommend reading this article to learn more about swap in Linux. Traditionally, swap memory is used as a separate partition on the hard drive. When installing Linux, create a separate partition just for the swap. But that trend has changed in recent years. With the swap file, you no longer need a separate partition. You create a file in root and tell your system to use it as a swap space.

With a dedicated swap partition, resizing the amount of swap space is a nightmare and, in many cases, an impossible task. In the case of swap files, however, you can change the size as you wish. Swap memory is part of the system’s permanent memory (hard disk). When the system detects that the system’s physical memory is full, it automatically starts using the swap space available on the system. In simple terms, swap space is a part of the hard drive that is used as system memory.

How to Create swap file in Linux

  • Displays current swap space and RAM usage on Linux.
  • $ free -h
  • $swapon – summary
  • Then create a new file that you will use for swap space using the dd command:
  • # Example: 2 Linux Gib swap file #
  • $ sudo dd if=/dev/zero of=/swap-file count=2 bs=1GiB
  • Make sure to set up fix the Linux file system permissions for security reasons using the chmod command and the chown command:
  • $ sudo chmod -v 0600 / swap file
  • $ sudo chown -v root: root / swap-file
  • # List permissions #
  • $ls -l / swap-file
  • Run the following mkswap command to set up Linux swap area using a file:
  • Activating a swap space by typing the swapon command:
  • How to enable swap space file persistently on Linux

  • Please make sure our swap file is enabled after Linux system reboot. To make the change permanent, edit the /etc/fstab file:
  • Attach the following configuration directives:
  • / swap-file swap swap patterns 0 0
  • Save and close the file.
  • Final note

    I hope you like the guide How to Make Swap File on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

    We will be happy to hear your thoughts

    Leave a reply

    How to Guides, Best Lists, Top 10 and Tech News - Compsmag
    Logo