Task: 
VM has no swap. Would like to add a swap file. 
 
 
Steps: 
1. Become root: 
$ sudo su - 
<enter password> 
 
2. Verify VM memory and if has a swap: 
# free -h 
               total        used        free      shared  buff/cache   available 
Mem:           7.7Gi       5.8Gi       102Mi       299Mi       2.4Gi       2.0Gi 
 
Note: 
If you see a Swap entry like below, then the server already has one - either a partition or a file. 
Check the /etc/fstab for the existing swap config. 
e.g.  
Swap:          4.0Gi          0B       4.0Gi 
 
 
3. If there was a swap file, and not partition, and you wanted to change it, turn it off with: 
# swapoff -a 
 
Note: 
Ensure beforehand that you have the physical memory to transfer any used swap to physical memory. 
 
4. Create the empty swapfile and set its permissions for system access: 
# dd if=/dev/zero of=/swapfile bs=1M count=4096 
<wait a few seconds, the bigger the swap file, the longer to wait> 
 
# chmod 0600 /swapfile 
 
5. Initialize the swapfile and enable it: 
# mkswap /swapfile 
# swapon /swapfile 
 
5. Confirm the swapfile is working: 
# free -h 
               total        used        free      shared  buff/cache   available 
Mem:           7.7Gi       5.8Gi       102Mi       299Mi       2.4Gi       2.0Gi 
Swap:          4.0Gi          0B       4.0Gi 
 
6. If swap is listed, it is working. Add the swapfile to the end of the boot /etc/fstab config: 
# vi /etc/fstab 
... 
/swapfile none swap sw 0 0 
 
<esc>:wq (to save) 
  
previous page
 
  |