This document describes the process used to configure 8 GB of swap space on our servers.
Swap space is disk storage that the Linux operating system can use as virtual memory when physical RAM is under pressure. When the server begins running low on available RAM, the Linux kernel can move less frequently used memory pages from RAM to the swap area.
The configured swap file is located at:
/swapfile
The swap file size is:
8 GB
The following commands were used to create and activate the swap file:
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
The following entry was also added to /etc/fstab:
/swapfile none swap sw 0 0
This ensures that the swap file is automatically activated when the server boots.
sudo fallocate -l 8G /swapfile
The fallocate command creates a file and allocates disk space for it.
fallocate allocates disk space to a file.-l 8G specifies that the file should have a size of 8 GB./swapfile specifies the location and name of the file.After running the command, an 8 GB file named /swapfile exists on the server.
The storage allocated to the swap file comes from the server's disk space, not from physical RAM.
The file can be verified with:
ls -lh /swapfile
Expected output:
-rw-r--r-- 1 root root 8.0G /swapfile
At this stage, the file exists but has not yet been configured as swap space.
sudo chmod 600 /swapfile
This command restricts access to the swap file.
chmod changes file permissions.600 gives the file owner read and write permissions.The resulting permissions are:
rw-------
Because the file is owned by root, only the root user can read or modify the swap file.
This is important because memory pages moved to swap may contain sensitive information from running applications.
The permissions can be verified with:
ls -l /swapfile
Expected output:
-rw------- 1 root root 8589934592 /swapfile
sudo mkswap /swapfile
The mkswap command initializes the file as Linux swap space.
Before running this command, /swapfile is simply a regular file containing allocated disk space.
The mkswap command writes the necessary swap metadata to the file so that the Linux kernel can recognize and use it as swap space.
Typical output:
Setting up swapspace version 1, size = 8 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
At this stage, the swap file is properly formatted but is not yet active.
sudo swapon /swapfile
The swapon command immediately enables the swap file.
After this command is executed, the Linux kernel can begin using /swapfile as swap space when needed.
The active swap configuration can be verified with:
sudo swapon --show
Example output:
NAME TYPE SIZE USED PRIO
/swapfile file 8G 0B -2
Swap memory can also be viewed with:
free -h
Example output:
total used free shared buff/cache available
Mem: 15Gi 3Gi 8Gi 200Mi 4Gi 11Gi
Swap: 8.0Gi 0B 8.0Gi
The swap file is now active.
However, the swapon command only activates the swap file for the current running system.
Without additional configuration, the swap file may not automatically be activated after the server reboots.
To ensure the swap file is automatically activated during system startup, the following entry was added to:
/etc/fstab
Entry:
/swapfile none swap sw 0 0
The /etc/fstab file defines filesystems and swap areas that should be configured during the system boot process.
| Field | Value | Description |
|---|---|---|
| Swap File | /swapfile |
Location of the swap file |
| Mount Point | none |
Swap space does not require a filesystem mount point |
| Type | swap |
Identifies the resource as swap space |
| Options | sw |
Enables the swap area |
| Dump | 0 |
Disables filesystem backup using dump |
| Filesystem Check | 0 |
Prevents fsck from checking the swap file |
After adding this entry, the system will automatically activate /swapfile during the boot process.
The active swap areas can be displayed with:
sudo swapon --show
The server's overall RAM and swap usage can be viewed with:
free -h
The swap entry in /etc/fstab can be verified with:
grep swap /etc/fstab
Expected output:
/swapfile none swap sw 0 0
The swap file itself can be inspected with:
ls -lh /swapfile
Expected output:
-rw------- 1 root root 8.0G /swapfile
To test whether the /etc/fstab configuration is valid without rebooting the server, first disable the swap file:
sudo swapoff /swapfile
Verify that the swap file is no longer active:
sudo swapon --show
Then activate all swap entries defined in /etc/fstab:
sudo swapon -a
Verify the configuration again:
sudo swapon --show
If /swapfile appears in the output, the /etc/fstab configuration is working correctly.
The server was configured with an 8 GB swap file using the following process:
Allocate 8 GB of disk space
│
▼
Create /swapfile
│
▼
Restrict permissions to root
│
▼
Format the file as Linux swap space
│
▼
Activate the swap file
│
▼
Add the swap file to /etc/fstab
│
▼
Automatically activate swap during system boot
The final configuration provides the server with: