swap

There are 3 ways to check the current swap usage:

root:~> free -h
       total    used    free    shared   buff/cache    available
Mem:     15G    2.0G    9.1G      300M         4.4G          13G
Swap:   3.8G      0B    3.8G
.
root:~> swapon -s
Filename         Type           Size        Used     Priority
/dev/sda2        partition      3999740     0        -1
.
root:~> cat /proc/swaps
Filename         Type           Size        Used     Priority
/dev/sda2        partition      3999740     0        -1

If we need more swap space, we can either add a new swap partition or a new swap file. Adding a new swap file tends to be a temporary measure when we can’t use a new partition. We can do it with a sequence of 4 commands:

# dd if=/dev/zero of=/root/swapfile1 bs=1024 count=1024  → create blank 1G file and zero it
# chmod 0600 /root/swapfile1                             → set permissions
# mkswap /root/swapfile1                                 → make it a swap file
# swapon /root/swapfile1                                 → enable it for use

If we want the swap file to be available after the next reboot then we need to add an entry like the following one to /etc/fstab:

/root/swapfile1          swap      swap       defaults      0  0

If we want to test the fstab entry, we can do it by switching off and on all swap:

swapoff -a
swapon -a

Adding a swap partition is even easier. Once we have the spanky new partition available for use (i.e. /dev/sdh1), we could just execute …

mkswap /dev/sdh1
swapon /dev/sdh1
# echo “/dev/sdh1       swap    swap     defaults       0   0” >> /etc/fstab


<< LVM                  NFS & CIFS clients >>