Setup SWAP Memory on Ubuntu

Exported on 23-Aug-2021 16:07:55

This blueprint covers the steps necessary to add a swap file on Ubuntu.

Parameters

Name Type Script Reference Default Value Comment
s1 Linux / Unix Server s1
s1c Linux OS Credential s1c

1 - Create A Swap File

Start by creating a file which will be used for swap, here we are creating a swapfile of 1GB.

The connection details have changed from the last step.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
sudo fallocate -l 1G /swapfile

2 - Set Swap File Permissions

Set the correct permissions so that only the root user should be able to write and read the swap file.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
sudo chmod 600 /swapfile

3 - Setup Swap Area On The File

Use the mkswap utility to set up a Linux swap area on the file.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
sudo mkswap /swapfile

4 - Mount The Swap File

Activate the swap file using the following command

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
sudo swapon /swapfile

5 - Configure The Swap File To Mount On Reboot

To make the change permanent add the swapfile entry the /etc/fstab file

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
sudo bash -c 'echo "/swapfile swap swap defaults 0 0" >> /etc/fstab'

6 - Verify Swap Memory

Verify that the swap is active by using the swapon command.

Login as user on node

Connect via SSH
ssh user@hostname
This is a Bash Script make sure you run it with bash -l from a terminal session
sudo swapon --show