Links: https://help.ubuntu.com/community/SwapFaq
Steps to create a swapfile
Create the swapfile using dd
Use the dd command to create a swapfile of the desired size. For example:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024Explanation:
if=/dev/zero: Input file is a stream of zero bytes.of=/swapfile: Output file is the swapfile to be created.bs=1M: Block size is 1 megabyte.count=1024: Number of blocks to write (1024 blocks of 1 MB = 1 GB).
Set the correct permissions
Restrict access to the swapfile for security:
sudo chmod 600 /swapfileFormat the file as swap
Prepare the file to be used as swap space:
sudo mkswap /swapfileEnable the swapfile
Activate the swapfile:
sudo swapon /swapfileMake the swapfile permanent
Add the swapfile entry to the fstab file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabMake sure to check this line wan't there already, or you are mounting a swap partition
How much swap to add
Acording to the ubutu faq
| RAM | No Hibernation | With Hibernation | Maximum |
|---|---|---|---|
| 256MB | 256MB | 512MB | 512MB |
| 512MB | 512MB | 1024MB | 1024MB |
| 1024MB | 1024MB | 2048MB | 2048MB |
| 1GB | 1GB | 2GB | 2GB |
| 2GB | 1GB | 3GB | 4GB |
| 3GB | 2GB | 5GB | 6GB |
| 4GB | 2GB | 6GB | 8GB |
| 5GB | 2GB | 7GB | 10GB |
| 6GB | 2GB | 8GB | 12GB |
| 8GB | 3GB | 11GB | 16GB |
| 12GB | 3GB | 15GB | 24GB |
| 16GB | 4GB | 20GB | 32GB |
| 24GB | 5GB | 29GB | 48GB |
| 32GB | 6GB | 38GB | 64GB |
| 64GB | 8GB | 72GB | 128GB |
| 128GB | 11GB | 139GB | 256GB |
| 256GB | 16GB | 272GB | 512GB |
| 512GB | 23GB | 535GB | 1TB |
| 1TB | 32GB | 1056GB | 2TB |
| 2TB | 46GB | 2094GB | 4TB |
| 4TB | 64GB | 4160GB | 8TB |
| 8TB | 91GB | 8283GB | 16TB |