mkswap – Create and set up a swap partition

The mkswap command is used to create and set up a swap partition on a Linux system. Swap space is a portion of a hard disk drive (HDD) or solid-state drive (SSD) that is used as virtual memory when the system is running out of physical memory (RAM). The mkswap command formats the partition as a swap partition and sets up the necessary data structures to use it as virtual memory.


To create and set up a swap partition using mkswap, follow the steps below:

  1. Identify the partition that you want to use as swap. This can be done using the fdisk command to list all partitions on the system:
    $ sudo fdisk -l
  2. Once you have identified the partition, use the mkswap command to format it as a swap partition:
    $ sudo mkswap /dev/sdX1

    Replace /dev/sdX1 with the actual partition name.

  3. Once the partition has been formatted, use the swapon command to activate it:
    $ sudo swapon /dev/sdX1

    This will start using the partition as virtual memory.

To make the swap partition permanent, you need to add an entry for it in the /etc/fstab file. Open the file in a text editor:

$ sudo vi /etc/fstab

Add the following line at the end of the file:

/dev/sdX1 none swap defaults 0 0

Replace /dev/sdX1 with the actual partition name.


The mkswap command has the following options:

Option Description
-c Check the partition for bad blocks before formatting
-f Force formatting even if the partition appears to contain a file system
-L Set the label for the swap partition
-v Verbose output


  • If you get an error message saying “swapon: /dev/sdX1: swapon failed: Device or resource busy”, it means that the partition is already in use. You can use the swapoff command to deactivate it before trying again:
    $ sudo swapoff /dev/sdX1
  • If you get an error message saying “mkswap: /dev/sdX1: warning: wiping old swap signature.”, it means that the partition already contains a swap signature. You can ignore the warning and proceed with formatting the partition.


  • The recommended size for a swap partition is twice the amount of RAM on the system. However, this may not always be necessary or practical, depending on the system’s usage.
  • You can create a swap file instead of a swap partition using the fallocate and mkswap commands. This may be useful if you don’t have a spare partition available.