How to create and remove swap memory in CLI on Ubuntu 21.04
To Create and remove swap memory in CLI on Ubuntu 21.04
Introduction:
The swap memory feature in Linux is used when the amount of physical memory is full. Physical memory pages that are inactive are moved to swap memory, which cannot be used instead of physical memory since the swap memory is on a hard drive and relatively slow.
Step 1: Check the OS version by using the following command
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.04
Release: 21.04
Codename: hirsute
Step 2: Check the available swap memory
root@linuxhelp:~# top
top - 02:37:25 up 48 min, 1 user, load average: 0.00, 0.01, 0.00
Tasks: 286 total, 1 running, 285 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.5 us, 1.2 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1946.3 total, 445.3 free, 853.8 used, 647.2 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 916.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1195 linuxhe+ 20 0 4157940 253756 108896 S 3.0 12.7 0:31.12 gnome-shell
2233 linuxhe+ 20 0 412472 51484 39784 S 1.0 2.6 0:06.49 gnome-terminal-
738 root 20 0 177264 8580 7436 S 0.3 0.4 0:03.68 vmtoolsd
1025 linuxhe+ 20 0 9572 6064 4252 S 0.3 0.3 0:00.99 dbus-daemon
1531 linuxhe+ 20 0 397596 11548 7156 S 0.3 0.6 0:02.83 ibus-daemon
2337 root 20 0 0 0 0 I 0.3 0.0 0:00.02 kworker/u256:1-events_unbound
2347 root 20 0 21576 4160 3352 R 0.3 0.2 0:00.05 top
1 root 20 0 98924 10936 7856 S 0.0 0.5 0:02.16 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_gp
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-events_highpri
9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_rude_
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_trace
12 root 20 0 0 0 0 S 0.0 0.0 0:00.08 ksoftirqd/0
13 root 20 0 0 0 0 I 0.0 0.0 0:00.77 rcu_sched
Step 3: Open fdisk to create partions on /dev/sdb disk
root@linuxhelp:~# 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.
Creating New Partition
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Using default partition number and using entire disk space partition by pressing enter for all
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Change the partition type to Linux Swap
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'.
Saving the all changes made to the disks
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Step 4: Format the partition /dev/sdb1 to the swap file system
root@linuxhelp:~# mkswap /dev/sdb1
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=2415e6ca-c083-431c-a498-04cfd07b67e8
Step 5: Turn on the swap memory by using the following command
root@linuxhelp:~# swapon /dev/sdb1
Step 6: Make fstab entry for swap partition
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 7: Make permanent mounts of all entries in fstab
root@linuxhelp:~# mount -a
Step 8: Check the swap memory by using the following command
root@linuxhelp:~# top
top - 02:43:32 up 54 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 290 total, 1 running, 289 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.5 us, 0.5 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1946.3 total, 425.5 free, 869.3 used, 651.5 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 898.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1195 linuxhe+ 20 0 4169812 256864 112084 S 1.3 12.9 0:38.57 gnome-shell
2233 linuxhe+ 20 0 416604 55656 42928 S 0.7 2.8 0:09.17 gnome-terminal-
2458 root 20 0 21576 4132 3328 R 0.7 0.2 0:00.05 top
738 root 20 0 177264 8580 7436 S 0.3 0.4 0:04.20 vmtoolsd
2361 root 20 0 0 0 0 I 0.3 0.0 0:00.38 kworker/1:2-events
1 root 20 0 98924 10936 7856 S 0.0 0.5 0:02.19 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_gp
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-events_highpri
9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_rude_
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_trace
12 root 20 0 0 0 0 S 0.0 0.0 0:00.08 ksoftirqd/0
13 root 20 0 0 0 0 I 0.0 0.0 0:00.93 rcu_sched
14 root rt 0 0 0 0 S 0.0 0.0 0:00.02 migration/0
15 root -51 0 0 0 0 S 0.0 0.0 0:00.00 idle_inject/0
16 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/0
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/1
18 root -51 0 0 0 0 S 0.0 0.0 0:00.00 idle_inject/1
19 root rt 0 0 0 0 S 0.0 0.0 0:00.14 migration/1
20 root 20 0 0 0 0 S 0.0 0.0 0:00.12 ksoftirqd/1
22 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/1:0H-events_highpri
23 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kdevtmpfs
24 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 netns
25 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 inet_frag_wq
26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kauditd
Step 9: Turn Off the swap memory by using the following command
root@linuxhelp:~# swapoff /dev/sdb1
Step 10: Remove the fstab entries for /dev/sdb1 partition
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
Step 11: Delete the /dev/sdb1 partition by using fdisk
root@linuxhelp:~# 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.
Delete the partition
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Save the all changes made to the disks
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Step 12: Check the swap memory by using the following command
root@linuxhelp:~# top
top - 02:45:40 up 56 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 290 total, 1 running, 289 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 : 1946.3 total, 425.8 free, 868.8 used, 651.8 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 898.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1195 linuxhe+ 20 0 4169812 256828 112084 S 2.0 12.9 0:39.83 gnome-shell
2233 linuxhe+ 20 0 416736 55656 42928 S 0.7 2.8 0:09.67 gnome-terminal-
738 root 20 0 177264 8580 7436 S 0.3 0.4 0:04.36 vmtoolsd
2304 root 20 0 0 0 0 I 0.3 0.0 0:00.49 kworker/0:0-events
2484 root 20 0 21576 4128 3324 R 0.3 0.2 0:00.03 top
1 root 20 0 98924 10936 7856 S 0.0 0.5 0:02.21 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_gp
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-events_highpri
9 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_rude_
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_trace
12 root 20 0 0 0 0 S 0.0 0.0 0:00.09 ksoftirqd/0
13 root 20 0 0 0 0 I 0.0 0.0 0:00.96 rcu_sched
By this creating and removing swap memory in CLI on Ubuntu 21.04 comes to an end