Swap partition is a space on your hard disk that serves as overflow for your RAM. It can help you run more applications, prioritize data, and hibernate your system. In this article, we will explain how to create, size, and manage a swap partition in Linux with practical examples.
How to Create the Swap Partition
To create a swap partition, you need to have some free space on your hard disk. You can use a tool like fdisk
or gparted
to create a new partition or resize an existing one. For this example, we will use fdisk
to create a new swap partition of 2 GB on /dev/sda
.
First, run sudo fdisk /dev/sda
to enter the interactive mode of fdisk
. You will see a prompt like this:
Command (m for help):
Type m
to see the list of available commands. You will see something like this:
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Type p
to print the current partition table. You will see something like this:
Disk /dev/sda: 20.0 GB, 20000000000 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1913 15360000 83 Linux
/dev/sda2 1914 2432 4161536 83 Linux
Note the size and the end of the last partition. In this case, it is /dev/sda2
with 4 GB and ending at cylinder 2432. We will use this information to create a new partition after it.
Type n
to add a new partition. You will see a prompt like this:
Command action
e extended
p primary partition (1-4)
Type p
to create a primary partition. You will see a prompt like this:
Partition number (1-4):
Type 3
to create the third partition. You will see a prompt like this:
First cylinder (1915-2432, default 1915):
Type 1915
to start the partition from the next available cylinder. You will see a prompt like this:
Last cylinder, +cylinders or +size{K,M,G} (1915-2432, default 2432):
Type +2G
to specify the size of the partition as 2 GB. You will see a message like this:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1915-2432, default 1915): 1915
Last cylinder, +cylinders or +size{K,M,G} (1915-2432, default 2432): +2G
Command (m for help):
Type p
again to print the new partition table. You will see something like this:
Disk /dev/sda: 20.0 GB, 20000000000 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1913 15360000 83 Linux
/dev/sda2 1914 2432 4161536 83 Linux
/dev/sda3 1915 2178 2113536 83 Linux
Note the size and the device name of the new partition. In this case, it is /dev/sda3
with 2 GB.
How to Assign Its Type in Fdisk
To use the new partition as swap space, we need to change its type from Linux (83) to Linux swap (82). To do this, type t
to change a partition’s system id. You will see a prompt like this:
Partition number (1-4):
Type 3
to select the third partition. You will see a prompt like this:
Hex code (type L to list codes):
Type 82
to change the type to Linux swap. You will see a message like this:
Changed system type of partition 3 to 82 (Linux swap / Solaris)
Type p
again to print the new partition table. You will see something like this:
Disk /dev/sda: 20.0 GB, 20000000000 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1913 15360000 83 Linux
/dev/sda2 1914 2432 4161536 83 Linux
/dev/sda3 1915 2178 2113536 82 Linux swap / Solaris
How to Format and Activate the Swap Partition
To use the swap partition, we need to format it with the mkswap
command and activate it with the swapon
command. To do this, run the following commands:
sudo mkswap /dev/sda3
sudo swapon /dev/sda3
The mkswap
command will create a swap area on the partition and print a message like this:
Setting up swapspace version 1, size = 2113532 KiB
no label, UUID=12345678-1234-1234-1234-123456789012
The swapon
command will enable the swap space and print no message.
To verify that the swap partition is active, run the free -m
command to see the memory usage. You will see something like this:
total used free shared buffers cached
Mem: 1000 800 200 0 50 300
-/+ buffers/cache: 450 550
Swap: 2000 0 2000
Note the swap size and usage. In this case, it is 2000 MB and 0 MB.
How to Deactivate the Swap Partition
To deactivate the swap partition, you can use the swapoff
command. To do this, run the following command:
sudo swapoff /dev/sda3
The swapoff
command will disable the swap space and print no message.
To verify that the swap partition is inactive, run the free -m
command again. You will see something like this:
total used free shared buffers cached
Mem: 1000 800 200 0 50 300
-/+ buffers/cache: 450 550
Swap: 0 0 0
Note the swap size and usage. In this case, it is 0 MB and 0 MB.
How to Make Entry of the Swap Partition in /etc/fstab
The /etc/fstab
file is a configuration file that contains information about the filesystems and partitions that are mounted at boot time. To make the swap partition persistent across reboots, you need to add an entry of it in the /etc/fstab
file. In this article, we will explain how to find the UUID of the swap partition and how to edit the /etc/fstab
file with an example.
Finding the UUID of the Swap Partition
The UUID (Universally Unique Identifier) is a unique string that identifies a partition or a device. It is recommended to use the UUID instead of the device name (such as /dev/sda3
) in the /etc/fstab
file, because the device name may change depending on the hardware configuration or the boot order.
To find the UUID of the swap partition, you can use the blkid
command. For example, if your swap partition is /dev/sda3
, you can run the following command:
sudo blkid /dev/sda3
You will see an output like this:
/dev/sda3: UUID="12345678-1234-1234-1234-123456789012" TYPE="swap"
Note the UUID value of the swap partition. In this case, it is 12345678-1234-1234-1234-123456789012
.
Editing the /etc/fstab File
To edit the /etc/fstab
file, you need to use a text editor such as nano
or vi
. For this example, we will use nano
.
First, run sudo nano /etc/fstab
to open the file in nano
. You will see something like this:
UUID=ef6ba050-6cdc-416a-9380-c14304d0d206 / xfs defaults 0 0
These are the existing entries in the /etc/fstab
file. They are the partition on the existing hard drive /dev/sda1
, and the swap file system. Be careful not to alter these entries.
We need to add a new entry to the /etc/fstab
file for the swap partition. The format of the entry is:
UUID= device_UUID none swap defaults 0 0
Where device_UUID
is the UUID of the swap partition that we found earlier.
For example, if the UUID of the swap partition is 12345678-1234-1234-1234-123456789012
, the entry would be:
UUID=12345678-1234-1234-1234-123456789012 none swap defaults 0 0
Add this entry to the end of the /etc/fstab
file and save the file.
Testing the /etc/fstab File Without Rebooting
To test the /etc/fstab
file without rebooting, you can use the mount -a
command. This command will mount all the filesystems listed in the /etc/fstab
file that are not already mounted.
Run the following command:
sudo mount -a
If there is no error message, it means that the /etc/fstab
file is valid and the swap partition is mounted correctly.
To verify that the swap partition is active, run the free -m
command to see the memory usage. You will see something like this:
total used free shared buffers cached
Mem: 1000 800 200 0 50 300
-/+ buffers/cache: 450 550
Swap: 2000 0 2000
Note the swap size and usage. In this case, it is 2000 MB and 0 MB.