centos swapper官方文档
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:
-
Create a swap file (e.g., 2GB):
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048if=/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).
-
Set strict permissions (only root can read/write):
sudo chmod 600 /swapfile -
Format the file as swap:
sudo mkswap /swapfile -
Activate the swap file:
sudo swapon /swapfile -
Enable auto-mount on boot:
Edit/etc/fstaband append:echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstabThis 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:
-
Identify unused disk space:
sudo fdisk -lLook for a disk with unallocated space (e.g.,
/dev/sdb). -
Create a new partition:
Usefdiskto create a partition (replace/dev/sdbwith 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).
- Press
-
Format the partition as swap:
sudo mkswap /dev/sdb1 -
Activate the partition:
sudo swapon /dev/sdb1 -
Enable auto-mount on boot:
Edit/etc/fstaband append:echo "/dev/sdb1 swap swap defaults 0 0" | sudo tee -a /etc/fstab -
Verify:
Usesudo swapon --showorfree -hto 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:
-
Check current value:
cat /proc/sys/vm/swappiness -
Temporarily modify (until reboot):
sudo sysctl vm.swappiness=10 -
Permanently modify:
Edit/etc/sysctl.confand add:vm.swappiness=10Apply changes:
sudo sysctl -p -
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
sysstatfirst):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
