Creating Swap File
A swap file is a system file that creates temporary storage space on a solid-state drive or hard disk when the system runs low on memory. The file swaps a section of RAM storage from an idle program and frees up memory for other programs.
1. Create a Swap File
The following command creates a file named /swapfile
and sets its size to 4 GB. This file will be used as the swap area.
sudo fallocate -l 4G /swapfile
When fallocate
fails with the error Operation not supported
, you can use the dd
command as an alternative method to create a swap file. This guide walks you through the steps to create and enable a swap file using dd
on Ubuntu.
Run the following command to create a 4GB swap file using dd
:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
if=/dev/zero
: Reads from/dev/zero
, which provides null bytes.of=/swapfile
: Output file location, i.e.,/swapfile
.bs=1M
: Block size of 1MB.count=4096
: Number of blocks, making a 4GB swap file.
2. Set the Correct Permissions
To secure the swap file, set the appropriate file permissions:
sudo chmod 600 /swapfile
This restricts access to the root user.
3. Format the File as Swap
Turn the newly created file into swap space:
sudo mkswap /swapfile
This formats the file as a valid swap area.
4. Enable the Swap File
Activate the swap file for immediate use:
sudo swapon /swapfile
At this point, the system will start using the swap file.
5. Make the Swap File Permanent
To ensure that the swap file is automatically used after reboot, add it to the /etc/fstab
configuration file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
This command appends the necessary line to /etc/fstab
.
Verify the Swap
To confirm that the swap is active, use:
sudo swapon --show
This will display all active swap areas.
Notes
- You can adjust the swap file size by changing the
count=
value (for example,count=2048
for 2GB). - If you ever need to remove the swap file, first deactivate it using
sudo swapoff /swapfile
, then delete the file usingsudo rm /swapfile
.
By following these steps, you ensure your system has a functional swap space that will persist across reboots.