Last active
April 23, 2025 17:26
-
-
Save gigabyteservice/1494c2e45532b061501419d21926fff3 to your computer and use it in GitHub Desktop.
Bash script to create 4GB swap memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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