This will show you the current swap size, as well as the amount of used and available swap space:
free -h
Create a new swap file with the desired size. For example, to create a 1GB swap file:
sudo fallocate -l 1G /swapfile
Change the permissions on the new swap file so that it is only accessible to the root user:
sudo chmod 600 /swapfile
Format the new swap file as a swap partition:
sudo mkswap /swapfile
Enable the new swap partition:
sudo swapon /swapfile
To make the changes permanent, you need to add the new swap partition to the fstab file. Open the file by running the command:
sudo nano /etc/fstab
Add something like the following line (example):
/swapfile none swap sw 0 0
Or here's an example fstab file that I've configured for performance:
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/sda / ext4 errors=remount-ro 0 1
/dev/sdb none swap rw,noatime 0 0
/swapfile none swap rw,noatime 0 0
This will check the file system and tell you if there's an error with the fstab file, which you can fix before reboot:
sudo mount -a
Note: No output means there were no errors.
Check if the new swap file is active. You should see the new swap size in the output:
free -h
Finally, we can see that both swap devices are present:
sudo swapon -s
Comments
0 B
|👍
/👎