How to create and remove swap memory in CLI on Debian 11.3
To Create And Remove Swap Memory In CLI On Debian 11.3
Introduction:
In Linux, swap memory is used when physical memory is full. As swap memory is on a hard drive and relatively slow, it cannot be used instead of physical memory when it is inactive.
Procedure:
Step 1: Check the OS version by using the following command
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye
Step 2: List the disk by using the below command
root@linuxhelp:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 59G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 975M 0 part [SWAP]
sdb 8:16 0 10G 0 disk
sr0 11:0 1 3.6G 0 rom
Step 3: Check the available swap memory by using the below command
root@linuxhelp:~# top
top - 21:40:09 up 1:41, 2 users, load average: 0.62, 0.22, 0.12
Tasks: 278 total, 1 running, 277 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.0 us, 0.7 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 2920.1 total, 101.4 free, 1713.9 used, 1104.9 buff/cache
MiB Swap: 975.0 total, 974.0 free, 1.0 used. 957.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1133 asterisk 20 0 757936 80180 38432 S 0.7 2.7 0:36.50 asterisk
1837 linuxhe+ 20 0 3346964 189976 99628 S 0.7 6.4 0:08.89 gnome-s+
2201 linuxhe+ 20 0 400844 45448 35928 S 0.7 1.5 0:00.75 gnome-t+
596 root 20 0 236416 9456 6216 S 0.3 0.3 0:10.11 vmtoolsd
3054 root 20 0 0 0 0 I 0.3 0.0 0:01.21 kworker+
3241 root 20 0 10228 3960 3224 R 0.3 0.1 0:00.12 top
1 root 20 0 164188 10560 7824 S 0.0 0.4 0:02.67 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par+
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker+
8 root 0 -20 0 0 0 I 0.0 0.0 0:01.43 kworker+
9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_perc+
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tas+
Step 4: Open fdisk to create partitions on /dev/sdb disk by using the below command
root@linuxhelp:~# sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.36.1).
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.
Created a new DOS disklabel with disk identifier 0x71a2e1a2.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): +5G
Created a new partition 1 of type 'Linux' and of size 5 GiB.
Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): 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.
Step 5: Format the partition /dev/sdb1 to the swap file system by using the below command
root@linuxhelp:~# sudo mkswap /dev/sdb1
Setting up swapspace version 1, size = 5 GiB (5368705024 bytes)
no label, UUID=148cfbb0-c15e-450a-b32b-8477c3d10cd3
Step 6: Turn on the swap memory by using the below command
root@linuxhelp:~# sudo swapon /dev/sdb1
Step 7: Make fstab entry for swap partition by using the below command
root@linuxhelp:~# vi /etc/fstab
#/etc/fstab: static file system information.
#Use 'blkid' to print the universally unique identifier for a
#device; this may be used with UUID= as a more robust way to name devices
#that works even if disks are added and removed. See fstab(5).
#<file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda3 during installation
UUID=6183834d-0563-4576-a37d-b64b78a1640a / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda2 during installation
UUID=BD43-BD7D /boot/efi vfat umask=0077 0 1
/dev/sdb1 swap swap defaults 0 0
Step 8: Make permanent mounts of all entries in fstab by using the below command
root@linuxhelp:~# mount -a
Step 9: Check the swap memory by using the below command
root@linuxhelp:~# top
op - 21:46:36 up 1:47, 2 users, load average: 0.21, 0.24, 0.16
Tasks: 279 total, 1 running, 278 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 2920.1 total, 114.3 free, 1721.2 used, 1084.7 buff/cache
MiB Swap: 6095.0 total, 6093.7 free, 1.3 used. 952.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1837 linuxhe+ 20 0 3613264 189808 99364 S 1.0 6.3 0:12.48 gnome-s+
596 root 20 0 236416 9456 6216 S 0.3 0.3 0:10.50 vmtoolsd
793 mysql 20 0 1298904 429036 33028 S 0.3 14.3 0:09.97 mysqld
1133 asterisk 20 0 757936 80152 38404 S 0.3 2.7 0:38.88 asterisk
2201 linuxhe+ 20 0 401012 45436 35916 S 0.3 1.5 0:02.07 gnome-t+
3279 root 20 0 10228 3908 3176 R 0.3 0.1 0:00.17 top
1 root 20 0 164028 10556 7824 S 0.0 0.4 0:02.69 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par+
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker+
8 root 0 -20 0 0 0 I 0.0 0.0 0:01.47 kworker+
9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_perc+
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tas+
Step 10: Turn Off the swap memory by using the below command
root@linuxhelp:~#sudo swapoff /dev/sdb1
Step 11: Remove the fstab entries for /dev/sdb1 partition by using the below command
root@linuxhelp:~# vi /etc/fstab
Step 12: Delete the /dev/sdb1 partition by using fdisk by using the below command
root@linuxhelp:~# sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Step 13: Check the swap memory by using the below command
root@linuxhelp:~# top
top - 21:48:11 up 1:49, 2 users, load average: 0.20, 0.22, 0.15
Tasks: 279 total, 1 running, 278 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 16.7 sy, 0.0 ni, 83.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 2920.1 total, 117.9 free, 1717.4 used, 1084.8 buff/cache
MiB Swap: 975.0 total, 973.8 free, 1.2 used. 955.8 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3315 root 20 0 10228 3912 3176 R 11.8 0.1 0:00.03 top
1837 linuxhe+ 20 0 3613248 189760 99364 S 5.9 6.3 0:13.80 gnome-s+
1 root 20 0 164028 10556 7824 S 0.0 0.4 0:02.71 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par+
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker+
8 root 0 -20 0 0 0 I 0.0 0.0 0:01.49 kworker+
9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_perc+
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tas+
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to Create and remove swap memory in CLI on Debian 11.3. Your feedback is much welcome.