Linux Swap partition partition Setup tutorial

Source: Internet
Author: User

What is a swap partition

Swap area is considered as part of memory, but it is divided from the hard disk, it is like the virtual memory under Windows, its role is to cache data, divided it, the size of your physical memory size of twice times.

Set up swap swap partitions

Size allocation recommendation for swap partitions

Less than 4GB memory, recommended not less than 2GB of swap space;

Memory 4GB~16GB, recommend not less than 4GB of swap space;

Memory 16GB~64GB, recommend not less than 8GB of swap space;

Memory 64GB~256GB, recommended no less than 16GB swap space.

1, login ssh as root, type the following command to create a 512M swap file (1024 * 512MB = 524288).
DD If=/dev/zero of=/swapfile1 bs=1024 count=524288
2. Create Linux Exchange files
Mkswap/swapfile1
3. Activate/swapfile1 Exchange file immediately
Swapon/swapfile1
4, set up the system to activate the Exchange file. Edit the/etc/fstab file and add the following second line of code.
Vi/etc/fstab
/swapfile1 Swap Defaults 0 0
5, reboot the system, use Free-m to view the swap size.

commands for viewing swap in Linux

You can view the swap partition with commands such as "Cat/proc/swaps", "swapon-s", "free", and so on.

[Root@jay-linux ~]# Swapon-s
Filename Type Size Used Priority
/dev/sda3 Partition 2096120 840-1
[Root@jay-linux ~]# Cat/proc/swaps
Filename Type Size Used Priority
/dev/sda3 Partition 2096120 840-1
[Root@jay-linux ~]# Free-m
Total used free shared buffers Cached
mem:7872 7280 592 0 429 5964
-/+ buffers/cache:886 6986
swap:2046 0 2046
Based on the example above, I actually have to allocate 4GB of swap space, which is a demo of 2GB (probably not quite the recommended value for Redhat, because the amount of memory used on this machine is not too large and there is little use of swap).

Re-dividing swap swap partitions

Let's do a fdisk-l. Check the device name and partition status

1, the execution Swapoff swap partition's device name closes the Exchange file.
2, see the device name swap delete the original swap partition, in a new swap partition. You can do this with Fdisk
If you are partitioning the/DEV/SDA device:
Fdisk/dev/sda
Screen tip:
Command (M for help):
Type P
The screen lists all the current partition device number, size, file format, and other parameters;
The following is done by deleting www.111cn.net except partitions:
Screen tip:
Command (M for help):
Type D
Screen tip:
Partition number (1-x):
X is the number of partitions you have already, with two partitions x is 2; When you delete a partition, start with the last partition, type 2:
Screen tip:
Command (M for help):
Type P
Lists the current partition situation and finds that the sda2 was deleted;
Continue typing D, then 1, and then type p to find that the SDA partition is deleted;
Screen tip:
Command (M for help):
Type W
Save the currently deleted operation, return to the main interface, and if there are partitions on the/DEV/SDB, delete them according to the above method.
The following are the actions for creating partitions:
Fdisk/dev/sda
Screen tip:
Command (M for help):
Type P
There should be no partitions on the hard drive at this time;
Type N
Screen tip:
Cylinder (ABCD-EFGH, default ABCD):
ABCD and EFGH are numbers, indicating the number of cylinders from which the new partition originated, and simply typing in a carriage return;
Screen tip:
Last cylinder or +size or +sizem or +sizek (ABCD-IJKL, default IJKL):
ABCD and IJKL are also numbers, this is required to define the size of the partition, if you want to build 2G space, type +2000m, if you want to all the rest of the space out, directly type a carriage return; Note that the partition is large
Small units M and K must be capitalized!
Screen tip:
Command (M for help):
Type P
found that the partition SDA1 has been established, the size is 2G
Follow the above method to create more partitions, you can view with P, and finally type the W save operation.
3. After this swap is reconstructed, you can use mkswap/dev/sda[x], where [x] represents the partition of your first few.
4. Activate the swap partition swapon/dev/sda[x]
5, want to activate this exchange file automatically when the system starts, should add the following line in/etc/fstab:
/DEV/SDA[X] Swap swap defaults 0 0
After you add the swap file and enable it, view the swap file with the Cat/proc/swaps or free command
was successfully enabled.

Workaround with no swap partition partition (replace swap with file)

File system type capacity used available% mount point
/DEV/SDB2 ext3 7.6G 555M 6.7G 8%/
/DEV/SDA1 ext3 466G 453G 13G 98%/home1
/DEV/SDB7 ext3 4.8G 149M 4.4G 4%/tmp
/DEV/SDB6 ext3 4.8G 1.6G 3.0G 35%/var
/DEV/SDB5 ext3 5.7G 140M 5.3G 3%/swap
/DEV/SDB3 ext3 5.7G 2.1G 3.4G 39%/usr
/DEV/SDB1 ext3 99M 18M 77M 19%/boot
/DEV/SDB8 ext3 437G 175G 262G 41%/home2
Tmpfs tmpfs 2.0G 2.2M 2.0G 1%/DEV/SHM

Swap swap partitions are not used at all, because this server has been running the business, can not easily shut down, but also can not reload the system.

[Root@localhost ~]# Free-m
Total used free shared buffers Cached
mem:3910 3886 23 0 16 1652
-/buffers/cache:2217 1692
swap:0 0 0

Workaround:

#制作一个swapfile文件, root permissions Required
Su
DD If=/dev/zero of=/swap/swapfile bs=4096 count=1310720
#说明: The BS parameter is the number of bytes per read and input. Because the smallest unit of hard disk access is sector, setting the BS parameter is equivalent to setting the size of each sector. I set it to 4096 or 4k. Parameter count is used primarily to set the number of sectors that can be used. So the space occupied by this file is the product of the above parameters. Www.111cn.net Here you need to note that if you want to increase the volume of the swap file, it is generally done by adjusting the count parameter, the BS parameter is not moving. See my/swap partition is completely unused, there is 5.3G of space left, all I have built a 5G swapfile file.

#格式化swapfile, root permissions are also required
Mkswap/swap/swapfile

#启用swap
Swapon/swap/swapfile

#不想每次重启后手动启用swap就可以将挂载写入/etc/fstab (because I am a special partition, the partition name is the same, for the sake of insurance, I did not write to the fstab)
/swap/swapfile Swap Defaults 0 0

#用free-M check that the swap setting is in effect:
[Root@localhost ~]# Free-m
Total used free shared buffers Cached
mem:3910 3885 25 0 21 1639
-/buffers/cache:2224 1686
swap:5119 0 5119

Well done.

Release Swapfile
Swapoff/swap/swapfile
rm-f/swap/swapfile

When you really don't need to swap partitions with swap

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.