()

Steps to Add and Extend Swap Space in Linux


Swap
space is a virtual amount of physical memory that is allocated for use by the operating system when available RAM memory has been fully utilized. This swap memory is located on the disc and since disc reads and writes are slower than reading from RAM, accessing memory pages there will result in a delay. It is recommended to set up swap space when installing the operating system. But if Linux Admin miss to configure Swap memory It can be configured even after Linux installation.


Below are the topics we will discuss in this blog.
1. How to check swap space
2. Adding swap partition
3. Adding a swap file
4. Removing Swap


How to check swap space: We can use any of the below commands to check and monitor swap utilization in Server. As swap space is not configured its showing 0.

[root@test-machine01 ~]#
[root@test-machine01 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        5806308      272188     4942212        9316      591908     5434916
Swap:             0           0           0
[root@test-machine01 ~]#

[root@test-machine01 ~]#
[root@test-machine01 ~]# grep Swap /proc/meminfo
SwapCached:            0 kB
SwapTotal:                0 kB
SwapFree:                 0 kB
[root@test-machine01 ~]#
[root@test-machine01 ~]#

[root@test-machine01 ~]# top
top - 13:52:39 up 56 min,  1 user,  load average: 0.16, 0.30, 0.14
Tasks: 153 total,   1 running,  74 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  3.0 sy,  0.0 ni, 97.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  5806308 total,  4942128 free,   272164 used,   592016 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  5434940 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0  151444  10160   7024 S   0.0  0.2   0:04.82 systemd
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd
     4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H
     6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq
     7 root      20   0       0      0      0 S   0.0  0.0   0:00.26 ksoftirqd/0
     8 root      20   0       0      0      0 I   0.0  0.0   0:02.04 rcu_sched
 
[root@test-machine01 ~]#


Method 1. Adding swap partition: We have already added 3 GB of new disk /dev/sdd on Server. We will use the same disk to create a swap partition. First, create a new partition of type swap (82). Make sure you modify the partition type to 82 (Linux swap / Solaris) and save the partition table at the end. We can use command l to verify Hex Code 82 represent Linux swap / Solaris.

Follow below steps to create partition 

  • n : add a new partition
  • p : primary partition
  • partition number : 1
  • t : change a partition’s system id
  • 82 : Hex code for Linux swap / Solaris
[root@test-machine01 ~]#
[root@test-machine01 ~]# fdisk -l /dev/sdd

Disk /dev/sdd: 3221 MB, 3221225472 bytes, 6291456 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@test-machine01 ~]#
[root@test-machine01 ~]#
[root@test-machine01 ~]# fdisk /dev/sdd
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 0x6aba9ab5.

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-6291455, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-6291455, default 6291455):
Using default value 6291455
Partition 1 of type Linux and of size 3 GiB is set

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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)

Command (m for help): 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

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@test-machine01 ~]# fdisk -l /dev/sdd

Disk /dev/sdd: 3221 MB, 3221225472 bytes, 6291456 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: 0x6aba9ab5

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048     6291455     3144704   82  Linux swap / Solaris
[root@test-machine01 ~]#
[root@test-machine01 ~]#


Run the command mkswap against the device/partition created earlier using fdisk. Use option -L to set LABEL on the swap partition. Add the device name in /etc/fstab file.

