How to setup swap on Ubuntu?

How to setup swap on Ubuntu?

December 27, 2023
Last Updated
Last updated December 27, 2023
A short guide to setting up swap on your Ubuntu server

Setup Swap

To set up swap on your Ubuntu server, you can follow these general steps:

Login into your server

ssh ubuntu@lightsail-ipv4-address-here
Access your AWS EC2 / Lightsail Amazon Linux 2 VM using the ssh command:

Switch to the root user

Login as the root user using the sudo command:
sudo su

Create a swapfile

Type the dd command to create a new 8GB swap file storage named /aws-swapfile-1 (1M * 8192 counts {8*1024} == 8GB):
dd if=/dev/zero of=/aws-swapfile-1 bs=1M count=8192 status=progress
The status=progress option is passed to the dd command to display progress on your screen as follows:
8588886016 bytes (8.6 GB) copied, 132.245227 s, 64.9 MB/s 8192+0 records in 8192+0 records out 8589934592 bytes (8.6 GB) copied, 132.257 s, 64.9 MB/s
The /dev/zero is used as a character stream for initializing data storage for the swap file.

Set appropriate permissions

Set permission using the chmod command for security reasons:
chmod -v 0600 /aws-swapfile-1
The verbose info indicates that only our root user and system Linux kernel can access the swap file:
mode of ‘/aws-swapfile-1’ changed from 0644 (rw-r--r--) to 0600 (rw-------)
Next use the mkswap command to set up a Linux swap area using the file named /aws-swapfile-1. For example:
mkswap /aws-swapfile-1
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes) no label, UUID=6713d1b4-13d0-44d5-908f-f7479da3401e

Enable the swapfile

Enable swap space on your AWS/Lighsail Amazon Linux for paging and swapping using the swapon command:
swapon -v /aws-swapfile-1
The system confirms that I allocated memory to work as swap space in an Amazon EC2 instance:
swapon: /aws-swapfile-1: found signature [pagesize=4096, signature=swap] swapon: /aws-swapfile-1: pagesize=4096, swapsize=8589934592, devsize=8589934592 swapon /aws-swapfile-1

Verify the swapfile

Verify it using the swapon command/free command or top command/htop command:
swapon -s htop free -h -t

Persist the swapfile

Edit the /etc/fstab file using a text editor:
nano /etc/fstab
and append the following line to turn on SWAP at boot time:
/aws-swapfile-1 swap swap defaults 0 0

Increase Swap

To increase the swap size on your Ubuntu server from 1GB to 2GB, you can follow these general steps:

Turn off the existing swap file

Before resizing the swap, you need to deactivate the current swap file. You can do this with the command:
sudo swapoff /swapfile

Resize the swap file

To increase the size of your swap file to 2GB, you can use the fallocate command (or dd if fallocate isn't available). The command will look something like this:
sudo fallocate -l 2G /swapfile
Or, if using dd:
sudo dd if=/dev/zero of=/swapfile bs=1G count=2
These commands will create a 2GB file named /swapfile.

Make the file a swap file

After creating the file, you need to designate it as swap space:
sudo mkswap /swapfile

Set the correct permissions

For security reasons, the swap file should have restricted permissions:
sudo chmod 600 /swapfile

Turn on the swap file

Finally, activate the new swap file:
sudo swapon /swapfile

Make the change permanent

To ensure that the swap file is activated on boot, you need to add it to the /etc/fstab file. You can do this by editing the file with a text editor like nano or vim, and adding the following line:
/swapfile swap swap defaults 0 0

Verify the swap is active

After completing these steps, you can verify that your swap is active by using:
swapon --show
This is a general guide, and the exact commands might vary slightly based on your specific system configuration and the tools available on your server. It's important to ensure you have adequate disk space for the swap file and to perform these operations with caution, as they can affect system stability if done incorrectly.