2 Simple Ways to Create or Extend Swap Space in Linux

Users can create swap space during installation of any Linux operating system and its necessary too.

If you forget to create or later if you want increase, you can do that at any point of time.

Sometimes it is necessary to add more swap space when you upgrade the RAM after installation.

Say for example, if you upgrade the RAM in your system from 1GB to 2GB then you have to upgrade swap too (From 2GB to 4GB) since it’s uses double amount of physical RAM.

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 some more time but it should not be considered a replacement for more 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 swap partitions and swap files.

Swap space is generally recommended for user at least 4 GB, also users can create a swap space depends upon their requirement and environment.

I have found most of the VM’s and Cloud servers comes without swap partition, so in such cases we can create, extend or increase swap space using the below three methods.

How to check current swap size on Linux

Lets first check the size of existing swap space partition using free & swapon command.

$ 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

The above output clearly shows 2GB is current swap space.

Method-1: How to Create Swap Space Using a Hard drive 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.

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

Method-2 : How to Create Swap Space Using a File

To do so, you need to create a file of a certain size on Linux system. This can be done in a number of ways, but I prefer to go with the dd command.

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 below 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.

You may also like...