[root@test-machine01 ~]#
[root@test-machine01 ~]# mkswap -L swaptest /dev/sdd1
Setting up swapspace version 1, size = 3144700 KiB
LABEL=swaptest, UUID=c97f98d1-f316-4d97-b61c-00e0574b2984
[root@test-machine01 ~]#
[root@test-machine01 ~]#
[root@test-machine01 ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov  1 09:53:52 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/ol-root     /                        xfs     defaults        0 0
UUID=dbf762a7-9188-4494-9dac-3fba8badd022 /boot  xfs     defaults        0 0
UUID=a2e7c8a2-444a-4752-b39c-78a949d80c14 /u01   xfs     defaults        0 0
/dev/sdd1                swap                    swap    defaults        0 0
:wq!
[root@test-machine01 ~]#
[root@test-machine01 ~]#

Run the “swapon -a” command to enable all swap devices listed in the /etc/fstab file and verify the added swap using “swapon -s”.

[root@test-machine01 ~]#
[root@test-machine01 ~]# swapon -a
[root@test-machine01 ~]#
[root@test-machine01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdd1                               partition       3144700 0       -2
[root@test-machine01 ~]#
[root@test-machine01 ~]#

Check the swap memory again using the below commands. Now Swap size is showing 3GB.

[root@test-machine01 ~]#
[root@test-machine01 ~]#
[root@test-machine01 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        5806308      274428     4939516        9320      592364     5432676
Swap:       3144700           0     3144700
[root@test-machine01 ~]#
[root@test-machine01 ~]# grep Swap /proc/meminfo
SwapCached:            0 kB
SwapTotal:       3144700 kB
SwapFree:        3144700 kB
[root@test-machine01 ~]#
[root@test-machine01 ~]# top
top - 14:02:08 up  1:05,  1 user,  load average: 0.00, 0.04, 0.07
Tasks: 153 total,   2 running,  74 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  3.1 sy,  0.0 ni, 96.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  5806308 total,  4939128 free,   274812 used,   592368 buff/cache
KiB Swap:  3144700 total,  3144700 free,        0 used.  5432292 avail Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  4222 root      20   0       0      0      0 I   6.7  0.0   0:03.15 kworker/1:1
  7168 root      20   0  163584   4440   3716 R   6.7  0.1   0:00.01 top
     1 root      20   0  151444  10160   7024 S   0.0  0.2   0:05.15 systemd
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd
     4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H
     6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq
     7 root      20   0       0      0      0 S   0.0  0.0   0:00.28 ksoftirqd/0
[root@test-machine01 ~]#


Method 2. Adding swap file: If it is not possible to add a swap partition we can also use the file as a swap. To do so, use dd command to create a file of the required size. You can change the value of bs=2M as per your requirements. To make it more secure apply 600 file permissions to the file.

[root@test-machine01 ~]#
[root@test-machine01 ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             2.8G     0  2.8G   0% /dev
tmpfs                2.8G     0  2.8G   0% /dev/shm
tmpfs                2.8G  9.2M  2.8G   1% /run
tmpfs                2.8G     0  2.8G   0% /sys/fs/cgroup
/dev/mapper/ol-root   46G  8.3G   38G  18% /
/dev/sda1           1014M  277M  738M  28% /boot
/dev/sdb1             60G   46G   15G  76% /u01
tmpfs                568M     0  568M   0% /run/user/0
[root@test-machine01 ~]#
[root@test-machine01 ~]#
[root@test-machine01 ~]# dd if=/dev/zero of=/root/swapfile01 bs=2M count=1024
1024+0 records in
1024+0 records out
2147483648 bytes (2.1 GB) copied, 45.94 s, 46.7 MB/s
[root@test-machine01 ~]#
[root@test-machine01 ~]# chmod 600 /root/swapfile01
[root@test-machine01 ~]#

Run mkswap command to convert the file to a swap file. Add in /etc/fstab file to add the new swap file.

[root@test-machine01 ~]#
[root@test-machine01 ~]# mkswap -L swap01 /root/swapfile01
Setting up swapspace version 1, size = 2097148 KiB
LABEL=swap01, UUID=234f41a5-6d35-4240-a936-0306bf75ccdc
[root@test-machine01 ~]#
[root@test-machine01 ~]#  vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov  1 09:53:52 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/ol-root     /                        xfs     defaults        0 0
UUID=dbf762a7-9188-4494-9dac-3fba8badd022 /boot  xfs     defaults        0 0
#/dev/mapper/ol-swap     swap                    swap    defaults        0 0
UUID=a2e7c8a2-444a-4752-b39c-78a949d80c14 /u01   xfs     defaults        0 0
/dev/sdd1                   swap                    swap    defaults        0 0
/root/swapfile01         swap                    swap    defaults        0 0
:wq!
[root@test-machine01 ~]#


Run the “swapon -a” command to enable all swap devices listed in the /etc/fstab file and verify the added swap using “swapon -s“.

[root@test-machine01 ~]#
[root@test-machine01 ~]# swapon -a
[root@test-machine01 ~]#
[root@test-machine01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdd1                               partition       3144700 0       -2
/root/swapfile01                        file            2097148 0       -3
[root@test-machine01 ~]#
[root@test-machine01 ~]#


Check the swap memory again using the below commands. You will notice swap size increased from 3GB to 5GB. Now 3GB swap size is from disk and 2GB swap size from file.

[root@test-machine01 ~]#
[root@test-machine01 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        5806308      275392     2783540        9288     2747376     5431744
Swap:       5241848           0     5241848
[root@test-machine01 ~]#

[root@test-machine01 ~]#
[root@test-machine01 ~]#  grep Swap /proc/meminfo
SwapCached:            0 kB
SwapTotal:       5241848 kB
SwapFree:        5241848 kB
[root@test-machine01 ~]#


Removing Swap: In case you want to remove added swap space you can do it with “swapoff -a” command to remove all swap devices mention in file /etc/fstab. Or “swapoff <device name>” to remove individual swap. Make sure you remove the respective swap entry from the /etc/fstab file after disabling the swap.

[root@test-machine01 ~]#
[root@test-machine01 ~]#
[root@test-machine01 ~]# swapoff /root/swapfile01
[root@test-machine01 ~]#  swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdd1                               partition       3144700 0       -2
[root@test-machine01 ~]#
[root@test-machine01 ~]# swapoff -a
[root@test-machine01 ~]#
[root@test-machine01 ~]# swapon -s
[root@test-machine01 ~]#
[root@test-machine01 ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov  1 09:53:52 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/ol-root     /                        xfs     defaults        0 0
UUID=dbf762a7-9188-4494-9dac-3fba8badd022 /boot  xfs     defaults        0 0
UUID=a2e7c8a2-444a-4752-b39c-78a949d80c14 /u01   xfs     defaults        0 0
#/dev/sdd1                swap                    swap    defaults        0 0
#/root/swapfile01         swap                    swap    defaults        0 0
:wq!
[root@test-machine01 ~]#
[root@test-machine01 ~]#


This document is just for learning purposes and always validate in the LAB environment first before applying in the LIVE environment.


Hope so you like this article
!
Please share your valuable feedback/comments/subscribe and follow us below and don’t forget to click on the bell icon to get the most recent update. Click here to understand more about our pursuit.


Related Articles


Loading

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Jamsher Khan

Hello and welcome to DBsGuru,I’m Jamsher Khan working as Senior Oracle DBA based in KSA-Jeddah, I have working experience in Oracle DBA, SQL Server, MySql, PostgreSQL, Linux, Golden Gate, ODA.Thanks for the visits!Share Learn Grow!