Cyberithub

10 Useful swapon command examples in Linux (RedHat/CentOS 7/8)

In this article, we will go through 10 Useful swapon command examples in Linux (RedHat/CentOS 7/8). swapon command is used to enable device/partition swap in Linux. This command is also used to manage and check the swap stats. In this session, we will go through multiple examples of swapon command to understand its usages.

10 Useful swapon command examples in Linux (RedHat/CentOS 7/8) 1

swapon command examples

Also Read: How to Disable or Delete Logical Volume (LVM) in Linux Using 4 Easy Steps

Example 1: Check swapon version

If you want to check the swapon command version then you need to run swapon --version command as shown below. As you can see from output, current version is 2.23.2

[root@localhost ~]# swapon --version
swapon from util-linux 2.23.2

–version : Display version information and exit

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to User.

Example 2: Display Swap Stats using swapon -s command

If you want check the summary of all currently enabled swap devices and partitions the you need to use swapon -s command as shown below.

[root@localhost ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 776     -2
/root/testswap                          file            282056  0       -3
/dev/dm-2                               partition       204796  0       -4

-s : Display swap usage summary by device. More information on swapon man page.

Example 3: Enable Discard using swapon -d command

If you want to enable discard option then you need to use swapon -d command as shown below. Check How to Add the swap space in Linux (RedHat/CentOS 7/8) Using Best Methods to know more about adding swap space in Linux.

[root@localhost ~]# swapon -d
NAME           TYPE        SIZE USED PRIO
/dev/dm-1      partition     2G 776K   -2
/root/testswap file      275.5M   0B   -3
/dev/dm-2      partition   200M   0B   -4

-d : Enable swap discards, if the swap backing device supports the discard or trim operation.

Example 4: Skip devices which do not exist using swapon -e

If you want to silently skip all the devices that does not exists then you need to use swapon -e command as shown below.

[root@localhost ~]# swapon -e
NAME           TYPE        SIZE USED PRIO
/dev/dm-1      partition     2G 776K   -2
/root/testswap file      275.5M   0B   -3
/dev/dm-2      partition   200M   0B   -4

-e : Silently skip devices that do not exist.

Example 5: Use Verbose Option -v with swapon command

If you want to use verbose option then you need to use -v option with swapon command as shown below.

[root@localhost ~]# swapon -v
NAME           TYPE        SIZE USED PRIO
/dev/dm-1      partition     2G 776K   -2
/root/testswap file      275.5M   0B   -3
/dev/dm-2      partition   200M   0B   -4

-v : Be verbose.

Example 6: Reinitialize Swap using swapon -f command

If you want to reinitialize the swap space then you need to use swapon -f command as shown below.

[root@localhost ~]# swapon -f
NAME           TYPE        SIZE USED PRIO
/dev/dm-1      partition     2G 776K   -2
/root/testswap file      275.5M   0B   -3
/dev/dm-2      partition   200M   0B   -4

-f : Reinitialize (exec mkswap) the swap space if its page size does not match that of the current running kernel.

Example 7: Show swap partition information using swapon command

If you want to see only selective information about active swap devices then you need to use swapon --show command as shown below. In this example, we are trying to check NAME and TYPE of all active swap partitions using swapon --show=NAME,TYPE command as shown below.

[root@localhost ~]# swapon --show=NAME,TYPE
NAME           TYPE
/dev/dm-1      partition
/root/testswap file
/dev/dm-2      partition

–show : Display a definable table of swap areas.

Example 8: Show priority swap device using -p option

If you want to show all the partition with certain priority then you need to use -p option with swapon command. In this example we are trying to check all the swap partition which has priority set to -2.

[root@localhost ~]# swapon -p -2
NAME        TYPE    SIZE USED PRIO
/dev/dm-1 partition  2G  264K -2

-p : Specify the priority of the swap device.

Example 9: Do not show headings 

If you do not want to show the headers then you can use swapon --noheadings command as shown below. As you can see from below output headers like NAME, TYPE, SIZE, USED and PRIO are not visible now.

[root@localhost ~]# swapon --noheadings
/dev/dm-1      partition     2G 776K   -2
/root/testswap file      275.5M   0B   -3
/dev/dm-2      partition   200M   0B   -4

–noheadings : Do not print headings when displaying –show output.

Example 10: Enable Swap on Device or Partition using swapon command

If you want to enable swap for any of the partition then you need to use swapon /dev/vol_grp/log_vol command as shown below.

[root@localhost ~]# swapon /dev/vol_grp/log_vol

Then check the swap status again by running swapon -s command as shown below.

[root@localhost ~]# swapon -s
Filename            Type      Size   Used Priority
/dev/dm-1         partition  2097148  776   -2
/root/testswap     file      282056    0    -3
/dev/dm-2         partition  204796    0    -4

Example 11: Check other swapon command options

If you want to check all the other available options with swapon command then you need to use swapon --help command as shown below.

[root@localhost ~]# swapon --help

Usage:
swapon [options] [<spec>]

Options:
-a, --all enable all swaps from /etc/fstab
-d, --discard[=<policy>] enable swap discards, if supported by device
-e, --ifexists silently skip devices that do not exist
-f, --fixpgsz reinitialize the swap space if necessary
-p, --priority <prio> specify the priority of the swap device
-s, --summary display summary about used swap devices
--show[=<columns>] display summary in definable table
--noheadings don't print headings, use with --show
--raw use the raw output format, use with --show
--bytes display swap size in bytes in --show output
-v, --verbose verbose mode

-h, --help display this help and exit
-V, --version output version information and exit

The <spec> parameter:
-L <label> synonym for LABEL=<label>
-U <uuid> synonym for UUID=<uuid>
LABEL=<label> specifies device by swap area label
UUID=<uuid> specifies device by swap area UUID
PARTLABEL=<label> specifies device by partition label
PARTUUID=<uuid> specifies device by partition UUID
<device> name of device to be used
<file> name of file to be used

Available discard policy types (for --discard):
once : only single-time area discards are issued. (swapon)
pages : discard freed pages before they are reused.
* if no policy is selected both discard types are enabled. (default)

Available columns (for --show):
NAME device file or partition path
TYPE type of the device
SIZE size of the swap area
USED bytes in use
PRIO swap priority

For more details see swapon(8).

–help : Display help text and exit

 

 

 

Popular Recommendations:-

Create SAN Certificate

How to Enable or Disable SELinux Temporarily or Permanently on RedHat/CentOS 7/8

10 Popular Examples of sudo command in Linux(RedHat/CentOS 7/8)

9 useful w command in Linux with Examples

12 Most Popular rm command in Linux with Examples

Create a Self Signed Certificate using OpenSSL

Leave a Comment