Use the fdisk and fallocate commands to create swap partitions

Source: Internet
Author: User

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/sdaAnd the partition is named/dev/sda1,/dev/sda2. In this article, we use a hard disk with two primary partitions:/dev/sda1,/dev/sda2And we use/dev/sda3For partition exchange.

First, create a new partition,

  1. $ fdisk/dev/sda

PressnCreate 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 presstPress enter to set the partition type.

Enter the Partition Number.3And then enter the disk category. The partition type of the SWAp partition is82(To display all available partition types, pressl), And then presswSave the disk partition table.

Swap

Next stepmkswapCommand to format the swap partition:

  1. $ mkswap/dev/sda3

Then activate the new swap partition:

  1. $ swapon/dev/sda3

However, our swap partitions are not automatically mounted after restart. To achieve permanent mounting, we need to add the content/etc/fstabFile. Open/etc/fstabFile and enter the following line:

  1. $ vi/etc/fstab
  2. /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.fallocateIs one of the most undervalued and least used commands.fallocateCommand is used to pre-allocate block/size for the file.

UsefallocateCreate a swap space./Create a directory namedswap_space. Then allocate 2 GBswap_spaceFile:

  1. $ fallocate -l 2G/swap_space

Run the following command to verify the file size:

  1. $ ls-lh /swap_space

Then change the File Permission/swap_spaceMore secure:

  1. $ chmod600/swap_space

In this way, only the root user can read and write the file. Let's format the swap partition.swap_spaceIs a file, but we regard it as a partition for mounting ):

  1. $ mkswap/swap_space

Enable the swap space:

  1. $ swapon-s

After each restart, you must re-mount the disk partition. So to make it persistent, just like above, we edit/etc/fstabEnter the following line:

  1. /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 -mTo 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.