Skip to content

Q&A · Answered

How to add swap space on Linux

asked 9 months, 1 week ago 1172 views 1 answer
36

My server is running out of memory. How can I add swap space without reinstalling the system?

1 Answer

One accepted
12

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago

Create swap file

# Create 4GB swap file
sudo fallocate -l 4G /swapfile
# Or using dd
sudo dd if=/dev/zero of=/swapfile bs=1G count=4

# Set correct permissions
sudo chmod 600 /swapfile

# Set up swap area
sudo mkswap /swapfile

# Enable swap
sudo swapon /swapfile

Make permanent

# Add to /etc/fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Verify
free -h

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.