translated

This commit is contained in:
chenxinlong 2017-07-15 18:47:06 +08:00
parent 6e667f2d35
commit f8ec5914d6
2 changed files with 211 additions and 211 deletions

View File

@ -1,211 +0,0 @@
translating by chenxinlong
3 Easy Ways To Create Or Extend Swap Space In Linux
============================================================
Users can create swap space during installation of any Linux operating system and its necessary too. If you forget to create or later if you want increase, you can at any point of time.
Sometimes it is necessary to add more swap space when you upgrade the RAM after installation, say for example, if you upgrade the RAM in your system from 1GB to 2GB then you have to upgrade swap too (From 2GB to 4GB) since its uses double amount of physical RAM.
Swap is space on a disk that is reserved to be used as virtual memory when the amount of physical memory (RAM-random access memory) is full. If the system needs more memory resources when the RAM is full, inactive pages in memory are moved to the swap space which can help system to run the applications some more time but it should not be considered a replacement for more RAM.
Its recommended to create a dedicated swap partition but if you dont have a free partition then use a swap file, or a combination of swap partitions and swap files. Swap space is generally recommended for user at least 4 GB, also users can create a swap space depends upon their requirement and environment.
I have found most of the VMs and Cloud servers comes without swap partition, so in such cases we can create, extend or increase swap space using below three methods.
#### How to check current swap size
Lets first check the size of existing swap space partition using **[free][1]** & `swapon` command.
```
$ free -h
total used free shared buff/cache available
Mem: 2.0G 1.3G 139M 45M 483M 426M
Swap: 2.0G 655M 1.4G
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 655.2M -1
```
The above output clearly shows `2GB` is current swap space.
#### Method-1 : Create the swap file using fallocate
fallocate program is a best way to create a file of a preallocated size instantly.
The following command will create 1GB of `/swapfile`.
```
$ sudo fallocate -l 1G /swapfile
```
Check whether its created correct size of file or not.
```
$ ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Jun 7 09:49 /swapfile
```
Change the file permission to `600` to only accessible by root user.
```
$ sudo chmod 600 /swapfile
```
Convert the file as swap area by running below command.
```
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=cda50e0e-41f3-49c7-af61-b8cb4a33a464
```
Enable the swap file by running below command.
```
$ sudo swapon /swapfile
```
Add newely created swap file into fstab file, so that swap space partition available even after the reboot.
```
$ vi /etc/fstab
/swapfile swap swap defaults 0 0
```
Check newly created swap file.
```
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 657.8M -1
/swapfile file 1024M 0B -2
```
Yes i can see the new `1GB swapfile`. Reboot the system to use the new swap file.
#### Method-2 : Create the swap file using dd command
dd command is another utility which help us to create a file of a preallocated size instantly.
The following dd command will create 1GB of `/swapfile1`.
```
$ sudo dd if=/dev/zero of=/swapfile1 bs=1G count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 16.6154 s, 64.6 MB/s
```
**Details :**
* **if=/dev/zero** is a input file, /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it.
* **of=/swapfile1** is an output file
* **bs=1G :** Read and write up to 1GB bytes at a time
* **count=1 :** Copy only 1 BLOCKS input blocks.
Check whether its created correct size of file or not.
```
$ ls -lh /swapfile1
-rw-r--r-- 1 root root 1.0G Jun 7 09:58 /swapfile1
```
Change the file permission to `600` to only accessible by root user.
```
$ sudo chmod 600 /swapfile1
```
Convert the file as a swap file by running below command.
```
$ sudo mkswap /swapfile1
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=96def6d7-b2da-4954-aa72-aa32316ec993
```
Enable the swap file by running below command.
```
$ sudo swapon /swapfile1
```
Add newely created swap file into fstab file, so that swap space partition available even after the reboot.
```
$ vi /etc/fstab
/swapfile1 swap swap defaults 0 0
```
Check newly created swap file.
```
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 1.3G -1
/swapfile file 1024M 0B -2
/swapfile1 file 1024M 0B -3
```
Yes i can see the new `1GB swapfile1`. Reboot the system to use the new swap file.
#### Method-3 : Create the swap file using Hard drive partition
Hard drive partition is recommended method to create a swap space.
If you have an additional hard disk create the new partition using fdisk command. Let us assume that we have created the partition called /dev/sda4.
Use mkswap command to convert the partition as swap area.
```
$ sudo mkswap /dev/sda4
```
Enable the swap file by running below command.
```
$ sudo swapon /dev/sda4
```
Add newely created swap file into fstab file, so that swap space partition available even after the reboot.
```
$ vi /etc/fstab
/dev/sda4 swap swap defaults 0 0
```
Check newly created swap file.
```
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 1.3G -1
/swapfile file 1024M 0B -2
/swapfile1 file 1024M 0B -3
/dev/sda4 partition 1G 0B -4
```
Yes i can see the new `1GB /dev/sda4` swap partition. Reboot the system to use the new swap partition.
--------------------------------------------------------------------------------
via: http://www.2daygeek.com/add-extend-increase-swap-space-memory-file-partition-linux/
作者:[2DAYGEEK ][a]
译者:[chenxinlong](https://github.com/chenxinlong)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.2daygeek.com/author/2daygeek/
[1]:http://www.2daygeek.com/free-command-to-check-memory-usage-statistics-in-linux/
[2]:http://www.2daygeek.com/author/2daygeek/

View File

@ -0,0 +1,211 @@
三种在 Linux 上创建或扩展交换分区的简单方法
============================================================
用户可以在任何 Linux 操作系统的安装过程中或者是其它必要的时候创建交换空间。如果你在安装 Linux 的时候忘记了创建或是你想要再增加交换分区的空间,你随时都可以再创建或增加。
有时候在你安装后摇升级 RAM 的时候需要增加一点交换分区的空间,比如你要将你的系统的 RAM 从 1GB 升级到 2GB 你,那么你就不得不将你的交换分区空间也升级一下(从 2GB 到 4GB这是因为它使用了双倍的物理 RAM 。
Swap 是当物理内存RAM 随机存取存储器)的数量已满时,被保留用作虚拟内存的磁盘上的空间。 如果系统在 RAM 满载时需要更多的内存资源,内存中的非活动页面将被移动到交换空间,这样可以帮助系统运行应用程序更多的时间,但不应该把它当做 RAM 的扩展。
建议你创建一个专用的交换分区,但是如果你没有可用的分区,那么可以使用交换文件,或交换分区和交换文件的组合。 交换空间通常建议用户至少 4 GB用户也可以根据自己的要求和环境创建交换空间。
我发现大部分 VM 和 云服务器都没有交换分区,所以在这种情况下,我们可以使用以下三种方法创建,扩展或增加交换空间。
#### 如何检测当前交换分区大小
通过 **[free][1]** & `swapon` 命令来检测当前的交换分区空间的大小。 
```
$ free -h
total used free shared buff/cache available
Mem: 2.0G 1.3G 139M 45M 483M 426M
Swap: 2.0G 655M 1.4G
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 655.2M -1
```
上面的输出显示了当前的交换分区空间是 `2GB`
#### 方法 1 : 通过 fallocate 命令创建 swap 文件
fallocate 程序是立即创建预分配大小的文件的最佳方法。
下面这个命令会创建一个 1GB 大小的 `/swapfile`.
```
$ sudo fallocate -l 1G /swapfile
```
检查一下创建的文件的大小是否正确。
```
$ ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Jun 7 09:49 /swapfile
```
将该文件的权限设置为 `600` 这样只有 root 用户可以访问这个文件。
```
$ sudo chmod 600 /swapfile
```
通过运行以下的命令来将此文件转换为 swap 文件。
```
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=cda50e0e-41f3-49c7-af61-b8cb4a33a464
```
通过运行以下的命令来使 swap 文件生效。
```
$ sudo swapon /swapfile
```
将新创建的 swap 文件添加到 fstab 文件中,这样交换分区空间的修改即使在重启后也可以生效。
```
$ vi /etc/fstab
/swapfile swap swap defaults 0 0
```
检查一下新创建的 swap 文件。
```
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 657.8M -1
/swapfile file 1024M 0B -2
```
现在我可以看到一个新的  `1GB swapfile1`。重启系统以使新的 swap 文件生效。
#### 方法 2 : 通过 dd 命令来创建 swap 文件
dd 命令是另一个实用程序,可以帮助我们立即创建预分配大小的文件。
以下 dd 命令将创建 1GB 的 `/swapfile1`
```
$ sudo dd if=/dev/zero of=/swapfile1 bs=1G count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 16.6154 s, 64.6 MB/s
```
**详解 :**
* **if=/dev/zero** 是输入文件,/dev/zero 是类 Unix 操作系统中的一个特殊文件它提供从它读取的尽可能多的空字符ASCII NUL0x00
* **of=/swapfile1** 设置输出文件
* **bs=1G :** 一次性读写的大小为 1GB
* **count=1 :** 仅复制一个 BLOCKS 输入块
检查一下创建的文件的大小是否正确。
```
$ ls -lh /swapfile1
-rw-r--r-- 1 root root 1.0G Jun 7 09:58 /swapfile1
```
将该文件的权限设置为 `600` 这样只有 root 用户可以访问这个文件。
```
$ sudo chmod 600 /swapfile1
```
通过运行以下的命令来将此文件转换为 swap 文件。
```
$ sudo mkswap /swapfile1
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=96def6d7-b2da-4954-aa72-aa32316ec993
```
通过运行以下的命令来使 swap 文件生效。
```
$ sudo swapon /swapfile1
```
将新创建的 swap 文件添加到 fstab 文件中,这样交换分区空间的修改即使在重启后也可以生效。
```
$ vi /etc/fstab
/swapfile1 swap swap defaults 0 0
```
检查新创建的 swap 文件。
```
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 1.3G -1
/swapfile file 1024M 0B -2
/swapfile1 file 1024M 0B -3
```
现在我可以看到一个新的  `1GB swapfile1`。重启系统以使新的 swap 文件生效。
#### 方法 3 : 通过硬盘分区来创建 swap 文件
我们也推荐使用通过硬盘分区的方式来创建交换分区。
如果你已经在你的另一个硬盘上通过 fdisk 命令创建了一个新的分区,假设我们已经创建了一个叫做 /dev/sda4 的分区。
使用 mkswap 命令来将这个分区转换成交换分区。
```
$ sudo mkswap /dev/sda4
```
通过运行以下命令来使 swap 文件生效。
```
$ sudo swapon /dev/sda4
```
把新增的 swap 文件添加到 fstab 文件中,这样即使是重启了系统交换分区的修改也能生效。
```
$ vi /etc/fstab
/dev/sda4 swap swap defaults 0 0
```
检查新创建的 swap 文件。
```
$ swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 1.3G -1
/swapfile file 1024M 0B -2
/swapfile1 file 1024M 0B -3
/dev/sda4 partition 1G 0B -4
```
我可以看到新的交换分区 `1GB /dev/sda4`。重启系统就可以使用新的交换分区了。
--------------------------------------------------------------------------------
via: http://www.2daygeek.com/add-extend-increase-swap-space-memory-file-partition-linux/
作者:[2DAYGEEK ][a]
译者:[chenxinlong](https://github.com/chenxinlong)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.2daygeek.com/author/2daygeek/
[1]:http://www.2daygeek.com/free-command-to-check-memory-usage-statistics-in-linux/
[2]:http://www.2daygeek.com/author/2daygeek/