Operations 5 min read

How to Configure Swap Space on Linux: Step‑by‑Step Guide

This article explains what swap space is, why it matters for system stability and performance, and provides detailed Linux commands and procedures for creating, configuring, activating, and monitoring swap files or partitions.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Configure Swap Space on Linux: Step‑by‑Step Guide

Overview

Swap space is a disk‑based extension of RAM that stores inactive memory pages when physical memory is insufficient, preventing crashes despite being slower than RAM.

Configuration

Configuration Principles

The total swap size is usually recommended to be the larger of twice the physical RAM or 32 MB, but not exceeding 2 GB, adjusted per actual needs.

Using a dedicated swap partition is preferred for better performance and manageability.

Linux Configuration Method

Creating a swap file:

Disable any existing swap.

Use the dd command to create a file, e.g., a 10 GB swap file: sudo dd if=/dev/zero of=/swapfile bs=1G count=10 Set file permissions so only root can read/write: sudo chmod 600 /swapfile Mark the file as swap space: sudo mkswap /swapfile Activate the swap file: sudo swapon /swapfile Configure a swap partition (optional):

Partition the disk using fdisk or parted to create a swap partition.

Format it with mkswap.

Activate it with swapon.

Viewing Swap Usage

Linux provides several commands to inspect swap status: free -m – shows memory and swap usage in megabytes. free -m Option -m displays values in MB. swapon --show – lists active swap files or partitions.

swapon --show
cat /proc/swaps

– displays detailed swap information. cat /proc/swaps Tools like top or htop display real‑time swap usage.

Functions

Memory extension: provides extra storage when RAM is low, keeping the system running.

Performance optimization: frees physical RAM for active processes by moving idle pages to swap.

System stability: prevents crashes or severe slowdowns caused by memory exhaustion.

Example

Creating an 8 GB swap file and activating it:

sudo dd if=/dev/zero of=/swapfile bs=1G count=8
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Verify the configuration with: free -m The system now has swap space enabled, improving stability and performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Performance OptimizationMemory ManagementLinuxSystem Administrationswap_space
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.