debian创建交换分区_如何在Debian 8上添加交换空间

debian创建交换分区

介绍 (Introduction)

One of the easiest way of guarding against out-of-memory errors in applications is to add some swap space to your server. In this guide, we will cover how to add a swap file to a Debian 8 server.

防止应用程序中出现内存不足错误的最简单方法之一就是为服务器添加一些交换空间。 在本指南中,我们将介绍如何将交换文件添加到Debian 8服务器。

Warning: Although swap is generally recommended for systems using traditional spinning hard drives, using swap with SSDs can cause issues with hardware degradation over time. Due to this consideration, we do not recommend enabling swap on DigitalOcean or any other provider that utilizes SSD storage. Doing so can impact the reliability of the underlying hardware for you and your neighbors. This guide is provided as reference for users who may have spinning disk systems elsewhere.

警告:虽然通常建议对使用传统旋转硬盘驱动器的系统进行交换,但是与SSD一起使用交换可能会导致随时间推移而出现硬件降级的问题。 基于此考虑,我们不建议在DigitalOcean或任何其他使用SSD存储的提供商上启用交换。 这样做可能会影响您和您的邻居的基础硬件的可靠性。 本指南仅供可能在其他地方使用磁盘系统的用户参考。

If you need to improve the performance of your server on DigitalOcean, we recommend upgrading your Droplet. This will lead to better results in general and will decrease the likelihood of contributing to hardware issues that can affect your service.

如果您需要提高DigitalOcean上服务器的性能,建议升级您的Droplet。 通常,这将导致更好的结果,并减少导致可能影响服务的硬件问题的可能性。

什么是交换? (What is Swap?)

Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM. Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.

交换是硬盘驱动器上的一个区域,已被指定为操作系统可以暂时将其不再保存在RAM中的数据存储的位置。 基本上,这使您能够增加服务器可以在其工作“内存”中保留的信息量,但有一些注意事项。 硬盘驱动器上的交换空间将主要在RAM中没有足够的空间来保存使用中的应用程序数据时使用。

The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.

写入磁盘的信息比保留在RAM中的信息要慢得多,但是操作系统将更喜欢将运行的应用程序数据保留在内存中,并对较旧的数据使用交换。 总体而言,在系统内存不足的情况下,使用交换空间作为后备,可以很好地防止具有非SSD存储的系统发生内存不足异常。

步骤1 –检查系统的交换信息 (Step 1 – Checking the System for Swap Information)

Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.

在开始之前,我们可以检查系统是否已经有一些交换空间可用。 可以有多个交换文件或交换分区,但是通常一个就足够了。

We can see if the system has any configured swap by typing:

我们可以通过键入以下命令查看系统是否具有任何已配置的交换:

  • sudo swapon --show

    sudo swapon-显示

If you don’t get back any output, this means your system does not have swap space available currently.

如果没有返回任何输出,则意味着您的系统当前没有可用的交换空间。

You can verify that there is no active swap using the free utility:

您可以使用free实用工具验证是否没有活动的交换:

  • free -h

    免费-h

   
   
Output
total used free shared buffers cached Mem: 1.0G 331M 668M 4.3M 11M 276M -/+ buffers/cache: 44M 955M Swap: 0B 0B 0B

As you can see in the Swap row of the output, no swap is active on the system.

如您在输出的“ 交换”行中所见,系统上没有任何交换处于活动状态。

第2步–检查硬盘分区上的可用空间 (Step 2 – Checking Available Space on the Hard Drive Partition)

Before we create our swap file, we’ll check our current disk usage to make sure we have enough space. Do this by entering:

在创建交换文件之前,我们将检查当前的磁盘使用情况,以确保有足够的空间。 输入以下内容来执行此操作:

  • df -h

    df -h

   
   
Output
Filesystem Size Used Avail Use% Mounted on /dev/vda1 25G 946M 23G 4% / udev 10M 0 10M 0% /dev tmpfs 201M 4.3M 196M 3% /run tmpfs 501M 0 501M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 501M 0 501M 0% /sys/fs/cgroup tmpfs 101M 0 101M 0% /run/user/1001

