Swap memory is required when the system requires more memory than it is physically available. The kernel swaps out less used pages and gives memory to the current process that needs the memory immediately. So a page of memory is copied to the preconfigured space on the hard disk. Disk speed is much slower compared to memory speed. Swapping pages give more space for current applications in the memory (RAM) and make the application run faster.
Swap space is located on hard drives, which have a slower access time than physical memory. Swap space can be a dedicated swap partition or swap file, or a combination of both.
In this tutorial, we learn how to create a swap partition on a Linux system.
Lets first check disk space, then create a partition and followed by enabling swap.
CREATE A SWAP PARTITION IN LINUX
In this artical we are going to learn how to create a swap partition using command fdisk, Before that let me explain you what is the use of swap partition in Linux.
SWAP Partition is also known as SWAP memory, It is act as a Virtual Memory in Linux. When the physical memory of the system is full then Swap memory helps to system with a small amount of memory but we can’t consider it as a replacement of more memory.
First check the available free disks to create a new partition by below command.
# To list the available free disks and created partitions.[root@cloud_devopstraining_9160207373 ~]# fdisk -l
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0003d1b7 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 83886079 40893440 8e Linux LVM Disk /dev/mapper/centos-root: 37.7 GB, 37706792960 bytes, 73646080 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes![]()
Partition List
Create a partition by below command.
[root@cloud_devopstraining_9160207373 ~]# fdisk /dev/sdb      # To Create a new partition
Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x047a6520. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-209715199, default 2048):     press enter Using default value 2048     press enter Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):+5G Partition 1 of type Linux and of size 5 GiB is set Command (m for help): p Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x047a6520 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@cloud_devopstraining_9160207373 ~]#
create one more partition same as like above
Here we can enter the partition size in two ways in GB’s or in MB’s, for Example If you want to give the partition size as 1 GB then you can Enter +1G or +1024M.
here i am using 5GB for Swap..
[root@cloud_devopstraining_9160207373 ~]# fdisk /dev/sdb      # To Create a new partition
Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x047a6520. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-209715199, default 2048):     press enter Using default value 2048     press enter Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):+5G Partition 1 of type Linux and of size 5 GiB is set Command (m for help): p Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x047a6520 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
[root@cloud_devopstraining_9160207373 ~]#
[root@cloud_devopstraining_9160207373 ~]# fdisk -l Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x047a6520 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0003d1b7 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 83886079 40893440 8e Linux LVM Disk /dev/mapper/centos-root: 37.7 GB, 37706792960 bytes, 73646080 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@cloud_devopstraining_9160207373 ~]# fdisk -l
Assign a Number to Partition
[root@cloud_devopstraining_9160207373 ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x047a6520 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux /dev/sdb2 10487808 20973567 5242880 83 Linux Command (m for help): t Partition number (1,2, default 2): 1 Hex code (type L to list all codes): l 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): p Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x047a6520 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris /dev/sdb2 10487808 20973567 5242880 83 Linux
Creating Second Partition
So we create the partition successfully but our goal is to create a additional swap partition, for that we need to change the partition ID for that we have to enter "t" or for help we can go for "m".
Command (m for help): t
Here enter the available partition number for that disk and Press Enter.
Partition number (1,2, default 2):2 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): p Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x047a6520 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris /dev/sdb2 10487808 20973567 5242880 82 Linux swap / Solaris Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
[root@cloud_devopstraining_9160207373 ~]#
[root@cloud_devopstraining_9160207373 ~]# fdisk -l Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x047a6520 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 82 Linux swap / Solaris /dev/sdb2 10487808 20973567 5242880 82 Linux swap / Solaris Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0003d1b7 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 83886079 40893440 8e Linux LVM Disk /dev/mapper/centos-root: 37.7 GB, 37706792960 bytes, 73646080 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@cloud_devopstraining_9160207373 ~]#
In Linux based system after create a partition we suppose to restart the system to take effect but we can skip that by running "partprobe command"
# Changes take effect without restat the system[root@cloud_devopstraining_9160207373 ~]# partprobe /dev/sdb [root@cloud_devopstraining_9160207373 ~]#Before creating the swap memory
[root@cloud_devopstraining_9160207373 ~]# free -h total used free shared buff/cache available Mem: 3.7G 1.7G 1.2G 24M 810M 1.7G Swap: 3.9G 0B 3.9G [root@clouddevops ~]#
we can use "blkid" command to check the Labels given to File Systems.
[root@cloud_devopstraining_9160207373 ~]# blkid /dev/sda1: UUID="5b0b4da2-409f-44de-9434-46f1a3899ef1" TYPE="xfs" /dev/sda2: UUID="S1ncTT-Mlnf-XSfH-J0yi-E6dd-CKri-0GAy4U" TYPE="LVM2_member" /dev/sr0: UUID="2020-04-22-00-54-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" /dev/mapper/centos-root: UUID="bc6c57d9-0afa-4dbc-b989-7d3dd8139b41" TYPE="xfs" /dev/mapper/centos-swap: UUID="32bed349-39dc-4f59-af3d-d1a623e1fd48" TYPE="swap" [root@cloud_devopstraining_9160207373 ~]#
Step: 2 Create Swap Signature
Now Let’s create the swap Signature with "mkswap" command.
# To make a swap partitionCreate Swap Partition with "mkswap"
L – To Label your Partition for Identification, This can be leave as a optional but Highly recommended.
[root@cloud_devopstraining_9160207373 ~]# mkswap /dev/sdb1 -L new-swap-part-one Setting up swapspace version 1, size = 5242876 KiB mkswap: Label was truncated. LABEL=new-swap-part-o, UUID=0b1264e7-ff27-477b-b029-cd7a52ec0241 [root@cloud_devopstraining_9160207373 ~]#After creating the swap memory
we can use "blkid" command to check the Labels given to File Systems.
[root@cloud_devopstraining_9160207373 ~]# blkid /dev/sda1: UUID="5b0b4da2-409f-44de-9434-46f1a3899ef1" TYPE="xfs" /dev/sda2: UUID="S1ncTT-Mlnf-XSfH-J0yi-E6dd-CKri-0GAy4U" TYPE="LVM2_member" /dev/sr0: UUID="2020-04-22-00-54-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" /dev/mapper/centos-root: UUID="bc6c57d9-0afa-4dbc-b989-7d3dd8139b41" TYPE="xfs" /dev/mapper/centos-swap: UUID="32bed349-39dc-4f59-af3d-d1a623e1fd48" TYPE="swap" /dev/sdb1: LABEL="new-swap-part-o" UUID="0b1264e7-ff27-477b-b029-cd7a52ec0241" TYPE="swap" [root@cloud_devopstraining_9160207373 ~]#
Checking the Label of the Partition
Now enable the swapping and publish the swap space to the kernel by below command.
# Enable swapping on /dev/sdb1[root@cloud_devopstraining_9160207373 ~]# swapon /dev/sdb1After creating the swap memory Size
[root@cloud_devopstraining_9160207373 ~]# free -h total used free shared buff/cache available Mem: 3.7G 915M 1.7G 25M 1.1G 2.5G Swap: 8.9G 0B 8.9G [root@cloud_devopstraining_9160207373 ~]#
OR we can use swapon -a to activate all swap partitions (Multiple swap Partitions)
Now It’s time to add the newly created swap partition on "/etc/fstab" file to start the partition at startup.
# Edit the "fstab" file[root@cloud_devopstraining_9160207373 ~]#vim /etc/fstab
# # /etc/fstab # Created by anaconda on Fri Jun 12 20:05:53 2020 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=5b0b4da2-409f-44de-9434-46f1a3899ef1 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 LABEL=new-swap-part-one swap swap defaults 0 0
Mounting SWAP Partition in fstab
We can check the swap status with below command.
[root@cloud_devopstraining_9160207373 ~]# swapon -s Filename Type Size Used Priority /dev/dm-1 partition 4063228 0 -2 /dev/sdb1 partition 5242876 0 -3 [root@cloud_devopstraining_9160207373 ~]#
"swapon -s" shows swap usage summary by device, Equivalent to "cat /proc/swaps"
[root@cloud_devopstraining_9160207373 ~]# cat /proc/swaps Filename Type Size Used Priority /dev/dm-1 partition 4063228 0 -2 /dev/sdb1 partition 5242876 0 -3 [root@cloud_devopstraining_9160207373 ~]#
Checking SWAP Partition Usage
As we can see on above snapshot we have two swap partitions, out of which highlighted one is recently created and currently it’s usage status is 0.
We have another command to check the status of the swap memory is "free -m"
# Display amount of free and used memory in the system[root@cloud_devopstraining_9160207373 ~]# free -m total used free shared buff/cache available Mem: 3770 874 1752 27 1144 2621 Swap: 9087 0 9087OR
[root@cloud_devopstraining_9160207373 ~]# free -h total used free shared buff/cache available Mem: 3.7G 874M 1.7G 27M 1.1G 2.6G Swap: 8.9G 0B 8.9G [root@cloud_devopstraining_9160207373 ~]#
So This is how we can create a swap partition for better performance in our Linux System.
The DevOps seminar will help you to learn DevOps from scracth to deep knowledge of various DevOps tools such as fallowing List.
  Kubernetes.DevOps
AWS
Python
Azure
G Cloud
Linux
Web Tech
Data Bases
Testing
Git
Ansible
Chef
Kubernetes
HTML
CSS