2 Ways to create or extend Swap space in Linux

Users can create a swap space during installation of any Linux operating system, which is essential. If you forget to create it or want to increase it later, you can do it at any time.

Sometimes you may need to add more swap space when upgrading the RAM after installing the operating system.

For example: When you upgrade the RAM from 1GB to 2GB, the swap should be upgraded from 2GB to 4GB because the swap will be added as double the amount of physical RAM.

It’s recommended to create a dedicated swap partition, but if you don’t have a free partition then use a swap file, or a combination of it.

Swap space is generally recommended up to a minimum of 4GB, but this can be created depending on their application requirement and environment.

What is swap space?

Swap is space on a disk that is reserved to be used as virtual memory when the amount of physical memory (RAM – Random access memory) is full.

If the system needs more memory resources when the RAM is full, inactive pages in memory are moved to the swap space which can help system to run the applications for some more time but it should not be considered a replacement for more RAM.

How to check current swap size on Linux

Let’s first check the size of existing swap space using free command & swapon command. As per the following output, the current swap space is '2GB.

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           2.0G        1.3G        139M         45M        483M        426M
Swap:          2.0G        655M        1.4G

$ swapon --show
NAME      TYPE      SIZE   USED PRIO
/dev/sda3 partition   2G 655.2M   -1

Method-1: Creating a Swap partition

Hard drive partition is one of the recommended method to create a swap space.

If you have an additional hard disk create the new partition using fdisk command. Let us assume that we have created the partition called “/dev/sda4”.

Use ‘mkswap’ command to convert the partition as swap area.

$ sudo mkswap /dev/sda4

Enable the swap file by running below command.

$ sudo swapon /dev/sda4

Add newly created swap file into fstab file, so that swap space partition available even after the reboot.

$ vi /etc/fstab

/dev/sda4  swap  swap  defaults  0 0

Check newly created swap file.

$ swapon --show
NAME       TYPE       SIZE USED PRIO
/dev/sda3  partition    2G 1.3G   -1
/dev/sda4  partition    1G   0B   -2

Now you can see the new 1GB /dev/sda4 swap partition. Reboot the system to use the new swap partition.

Alternatively, you can create swap space using the LVM partition, which allow you to extent the swap space whenever you want.

Removing a Swap partition

Refer the following three steps to deactivate and remove the swap partition.

1) Deactivating the swap space by running the below command.

$ sudo swapoff -v /dev/sda4

2) Next, remove the swap file entry from the ‘/etc/fstab’ file.

/dev/sda4  swap  swap  defaults  0 0

3) Finally, remove the partition using the fdisk command.

$ sudo fdisk /dev/sda
Command (m for help): p
Command (m for help): d
Partition number (1-4): 4
Command (m for help): w

Method-2 : Creating a Swap file

When you don’t have an additional partition, the swap space can be created using swap file, which can be done in a number of ways, but I prefer to go with the dd command.

To do so, you need to create a file of a certain size on Linux system. dd command is a utility, which helps you to create a file with a pre-allocated size instantly.

The following dd command will create 1GB of /swapfile.

$ sudo dd if=/dev/zero of=/swapfile1 bs=1G count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 16.6154 s, 64.6 MB/s

Details :

  • if=/dev/zero is a input file, /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it.
  • of=/swapfile1 is an output file
  • bs=1G : Read and write up to 1GB bytes at a time
  • count=1 : Copy only 1 BLOCKS input blocks.

Check whether its created correct size of file or not.

$ ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Jun  7 09:58 /swapfile

Change the file permission to 600 to only accessible by root user.

$ sudo chmod 600 /swapfile

Convert the file as a swap file by running the following command.

$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=96def6d7-b2da-4954-aa72-aa32316ec993

Enable the swap file by running below command.

$ sudo swapon /swapfile

Add newly created swap file into ‘fstab’ file, so that swap space partition available even after the reboot.

$ vi /etc/fstab

/swapfile  swap  swap  defaults  0 0

Check newly created swap file.

$ swapon --show
NAME       TYPE       SIZE USED PRIO
/dev/sda5  partition    2G 1.3G   -1
/dev/sda4  partition    1G   0B   -2
/swapfile  file      1024M   0B   -3

Now you can see the new 1GB swapfile swap file. Reboot the system to use the new swap file.

Removing a Swap file

Refer the following three steps to deactivate and remove the swap file.

1) Deactivating the swap space by running the below command.

$ sudo swapoff -v /swapfile

2) Next, remove the swap file entry from the ‘/etc/fstab’ file.

/swapfile  swap  swap  defaults  0 0

3) Finally, remove the actual ‘swapfile’ file using the rm command:

$ sudo rm /swapfile

Conclusion

In this guide, you have learned how to create, activate & remove a swap file and partition on Linux.

If you have question or feedback, leave a comment below.

2 Comments on “2 Ways to create or extend Swap space in Linux”

Leave a Reply

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