首页主机资讯centos swapper官方文档

centos swapper官方文档

时间2025-10-30 20:52:03发布访客分类主机资讯浏览282
导读:Note: There is no dedicated “official documentation” for “centos swapper” from CentOS/RHEL. The term “swapper” typically...

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).

  • 0: Avoids swapping unless absolutely necessary (ideal for systems with lots of RAM).
  • 60: Default value (balanced for general use).
  • 100: Aggressively swaps to disk (suitable for memory-intensive workloads).

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:

  • View total/used swap:
    free -h
    
  • List active swap devices:
    sudo swapon --show
    
  • Detailed swap statistics:
    cat /proc/swaps
    
  • Real-time monitoring (install sysstat first):
    sar -W 1 5
    

Key Notes

  • Swap size recommendations:
    • For systems with < 4GB RAM, allocate 2–4x RAM size.
    • For systems with 4–16GB RAM, allocate 1–2x RAM size.
    • For systems with > 16GB RAM, 2–4GB is usually sufficient.
  • Performance impact: Swap is much slower than RAM. Avoid over-reliance on swap for production systems with high memory demands.
  • CentOS version compatibility: The steps above work for CentOS 7/8/Stream. For RHEL-based systems, commands are identical.

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: centos swapper官方文档
本文地址: https://pptw.com/jishu/739262.html
centos中trigger命令怎么用 centos僵尸进程如何报警

游客 回复需填写必要信息