歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux交換區(swap)的建立

linux交換區(swap)的建立

日期:2017/3/2 10:06:29   编辑:關於Linux

1. dd指令

使用 dd 這個指令來新增一個 128MB 的檔案在 /tmp 底下:

[root@www ~]# dd if=/dev/zero of=/tmp/swap bs=1M count=128

128+0 records in

128+0 records out

134217728 bytes (134 MB) copied, 1.7066 seconds, 78.6 MB/s

查看

[root@www ~]# ls -l /tmp/swap

-rw-r--r-- 1 root root 128M Oct 28 15:33 /tmp/swap

這樣一個 128MB 的檔案就建置妥當。

2. mkswap指令

使用 mkswap 將 /tmp/swap 這個檔案格式化為 swap 的檔案格式:

[root@www ~]# mkswap /tmp/swap

Setting up swapspace version 1, size = 134213 kB

# 這個指令下達時請'特別小心',因為下錯字元控制,將可能使您的檔案系統掛掉!

3. swapon指令

使用 swapon 來將 /tmp/swap 啟動。

查看現有swap 大小

[root@www ~]# free

total used free shared buffers cached

Mem: 742664 450860 291804 0 45584 261284

-/+ buffers/cache: 143992 598672

Swap: 1277088 96 1276992

[root@www ~]# swapon /tmp/swap

查看現在swap 大小

[root@www ~]# free

total used free shared buffers cached

Mem: 742664 450860 291804 0 45604 261284

-/+ buffers/cache: 143972 598692

Swap: 1408152 96 1408056

[root@www ~]# swapon -s

Filename Type Size Used Priority

/dev/hdc5 partition 1020088 96 -1

/tmp/swap file 131064 0 -3

4. swapoff指令

使用 swapoff 關掉 swap file

[root@www ~]# swapoff /tmp/swap

[root@www ~]# free

total used free shared buffers cached

Mem: 742664 450860 291804 0 45660 261284

-/+ buffers/cache: 143916 598748

Swap: 1020088 96 1019992 <==回復成最原始的樣子了!

我們需要將swap掛載在/etc/fstab中。

利用命令:sudo gedit /etc/fatab,打開fatab文件,在其中添加:

# <file system> <mount point> <type> <options> <dump> <pass>

proc /proc proc defaults 0 0

/tmp/swap none swap defaults 0 0

source /etc/fstab即可即時生效,也可以保證下次重新啟動的時候依然生效。就這麼簡單。

Copyright © Linux教程網 All Rights Reserved