Open In App

How to Create a Swap File on Linux?

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Linux Operating System has some different features that are unique and not present in any other major operating system like Windows. The Linux Memory Distribution is one of them. The Memory Space in Linux is designed in such a way that the efficiency of the Memory Management could be the best.

The Linux Swap Memory is the key driver in this feature. From the Swap Memory or Swap Space in Linux, the Linux Swap File concept is developed. By default in Linux, the Swap File is not present. As a user, we have to Create a Swap File in Linux to utilize the Memory Management Feature at the full course.

In this article, we will discuss the required steps to Add Swap Files on Linux OS without creating any errors in no time.

Quick Preview to Create Linux Swap File:

  1. Execute the command to create a Swap File along with a certain memory space.
  2. We have to set the Permission Type of the Swap File as only the root user can create it using the chmod command.
  3. Now, the created file will be marked as the Swap File in Linux using the mkswap command.
  4. At last, the Swap File needs to be enabled with the swapon command.

Quick Highlights on Swap File on Linux:

  • The Swap File is part of the Swap Space concept in Linux.
  • The Swap File is created inside the HDD or SDD which is a type of memory extension.
  • Swap File works as the Virtual Memory Management Process.
  • The Swap File works a bit slower than the RAM.
  • When there is a space deficiency in RAM, the Swape Files are used.
  • The RAM can share infrequent files to Swap File to increase device speed.

Steps to Create Swap File on Linux

Step 1: Open the Linux Terminal & execute the following command. It will make the Memory Space for a new Swap File.

Command: sudo dd if=/dev/zero of=/<Enter Name of Swapfile> bs=1024 count=<Enter Size>

1--Create-Swap-File

Note: You have to enter the Memory Size. For that, there is a formula. If you want to create a 50 MB Linux Swap File Size on memory, you have to put 51,200 (50*1024) after the count. Here, we are creating a 1 MB Swap File, so the 1024 is provided.

Step 2: Now, enter the following Linux Command. It will provide permission for the Swap File to be accessed by the root user only.

Command: sudo chmod 0600 /<Provided Name>

2--Give-Permission

Step 3: Now as we Create Swap Partition, it is time to mark it as the Swap Space in the Linux Memory. For that purpose, the following command will be executed.

Command: sudo mkswap /<Provided Name>

3--Mark-File

Step 4: It is the last command yet necessary to Enable Swap File on Linux. Execute it on the Linux Command Line Tool.

Command: sudo swapon /<Provided Name>

4--Enable-File

Step 5: Now, execute the following command. It will help to verify the Created Swap File on Memory.

Command: cat /proc/swaps

5--Verify

These are the central commands you have to use to Start Swap File on Ubuntu or Linux Distributions. You have to execute each command mentioned here to Enable Swap Space Operation in Linux. Otherwise, the Swap File in Linux will be created, but the device can’t use it to store RAM Items as it is disabled.

Also Read

Frequently Asked Questions to Create Swap Files on Linux

How to Create Swap Files on Linux?

The steps required to create a Swap File on Linux are the following. Do execute them properly to create one.

  1. Execute the command sudo dd if=/dev/zero of=/swapfile bs=1024 count= <Enter Size>
  2. Now, run the command sudo chmod 0600 /swapfile to give permission.
  3. After that, the command sudo mkswap /swapfile will be used.
  4. To enable it, the command sudo swapon /swapfile will be entered.

What is the main command behind creating the Swap Linux File?

To create a Swap File on Linux, a series of commands is used. However, the main and important command for creating a swap file will be the swapon command. It is the command that enables the Swap File in the memory. If you fail to execute this command, the Swap File will not be enabled & the system can’t utilize it.

How to remove the Swap File on Linux?

To remove Swap Files on Linux, the following steps should be executed.

  1. Open Linux Terminal.
  2. Execute the command sudo swapoff -v /<Swap File Name>


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads