1. 程式人生 > >Ubuntu 18.04 修改swap分區大小

Ubuntu 18.04 修改swap分區大小

如果 設置 cdrom 刪掉 啟動 rom down 緩存 generic

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
總用量 14680188
drwxr-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-generic
lrwxrwxrwx   1 root root          33 4月   4 06:31 initrd.img.old -> boot/initrd.img-4.18.0-16-generic
drwxr-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-generic
lrwxrwxrwx   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 swapfile1
mkswap: swapfile1:不安全的權限 0644,建議使用 0600。
正在設置交換空間版本 1,大小 = 12 GiB (12884897792  個字節)
無標簽, UUID=3779f693-8356-42e9-8a2c-2ab51f12654a
[email protected]:/# chmod 0600 swapfile1
[email protected]:/# ll
總用量 14680188
drwxr-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-generic
lrwxrwxrwx   1 root root          33 4月   4 06:31 initrd.img.old -> boot/initrd.img-4.18.0-16-generic
drwxr-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 swapfile1
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-generic
lrwxrwxrwx   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

Ubuntu 18.04 修改swap分區大小