大部分vps都没有swap分区,下面的办法提供了增加一个2G swap分区的方式
创建分区
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo “/swapfile swap swap defaults 0 0” >> /etc/fstab
删除分区
swapoff -v /swapfile
大部分vps都没有swap分区,下面的办法提供了增加一个2G swap分区的方式
创建分区
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo “/swapfile swap swap defaults 0 0” >> /etc/fstab
删除分区
swapoff -v /swapfile
首先,创建swap的基本步骤是:
sudo fallocate -l 4G /swapfile
# 或者用 dd 命令替代
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
删除swap:
sudo swapoff /swapfile
sudo vim /etc/fstab
/swapfile none swap sw 0 0
或者用sed命令:
sudo sed -i '/swapfile/d' /etc/fstab
sudo rm /swapfile
确认swap已被删除:
# 查看当前swap使用情况
free -h
# 或者
swapon --show
@Chatbot Translate this thread to English please!
Certainly! Here’s the translation of the thread:
linuxren said: Most VPS do not have a swap partition. The following method provides a way to add a 2GB swap partition.
Creating the partition:
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
Deleting the partition:
swapoff -v /swapfile
Tristan said: First, the basic steps to create a swap file are:
sudo fallocate -l 4G /swapfile
# Or use the dd command as an alternative
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
fallocate
is faster but may not be supported by some file systems.dd
is more universal; bs
is block size, and count
is the number of blocks.sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Tristan said: To delete swap:
sudo swapoff /swapfile
sudo vim /etc/fstab
/swapfile none swap sw 0 0
Or use the sed command:
sudo sed -i '/swapfile/d' /etc/fstab
sudo rm /swapfile
Confirm that the swap has been deleted:
# Check current swap usage
free -h
# Or
swapon --show
Let me know if you need anything else!