Steps to Add Swap Partition On Linux

Add Swap Partition On Linux

Swap Partition:

Swap is extra space which is used when the physical memory (RAM) running out of space or full. When the system needs extra memory and the RAM is full that time inactive pages in RAM memory will move to the swap storage.
You can also call Swap as another small RAM which helps to boost performance while physical ram running out of space.

Benefits of SWAP memory:

  • Provide extra space when physical memory full
  • Move not-so-needed items from high-speed memory
  • Allow to hibernate which means without swap hibernation is not possible on Linux.

Disadvantages:

  • Consume disk space on server
  • Not necessarily improve performance

Let's check the steps to create or add new Swap partition on Linux systems.

1. Check for Swap Memory

Before we start with the swap creation process use below command to make sure no swap memory is available on the system.

root@ip:~# free -m
total used free shared buffers cached
Mem: 2008 1934 74 96 121 1192
-/+ buffers/cache: 620 1388
Swap: 0 0 0

Swap showing "0" which means no swap partition available on the system.

2. Check Available Disk Space

You need disk storage available on the system to create a swap partition. use below command to check disk space and check for enough space on disk.

root@ip:~# df  -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda2 7.8G 2.9G 4.5G 40% /
udev 10M 0 10M 0% /dev
tmpfs 402M 41M 362M 11% /run
tmpfs 1005M 0 1005M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1005M 0 1005M 0% /sys/fs/cgroup
/dev/xvdb 30G 7.5G 21G 27% /data

I have 21GB disk space available on /data partition.

3. Create Swap

You can use any amount of disk space to create a swap file. But mostly we will use space as per the RAM space or double of RAM memory. I have 2GB of RAM so I will create 4GB of Swap memory. For creating a swap file, we are using "dd" command.

$ dd if=/dev/zero of=/data/swapfile bs=2048M count=2

In above command, I have created swapfile under "/data" directory and using block size 2048M & a count is "2" which means dd will run count of 2 and create swapfile with 4GB of size.

Another way, sometimes using huge block size produce error so you can use below command. where you will be using block size "1M" and a count is 4096. This "dd" command also create a swapfile with "4GB" of space.

$ dd if=/dev/zero of=/data/swapfile bs=1M count=4096

4. Use mkswap command

Now run mkswap command on swapfile which will tell your system to create swap space.

$ mkswap /data/swapfile

Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=e2f1e9cf-c0a9-4ed4-b8ab-714b8a7d6944

5. Enable Swap

Now we have swap space available on the system. Make it enable using swapon command.

$ swapon /data/swapfile

6. Change Permission

For the security reasons change swapfile permission and only give read/write permission to root user. So swapfile will not accessible by any other user.

$ chmod 600 /data/swapfile

7. Add Swap in Fstab

Our swapfile is now enabled but it will not work when we reboot the system. To make the swapfile permanent add below entry in "/etc/fstab" file.

vim /etc/fstab

/data/swapfile none swap sw 0 0

save & close the file.

I hope you like the article if you find any difficulties to add swap partition then please do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles.

Leave a Reply

Your email address will not be published. Required fields are marked *