How to create swap partition in Linux

The swap space is a hard disk space. Linux uses it to deal with the shortage of RAM. If the swap space is available, Linux assigns a threshold value to RAM. It moves ideal processes' data from RAM to the swap space when RAM usage crosses the threshold value. Until the RAM usage remains under the threshold value, Linux does not use the swap space.

We can use a partition or a file to create the swap space. If the hard disk has un-partitioned space, we can create a new partition and use it as the swap space. If no un-partitioned space is available, we can create an empty file and use it as the swap space.

Using a partition for the swap space

Linux includes many native tools to create and manage partitions. You can choose any one you prefer. In this tutorial, we will use the gdisk command .

The gdisk command needs the disk path as an argument. It allows us to create, update, and delete partitions on the specified disk. We can use the lsblk command to view all attached hard disks with their partitions.

Select the disk having un-partitioned space and specify its path as an argument to the gdisk command.


The gdisk command starts on a sub-shell. On the sub-shell, it uses its commands to manage the specified disk. The p command prints the current disk layout.

printing disk layout

As the above output shows, this disk has no partitions. We use the following commands to create a swap partition.

Command Description
n Create a new partition
Enter key Start the new partition from the first available sector on the disk
+1G Convert 1Gb into sectors and add the result into the starting sector. It creates a partition of 1Gb.
8200 Set the partition type to Linux swap

creating partition

We can use the p (print) command to verify the new partition.

verifying swap partition

By default, the gdisk command keeps all changes in memory. We use the w (write) command to apply the changes.

saving partition information

Formatting the partition

The mkswap command formats the partition and creates a swap signature. Linux uses the swap signature to identify the swap partitions and files.


Activating the swap space

The swapon command activates the swap space. We can use the free command before and after this command to verify the new swap partition we created. The free command prints the memory status.


Mounting the swap space

Linux uses the /etc/fstab file to save partition information. It reads this file at the boot time to mount partitions. If we want to mount a partition permanently, we must create an entry for that partition in this file. A fstab file entry has the following six fields.

Field name Description Value
(in this example)
Path/UUID Absolute path or UUID of the partition /dev/sda1
Label/Name Name or label of the partition none
File system File system on the partition swap
Options Mount options default
Dump The dump command uses it to determine whether it should dump the mounted partition at the boot time. 0
File system checks The fsck command uses it to determine whether it should check and repair the mounted partition at the boot time. 0

fstab file entry

Verifying the swap partition

Restart the system and use the free command to verify the swap partition. If the system boots without errors and the output of the free command displays increased swap space, it verifies the new swap partition.

verify swap

Using a file for the swap space

If un-partitioned disk space is not available and we need to increase the swap space, we can use a file as the swap space. Since Linux uses the swap space to save temporary memory pages, we can't use a data file for the swap space. The file we want to use for the swap space must not contain meaningful data.

We can use the dd command to create a text file having only raw data. This command needs four arguments: source, destination, count, and block size.

It copies data in the specified block size from the source and pastes it on the destination as many times as we specify in the count argument. For example, the following command copies 1Mb data from the zero device and pastes it 1000 times in the file /swap-file. It copies data from the zero device. The zero device supplies infinite numbers of zero.

the dd command

As mentioned earlier, Linux uses a partition or a file for the swap space when it has the swap signature. Use the mkswap command to create the swap signature on this file.

mkswap command

The default file permission is 0644. Since only the system uses the swap file and this permission grants the group and other users read and execute permissions, the mkswap command suggests using 0600 permission. The 0600 grants permission only to the owner.

Change permission to 0600.

the chmod command

After changing the permission, use the swapon command to activate the newly created file as the swap space.

swapon command

The system will use this file as the swap file in the current session only. If you permanently want to use this file as the swap file, you need to add an entry for this file in the /etc/fstab file. You can create an entry for this file by following the same steps we used to create an entry for the swap partition.

ComputerNetworkingNotes Linux Tutorials How to create swap partition in Linux