Best Practices for Swap File Location in Linux: Performance and Filesystem Considerations


10 views

When creating swap files in Linux, placement matters more than many administrators realize. While /var/swapfile works functionally, it's not always the optimal location. Modern Linux distributions handle swap differently:

# Standard swap creation commands
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

The Filesystem Hierarchy Standard doesn't mandate swap file locations, but provides guidelines:

  • /var: Meant for variable data - acceptable but not ideal
  • / (root): Simple but lacks organization
  • Custom location: Best for specific performance tuning

For best performance, consider these factors:

# Check disk performance before placement
hdparm -Tt /dev/sdX
# or
sudo fio --filename=/mnt/test --size=2G --runtime=1m --ioengine=libaio --direct=1 --name=seq-read --rw=read --bs=128k

Optimal locations include:

  1. Fastest available storage device
  2. Separate partition from busy system directories
  3. Away from high-IO paths like /var/log

In production environments, we often see:

# Common enterprise locations
/mnt/swap/swapfile
/opt/swap/swapfile
/home/swap/swapfile

Each has advantages:

Location Advantage Disadvantage
/mnt/swap Clear purpose Requires separate mount
/opt/swap Follows optional software standard Might get overlooked
/home/swap User-space isolation Permission complexities

For cloud instances and containers, best practices differ:

# Docker/Kubernetes approach
mkdir -p /etc/docker/swaps
fallocate -l 1G /etc/docker/swaps/container_swap

Key recommendations:

  • Avoid swap in ephemeral storage
  • Place in persistent volumes when possible
  • Consider instance-specific swap files in /etc/instance-swap

For automated deployments, make the location configurable:

# Ansible example
- name: Create swap file
  community.general.filesystem:
    fstype: swap
    dev: "{{ swap_location | default('/swapfile') }}"
    state: present

Best practices include:

  1. Making location configurable via environment variables
  2. Documenting the chosen standard
  3. Implementing cleanup routines for temporary swap

In Linux systems, swap files serve as virtual memory extensions when physical RAM is exhausted. Unlike swap partitions, swap files offer more flexibility in sizing and management. The placement of these files can impact both system performance and maintenance.

While there's no strict enforcement in the Filesystem Hierarchy Standard (FHS) for swap file location, several conventions have emerged:


# Common locations seen in production systems
/swapfile
/var/swap/swapfile
/mnt/swap/swapfile
/home/swap/swapfile

For optimal performance, consider these factors when choosing a location:

  • Filesystem type: XFS and ext4 handle swap files well, while btrfs requires special configuration
  • Disk speed: Place on fastest available storage (NVMe > SSD > HDD)
  • Disk usage: Avoid placing on heavily used partitions

Here's how to create and activate a swap file in a recommended location:


# Create 1GB swap file in /var/swap (recommended for servers)
sudo mkdir -p /var/swap
sudo fallocate -l 1G /var/swap/swapfile
sudo chmod 600 /var/swap/swapfile
sudo mkswap /var/swap/swapfile
sudo swapon /var/swap/swapfile

# Make persistent across reboots
echo '/var/swap/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

For desktop users who might reinstall frequently, placing swap in /home can be practical:


# Create swap in home directory (useful for single-user systems)
mkdir ~/swap
fallocate -l 4G ~/swap/swapfile  # Adjust size as needed
sudo chown root:root ~/swap/swapfile
sudo chmod 600 ~/swap/swapfile
sudo mkswap ~/swap/swapfile
sudo swapon ~/swap/swapfile

For systems with specific requirements:


# Multiple swap files on different devices
sudo fallocate -l 2G /mnt/ssd/swapfile
sudo fallocate -l 4G /mnt/hdd/swapfile
# Activate with different priorities
sudo swapon -p 10 /mnt/ssd/swapfile
sudo swapon -p 5 /mnt/hdd/swapfile

After setup, verify your swap configuration:


# Check active swap
swapon --show

# Monitor swap usage
free -h
vmstat 1

Remember to:

  • Set proper permissions (root:root with 600)
  • Consider encrypting swap if dealing with sensitive data
  • Avoid world-readable locations like /tmp