1 minute read



(copying from ChatGPT advice 😄 )

First, you need to create an empty file with the desired size. The dd command can do this. You will use the /swapfile path for the file.

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

In this command, bs=1M sets the block size to 1 megabyte and count=2048 specifies that 2048 blocks should be copied, resulting in a 2GB file.

Next, you need to set the correct permissions on the file. Only root should have read and write permissions.

sudo chmod 600 /swapfile

Now you can format the file for swapping:

sudo mkswap /swapfile

And finally, enable the swap file:

sudo swapon /swapfile

The swap file is now enabled for your current session. If you want the swap file to persist across reboots, you will need to add an entry to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

This appends the required configuration to the end of the fstab file.

Please note, excessive use of swap can degrade overall system performance and it is recommended to add more physical memory (RAM) for memory-intensive applications. Swap should be used as a buffer for temporary spikes in memory usage, not as an extension of your system memory.