هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- Check what you have:
swapon --show
free -h
total used free shared buff/cache available
Mem: 3.8Gi 3.1Gi 180Mi 40Mi 550Mi 420Mi
Swap: 0B 0B 0B- How much? A practical rule for servers: 2–4 GB, or about the size of RAM for small VMs (≤ 4 GB RAM). Swap is a buffer, not extra memory.
- Create the swap file (quick answer).
fallocateis fast on XFS and ext4. IfswaponsaysInvalid argument(some file systems), create it withddinstead:
dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progressThe file must be 600 — swap can contain passwords and keys from memory.
- On LVM you can use a logical volume instead of a file:
fstab line: /dev/rhel/swap none swap defaults 0 0.
- Tune swappiness — how early the kernel moves memory to swap (0–200, default 60). For servers with databases a lower value keeps more in RAM:
vm.swappiness = 10- Know when swap does not help: if
vmstatshowssi/soall the time, the server needs more RAM or a smaller app — swap only makes it slow. Kubernetes nodes often run without swap; check your Kubernetes version and its swap settings before you add it there.
Check that it worked
The swap is active, the fstab line is valid, and after a reboot swapon --show still lists it.
Common problems
| Problem | Cause | Fix |
|---|---|---|
swapon: /swapfile: insecure permissions 0644 | The file is readable by others. | chmod 600 /swapfile. |
swapon failed: Invalid argument | The file has holes (fallocate on some file systems). | Create it with dd. |
| Swap is gone after reboot | No fstab line. | Add /swapfile none swap defaults 0 0. |
| Server is slow, swap used a lot | Not enough RAM for the work. | Add RAM or reduce the app's memory; swap is only a buffer. |
| Still OOM kills with swap | Memory limits (cgroups) or swap too small. | Check the service's MemoryMax/container limits; size swap better. |
mkswap: … contains holes or other unsupported extents | Same as above (Btrfs etc.). | dd; on Btrfs use btrfs filesystem mkswapfile. |
Go further
- Command pages: swapon, mkswap, free, findmnt.
- Next how-tos: Check memory and CPU usage, Mount a disk at boot with fstab, Extend an LVM volume.
- Learn it: RHCSA — Swap file, RHCSA — Swap partition, RH134 — Swap, RH134 — Swap on LVM.
- Practise: Lab: partitions, file systems, swap and fstab.
1Why must a swap file have 600 rights?
Swap holds memory pages, which can contain passwords and keys.
2What does a low vm.swappiness (for example 10) do?
The kernel keeps more data in RAM and uses swap later.