Create and Setup SWAP Memory on Ubuntu

Exported on 28-Sep-2021 12:44:29

Configure SWAP Memory on Ubuntu With AttuneOps

This Blueprint Configures SWAP Memory on Ubuntu With AttuneOps

Swap memory or swap space is the on-disk component of the virtual memory system. Memory swapping is a computer technology that enables an operating system to provide more memory to a running application or process that is available in physical random access memory (RAM). In short, swap memory is the dedicated amount of hard drive that is used whenever RAM runs out of memory.

Pre-Blueprint Attune setup
  1. On the Inputs tab, create a Linux node for the host you wish to install the stack on.
  2. On the Inputs tab, create Linux credentials to connect to the host you wish to install the stack on.
Steps Involved
  • Create a Swap File
  • Configure SWAP file and mount it
  • Configure SWAP to mount on system-boot.

Parameters

Name Type Script Reference Default Value Comment
Linux Node Linux / Unix Server linuxNode
Linux User Linux OS Credential linuxUser

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