How to Create a Swap File on Linux?
Last Updated :
23 Jul, 2025
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:
- Execute the command to create a Swap File along with a certain memory space.
- We have to set the Permission Type of the Swap File as only the root user can create it using the chmod command.
- Now, the created file will be marked as the Swap File in Linux using the mkswap command.
- 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>

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>

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>

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>

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

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
Similar Reads
How to Attach a Swap Partition to Linux? Virtual memory in Linux OS is RAM + swap space. It is used when memory usage of the system exceeds a certain point then swap space is used all the idle processes are shifted to swap and new processes are assigned to RAM. Now how to allocate swap space ideally swap space should be twice RAM size for
3 min read
How to Create a File in the Linux Using the Terminal? In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read
How to Check Swap Space in Linux Swap space is like an extra space in your computer's memory. When your computer's main memory (RAM) gets full it uses this extra room (swap space) to store things that aren't being used right now. This extra room is located on your computer's hard drive. Keeping an eye on how much of this extra room
5 min read
How to Permanently Disable Swap in Linux? Swapping or swapping space is a physical memory page placed at the top of a disk partition or a special disk file that is used to expand a system's RAM as the physical memory fills up. Inactive memory pages are often dumped into the swap area when no RAM is available, using this method of expanding
3 min read
How to Clear RAM Memory Cache, Buffer and Swap Space on Linux As you use your Linux system, it constantly manages memory to keep your system running smoothly. Over time, however, certain parts of the memory like cache, buffers, and swap space can build up and slow things down. In this article, weâll explore how to clear RAM Memory Cache, Buffer, and Swap Space
6 min read
How to create a RAM disk using Node.js ? A RAM disk is a virtual disk device that stores data on a portion of a computer's RAM. It has faster read and write speeds than conventional hard drives or solid-state drives because data can be accessed and written to and from RAM much faster. We'll look at how to make a RAM disk with Node.js, a po
3 min read