Skip to content

Instantly share code, notes, and snippets.

@gigabyteservice
Last active April 23, 2025 17:26
Show Gist options
  • Save gigabyteservice/1494c2e45532b061501419d21926fff3 to your computer and use it in GitHub Desktop.
Save gigabyteservice/1494c2e45532b061501419d21926fff3 to your computer and use it in GitHub Desktop.
Bash script to create 4GB swap memory
#!/bin/bash
# Set swap file size (4GB)
SWAPFILE="/swapfile"
SIZE="4G"
echo "Creating a $SIZE swap file at $SWAPFILE..."
# Create the swap file
sudo fallocate -l $SIZE $SWAPFILE || sudo dd if=/dev/zero of=$SWAPFILE bs=1G count=4
# Set the correct permissions
sudo chmod 600 $SWAPFILE
# Set up the swap area
sudo mkswap $SWAPFILE
# Enable the swap
sudo swapon $SWAPFILE
# Make the swap permanent
if ! grep -q "$SWAPFILE" /etc/fstab; then
echo "$SWAPFILE none swap sw 0 0" | sudo tee -a /etc/fstab
fi
echo "Swap file created and activated successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment