In this article, we will learn how to create swap space using a swap file in Linux: this is important in case we don't have a swap partition created on the hard disk. 1. In this example, we will create a swap file of size 2GB using the dd command as follows. Note that bs=1024 means read and write up to 1024 bytes at a time and the file size is equal to the number of blocks (filesize = bs * count) 1
$ sudo dd if=/dev/zero of=/root/swapfile bs=16M count=1024
1
$ sudo chmod 600 /root/swapfile
1
$ sudo mkswap /root/swapfile
1
$ sudo swapon /root/swapfile
1
/root/swapfile swap swap defaults 0 0
- /mnt/swapfile – device/file name
- swap – defines device mount point
- swap – specifies the file-system type
- defaults – describes the mount options
- 0 – specifies the option to be used by the dump program
- 0 – specifies the fsck command option
To change the swappiness value, add the following line to the file at /etc/sysctl.conf:
Start with a value of 10 and increase if it necessary. A typical default value for swappiness is 60. The higher the number (up to 100), the more often the system uses swap.1
vm.swappiness=60
Now verify the swap file was created using the swapon command.
1
$ swapon -s
