You are on page 1of 1

Creating and Enabling a Static Swapfile

Tip: There is no reason you can't have both a swap partition and a swapfile. This is an easy way to add more
swap without repartitioning

First create and intialize the file to hold the swap. For example, to create a 4GB swapfile, you could use the
command:

sudo fallocate -l 4G /swapfile


sudo mkswap /swapfile

Set the appropriate permissions on the file. It should be readable and writable only by root. This can be done
with the command:

sudo chmod u=rw,go= /swapfile

Next we need to enable the swapfile with the swapon command. Following our example above this could be
done with:

sudo swapon /swapfile

In order to ensure that the swap is enabled at boot we can add an entry to /etc/fstab. You can add the line to
ftab manually or using the command:

sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab"

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Having no swap partition might be a problem if you try to set the machine to sleep. What you can do is:
1. boot on a live cd
2. shrink one of this partitions using something like gparted
create a swap partition Then you need to add the swap partition to fstab. To discover its UUID do:
3. ls -l /dev/disk/by-uuid
Then add the following line to fstab:
UUID=<swap partition uudid> swap
swap defaults,noatime,discard 0 0
If you are afraid of messing the partitions you might consider creating a swap file:
dd if=/dev/zero of=/swapfile bs=1M count=512
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile
Then add it to fstab:
/etc/fstab
/swapfile none swap defaults 0 0

You might also like