如何在centos6.x 下建立swap文件

原文出处

https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-centos-6


Check for Swap Space

检查Swap空间

Before we proceed to set up a swap file, we need to check if any swap files have been enabled by looking at the summary of swap usage.

在我们开始建立一个swap文件之前我们需要先检查是否已经有swap并正在使用了。通过swap查看summary来看

swapon -s

If nothing is returned, the summary is empty and no swap file exists.

如果什么都没有返回,summary就是空的并没有swap文件被显示出来

Check the File System

检查文件系统

After we know that we do not have a swap file enabled, we can check how much space we have on the server with the df command. The swap file will take 512MB— since we are only using up about 7% of the /dev/hda, we can proceed.

在我们确定我们没有swap文件被启用后,我们可以检查我们的服务器有多少空间。使用df命令来查看。swap文件将会占用512MB。因为我们现在只用了/dev/hda的7%,所以是可行的

df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda              20642428   1347968  18245884   7% /

Create and Enable the Swap File

创建并启用swap文件

Now it’s time to create the swap file itself using the dd command :

现在是通过dd命令来创建swap文件的时候了

sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

“of=/swapfile” designates the file’s name. In this case the name is swapfile.

"of=/swapfile" 指定了文件的名字,在这个例子中文件名是 swapfile

Subsequently we are going to prepare the swap file by creating a linux swap area:

随后我们要通过创建linux swap区域来初始化swap文件

sudo mkswap /swapfile

The results display:

执行结果是

Setting up swapspace version 1, size = 536866 kB

Finish up by activating the swap file:

最后我们激活swap文件

sudo swapon /swapfile

You will then be able to see the new swap file when you view the swap summary.

现在当你查看swap summary的时候你就可以看到那个新的swap文件了

 swapon -s
Filename				Type		Size	Used	Priority
/swapfile                               file		524280	0	-1

This file will last on the server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.

这个文件会一直存在直到你重启。如果你想让这个文件永久保留,可以通过添加以下行到fstab文件里

Open up the file:

打开fstab文件

sudo nano /etc/fstab

Paste in the following line:

把下面这行粘贴进去

/swapfile          swap            swap    defaults        0 0

To prevent the file from being world-readable, you should set up the correct permissions on the swap file:

为了防止匿名用户访问这个文件,我们要设置一下swap文件的权限

chown root:root /swapfile 
chmod 0600 /swapfile

猜你喜欢

转载自blog.csdn.net/nsrainbow/article/details/52343351