How to Create Swap Space in Linux for Beginners
Create Swap Space in Linux by using commands to allocate disk space for virtual memory. This extra memory helps improve system performance. Swap can be created with a file or a dedicated partition.
🤖AI Overview:
Create Swap Space in Linux refers to the process of adding additional virtual memory to a Linux system by designating part of the disk to be used as swap space. This helps the operating system manage memory more efficiently when physical RAM is full. Creating swap space ensures smoother performance by allowing the system to temporarily move inactive data from RAM to disk storage. This practice is useful for maintaining system stability and performance, especially on servers or devices with limited memory.
Steps to create a Linux swap file
By following the steps of this article, you can optimally manage the main storage memory of your server after purchasing a Linux VPS by creating Swap virtual memory. You can also add any amount of memory you need to the server (1G or 2G or so on).
Step 1: Displaying RAM and Swap space consumption
The first step is to view the current swap space if any, and RAM usage. For this purpose, use the following commands:
free -h
swapon --summary
It should be noted that each of the following commands can be used to display the used space of RAM and Swap:
swapon --summary
free -h
top
htop
vmstat
Step 2: Creating a new file for the Swap space
In this step, create a file with 1G space (this value can be different, we have considered 1G in this tutorial) to be used as Swap space. You can use two commands to create a swap file:
- Creating a swap file using the
fallocate
command:
sudo fallocate –l 1G /swapfile
Running the fallocate command will create a swap file when your operating system supports this command, but if you get the” fallocate failed: Operation not supported “ error message by running the above command, you have to run the next method, which is to use thedd
command.
- Creating a swap file using the
dd
command:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Step 3: Setting Linux file system permissions
To improve security, you must set permissions for the Swap file through the following command, and only the root user should have permission to read and write the Swap file.
sudo chmod -v 0600 /swap-file
Note: You can also use the following command to set the Swap file permissions:
sudo chown -v root:root /swap-file
Also, the following command is executed to access a list of permissions:
ls -l /swap-file
Step 4: Setting up the Swap area
To set the swap area, you must run the following command:
sudo mkswap /swapfile
Step 5: enabling Swap space
Running theswapon
command enables the Swap space:
sudo swapon /swapfile
Step 6: Enabling Swap File in Linux Permanently
To ensure that the changes(enabling the Swap file) are applied and saved after restarting the Linux operating system, Open the /etc/fstab file for editing:
sudo vi /etc/fstab
Then enter the following lines in the /etc/fstab file:
/etc/fstab
/swapfile swap swap defaults 0 0
Save and close the file.
Step 7: Checking if Swap is active
By executingswapon
orfree
commands, you will make sure that Swap is active:
sudo swapon --show
Output
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 507.4M -1
sudo free -h
Output
total used free shared buff/cache available
Mem: 488M 158M 83M 2.3M 246M 217M
Swap: 1.0G 506M 517M
Note: In the early steps of the article, we mentioned that other commands are also executed to check whether the Swap File is active.
Adjusting the value of swappiness
The swappiness feature determines the period and how the Linux kernel uses the swap space. That is, it determines how often and to what extent the system can use Swap virtual memory. The swappiness property allows setting values between 0 and 100 which is set to 60 by default.
The /proc/sys/vm/swappiness file contains swappiness values. To see the default value of swappiness, run the following command:
cat /proc/sys/vm/swappiness
Output
60
The default value of this feature is ideal for all Linux users, but you can change it if you need. For this, consider the following example as a model.
If you want to change the default value of swappiness to 30, run thesysctl
command:
sudo sysctl vm.swappiness=30
Then, to set this value instead of the default value of swappiness, you need to edit the /etc/sysctl.conf file and add the following line to the file:
vm.swappiness=30
We mentioned earlier that the default value of the swappiness feature is suitable for all users, but sometimes you need to change the default value, the value you want to replace should be appropriate to your system’s workload and memory usage.
Setting how to use Swap and file priority
By default, the Linux kernel uses the first active Swap space, and after using the first Swap space completely, it goes to the second Swap space and continues in the same way. Therefore, you can prioritize to use the Swap space. To prioritize the use of Swap space, you must open the /etc/fstab file and add the desired priority to the file according to our example:
/dev/sdb none swap sw 0 0
/swap-file swap swap pri=5 0 0
/swap-file-2 swap swap pri=10 0 0
Note: – 2 is the default priority that sets the priority in using Swap space.
In the example that we mentioned, the priority is to use /swap-file-2 with pri=10, and after its space is filled, the Linux kernel uses /swap-file with pri=5 and finally goes to /dev/ sdb.
Removing Swap file
Up to this part of the article, you have successfully added the swap file to the Linux system and learned the Practical settings related to the swap space. The last part of this article provided for solving your concern about how to remove the swap file. You may not need the swap file in some situations and you may don’t know how to remove the swap file. Don’t worry, we will guide you in disabling the swap file in this section. Follow the steps below:
- The first step is to disable Swap by running the following command:
sudo swapoff -v /swapfile
- In the /etc/fstab file, delete the
/swapfile swap swap defaults 0 0
entry.
Remove the swapfile by running the following command:
sudo rm /swapfile
FAQ
What command is executed to see the size of the swap space?
Different commands can be used to check the swap space size:
swapon -s
htop
- To access RAM usage and Swap space you can run the
free -m
command. - To view the swap space, one of the other methods is to refer to the /proc/swaps file.
Is it possible to clear the swap space?
Yes, for this, after ensuring enough RAM space, you must cycle off the swap so that the data is transferred to RAM again.
Conclusion
In this tutorial, you have successfully configured and created the Swap file in Linux to optimize Linux system memory management and increase available swap space. We hope that you can use your system memory optimally through our training.
You can also useman
commands to get more information about swap file settings:
man 8 mkswap
man 8 swapon
man 8 swapoff
man 8 mount
man 5 fstab
Conclusion
In summary, following these steps will help you create swap space in Linux easily and safely. Each step is designed to keep your data protected and your system efficient. Regularly checking your swap usage and making sure it is configured correctly is always a good habit for smooth system management.
FAQ
2. How do I check if my Linux system already has swap space configured?
You can check for existing swap space by using the “swapon --show” command or by viewing the output of the “free -h” command in your terminal. If there is an entry for swap, your system already has swap space configured.
3. What are the methods to create swap space in Linux?
There are two main methods to create swap space in Linux. You can either create a swap file or a swap partition. Creating a swap file is generally easier and more flexible for most beginners.
4. How do I create swap space using a swap file in Linux?
To create swap space using a swap file, you will need to use commands like “fallocate” or “dd” to create a file, set the correct permissions with “chmod”, format it as swap with “mkswap”, and enable it with “swapon”. Each step is simple and can be completed in the terminal.
5. What is the recommended size for swap space on a Linux server or desktop?
The recommended size for swap space depends on your system’s RAM and workload. For most users, swap space should be equal to or slightly larger than the amount of RAM, especially if you plan to use hibernation or run memory-intensive applications.
6. Can I add swap space to my Linux system without restarting it?
Yes, you can add swap space to your Linux system without a reboot. By creating and activating a swap file, changes take effect immediately, so your system can benefit from increased virtual memory right away.
7. How do I make my new swap space permanent in Linux?
To make your swap space permanent, you need to add its details to the “/etc/fstab” file. This ensures the swap space is activated automatically every time your system boots.
8. What are the risks or downsides of using swap space in Linux?
While swap space prevents crashes and helps manage memory, relying too much on it can slow down your system, since hard disks are much slower than RAM. It is best used as a backup when RAM is insufficient.
9. How can I remove or disable swap space in Linux if I no longer need it?
You can disable swap space temporarily with the “swapoff” command. To permanently remove a swap file or partition, delete the relevant entry in “/etc/fstab” and remove the swap file from your system.
10. Is creating swap space safe for beginners in Linux?
Yes, creating swap space is a safe and straightforward process if you follow step-by-step instructions carefully. Always make sure to have a backup of important data and use the recommended commands to avoid errors. If you need assistance, do not hesitate to contact technical support.