There are 2 main ways to create a swap partition in Ubuntu:
First, the traditional way of creation
In general, we will use the DD command to pre-create the swap partition file, and then use/dev/zero to reset the contents of the file to zero, the BS parameter will be used to set the size of the swap space to be created, and count is used to specify the number of blocks to create.
For example, to create a 4G swap partition file, you can use the following command:
if=/dev/zero of=/swapfile bs=1g count=4
Second, the rapid way to create
As you may have noticed, the process of creating swap files in these ways is slow, and if you want to quickly create swap partitions you can use the Fallocate program. The command immediately creates a pre-allocated swap file, which is written to the allocated size at the actual size.
If the same is to create a 4G size interchange creation file, the shortcut to create the command is as follows:
sudo fallocate-l 4g/swapfile
Third, enable swap partition file
Our Swapfile swap file has been created, but Ubuntu does not yet know it is being used as a swap partition, so we also need to format this file as a swap partition to turn it on.
1, first, we need to use the following command to change the permissions of the following Swapfile file:
sudo chmod 600/swapfile
2. Then we need to initialize the Swapfile as a swap file as follows:
sudo mkswap/swapfile
3. Finally, you need to use the following command to enable the swap file:
sudo swapon/swapfile
5. View Results
Free-m
6. Mount the swap partition file when the configuration is enabled
The swap file is already configured and enabled, but Ubuntu does not mount the swap partition file we just created after the reboot, so we also need to make changes to the/etc/fstab file to automatically mount the/swapfile file we created when the system starts.
sudo vim/etc/fstab
Add the following content:
/swapfile None swap SW 0 0
Tip: Press "ESC" and enter: ": wq!" Exit.
7. Unmount the Swap partition
sudo swapoff/swapfile
Iii. Summary
1, after testing, whether or not have created a swap partition, can be set with this method, and the capacity is superimposed.
Reference:
Http://www.linuxidc.com/Linux/2014-08/105223.htm (the above content is transferred from this article)
http://blog.csdn.net/xb_0226/article/details/51185326
http://blog.csdn.net/yanlai20/article/details/46907353
http://blog.csdn.net/mznewfacer/article/details/7334592
Ubuntu 16.04 Create swap partitions or increase swap partition capacity