How to: FreeBSD add an encrypted swap file

Fig.04: Encrypting swap file on a FreeBSD 10.x server

Fig.04: Encrypting swap file on a FreeBSD 10.x server

To Encrypting swap file on a FreeBSD 10.x server, type:

## Creating a swap 256MB ##
dd if=/dev/random of=/root/swap0.en.bin bs=1m count=256
mdconfig -a -t vnode -f /root/swap0.en.bin
kldload geom_eli
geom eli init md0
geom eli attach md0
swapon /dev/md0.eli
swapinfo -h

Write a script (say /root/en.swap.init.sh )with following in it:

#!/bin/sh
mdconfig -a -t vnode /root/swap0.en.bin
geli attach md0
swapon /dev/md0.eli

Run this script when you need to turn on and attach encrypted swap space:
# sh /root/en.swap.init.sh

Examples and usage: How To Add Swap on FreeBSD Unix Systems