Linux 调整 Swap

linux-swap

关于在 Linux 安装后调整 Swap 的方法

调整步骤

  1. 切换到超级用户

    1
    2
    $ su root
    > Password:
  2. 在 「/root/」目录下创建一个 2GB 名为「myswaofile」的交换文件

    1
    2
    3
    4
    5
    6
    7
    # dd if=/dev/zero of=/root/myswapfile bs=1G count=2
    > 2+0 records in
    > 2+0 records out
    > 2147483648 bytes (2.1 GB, 2.0 GiB) copied, 24.9961 s, 85.9 MB/s
    # ls -l /root/myswapfile
    > -rw-r--r--. 1 root root 2147483648 Sep 6 11:20 /root/myswapfile
  3. 设置文件权限,以保护此 swap 分区

    1
    # chmod 600 /root/myswapfile
  4. 使用 mkswap命令,让此文件设置 「swap」

    1
    2
    3
    # mkswap /root/myswapfile
    > Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
    > no label, UUID=efdad09d-d48c-4c2b-a2e4-7704b4991881
  5. 启动交换文件

    1
    # swapon /root/myswapfile
  6. 为了确保开机启动,在「/etc/fstab」末尾加入:

    CoreOS 无此文件

    1
    2
    # cat /etc/fstab
    > cat: /etc/fstab: No such file or directory
  7. 查看系统中交换文件设置情况

    1
    2
    3
    # swapon -s
    > Filename Type Size Used Priority
    > /root/myswapfile file 2097148 0 -1
  8. 使用 free 确认再次确认

    1
    2
    3
    4
    5
    # free -h
    > total used free shared buffers cached
    > Mem: 11G 4.6G 7.2G 16M 71M 2.6G
    > -/+ buffers/cache: 1.9G 9.8G
    > Swap: 2.0G 0B 2.0G

其他命令

打开所有交换空间

1
# swappon -a

关闭所有交换空间

1
# swappoff -a


题图:来源于 Google
本站点采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。