Skip to content

Instantly share code, notes, and snippets.

@chusiang
Last active March 19, 2017 17:34
Show Gist options
  • Save chusiang/965fae2e3a8707792eb5 to your computer and use it in GitHub Desktop.
Save chusiang/965fae2e3a8707792eb5 to your computer and use it in GitHub Desktop.
Enable swap with swapfile for Linux.
#!/bin/bash
# ============================================================
# Author: 凍仁翔 / chusiang.lai (at) gmail.com
# Blog: http://note.drx.tw
# Filename: create-swapfile.sh
# Modified: 2015-04-22 09:50
# Description: Enable swap with swapfile for Linux.
# Reference:
# 1. Linux Add a Swap File | nixCraft
# - http://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/
# ===========================================================
# Size of swapfile.
#
# 1G: 1048576
# 2G: 2097152
# 4G: 4194304
# 8G: 8388608
# 16G: 16777216 (not recommended)
#
SIZE="2097152"
# Create swapfile.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=$SIZE
# Format swapfile.
sudo mkswap /swapfile
# Enable swap.
sudo swapon /swapfile
# Mount swap at boot.
sudo su -c "cat >> /etc/fstab << EOF
/swapfile none swap sw 0 0
EOF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment