Initial State Verification
Evaluate current swap utilization using free -h. Prior to making modifications, deactivate existing swap instances to prevent conflicts.
swapoff -a
Constructing the Swap File
Allocate disk space for swap funcsionality. Execute the following command to generate a 4GB file named swapfile within /opt.
dd if=/dev/zero of=/opt/swapfile bs=1M count=4096
Key parameters:
if: Specifies the input stream (typically/dev/zero).of: Defines the output destination path.bs: Sets block size in bytes.count: Determines total blocks copied.
Convert this raw file into executable swap space.
mkswap /opt/swapfile
Configuring Kernel Behavior
Control memory pressure response using vm.swappiness. Query current values with:
sysctl vm.swappiness
Define the desired behavior in /etc/sysctl.conf. Lower numbers prioritize physical RAM usage over swapping out.
vm.swappiness = 10
Implementing Configuration
Load new sysctl parameters without rebooting. Subsequently, activate the newly created swap partition.
sysctl -p
swapon /opt/swapfile
Securing Mount Persistence
Update /etc/fstab to ensure swap remains active across reboots. Append a line specifying the file and its attributes.
/opt/swapfile swap swap defaults 0 0
Using UUIDs is recommended for robustness against device name changes.
UUID=<uuid-string> swap swap defaults 0 0
Field Definitions
- Device Identifier: Can be a path, label, or UUID.
- Target Mount: Should be set to
swap. - File System: Denoted as
swap. - Flags: Typically
defaultsapplies standard read/write and execution permissions. - Backup Frequency:
0indicates nodumpbackup required. - FSCK Order:
0skips consistency checks at boot.