How to enable swap on Linux
To enable swap permanently on Linux, you can follow these steps:
- Create a swap file or partition: If you haven't already created a swap file or partition, you can use the fallocate command to create a swap file of a desired size. For example, to create a 2GB swap file, you can use the following command:
sudo fallocate -l 2G /swapfile
- Change the permissions of the swap file: You should change the permissions of the swap file to only allow root to read and write to it. You can use the
chmod
command to do this:
sudo chmod 600 /swapfile
- Format the swap file: You should format the swap file as a swap area using the mkswap command:
sudo mkswap /swapfile
- Add the swap file to the system: To enable the swap file permanently, you need to add it to the system's
/etc/fstab
file. Open the file in a text editor with sudo privileges:
sudo nano /etc/fstab
- Add the following line at the end of the file to mount the swap file on boot:
/swapfile none swap sw 0 0
Save and close the file.
- Enable the swap file: To enable the swap file immediately, you can use the swapon command:
sudo swapon /swapfile
You can verify that the swap file has been enabled by running the free command:
free -h
This command should show the amount of swap space available in addition to the system's RAM.