Linux禁用swap

sudo swapoff -a

这一步会立即禁用swap,但是下次重启还会启用。

/etc/fstab中删除swap条目

mask相关systemd unit

systemd可能会从/etc/fstab中的swap条目生成对应的systemd unit:https://www.freedesktop.org/software/systemd/man/systemd.swap.html

但是从/etc/fstab里把swap条目删除貌似并不会把这些swap systemd unit给禁用。所以得手动mask。先查询这些swap条目:

systemctl --type swap

输出:

  UNIT          LOAD   ACTIVE SUB    DESCRIPTION
  dev-sda3.swap loaded active active Swap Partition

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

然后把这个条目给mask:

sudo systemctl mask dev-sda3.swap

之后重启运行free就会看到swap那里为0了。

注意事项

这里说最好不要systemctl mask swap.targethttps://askubuntu.com/questions/1065503/how-to-remove-systemd-targets

参考文献

https://unix.stackexchange.com/questions/551185/how-do-i-permanently-disable-swap-on-archlinux

https://wiki.archlinux.org/title/swap#Disabling_swap

posted @ 2024-09-28 13:07  寻找繁星  阅读(31)  评论(0编辑  收藏  举报