The Linux swapon command serves the purpose of activating a swap partition in Linux systems. A swap partition is essentially a dedicated space used when the system's RAM (Random Access Memory) becomes fully utilized.
It temporarily holds data that can be retrieved when required, making it particularly beneficial for systems with limited RAM capacity.
Swap space can take the form of either a device or a file. Typically, a device refers to a partition in most distributions, whereas a swapfile is a distinctive type of file located within the root directory (/). The swap partition or the swapfile is known as the specialfile which gives you two choices to activate it.
- -L Label: You can use this option to specify the device or file by its label, which is a human-readable name assigned to the device or file.
- -U uuid: You can use this option to indicate the device or file by its Universally Unique Identifier (UUID). A UUID is a unique and standardized identifier assigned to the device or file, providing a highly precise way of specifying it.
Note: You can utilize swapoff as an alternative to swapon for disabling a swap partition or swapfile. swapoff follows the same set of parameters as swapon.
Options available in `swapon` command in Linux
|
Enable all swap devices listed in /etc/fstab.
|
Enable the discard option for swap devices.
|
Set the priority of the swap device.
|
Display swap usage summary.
|
Display verbose information during execution.
|
Examples
1. `-a`, `-all` option in swapon
This command serves the purpose of enabling all swap partitions and swap files listed in the /etc/fstab entry. To review the comprehensive list of available swap partitions and files on your system, you can utilize the cat /etc/fstab command.
$ swapon -a
or
swapon -all
2. `-d`, `--discard` option in swapon
The discard option is related to the use of the TRIM command for SSDs (Solid State Drives). When this option is enabled on a swap device, it indicates that the swap space should inform the SSD when data is no longer in use and can be safely erased, essentially marking the space as free for future use.
$ swapon --discard
Commnad: swapon --discard3. `-p`, `--priority` option in swapon
This option serves to define a priority level for the swap space. Swap areas with higher priorities will be exhausted before lower-priority ones are utilized.
$ swapon -p <priority number> <path to swapfile or partition>
Setting priority to 1 for the swapfile4. `-s`, `--show` option in swapon
The --show option, when used, displays a comprehensive list of all currently active swap spaces on the system.
$ swapon --show
Showing all the active swap spaces5. `-v`, `--verbose` option in swapon
The --verbose option is used to provide detailed and verbose information during the execution of a command.
$ swapon --all --verbose
The --verbose option provides a detailed information regarding the ongoing execution process.Commonly Used Commands
1. Using the --help option
The --help option in the swapon command is a valuable resource for users as it offers a comprehensive and detailed list of all available options, flags, and features.
Complete set of options and flags provided by the swapon command2. Getting the summary of all swap partitions
The purpose of this command is to provide a summary of utilized swap devices.
$ swapon -s # or --summary
Command to show the summary of all swap partitions3. Activating a single swap area
When you supply a path to the swapon command, it activates a single swap area instead of enabling all swap areas simultaneously.
$ swapon <path to swap area>
Activating a single swap spaceTroubleshooting
The swap area is present in the system but remains inactive
If you encounter any issues with activating your swap partition, it's advisable to inspect your /etc/fstab file and verify that the swap area you're attempting to activate is listed there. If it's not present, you can follow the procedure given below to append a new swap area entry to the /etc/fstab file.
Open your /etc/fstab file with your preferred text editor as root, such as Emacs, Vim, or Nano, and append the provided line to the end of the file.
<path to swap area> swap swap defaults 0 0
Now, execute the command - swapon <path to swap area> to activate your swap area.
Changing Permissions
You want to ensure that only the root user has access to the swap area. To determine the owner of your swap area, execute the following command:
$ ls -la <path to your swap area> | grep swapfile

In the screenshot above, the default user is root. However, if this isn't the case for your system, you might need to alter the ownership. Follow these commands to do so:
$ sudo chown root:root <path to swap area>
$ sudo chmod 0600 <path to swap area>
The commands mentioned above will modify the ownership of the swap area and adjust the permissions for the root user.
Conclusion
In this article, we've delved into the swapon command and its practical applications. Swapon serves as a robust tool for managing swap spaces in various Linux distributions. It typically comes pre-installed in the majority of distributions, leaving minimal room for oversight by maintainers. Additionally, I've addressed two prevalent troubleshooting issues related to swap spaces: adding a swap entry to the /etc/fstab file and modifying the ownership of a swap area.
Similar Reads
Shift command in Linux with examples
Shift is a built-in command in bash that after getting executed, shifts/moves the command line arguments to one position left. The first argument is lost after using the shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command
3 min read
rev command in Linux with Examples
rev command in Linux is used to reverse the lines characterwise. This utility reverses the order of the characters in each line by copying the specified files to the standard output. If no files are specified, then the standard input will be read. Here, we will explore the syntax, examples, and opti
3 min read
How to Check Swap Space in Linux
Swap space is like an extra space in your computer's memory. When your computer's main memory (RAM) gets full it uses this extra room (swap space) to store things that aren't being used right now. This extra room is located on your computer's hard drive. Keeping an eye on how much of this extra room
5 min read
Sed Command in Linux/Unix With Examples
The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing. With SED, you can manipulate text files wi
9 min read
How to Create a Swap File on Linux?
Linux Operating System has some different features that are unique and not present in any other major operating system like Windows. The Linux Memory Distribution is one of them. The Memory Space in Linux is designed in such a way that the efficiency of the Memory Management could be the best. The L
4 min read
How to Permanently Disable Swap in Linux?
Swapping or swapping space is a physical memory page placed at the top of a disk partition or a special disk file that is used to expand a system's RAM as the physical memory fills up. Inactive memory pages are often dumped into the swap area when no RAM is available, using this method of expanding
3 min read
unordered_set swap() in C++ STL
The swap() method of âunordered_setâ swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_set object containing elements of the same type but the sizes may differ. After the ca
3 min read
unordered_multiset swap() in C++ STL
The swap() method of âunordered_multisetâ swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_multiset object containing elements of the same type but the sizes may differ. Af
3 min read
Program to Interchange or Swap Columns in a Matrix
Given a matrix having m rows and n columns, the task is to write a program to interchange any two Columns(i.e. column no. N and M given in the input) in the given matrix. Example: Input : N = 1, M = 2, mat[][] = {{2, 1, 4}, {1, 2, 3}, {3, 6, 2}}Output: mat[][] = {{1, 2, 4}, {2, 1, 3}, {6, 3, 2}} Inp
6 min read
Difference between Paging and Swapping in OS
Proper memory management in any operating system is required for the smooth execution of numerous processes. Two fundamental techniques include paging and swapping. In Paging, the memory of a process is divided into fixed-size pages; this permits non-contiguous memory allocation and minimizes fragme
4 min read