- Published on
Swap file
- Authors
- Name
- Jimmy Lai
Adding swap space
Adding swap space on Arch Linux
can be done in two primary ways: by creating a swap file or by setting up a swap partition. Swa
is useful for systems with limited RAM; it allows the system to use disk space for memory overflow. Using a swap file is generally easier and more flexible than partitioning, especially on systems that are already set up. Here’s how you can add a swap file on Arch Linux:
Creating a Swap File
Create a Swap File
First, you need to create a file that will serve as your swap space. You can create a swap file of the desired size using the dd command. For example, to create a 4GB swap file, you can use:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
This command will create a file called /swapfile with a size of 4GB.
###Set Permissions
For security, set the swap file's permissions so that only root can read and write to it:
sudo chmod 600 /swapfile
Make the File Usable as Swap
Format the file to a swap format using the mkswap command:
sudo mkswap /swapfile
Activate the Swap File
Turn on the swap file with the swapon command:
sudo swapon /swapfile
Make the Swap File Permanent
To ensure the swap file is used on system boot, add it to your /etc/fstab file. Open /etc/fstab in a text editor with root privileges:
sudo nano /etc/fstab
Add the following line at the end of the file:
/swapfile none swap sw 0 0
Save and close the file.
Check the Swap Status
You can verify that the swap is active by using:
swapon --show
Or:
free -m
Adjusting Swappiness (Optional)
The "swappiness" parameter controls how often your system uses swap space. It's a value between 0 and 100; a lower value makes the kernel try to avoid swapping whenever possible, while a higher value allows more aggressive swapping.
To temporarily change the swappiness value:
sudo sysctl vm.swappiness=10
To make this change permanent, add the following line to your /etc/sysctl.conf file:
Copy code
vm.swappiness=10
Conclusion
This approach of using a swap file is flexible and simple for most use cases. If you prefer a swap partition due to performance considerations or personal preference, the process involves using a partitioning tool like fdisk or gparted to create the swap partition, and then using mkswap and swapon to initialize and enable it, similarly to how the swap file is set up.