The device with / in the Mounted on column is our disk in this case. We have plenty of space available in this example (only 946M used). Your usage will probably be different.

在这种情况下,“已Mounted on列中带有/的设备就是我们的磁盘。 在此示例中,我们有足够的可用空间(仅使用946M)。 您的用法可能会有所不同。

Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.

尽管对于交换空间的适当大小有很多意见,但这实际上取决于您的个人喜好和您的应用程序要求。 通常,一个等于或大于系统RAM数量的内存是一个不错的起点。 另一个好的经验法则是,如果仅将4G交换用作RAM后备,则可能不需要任何交换。

第3步–创建交换文件 (Step 3 – Creating a Swap File)

Now that we know our available hard drive space, we can go about creating a swap file within our filesystem.

既然我们知道了可用的硬盘空间,我们就可以在文件系统中创建交换文件了。

We will create a file called swapfile in our root (/) directory. The file must allocate the amount of space we want for our swap file. There are two main ways of doing this:

我们将在根(/)目录中创建一个名为swapfile的文件。 该文件必须为交换文件分配我们想要的空间量。 有两种主要方法:

传统的慢速方式 (The Traditional, Slow Way)

Traditionally, we would create a file with preallocated space by using the dd command. This versatile disk utility writes from one location to another location.

传统上,我们将使用dd命令创建一个具有预分配空间的文件。 这个通用的磁盘实用程序可以从一个位置写入另一位置。

We can use this to write zeros to the file from a special device in Linux systems located at /dev/zero that just spits out as many zeros as requested.

我们可以使用它从位于/dev/zero Linux系统中的特殊设备向文件写入零,该设备只是根据请求吐出尽可能多的零。

We specify the file size by using a combination of bs for block size and count for the number of blocks. What we assign to each parameter is almost entirely arbitrary. What matters is what the product of multiplying them turns out to be.

我们使用的组合指定文件大小bs的块大小和count的块数。 我们分配给每个参数的内容几乎完全是任意的。 重要的是乘以它们的乘积结果是什么。

For instance, in our example, we’re looking to create a 1 Gigabyte file. We can do this by specifying a block size of 1 megabyte and a count of 1024:

例如,在我们的示例中,我们希望创建一个1 GB的文件。 我们可以通过指定1兆字节的块大小和1024的计数来做到这一点:

  • sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

    须藤dd if = / dev / zero of = / swapfile bs = 1M count = 1024

   
   
Output
1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 1.36622 s, 786 MB/s

Check your command before pressing ENTER because this has the potential to destroy data if you point the of (which stands for output file) to the wrong location.

在按ENTER之前,请先检查命令,因为如果将of (代表输出文件)指向错误的位置,则有可能破坏数据。

We can see that 1 Gigabyte has been allocated by typing:

我们可以看到通过键入以下内容分配了1 GB:

  • ls -lh /swapfile

    ls -lh / swapfile

   
   
Output
-rw-r--r-- 1 root root 1.0G May 30 15:07 /swapfile

If you’ve completed the command above, you may notice that it took a few seconds. Only 1.3 seconds for this small swapfile, but that could increase significantly for larger files on slower hard drives.

如果您完成了上面的命令,您可能会注意到它花了几秒钟。 这个小的交换文件只有1.3秒,但是对于速度较慢的硬盘驱动器上的较大文件来说,这可能会大大增加。

If you want to learn how to create the file faster, remove the file swapfile using sudo rm /swapfile, then follow along below:

如果要学习如何更快地创建文件,请使用sudo rm /swapfile删除文件sudo rm /swapfile ,然后按照以下步骤操作:

更快的方式 (The Faster Way)

The quicker way of getting the same file is by using the fallocate program. Note that this command only works with more modern filesystems, so if you’re using an ext3 system, for instance, this option is not available to you.

获得相同文件的更快方法是使用fallocate程序。 请注意,此命令仅适用于较新的文件系统,因此,例如,如果您使用的是ext3系统,则此选项不可用。

The fallocate command creates a file of a preallocated size instantly, without actually having to write dummy contents.

fallocate命令可立即创建一个预分配大小的文件,而无需实际写入虚拟内容。

We can create a 1 Gigabyte file by typing:

