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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read