Skip to content

Instantly share code, notes, and snippets.

@cuiwm
Forked from vutranvn/Create swap space on Ubuntu
Created October 9, 2021 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuiwm/4801e2aefd4adc3401a61cac68523e11 to your computer and use it in GitHub Desktop.
Save cuiwm/4801e2aefd4adc3401a61cac68523e11 to your computer and use it in GitHub Desktop.
** Create swap **
- Check swap info
> swapon --show
> free -h
- Check available space disk
> df -h
- Create swap
> sudo fallocate -l 2G /swapfile
- Verify
> ls -lh /swapfile
Output:
-rw-r--r-- 1 root root 2.0G Oct 8 04:57 /swapfile
- Enabling the swap file
> sudo chmod 600 /swapfile
- Mark the file as swap space
> mkswap /swapfile
Output:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=f05afaa0-48ba-453b-bcb1-f5d59a9e75f7
- Enable the swap file
> swapon /swapfile
- Verify info
> swapon --show
Output:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -1
- Save config when system reboot
> echo /swapfile none swap defaults 0 0 >> /etc/fstab
- Config the threshold RAM to using SWAP
/**
* swappiness = 0 : When RAM empty
* swappiness = 10: When RAM remaining 10%. [Recommend]
* swappiness = 60: When RAM remaining 60%
* swappiness = 100: Using swap same ram
**/
- Check swappiness
> cat /proc/sys/vm/swappiness
Output
60
- Set swappiness
> sysctl vm.swappiness=10
- Verify
> cat /proc/sys/vm/swappiness
- Save when system reboot
> nano /etc/sysctl.conf
- add more line in the end:
vm.swappiness = 10
- Config more vfs_cache_pressure
** Change space swap **
- Turn off swap
> swapoff /swapfile
- Remove swap
> rm -f /swapfile
- Renew swap
> sudo dd if=/dev/zero of=/swapfile bs=1024 count=2048k
- Make swap
> mkswap /swapfile
- Enable swap
> swapon /swapfile
- chmod, chown file
> chown root:root /swapfile
> chmod 0600 /swapfile
- Verify info
> swapon -s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment