Add a swap partition file in linux and a swap partition in linux

Source: Internet
Author: User

Add a swap partition file in linux and a swap partition in linux

To add a swap partition file in inux, follow these steps:

1. Check the current swap (use free-h, cat/proc/swaps or swapon-s ):

[root@compute ~]# free -h              total        used        free      shared  buff/cache   availableMem:           1.8G        329M        1.2G        8.3M        242M        1.3GSwap:          511M          0B        511M

 

2. View hard disk usage (df-hal ):

[root@compute ~]# df -lhFilesystem               Size  Used Avail Use% Mounted on/dev/mapper/centos-root   15G  2.4G   13G  16% /devtmpfs                 909M     0  909M   0% /devtmpfs                    920M     0  920M   0% /dev/shmtmpfs                    920M  8.4M  912M   1% /runtmpfs                    920M     0  920M   0% /sys/fs/cgroup/dev/mapper/centos-home   55G   33M   55G   1% /home/dev/sda1                797M  180M  618M  23% /boottmpfs                    184M     0  184M   0% /run/user/0

 

3. Run the dd command to create a swap file named swapfile (the file name and directory are arbitrary ):

dd  if=/dev/zero  of=/var/swapfile  bs=1024  count=2048k

  

Popular Science: if (input file, input file), of (output file ). Dev/zero is a special character device (input device) in Linux. It can be used to create an empty file with a specified length for initialization. For example, if a temporary file is exchanged, the device provides 0 infinitely, you can provide any number you need. Bs = 1024: the size of each block that is read/output at the same time is 1024 bytes, that is, 1 kb, and bs (that is, block size ). Count = 2048000: the number of data blocks is 2048000, that is, 2048000 1 kb. The size of swap partition can be calculated as: 1KB * 2097152 = 1KB * 1024 (k) * 1024*2 = 2097152 = 2G. (The unit M in the dd command indicates 1024*1024, and k indicates 1024 ).

After you press enter, a 2 GB read/write operation will be executed, and the system will be stuck for a while and wait patiently for the execution result.

After execution, format the swap file and convert it to a swap partition:

mkswap  /var/swapfile

 

4. Mount and activate the partition:

swapon   /var/swapfile

 

5. Run the free-h or swapon-s command to check whether the new swap partition is added and activated.

Modify the fstab configuration and set to automatically mount the partition at startup:

vim    /etc/fstab

/swapfile               swap                    swap    defaults        0 0

 

Reference: http://blog.csdn.net/ausboyue/article/details/73433990

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.