Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active November 19, 2017 16:14
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 leodutra/8e9d77395a796979a7c4636ca610c97e to your computer and use it in GitHub Desktop.
Save leodutra/8e9d77395a796979a7c4636ca610c97e to your computer and use it in GitHub Desktop.
Create swap file (Ubuntu)

READ THIS FOR AMAZON LINUX

Swap file creation

# As root use fallocate to create a swap file 
# the size of your choosing (M = Mebibytes, G = Gibibytes). 
# For example, creating a 512 MiB swap file:  
fallocate -l 512M /swapfile  

# Note: fallocate may cause problems with some file systems such as F2FS or XFS.[1] 
# As an alternative, using dd is more reliable, but slower:  
dd if=/dev/zero of=/swapfile bs=1M count=512  

# Set the right permissions (a world-readable swap file is a huge local vulnerability)  
chmod 600 /swapfile  

# After creating the correctly sized file, format it to swap:  
mkswap /swapfile  

# Activate the swap file:  
swapon /swapfile  

# Finally, edit fstab to add an entry for the swap file:  
/etc/fstab
/swapfile none swap defaults 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment