How to add a swap file on FreeBSD version 10.x and Later

Fig.03: How to add a swap file on FreeBSD version 10.x and Later

Fig.03: How to add a swap file on FreeBSD version 10.x and Later

The commands are as follows to add a swap space on FreeBSD version 10 or later. You must run all of the following command as the root user only. First, become the root:

su -
## OR ##
sudo -s

Type the following commands for creating a 256M swap file on a FreeBSD unix server:

## Create 256 MB swap file 
dd if=/dev/zero of=/usr/swap0 bs=1m count=256
 
## Set perms using the chmod command
chmod 0600 /usr/swap0
 
## Update  /etc/fstab 
## The md(4) device md99 is used, leaving lower device numbers available for other usage 
echo 'md99	none	swap	sw,file=/usr/swap0	0	0' >> /etc/fstab
 
## To add swap space immediately ##
swapon -aq
 
## Verify new settings
swapinfo -h

Sample outputs:

Device          1K-blocks     Used    Avail Capacity
/dev/ada0p3       1048540       0B     1.0G     0%
/dev/md99          131072       0B     256M     0%
Total             1179612       0B     1.1G     0%

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