متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 74System · Linux

How to add swap space

A small VM with no swap: one memory peak and the kernel kills your database (OOM). Swap gives a safety buffer on disk — slower than RAM, but better than a crash. This how-to adds a swap file (no partitioning needed), makes it permanent, sets how eagerly Linux uses it (swappiness), and explains when swap is not the right fix.

  • 5 الدروس
  • 11 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

74.1

The quick answer

swapon --show
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap defaults 0 0' >> /etc/fstab
free -h

74.2

Step by step

  1. 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
  1. 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.
  2. Create the swap file (quick answer). fallocate is fast on XFS and ext4. If swapon says Invalid argument (some file systems), create it with dd instead:
dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress

The file must be 600 — swap can contain passwords and keys from memory.

  1. On LVM you can use a logical volume instead of a file:
lvcreate -L 4G -n swap rhel
mkswap /dev/rhel/swap
swapon /dev/rhel/swap

fstab line: /dev/rhel/swap none swap defaults 0 0.

  1. 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:
/etc/sysctl.d/90-swap.conf
vm.swappiness = 10
sysctl --system
cat /proc/sys/vm/swappiness
  1. Know when swap does not help: if vmstat shows si/so all 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.

74.3

Check that it worked

swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file    4G   0B   -2
findmnt --verify

The swap is active, the fstab line is valid, and after a reboot swapon --show still lists it.

74.4

Common problems

ProblemCauseFix
swapon: /swapfile: insecure permissions 0644The file is readable by others.chmod 600 /swapfile.
swapon failed: Invalid argumentThe file has holes (fallocate on some file systems).Create it with dd.
Swap is gone after rebootNo fstab line.Add /swapfile none swap defaults 0 0.
Server is slow, swap used a lotNot enough RAM for the work.Add RAM or reduce the app's memory; swap is only a buffer.
Still OOM kills with swapMemory limits (cgroups) or swap too small.Check the service's MemoryMax/container limits; size swap better.
mkswap: … contains holes or other unsupported extentsSame as above (Btrfs etc.).dd; on Btrfs use btrfs filesystem mkswapfile.

74.5

Go further

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.

كل الأوامر في هذا الفصل