我们可以通过输入以下内容来创建一个1 GB的文件:

sudo fallocate -l 1G /swapfile

The prompt will be returned to you almost immediately. We can verify that the correct amount of space was reserved by typing:

提示将几乎立即返回给您。 我们可以通过输入以下内容来验证是否保留了正确的空间量:

  • ls -lh /swapfile

    ls -lh / swapfile

   
   
Output
-rw-r--r-- 1 root root 1.0G May 30 15:07 /swapfile

As you can see, our file is created with the correct amount of space set aside.

如您所见,我们的文件是在预留正确数量的空间的情况下创建的。

步骤4 –启用交换文件 (Step 4 – Enabling the Swap File)

Now that we have a file of the correct size available, we need to actually turn this into swap space.

现在我们有了一个正确大小的文件,我们实际上需要将其转换为交换空间。

First, we need to lock down the permissions of the file so that only the users with root privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.

首先,我们需要锁定文件的权限,以便只有具有root特权的用户才能读取内容。 这会阻止普通用户访问文件,这将对安全性产生重大影响。

Make the file only accessible to root by typing:

通过键入以下命令,使文件只能由root用户访问:

  • sudo chmod 600 /swapfile

    须藤chmod 600 / swapfile

Verify the permissions change by typing:

通过键入以下内容来验证权限更改:

  • ls -lh /swapfile

    ls -lh / swapfile

   
   
Output
-rw------- 1 root root 1.0G May 29 17:34 /swapfile

As you can see, only the root user has the read and write flags enabled.

如您所见,只有root用户启用了读取和写入标志。

We can now mark the file as swap space by typing:

现在,我们可以通过输入以下内容将文件标记为交换空间:

  • sudo mkswap /swapfile

    须藤mkswap / swapfile

   
   
Output
Setting up swapspace version 1, size = 1048572 KiB no label, UUID=757ee0b7-db04-46bd-aafb-adf6954ea077

After marking the file, we can enable the swap file, allowing our system to start utilizing it:

标记文件后,我们可以启用交换文件,从而使我们的系统可以开始使用它:

  • sudo swapon /swapfile

    sudo swapon /交换文件

Verify that the swap is available by typing:

通过键入以下命令验证交换是否可用:

  • sudo swapon --show

    sudo swapon-显示

   
   
Output
NAME TYPE SIZE USED PRIO /swapfile file 1024M 0B -1

We can check the output of the free utility again to corroborate our findings:

我们可以再次检查free实用程序的输出以证实我们的发现:

  • free -h

    免费-h

   
   
Output
total used free shared buffers cached Mem: 1.0G 925M 74M 4.3M 13M 848M -/+ buffers/cache: 63M 936M Swap: 1.0G 0B 1.0G

Our swap has been set up successfully and our operating system will begin to use it as necessary.

我们的交换已成功建立,并且我们的操作系统将在必要时开始使用它。

第5步–永久交换文件 (Step 5 – Making the Swap File Permanent)

Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our /etc/fstab file.

我们最近的更改已为当前会话启用了交换文件。 但是,如果重新启动,服务器将不会自动保留交换设置。 我们可以通过将交换文件添加到我们的/etc/fstab文件中来进行更改。

Back up the /etc/fstab file in case anything goes wrong:

备份/etc/fstab文件,以防万一出现问题:

  • sudo cp /etc/fstab /etc/fstab.bak

    须藤cp / etc / fstab /etc/fstab.bak

Add the swap file information to the end of your /etc/fstab file by typing:

通过输入以下命令将交换文件信息添加到/etc/fstab文件的末尾:

  • echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

    回显'/ swapfile无swap sw 0 0'| sudo tee -a / etc / fstab

Next we’ll review some settings we can update to tune our swap space.

接下来,我们将回顾一些我们可以更新以调整交换空间的设置。

步骤6 –调整交换设置 (Step 6 – Tuning your Swap Settings)

There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.

您可以配置一些选项,这些选项在处理交换时会影响系统的性能。

调整Swappiness属性 (Adjusting the Swappiness Property)

The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.

swappiness参数配置系统将RAM中的数据交换到交换空间的频率。 这是一个介于0到100之间的值,代表一个百分比。

