How To Setup a Swap Partition in Ubuntu on EC2

Many cloud servers don’t come with a swap partition setup by default, including Amazon EC2 servers. Luckily it’s fairly simple to setup. Below were using the file location of /var/swap1 but you can put the swap anywhere you want.

Create a 4 gig file

You can change the 4096 to whatever size you want in megabytes.

sudo /bin/dd if=/dev/zero of=/var/swap1 bs=1M count=4096


Secure swap file

Makes sure that swap file is locked down from prying eyes.

sudo chown root:root /var/swap1
sudo chmod 600 /var/swap1


Turn file into a swap file

sudo /sbin/mkswap /var/swap1


Activate swap file

Let’s the system know about the swap

sudo /sbin/swapon /var/swap1


Turn swap on by default on boot

Edit /etc/fstab and add this line below as the last line in the file.

/var/swap1 swap swap defaults 0 0

Leave a Reply

Your email address will not be published. Required fields are marked *