Ubuntu 18.04 修改swap分区大小 | ubuntu18.04分区设置

Ubuntu 18.04 修改swap分区大小 | ubuntu18.04分区设置
Ubuntu 18.04 修改swap分区大小

1、查看初始状态

查看原先swap大小

[email protected]:~# free -h              总计         已用        空闲      共享    缓冲/缓存    可用内存:         62G        417M         38G        3.0M         23G         61G交换:        2.0G          0B        2.0G[email protected]:~#

原先swap文件位置

[email protected]:~# swapon  -s文件名             类型      大小  已用  权限/swapfile1                              file        12582908    0   -2[email protected]:~#

2、创建一个新的swap文件

一般swap分区要大于或等于物理内存(1-1.5倍),最大一般有20G即可,我这里创建12G:

[email protected]:~# cd /[email protected]:/# dd if=/dev/zero of=/swapfile1 bs=1G count=12记录了12+0 的读入记录了12+0 的写出12884901888 bytes (13 GB, 12 GiB) copied, 17.1497 s, 751 MB/s[email protected]:/# ll总用量 14680188drwxr-xr-x  25 root root        4096 4月  10 17:22 ./drwxr-xr-x  25 root root        4096 4月  10 17:22 ../drwxr-xr-x   2 root root        4096 4月  10 06:59 bin/drwxr-xr-x   4 root root        4096 4月  10 06:59 boot/drwxrwxr-x   2 root root        4096 3月  25 20:44 cdrom/drwxr-xr-x   5 netc netc          58 4月   9 14:01 data/drwxr-xr-x  19 root root        4340 3月  29 07:50 dev/drwxr-xr-x 127 root root       12288 4月  10 06:59 etc/drwxr-xr-x   3 root root        4096 3月  25 20:45 home/lrwxrwxrwx   1 root root          33 4月   3 06:24 initrd.img -> boot/initrd.img-4.18.0-17-genericlrwxrwxrwx   1 root root          33 4月   4 06:31 initrd.img.old -> boot/initrd.img-4.18.0-16-genericdrwxr-xr-x  21 root root        4096 3月  25 20:56 lib/drwxr-xr-x   2 root root        4096 2月  10 08:12 lib64/drwx------   2 root root       16384 3月  25 20:41 lost+found/drwxr-xr-x   2 root root        4096 2月  10 08:12 media/drwxr-xr-x   2 root root        4096 2月  10 08:12 mnt/drwxr-xr-x   2 root root        4096 2月  10 08:12 opt/dr-xr-xr-x 323 root root           0 3月  26 10:39 proc/drwx------   8 root root        4096 3月  26 11:22 root/drwxr-xr-x  31 root root        1040 4月  10 17:20 run/drwxr-xr-x   2 root root       12288 4月  10 06:58 sbin/drwxr-xr-x  12 root root        4096 3月  26 08:54 snap/drwxr-xr-x   2 root root        4096 2月  10 08:12 srv/-rw-------   1 root root  2147483648 3月  25 20:41 swapfile     # 之前的swap文件-rw-r--r--   1 root root 12884901888 4月  10 17:22 swapfile1    # 新创建的swap文件dr-xr-xr-x  13 root root           0 4月  10 17:11 sys/drwxrwxrwt  10 root root       12288 4月  10 17:23 tmp/drwxr-xr-x  10 root root        4096 2月  10 08:12 usr/drwxr-xr-x  14 root root        4096 2月  10 08:20 var/lrwxrwxrwx   1 root root          30 4月   3 06:24 vmlinuz -> boot/vmlinuz-4.18.0-17-genericlrwxrwxrwx   1 root root          30 4月   4 06:31 vmlinuz.old -> boot/vmlinuz-4.18.0-16-generic[email protected]:/#

3、创建swap文件系统

[email protected]:/# mkswap -f swapfile1mkswap: swapfile1:不安全的权限 0644,建议使用 0600。正在设置交换空间版本 1,大小 = 12 GiB (12884897792  个字节)无标签, UUID=3779f693-8356-42e9-8a2c-2ab51f12654a[email protected]:/# chmod 0600 swapfile1[email protected]:/# ll总用量 14680188drwxr-xr-x  25 root root        4096 4月  10 17:22 ./drwxr-xr-x  25 root root        4096 4月  10 17:22 ../drwxr-xr-x   2 root root        4096 4月  10 06:59 bin/drwxr-xr-x   4 root root        4096 4月  10 06:59 boot/drwxrwxr-x   2 root root        4096 3月  25 20:44 cdrom/drwxr-xr-x   5 netc netc          58 4月   9 14:01 data/drwxr-xr-x  19 root root        4340 3月  29 07:50 dev/drwxr-xr-x 127 root root       12288 4月  10 06:59 etc/drwxr-xr-x   3 root root        4096 3月  25 20:45 home/lrwxrwxrwx   1 root root          33 4月   3 06:24 initrd.img -> boot/initrd.img-4.18.0-17-genericlrwxrwxrwx   1 root root          33 4月   4 06:31 initrd.img.old -> boot/initrd.img-4.18.0-16-genericdrwxr-xr-x  21 root root        4096 3月  25 20:56 lib/drwxr-xr-x   2 root root        4096 2月  10 08:12 lib64/drwx------   2 root root       16384 3月  25 20:41 lost+found/drwxr-xr-x   2 root root        4096 2月  10 08:12 media/drwxr-xr-x   2 root root        4096 2月  10 08:12 mnt/drwxr-xr-x   2 root root        4096 2月  10 08:12 opt/dr-xr-xr-x 323 root root           0 3月  26 10:39 proc/drwx------   8 root root        4096 3月  26 11:22 root/drwxr-xr-x  31 root root        1040 4月  10 17:20 run/drwxr-xr-x   2 root root       12288 4月  10 06:58 sbin/drwxr-xr-x  12 root root        4096 3月  26 08:54 snap/drwxr-xr-x   2 root root        4096 2月  10 08:12 srv/-rw-------   1 root root  2147483648 3月  25 20:41 swapfile-rw-------   1 root root 12884901888 4月  10 17:22 swapfile1dr-xr-xr-x  13 root root           0 4月  10 17:11 sys/drwxrwxrwt  10 root root       12288 4月  10 17:23 tmp/drwxr-xr-x  10 root root        4096 2月  10 08:12 usr/drwxr-xr-x  14 root root        4096 2月  10 08:20 var/lrwxrwxrwx   1 root root          30 4月   3 06:24 vmlinuz -> boot/vmlinuz-4.18.0-17-genericlrwxrwxrwx   1 root root          30 4月   4 06:31 vmlinuz.old -> boot/vmlinuz-4.18.0-16-generic[email protected]:/#

4、开启新的swap

[email protected]:/# swapoff /swapfile[email protected]:/# free -h              总计         已用        空闲      共享    缓冲/缓存    可用内存:         62G        417M         38G        3.0M         23G         61G交换:          0B          0B          0B[email protected]:/# swapon /swapfile1[email protected]:/# free -h              总计         已用        空闲      共享    缓冲/缓存    可用内存:         62G        420M         38G        3.0M         23G         61G交换:         11G          0B         11G[email protected]:/#

5、设置开机启动

[email protected]:/# vim /etc/fstab/swapfile                                 none            swap    sw              0       0改为/swapfile1                                none            swap    sw              0       0[email protected]:/#

6、重启

重启,然后查看是否有问题

shutdown -r now

可以先在虚拟机上测试,如果成功的话,再在物理机操作,如果都没问题的话,可以删掉旧的swap文件

rm -f /swapfile

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部