在linux下增加swap分区

本文详细介绍了如何在Linux系统中配置Swap空间,包括创建、激活Swap文件以及查看Swap空间使用情况。通过使用`/dev/zero`设备初始化文件,并使用`dd`命令创建指定大小的Swap文件。同时,提供了激活Swap文件的方法以及如何使用`free`命令检查Swap空间的使用状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.查看系统Swap空间使用

[root@jumper usr]# free
             total       used       free     shared    buffers     cached
Mem:       2026588     889060    1137528          0      43676     498624
-/+ buffers/cache:     346760    1679828
Swap:      1535996          0    1535996

2.在空间合适处创建swap文件

[root@localhost usr]# mkdir swap
[root@localhost usr]# cd swap
[root@localhost swap]# dd if=/dev/zero of=swapfile bs=1M count=1500
1500+0 records in
1500+0 records out
[root@localhost swap]# ls -al
total 1536012
drwxr-xr-x    2 root     root         4096  9月 19 11:09 .
drwxr-xr-x   13 root     root         4096  9月 19 11:02 ..
-rw-r--r--    1 root     root     10240000  9月 19 11:59 swapfile


[root@localhost swap]# mkswap swapfile
Setting up swapspace version 1, size = 1535996 KiB


3.激活swap文件

[root@localhost swap]# swapon swapfile
[root@localhost swap]# ls -l
total 1536004
-rw-r--r-- 1 root root 1572864000  9月 19 11:59 swapfile
[root@localhost swap]# free
             total       used       free     shared    buffers     cached
Mem:       2026588    1932212      94376          0     105504    1366920
-/+ buffers/cache:     459788    1566800
Swap:      3071992          0    3071992
[root@jumper swap]# 


来源:http://www.eygle.com/archives/2004/07/howto_add_linux_swap.html 



作者: hsvea 发布日期: 2009-10-16/dev/zero,是一个输入设备,你可你用它来初始化文件。
/dev/zero------该设备无穷尽地提供0,可以使用任何你需要的数目——设备提供的要多的多。他可以用于向设备或文件写入字符串0。

oracle@localhost oracle]$if=/dev/zero of=./test.txt bs=1k count=1
oracle@localhost oracle]$ ls -l
total 4
-rw-r--r-- 1 oracle dba 1024 Jul 15 16:56 test.txt

eg,

find / -name access_log 2>/dev/null

这样,一些诸如一些错误信息就不会显示出来。


/dev/zero:

In Unix-like operating systems, /dev/zero is a special file that provides as many null characters (ASCII NULL, 0x00; not ASCII character "digit zero", "0", 0x30) as are read from it. One of the typical uses is to provide a character stream for overwriting information. Another might be to generate a clean file of a certain size. Using mmap to map /dev/zero to RAM is the BSD way of implementing shared memory.

# Initialise partition (important note: trying out this command will eradicate
# any files that were on the partition, make sure you have a backup of any important data.)
dd if=/dev/zero of=/dev/hda7

# Create a large empty file called 'foobar'
dd if=/dev/zero of=foobar count=1000 bs=1000

Like /dev/null, /dev/zero acts as a source and sink for data. All writes to /dev/zero succeed with no other effects (the same as for /dev/null, although /dev/null is the more commonly used data sink); all reads on /dev/zero return as many NULs as characters requested.


来源:http://www.linuxdiyf.com/viewarticle.php?id=161384

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值