• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to create and remove swap memory in CLI on Ubuntu 21.04

  • 00:41 lsb_release -a
  • 00:55 top
  • 01:12 fdisk /dev/sdb
  • 02:44 mkswap /dev/sdb1
  • 03:11 swapon /dev/sdb1
  • 03:34 vi /etc/fstab
  • 04:20 mount -a
  • 04:32 top
  • 04:47 swapoff /dev/sdb1
  • 05:02 vi /etc/fstab
  • 05:27 fdisk /dev/sdb
  • 06:02 top
6758

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

Tags:
matthew
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

How to view available swap memory?

A

To view available swap memory use the command "swapon –s".

Q

How to deactivate swap?

A

To deactivate swap using "swapoff " command.

Q

What is the HEX code of Snap?

A

The hex code for SWAP is 82.

Q

How to update the partition table?

A

Update the partition table by using "partx –a" command.

Q

When swap memory is used?

A

Swap memory is mainly used when physical memory or RAM is fully occupied, Sometimes to increase performance RAM or physical memory is better used for disk cache than to store program memory, So Swap Memory is also used that program datas are transferred from Physical memory then the remaining free RAM or physical memory is effectively used for disk cache though it increases performance.

Related Tutorials in How to create and remove swap memory in CLI on Ubuntu 21.04

Related Tutorials in How to create and remove swap memory in CLI on Ubuntu 21.04

How to install Meld tool in Ubuntu
How to install Meld tool in Ubuntu
Feb 25, 2017
How to install Dconf-Editor on Ubuntu 18.04
How to install Dconf-Editor on Ubuntu 18.04
Jul 14, 2018
How to install and update OpenSSL on Ubuntu 16.04
How to install and update OpenSSL on Ubuntu 16.04
Mar 9, 2017
How to install GLib 2.0 on Ubuntu 17.04
How to install GLib 2.0 on Ubuntu 17.04
May 22, 2017
How to Install Android Emulator on Ubuntu 20.4.1
How to Install Android Emulator on Ubuntu 20.4.1
Jul 13, 2021
How To Install AnyDesk on Ubuntu 16.04
How To Install AnyDesk on Ubuntu 16.04
Apr 4, 2018
How to install Genymotion 2.12.1 on Ubuntu 18.04
How to install Genymotion 2.12.1 on Ubuntu 18.04
Jul 9, 2018
How to install Timeshift 18.4 on Ubuntu 18.04
How to install Timeshift 18.4 on Ubuntu 18.04
Jul 6, 2018

Related Forums in How to create and remove swap memory in CLI on Ubuntu 21.04

Related Forums in How to create and remove swap memory in CLI on Ubuntu 21.04

Ubuntu
matthew class=
Failed to enable unit: Refusing to operate on linked unit file sshd.service
Apr 15, 2019
Ubuntu
mason class=
Passwd: You may not view or modify password information for root On Ubuntu 19.04
May 27, 2019
Ubuntu
isaac class=
/etc/apt/sources.list Permission denied
May 18, 2017
Ubuntu
yousuf class=
lsb_release command not working : Debian
Jan 18, 2018
ifconfig command
jackbrookes class=
what is the location of the ifconfig program on your machine?
Jan 4, 2018
Ubuntu
mason class=
"E: Package 'php-mcrypt' has no installation candidate" error on Ubuntu 20.4.1
Mar 15, 2021
NFS
luke class=
clnt_create: RPC: Program not registered
Apr 25, 2017
Apache
isaac class=
How to disable apache welcome page on Ubuntu
Dec 15, 2018

Related News in How to create and remove swap memory in CLI on Ubuntu 21.04

Related News in How to create and remove swap memory in CLI on Ubuntu 21.04

How To Install Mixxx on Ubuntu 16.04
How To Install Mixxx on Ubuntu 16.04
Oct 11, 2017
Ubuntu 17.04 released with greater expectations
Ubuntu 17.04 released with greater expectations
Apr 15, 2017
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Ubuntu Core now available on i.MX6 based TS-4900 thanks to Technologic Systems Inc.
Mar 1, 2017
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Ubuntu 17.10 Artful Aardvark Beta 1 is now here. Download Now
Sep 2, 2017
Ubuntu Unity is no more: One Linux dream has been axed
Ubuntu Unity is no more: One Linux dream has been axed
Apr 7, 2017
What’s next for Ubuntu Linux Desktop?
What’s next for Ubuntu Linux Desktop?
Apr 11, 2017
Say Hi to Ubuntu's new mascot
Say Hi to Ubuntu's new mascot
Mar 22, 2019
KDE Connect App was removed from Google Play Store and brought back in 24 hours
KDE Connect App was removed from Google Play Store and brought back in 24 hours
Mar 22, 2019
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help legeek ?
Installation of the call center module

hello

I wish to install a call center in virtual with issabel, I downloaded the latest version of it , but I don' t arrive to install the call center module in issabel. please help me

thanks!

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.