Use the fdisk and fallocate commands to create swap partitions
Swap partitions are used to keep the content in the memory when the physical memory (RAM) is filled up. When RAM is exhausted, Linux will move the pages that are not active in the memory to the swap space to free up the memory for the system. However, swap space should not be considered a substitute for physical memory.
In most cases, it is recommended that the swap memory be 1 to 2 times the physical memory. That is to say, if you have 8 GB memory, the swap space should be between 8-16 GB.
If no swap partition is configured in the system, when the memory is used up, the system may kill running processes/applications, resulting in system crash. In this article, we will learn how to add swap partitions for Linux systems. We have two ways:
- Use the fdisk command
- Use the fallocate command
The first method (using the fdisk command)
Generally, the first hard disk of the system is named/dev/sda
And the partition is named/dev/sda1
,/dev/sda2
. In this article, we use a hard disk with two primary partitions:/dev/sda1
,/dev/sda2
And we use/dev/sda3
For partition exchange.
First, create a new partition,
$ fdisk/dev/sda
Pressn
Create a new partition. The system will ask you which cylinder you start from, simply press the Enter key to use the default value. Then the system asks you which cylinder ends. Here we enter the swap partition size (such as 1000 MB ). Here we enter+1000M
.
Swap
Now we have created a 1000 MB disk. However, we have not set the partition type. We presst
Press enter to set the partition type.
Enter the Partition Number.3
And then enter the disk category. The partition type of the SWAp partition is82
(To display all available partition types, pressl
), And then pressw
Save the disk partition table.
Swap
Next stepmkswap
Command to format the swap partition:
$ mkswap/dev/sda3
Then activate the new swap partition:
$ swapon/dev/sda3
However, our swap partitions are not automatically mounted after restart. To achieve permanent mounting, we need to add the content/etc/fstab
File. Open/etc/fstab
File and enter the following line:
$ vi/etc/fstab
/dev/sda3 swap swap default00
Save and close the file. Now we can use our swap partition after each restart.
Method 2 (use the fallocate command)
I recommend this method because it is the simplest and fastest way to create a swap space.fallocate
Is one of the most undervalued and least used commands.fallocate
Command is used to pre-allocate block/size for the file.
Usefallocate
Create a swap space./
Create a directory namedswap_space
. Then allocate 2 GBswap_space
File:
$ fallocate -l 2G/swap_space
Run the following command to verify the file size:
$ ls-lh /swap_space
Then change the File Permission/swap_space
More secure:
$ chmod600/swap_space
In this way, only the root user can read and write the file. Let's format the swap partition.swap_space
Is a file, but we regard it as a partition for mounting ):
$ mkswap/swap_space
Enable the swap space:
$ swapon-s
After each restart, you must re-mount the disk partition. So to make it persistent, just like above, we edit/etc/fstab
Enter the following line:
/swap_space swap swap sw 00
Save and exit the file. Now our swap partition will be mounted all the time. After restart, we can run it on the terminal.free -m
To check whether the swap partition takes effect.
Our tutorial is over now. I hope this article is easy to understand and learn. If you have any questions, please feel free to ask.
Via: http://linuxtechlab.com/create-swap-using-fdisk-fallocate/
Author: Shusain Translator: lujun9972 Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China