Skip to content

Instantly share code, notes, and snippets.

@roeniss
Created January 6, 2020 06:15
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 roeniss/a7f4b166c3fa25f10468194af432d670 to your computer and use it in GitHub Desktop.
Save roeniss/a7f4b166c3fa25f10468194af432d670 to your computer and use it in GitHub Desktop.
makeSwap.sh
# original article: [How To Add Swap Space on Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04)
# Cautious! Upgrading RAM is safer than this job.
# 1. check status
sudo swapon --show # no output == no available swap space
free -h # another way to do right above line
df -h # check available disk
# 2. create/enable swap file
sudo fallocate -l 1G /swapfile # "'fallocate' creates a file of a preallocated size instantly."
sudo chmod 600 /swapfile # "This prevents normal users from being able to access the file, which would have significant security implications."
ls -lh /swapfile # verify if above jobs correctly done
sudo mkswap /swapfile # "mark the file as swap space by typing this"
sudo swapon /swapfile # "allowing our system to start utilizing it"
sudo swapon --show # check if the swap is available now
free -h # another way to do right above line
# 3. make the swap permanent
# ''' Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our /etc/fstab file. '''
sudo cp /etc/fstab /etc/fstab.bak # "Back up the /etc/fstab file in case anything goes wrong"
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab # add line to /etc/fstab
@roeniss
Copy link
Author

roeniss commented Jan 6, 2020

in original post, there is a chapter 4 which introduces a little advanced tips on setting swap.

@roeniss
Copy link
Author

roeniss commented Jan 6, 2020

AWS introduced another command, dd: https://aws.amazon.com/ko/premiumsupport/knowledge-center/ec2-memory-swap-file/

sudo dd if=/dev/zero of=/swapfile bs=128M count=32
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s
sudo vi /etc/fstab # add: /swapfile swap swap defaults 0 0 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment