CentOS 7.x adding swap space using file

From Notes_Wiki

Home > CentOS > CentOS 7.x > System Administration > File system management > CentOS 7.x adding swap space using file

In CentOS 7.x swap can be configured via disk partitions. It can also be configured from file located on any of the existing file-systems. To create a file and configure it as swap use following steps

  1. Install qemu-img package to create swap file. We can also create file using dd.
    yum -y install qemu-img
  2. Use qemu-img to create swap file of desired size
    qemu-img create -f raw -o preallocation=full /swap.raw 2G
    The file can be created anywhere instead of /swap.raw suggested in these steps
  3. Set correct file permissions on the swap file
    chmod 0600 /swap.raw
  4. Format file as swap
    mkswap /swap.raw
  5. Add entry in /etc/fstab for the file using following contents
    /swap.raw none swap defaults 0 0
  6. Mount all partitions and enable swap
    mount -a
    swapon -a
  7. Check whether additional swap is visible
    free -m

These steps can be used to add swap to cloud (Amazon AWS or Azure) etc. VMs as they do not come with swap. However, various cloud providers recommend not enabling swap and increasing ram instead.


Home > CentOS > CentOS 7.x > System Administration > File system management > CentOS 7.x adding swap space using file