How to Create a Swap Partition On Linux

swap file
swap file

In this article, we are going to learn to create a Swap partition on Linux systems. This is very important to create a swap partition on Linux if we don’t have a swap partition created on Linux.

Before learning about the creation of a Swap file, Let’s understand first what a is swap file? and the advantages, disadvantages of a Swap file.

What is a Swap File?

The swap file is extra space that 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 file like another small RAM which helps to boost performance while physical ram running out of space.

Advantages of Swap Memory

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

Disadvantages

  • Consume disk space on a server.
  • Not necessarily improves performance

How to Create and Enable Swap File on Linux

Step 1. Check Swap is available or not

Before we start creating the Swap file on the system, use the below command to check Swap is already enabled or not on the system.

[email protected]:~# free -m
total used free shared buffers cached
Mem: 2008 1934 74 96 121 1192
-/+ buffers/cache: 620 1388
Swap: 0 0 0

The swap value is showing “0” which means the swap partition is not present on the system.

Step 2: Check Available Disk Space

As we mentioned earlier, Swap utilizing disk space on the server.

Before creating a swap file we need to check that enough disk space is available to create a swap partition.

Use the below command to check the available disk space on the system,

[email protected]:~# 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

Here, I have 21GB of space available on /data partition. I am going to create my swap file in /data partition. You can check and create a swap file in the available disk space partition.

Step 3: Create a Swap Partition

We can use any amount of space to create a swap partition. But as per the recommendation, the Swap partition will create as double of available RAM memory or available size of RAM memory. 

For example, If you have RAM of 2GB then you need to create a swap partition of 4GB.

I am going to create a swap of 4GB because I have 2GB ram on my system.

We are going to use the “dd” command to create a swap file,

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

In the above command, I have created a swapfile under the “/data” directory and using block size 2048M & a count is “2” which means dd will run count of 2 and create a 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 creates a swapfile with “4GB” of space.

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

Alternatively, use the fallocate command as follows.

$ fallocate --length 2GiB /data/swapfile
Step 4: Use mkswap command

mkswap command will help to create swap space on the system.

$ mkswap /data/swapfile

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

Now we have swap space available on the system. You can enable it using swapon command,

$ swapon /data/swapfile
Step 6: Change Swap file permission

For security reasons, we need to 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
Step 7: Add Swap entry in FSTAB file

This is a very important step because most of the time we create the swap space but forgot to add in fstab file. And when we reboot the system then the swap partition will not be getting mount again on the system.

To make the swapfile permanent add the below entry in the “/etc/fstab” file.

vim /etc/fstab

/data/swapfile none swap sw 0 0

save & close the file.

Read More about fstab file in my previous article. Check out the following link for Fstab,

Introduction and Working of Linux /etc/fstab File

Step 8: Configure Swappiness on system

Swappiness is used to set how often swap memory can be used by the kernel.

To set the swappiness, open the “/etc/sysctl.conf” file and add the below line to the file.

vm.swappiness=10

By default, the swappiness value is 60 and the maximum value is 100. The higher the number, the more frequent swap space utilization by the kernel. When the value is set to 0, the swap file will only be used if the operating system has fully utilized memory.

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 Linuxgrow.com for more such valuable articles.

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

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

Facebook