Creating swap on Linux

From PedrosBrainDump
Revision as of 22:25, 6 October 2024 by 413vhcu1lq0463ob (talk | contribs) (Created page with "== This should work in any Linux distro == === Create a swapfile === To create the swapfile let's just use the /dev/zero and append on /swapfile we're using 16MB blocks and making 64 of those, in another words 1GB, you can make 0.5GB or 4GB doesn't really matters: dd if=/dev/zero of=/swapfile bs=16M count=64 # 1GB or: dd if=/dev/zero of=/swapfile bs=16M count=128 # 2GB or: dd if=/dev/zero of=/swapfile bs=16M count=256 # 4GB Select one and go for it. === Changing fil...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This should work in any Linux distro

Create a swapfile

To create the swapfile let's just use the /dev/zero and append on /swapfile we're using 16MB blocks and making 64 of those, in another words 1GB, you can make 0.5GB or 4GB doesn't really matters:

dd if=/dev/zero of=/swapfile bs=16M count=64 # 1GB

or:

dd if=/dev/zero of=/swapfile bs=16M count=128 # 2GB

or:

dd if=/dev/zero of=/swapfile bs=16M count=256 # 4GB

Select one and go for it.

Changing file privileges to 600

Nothing crazy:

chmod 600 /swapfile

Formating this file as a swapfile

Just using mkswap and making /swapfile in fact a swapfile:

mkswap /swapfile

Activating swap on this file

To activate the swap now on this file you can just use swapon (swap on) and pass the file as a parameter:

swapon /swapfile

Deactivating swap on this file

To deactivate the swap on this file just use the inverse swapoff (swap off) and pass the file as a parameter.:

swapoff /swapfile

Appending swap configuration to fstab

Simple as this:

echo "/swapfile none swap sw 0 0" >> /etc/fstab

Or if you want to open the /etc/fstab file and append the configurations you can append just this:

/swapfile none swap sw 0 0

Single line and all done

dd if=/dev/zero of=/swapfile bs=16M count=128 ; chmod 600 /swapfile ; mkswap /swapfile ; swapon /swapfile ; echo "/swapfile none swap sw 0 0" >> /etc/fstab #2GB