centos

centos swapper官方文档

小樊
44
2025-10-30 09:54:36
栏目: 智能运维

Note: There is no dedicated “official documentation” for “centos swapper” from CentOS/RHEL. The term “swapper” typically refers to the Linux kernel’s swap subsystem (swap partition or swap file), which is managed using standard system tools. Below are the official and community-accepted best practices for configuring and managing swap space on CentOS systems.

1. Check Existing Swap Space

Before configuring swap, verify if your system already has swap enabled:

free -h

Look for the “Swap” line in the output. If “Swap” shows 0B, no swap is configured.

2. Create a Swap File (Recommended for Most Users)

A swap file is flexible and does not require modifying disk partitions.

Steps:

  1. Create a swap file (e.g., 2GB):

    sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
    
    • if=/dev/zero: Input source (infinite zero stream).
    • of=/swapfile: Output file path.
    • bs=1M: Block size (1MB).
    • count=2048: Number of blocks (2048 × 1MB = 2GB).
  2. Set strict permissions (only root can read/write):

    sudo chmod 600 /swapfile
    
  3. Format the file as swap:

    sudo mkswap /swapfile
    
  4. Activate the swap file:

    sudo swapon /swapfile
    
  5. Enable auto-mount on boot:
    Edit /etc/fstab and append:

    echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab
    

    This ensures swap is re-enabled after a reboot.

3. Create a Swap Partition (Alternative for Server Environments)

For production servers, a dedicated swap partition is often preferred for stability.

Steps:

  1. Identify unused disk space:

    sudo fdisk -l
    

    Look for a disk with unallocated space (e.g., /dev/sdb).

  2. Create a new partition:
    Use fdisk to create a partition (replace /dev/sdb with your disk):

    sudo fdisk /dev/sdb
    
    • Press n (new partition) → p (primary) → enter partition number → press Enter (default start sector) → +2G (set size, e.g., 2GB) → t (change partition type) → 82 (Linux swap type) → w (write changes).
  3. Format the partition as swap:

    sudo mkswap /dev/sdb1
    
  4. Activate the partition:

    sudo swapon /dev/sdb1
    
  5. Enable auto-mount on boot:
    Edit /etc/fstab and append:

    echo "/dev/sdb1 swap swap defaults 0 0" | sudo tee -a /etc/fstab
    
  6. Verify:
    Use sudo swapon --show or free -h to confirm.

4. Adjust Swappiness (Optional but Recommended)

The swappiness parameter controls how aggressively the system uses swap (range: 0–100).

Steps:

  1. Check current value:

    cat /proc/sys/vm/swappiness
    
  2. Temporarily modify (until reboot):

    sudo sysctl vm.swappiness=10
    
  3. Permanently modify:
    Edit /etc/sysctl.conf and add:

    vm.swappiness=10
    

    Apply changes:

    sudo sysctl -p
    
  4. Reboot (optional, to ensure persistence):

    sudo reboot
    

5. Monitor Swap Usage

Regularly check swap usage to ensure optimal performance:

Key Notes

0
看了该问题的人还看了