How to create a swap file on FreeBSD version 9.X and Earlier Commands

Fig.01: How to create a swap file on FreeBSD version 9.X and Earlier Commands

Fig.01: How to create a swap file on FreeBSD version 9.X and Earlier Commands

Here are the commands for creating a swap file on FreeBSD server, laptop or desktop version 9.X and earlier. You must type the commands as the root user:

su -

OR

sudo -s

Create the swap file, /root/swap0 (128M size)

Type the command:

dd if=/dev/zero of=/root/swap0 bs=1m count=128

Outputs:

128+0 records in
128+0 records out
134217728 bytes transferred in 1.358529 secs (98796359 bytes/sec)

Set permission and verify size:

chmod 0600 /root/swap0
ls -lh /root/swap0

Outputs:

-rw-------  1 root  wheel   128M Jan 27 15:11 /root/swap0

Enable the swap file in /etc/rc.conf:

Turn on swap space at boot

Type the following command:

echo 'swapfile="/root/swap0"' >> /etc/rc.conf

Reboot the server:

# reboot

Or enable it without rebooting the server, type:

mdconfig -a -t vnode -f /root/swap0 -u 0 
swapon /dev/md0
swapinfo

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