The “swapon” command lets you define which devices you want the paging and swapping to occur. The command has different options that let you work with your swap space comfortably, including monitoring it. We will cover the “swapon” command in detail throughout this post.
Understanding the Swap Space
In a Linux system, swap space is essential to keep the system running even when RAM is unavailable. Sometimes, you may get a case where your physical RAM is exhausted. When that happens, Linux will switch to the available swap space to keep your tasks uninterrupted. The swap space is a virtual memory that you create on your Linux system. You will use the “swapon” command to adjust or monitor the swap files or devices.
Working with Sudo Swapon Command in Linux
Swapon is a Linux command that is utilized when you want to set where the paging and swapping will occur. Its basic syntax is:
The arguments represent the options that you add when performing a swapon activity. As for the file, it is added when you want to target a given swap file, but you can run your “swapon” command without specifying any file.
Let’s begin by confirming that we have swapon on our system. For that, run the following command:
Swapon is installed on Linux systems, and you should get an output which shows the swapon version that runs on your Linux system.
1. Checking the Swap Summary
When you enabled the swap files or devices on your Linux system, you can access the swap usage summary using the “swapon” command. You must add the “-s” or “—summary” option when executing the command.
Use the following command:
The given output shows that our system only has one swap file. You might get a different output for your case. Let’s proceed and see how to create a swap file.
2. Creating a Swap File
When you have adequate space on your hard drive, you can create a swap file to offer a swap space for your RAM. The first step is to verify the available space using the “df” command.
We use the “/dev/sda3” partition to create our swap file.
To create a swap file named “testswapfile,” we must use the fallocate program and specify the size of the swap space that we want to create. For the following example, we want a swap file of 1G:
Next, verify that the created swap file has met your specified size requirements.
You then must change the swap file permissions to set the permission to 600. Use the “chmod” command for that and specify the swap file.
After changing the permissions, you must mark your file as a swap space. For that, run the following “mkswap” command:
You will get an output which shows that the swap space has been set up.
Lastly, use the “swapon” command to enable the swap file.
That’s it. You now have a temporary swap file created on your Linux system. You can rerun the swapon “-s” command to list the available swap usage information on your system.
The following output confirms that our system has two swap files:
Other Swapon Options
The “swapon” command has different options that you can use when executing it to achieve an intended goal. Let’s detail some of the commonly used options.
To skip those devices that don’t exist and only show those that exist when displaying the swap usage summary, add the “–e” or “—ifexists” option. We also add the “–v” verbose option in the following example:
To enable all the swap files and devices that are listed in your “/etc/fstab” file, run the “-a” option.
If you only want to enable a specific swap file or device, specify its name instead of the “-a” option.
When checking the swap usage summary, specify which properties to display in the output information. For instance, to only show the name and size of the swap file or device, we run our command as follows:
To avoid showing the headings in the output, add the “—noheadings” option.
To display the size of the swap file or device in bytes, run the following command:
Conclusion
The sudo “swapon” command helps with managing the swap space on Linux systems. We explained what swap space means and understood what the sudo “swapon” command does, giving various examples. Hopefully, the sudo “swapon” command is now clear.