With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Remember, interactions with the swap file are “expensive” in that they take a lot longer than interactions with RAM and they can cause a significant reduction in performance. Telling the system not to rely on the swap much will generally make your system faster.

值接近零时,除非绝对必要,否则内核不会将数据交换到磁盘。 请记住,与交换文件的交互是“昂贵的”,因为它们比与RAM的交互花费的时间长得多,并且它们可能会导致性能显着降低。 告诉系统不要太依赖交换,通常会使您的系统更快。

Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. Depending on your applications’ memory profile or what you are using your server for, this might be better in some cases.

接近100的值将尝试交换更多数据,以保持更多的RAM空间可用。 根据您的应用程序的内存配置文件或服务器使用的内容,在某些情况下可能会更好。

We can see the current swappiness value by typing:

我们可以通过输入以下内容来查看当前的swappiness值:

  • cat /proc/sys/vm/swappiness

    猫/ proc / sys / vm / swappiness

   
   
Output
60

For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.

对于台式机,swappiness设置为60并不是一个坏值。 对于服务器,您可能希望将其移近0。

We can set the swappiness to a different value by using the sysctl command.

我们可以使用sysctl命令将swappiness设置为其他值。

For instance, to set the swappiness to 10, we could type:

例如,要将swappiness设置为10,我们可以输入:

  • sudo sysctl vm.swappiness=10

    sudo sysctl vm.swappiness = 10

   
   
Output
vm.swappiness = 10

This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:

此设置将持续到下一次重新启动。 通过将行添加到/etc/sysctl.conf文件中,我们可以在重启时自动设置该值:

  • sudo nano /etc/sysctl.conf

    须藤nano /etc/sysctl.conf

At the bottom, you can add:

在底部,您可以添加:

/etc/sysctl.conf
/etc/sysctl.conf
vm.swappiness=10

Save and close the file when you are finished.

完成后保存并关闭文件。

调整缓存压力设置 (Adjusting the Cache Pressure Setting)

Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will choose to cache inode and dentry information over other data.

您可能要修改的另一个相关值是vfs_cache_pressure 。 此设置配置系统将选择在其他数据上缓存inodedentry信息的数量。

Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache. You can see the current value by querying the proc filesystem again:

基本上,这是有关文件系统的访问数据。 通常这查找起来非常昂贵,并且经常需要它,因此对于您的系统而言,这是一件很棒的事情。 您可以通过再次查询proc文件系统来查看当前值:

  • cat /proc/sys/vm/vfs_cache_pressure

    猫/ proc / sys / vm / vfs_cache_pressure

   
   
Output
100

As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:

由于当前已配置,因此我们的系统太快地从缓存中删除了索引节点信息。 我们可以通过输入以下内容将其设置为更保守的设置,例如50:

  • sudo sysctl vm.vfs_cache_pressure=50

    须藤sysctl vm.vfs_cache_pressure = 50

   
   
Output
vm.vfs_cache_pressure = 50

Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:

同样,这仅对我们当前的会话有效。 我们可以像通过swappiness设置一样将其添加到配置文件中来进行更改:

  • sudo nano /etc/sysctl.conf

    须藤nano /etc/sysctl.conf

At the bottom, add the line that specifies your new value:

在底部,添加用于指定新值的行:

/etc/sysctl.conf
/etc/sysctl.conf
vm.vfs_cache_pressure=50

Save and close the file when you are finished.

完成后保存并关闭文件。

结论 (Conclusion)

Following the steps in this guide will give you some breathing room in cases that would otherwise lead to out-of-memory exceptions. Swap space can be incredibly useful in avoiding some of these common problems.

遵循本指南中的步骤将为您提供喘息的机会,以防万一导致内存不足的异常。 交换空间对于避免这些常见问题非常有用。

If you are running into OOM (out of memory) errors, or if you find that your system is unable to use the applications you need, the best solution is to optimize your application configurations or upgrade your server.

如果遇到OOM(内存不足)错误,或者发现系统无法使用所需的应用程序,则最佳解决方案是优化应用程序配置或升级服务器。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-debian-8

debian创建交换分区

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值