Categories: HomeHow-to'sLinux

How to make a SWAP partition in Linux using the terminal !

Swap is an additional virtual memory used by the system when RAM is full. Linux divides it’s physical RAM into memory blocks, which are known as pages. These pages are transferred to swap memory as and when required. Swap is important for 2 reasons.

  • When system requires more memory than actual RAM size, then it uses swap memory to transfer less used pages.
  • At the time of start up, some applications are loaded into the RAM memory and they are never used again. System swap’s these pages into the swap memory.

How to create a swap:-

Check the existing partitions, enter

fdisk -l

Now, assuming that you’ve one hardisk installed by default.

fdisk  /dev/sda

Enter p to print the table, n for new partition.

Let’s make a swap memory of say 4Gb, enter +4G and then w to write.

Now we need to make the kernel aware of the change, let us assume that swap memory is sda6

partx -a /dev/sda

mkswap /dev/sda6

swapon /dev/sda6

Now, to list all the swap files

swapon -s

Finally make an entry in the fstab (file system table)

vi /etc/fstab

/dev/sda6       swap        swap      defaults       0 0

Finally mount all the partitions, enter

mount -a

That’s it you’ve now created a new swap partition.

Njoy !

[Note:- If you’re using RHEL or CentOS 5 or 6.3, this command(partx) won’t work, try partprobe and then init 6 after making a new partition ie. after entering n and giving the file size +100G, or any size as per your wish].

Akash Angle

I am a Full time Linux user who has quit using Windows for unknown reasons, making my life truly open source.

Recent Posts

Get the most juice from your ISP/router — setting MTU size & other handy tweaks

This is not an ad-vocation by any means for TP-link branding, however a real life…

7 months ago

How to make any Android phone up-to 3x faster – Developer options unleashed

First of all we need to hit the kernel version on any Android device. You…

7 months ago

systemd-resolve command not found in Ubuntu Desktop

Use resolvectl status instead. It's like something deprecates and suddenly things get broken! In systemd…

12 months ago

How to exclude multiple directories with rsync?

Geeky question: This is what people and friends have tried: rsync -arv --exclude "/home/john/.ccache:/home/ben/build" /home/john…

12 months ago

How to resolve apt-get -f not working

You might encounter this error which appears to be very common on Debian based Distro's…

12 months ago

How to install Broadcom STA wireless drivers on Kali Linux

PS: This article is for only Kali Linux users, that too having a Broadcom Wireless…

1 year ago