Most Linux installations recommend that you include a SWAP partition. Swap partition is the second type of memory in Linux systems. The primary function of swap is to use disk space for RAM memory when real RAM fills up and more space is needed.
In this article, you will learn that how to create swap. There are three types of swap that will be covered in this article respectively.
- Standard SWAP
- LVM SWAP
- SWAP File
Create Standard SWAP
Command to see available RAM and SWAP stats
# free -h # swapon -s
Step 1: Follow below instructions
- List partition table using below command
# fdisk -l
- Choose desired hard disk for swap partition List partition table using below command
# fdisk /dev/sdb
- Press m for help
- Press p to print partition table
- Press n to create new partition
- Press p to make primary partition.
- Press 1 to create first partition on selected disk.
There are 1-4 partition number. We can type any one number from them. - Press enter to leave first cylinder as default
- Now assign space to partition. Use + to assign space e.g.
+1G and press enter. - Press p to print partition table.
- Now set SWAP file system ID on partition.
- Press l to list all available file system volumes. We will set SWAP (82)
- Press t to set file system on partition
- Press 1 to select partition. It is the partition on which we set SWAP file system.
- type 82 and press enter to set LVM ID on selected partition.
- Press w to write/save partition.
Now run command to make changes in partition table using below command
# partprobe
Step 2: Format Partition (File System)
# mkswap /dev/sdb1
Step 3: Set Partition Label
Command to set Label on SWAP Partition
Syntax: mkswap  -L  Label-name   /dev/partition
# mkswap  -L  myswap   /dev/sdb1
Step 4: Now Mount partition in /etc/fstab File
# vim /etc/fstab
and add following entry:
LABEL=myswap          swap   swap   deafults   0    0
write changes to /etc/fstab and quite
Step 5: Now Activate Created SWAP
# swapoff -a (Disable all swap) # swapon -a (enable all swap) # swapon -s (list enabled all swap) # free  -m
You have created a standard swap successfully!
LVM SWAP
Step 1: Follow the Previous procedure
Step 2: Follow the Previous procedure
Step 3: Create PV (Physical Volume) of the partition
Use below command
#pvcreate /dev/sdb1 #pvs
Step 4: Create VG (Volume Group)
#vgcreate  swap_vg   /dev/sdb1 #vgs
Note: swap_vg is vg name you can use any
Step 5: Create LVM (Logical Volum Manager)
Use below command
# lvcreate  -l  100%free  -n   swap_lvm  swap_vg # lvs
swap_lvm  is the LVM Name. you can use any.
swap_vg   is the name of created VG
Step 6: Format Partition (File system)
# mkswap /dev/swap_vg/swap_lvm
Step 7: Mount Partition in /etc/fstab File
# vi /etc/fstab
and add the following entry:
/dev/swap_vg/swap_lvm swap swap defaults 0 0
write changes to /etc/fstab and quite
Step 8: Now Activate Created SWAP
# swapoff -a (Disable all swap) # swapon -a (enable all swap) # swapon -s (list enabled all swap) # free  -m
Make SWAP FILE
Step 1: Make File
We will create swap file of 3GB
Use the following command
# dd if=/dev/zero of=/myswap bs=1G count=3
Note: Above command will take some time to generate the file.
Step 2: Set Permission on SWAP File
set the appropriate permissions on the file to make it readable only by root user.
# cd / # chmod 600 /myswap # ls –lh myswap
Step 3:Â Set SWAP Filesystem on file
# mkswap /myswap
Step 4: Enable SWAP File
# swapon /myswap
Step 5:Â Mount SWAP File in /etc/fstab file
# vi /etc/fstab
and add following entry:
/myswap swap swap defaults 0 0
write changes to /etc/fstab and quite
Step 6: Now Activate Created SWAP
# swapoff -a (Disable all swap) # swapon -a (enable all swap) # swapon -s (list enabled all swap) # free  -m
You have learned that how to create different types of SWAP in Linux.