Open In App

How to Attach a Swap Partition to Linux?

Last Updated : 24 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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)

Block-devices-attached

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)

fdisk-command-checking-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)

fdisk-interactive-mode

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)

choosing-partition-size

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)

swap-partition-defined

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)

/etc/fstab

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)

checking-new-swap-partition

Congrats you have created a new swap partition on your device 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads