为什么我的交换区在 Ubuntu Server 18.04.2 中设置为零?

JRG*_*JRG 2 swap

通过检查我的 Ubuntu 服务器中的交换大小,我发现它设置为零。由于它是从 AWS EC2 以非常基本的配置安装的,因此我不确定是否需要执行其他步骤来调整交换大小区域。

我运行以下命令并得到以下结果:

    # grep Swap /proc/meminfo
       SwapCached:            0 kB
       SwapTotal:             0 kB
       SwapFree:              0 kB

    # swapon -s 

    #  free -m
                     total        used        free      shared  buff/cache   available
    Mem:           7975         187        7059           0         728        7549
    Swap:             0           0           0

# cat /proc/swaps
  Filename                         Type            Size    Used    Priority
Run Code Online (Sandbox Code Playgroud)

交换区设置为零是否正常?如果不是,我应该做什么来修复它?

谢谢 !

pa4*_*080 5

由于某些原因,一些云 (VPS) 提供商在其(修改后的)安装中禁用交换。现在Ubuntu支持交换文件,因此你不需要单独的交换分区。要通过交换文件启用交换,请按照下列步骤操作:

sudo fallocate -l 4G /swapfile  # Create a 'swap-file'; 4G in this case
sudo chmod 600 /swapfile        # Set the necessary file permissions
ls -lh /swapfile                # Check
sudo mkswap /swapfile           # Mark the file as 'swap'
sudo swapon /swapfile           # Enable the 'swap'
sudo swapon --show              # Check
free -h                         # Another check
Run Code Online (Sandbox Code Playgroud)

编辑/etc/fstab并使更改永久生效。使用这些命令进行更改:

sudo sed 's/^.*swap.*$//' /etc/fstab -i.bak                # Remove the previous swap entries 
                                                           # and create a backup copy of the file
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab # Add a new entry
Run Code Online (Sandbox Code Playgroud)

或者手动编辑/etc/fstab并添加以下条目:

/swapfile none swap sw 0 0
Run Code Online (Sandbox Code Playgroud)

这就对了。


其他调整的想法(我更愿意使用正常交换使用的默认设置):

  • 更改RAM的频率以进行SWAP数据复制:

    sudo sysctl vm.swappiness=10    # value 0-100: low value low frequency
    cat /proc/sys/vm/swappiness     # Check
    
    Run Code Online (Sandbox Code Playgroud)
  • 更改缓存刷新频率:

    sudo sysctl vm.vfs_cache_pressure=50 # 0-100: high value high frequency
    cat /proc/sys/vm/vfs_cache_pressure  # Check
    
    Run Code Online (Sandbox Code Playgroud)
  • 使上述更改永久生效:

    sudo cp /etc/sysctl.conf{,.bak} # Create a backup copy of the file '/etc/sysctl.conf'
    echo -e '\nCustom settings: value 0-100; default 100\nvm.swappiness=10\nvm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
    
    Run Code Online (Sandbox Code Playgroud)

    或者手动编辑/etc/sysctl.conf并添加以下条目:

    # Custom settings: value 0-100; default 100
    vm.swappiness=10
    vm.vfs_cache_pressure=50
    
    Run Code Online (Sandbox Code Playgroud)