How to Attach a Swap Partition to Linux?
Last Updated :
24 Feb, 2022
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 example if RAM is 64KB then swap should be 128KB. This was the case when RAM sizes were small. For the latest computers, we know the minimum RAM is 2GB so swap space is less than twice of RAM due to a performance issue
According to fedora or Cent OS swap space documentation
Amount of system RAM |
Recommended Swap space |
2GB or less |
Twice the RAM |
Between 2GB to 8GB |
Same as RAM |
Between 8GB to 64GB |
0.5 times the RAM |
More than 64GB |
Workload dependent |
We will see an example of how to manage swap space. I am using vagrant and virtual box and using centos7 image instead of a proper Linux OS. (But you should use Linux machine instead of VM as they don’t come with a proper partition table)
Step 1: Open a terminal in your machine and start by typing the command below
lsblk (Used lsblk to show all my block devices attached on the machine)

Step 2: We are going to create a new partition of 150 MB swap to demonstrate. Log in as the root user to your system in terminal using sudo su
fdisk -l (Check the memory using fdisk -l to check the existing partition)

Step 3: Start by first creating a new space using fdisk command
fdisk /dev/<device name> (can be sda1 or sda2)
# You will be pushed to interactive mode
press n (Type n to create new space partition)

Choose the size of the partition
first sector: press enter (chooses default value)
last sector:+150M (Choose the size of space in the case 150 MB)

Step 4: Choose the type of partition we want to create
press t
Specify partition number you want as swap
Press enter (to select default)
press 82 (82 which is linux swap partition type you can also type L to check all the code)
press w (type w to write new partition to disk)

Step 5: After that, you will exit fdisk interactive user mode will be back in the terminal
type partprobe (to re-read the partition table and avoid a reboot)
mkswap /dev/sdaX (can be sda1 or sda2) (Define new partition created as swap partition to memory)
swapon /dev/sdaX (can be sda1 or sda2) (makes new swap partition online)
Step 6: It is necessary to edit /etc/fstab file so that change stays even after reboot and remain permanent
Vim /etc/fstab (I have used vim editor but you can use any editor for this according to your choice)

Add a line to the bottom of the file
/dev/sdaX swap swap defaults 0 0
And exit and save your changes
(Where X is your partition number)
Reboot your device and open terminal
free -m (Use free -m to check the new swap partition)

Congrats you have created a new swap partition on your device
Similar Reads
How to Create a Swap File on Linux?
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 L
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 Create Primary Partitions in Windows 10?
The Windows Operating System comes with a feature where an extensive amount of memory can be stored without any hassle. To make it user-friendly, the large Windows Memory Space gets divided into some parts. These parts are known as the Windows Memory Partition. As per the need, users can create thei
5 min read
How to Change Boot Options on Linux
Users are presented with boot options in the boot menu during the computer startup process. This menu displays all the available bootable partitions on the computer. Users must decide within a specified time frame which operating system (OS) they want to boot into. If no option is selected within th
5 min read
How to mount a partition of 5 GB in Windows Server?
Mount point is a directory in a file system where additional information is logically connected from a storage location outside the operating system's root drive or partition. You can use it to make a group of files in a file system accessible to a user or group. The mounting point can also be creat
3 min read
How to install Gparted partition editor on Linux
some tools areOn Linux systems, managing disk partitions can be challenging, especially for new users. Fortunately, some tools are easy to use which can make this procedure simpler. GParted, is a user-friendly partition editor that enables users to manage their disk partitions. In this article, we w
3 min read
How to get started with Garuda Linux?
Garuda Linux is a recent Linux distro that is primarily based on Arch Linux (a former Linux distribution). This distro was mainly created by keeping performance-centric tasks in mind, such as - Gaming, Video Editing, and Compiling huge amounts of code. Other than this, it also offers easy-to-use fea
9 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 Back Up and Restore Data in Linux?
Backing up and restoring data in Linux is essential for ensuring the security and integrity of your files and system. Whether you're safeguarding personal data or managing critical system files, having a reliable backup strategy is crucial. Linux offers various methods and tools for creating backups
4 min read
How to Install Damn Small Linux On a USB?
Damn Small Linux (DSL) is a distro of Linux which is famous for its compact size. It gives very basic functionality of an Operating System but takes very little space as well (about 100 MB). It can easily be installed on a USB drive and can be taken anywhere. It is also one of the world's smallest O
2 min read