Adding swap

From Newroco Tech Docs
Revision as of 06:44, 10 May 2017 by Chris.puttick (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Sometimes it is useful to add additional swap space to a running Linux (or Unix) computer, server or PC.

First create a swap file:

# dd if=/dev/zero of=/addswap1 bs=1024 count=1048576

(creates a file of 1GB with a block size of 1024, where the count can be adapted to create a smaller or larger file as needed - note throughout the name and location of the file can be whatever you choose, but it is best having the word "swap" somewhere in it so that it's clear what it is in the future)

making the new file into a swap file

# mkswap /addswap1

secure the file so it's rw just for root

# chown root:root /addswap1
# chmod 0600 /addswap1

add it into the system

# swapon /addswap1

review the available swap space

# swapon -s

(where you should see the original swap partition, if any, with a higher priority than the newly added swap space; the swap file will be lower performance so only used if necessary. If you're adding a new swap partition or creating the swapfile on a dedicated disk you might want to tune the priorities)

finally, check to see the system can see the additional swap space.

# free

If you want to make the new swap permanent then edit fstab

# vi /etc/fstab

and append this line

/addswap1       none    swap    sw      0       0

If you just needed the swap space temporarily, say if running a [rsync] job to move a massive file tree, and would prefer to have the disk space back when you're done, you can remove the swap by

# swapoff /addswap1
# rm /addswap1