How to create SWAP in Linux

On the test I will create a SWAP partition in Ubuntu Server.
So, we think what size of the paging file we need and create an empty file:

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

In my case, 1 Gigabyte is specified, bs can be specified in K, M, G or simply in bytes without letters.
The size of the paging file is usually roughly equal to the size of the RAM, if there is a lot of RAM, then in this case you can do without it.

Let’s show the system what this swap-file is:

sudo mkswap /swapfile

Connect it:

sudo swapon /swapfile

To disable it, you can use the command:

sudo swapoff /swapfile

Let’s see the result by typing the following commands:

swapon -s

Information about the SWAP size should appear.

To automatically connect swap at system startup, open the /etc/fstab file, for example, in the nano editor (Ctrl+X to exit the editor, y/n to save or cancel changes):

sudo nano /etc/fstab

And add at the end of the line:

/swapfile none swap sw 0 0

Done, the swap file is active and will automatically connect when the system starts.

See also my article:
Linux Partitioning

Leave a comment

Leave a Reply