Create swap space on Linux

Updated . Posted . Visible to the public.

Create a 1 GB file to swap to (we have sufficient space on / on this machine. Use a different partition if necessary)

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

If you prefer 2GB swap, chose count=2048, 4GB: count=4096

Change permissions of swap file:

sudo chmod 0600 /var/swapfile

Set up swap file and enable it:

sudo mkswap /var/swapfile
sudo swapon /var/swapfile

You should see your swap space now:

thomas@machine:~$ free -m
              total       used       free     shared    buffers     cached
Mem:          7700       7655         45          0         75       1237
-/+ buffers/cache:       6342       1357
Swap:         2047          0       2047

If you want to make the changes permanent add those lines to /etc/fstab

/var/swapfile  none  swap  sw  0 0
Thomas Eisenbarth
Last edit
Deleted user #4941
Source code in this card is licensed under the MIT License.