This Bash script automates the process of creating and configuring a swap file on a Linux system for improved performance. Swapping is a technique used by the operating system to provide additional memory space when the physical RAM is insufficient.
Usage
- Download the Script: You can download the script directly using the following command:
wget https://raw.githubusercontent.com/abhranil26/swap-file-bash-script/main/create_swap.sh
Alternatively, you can also use curl to download the script:
curl -O https://raw.githubusercontent.com/abhranil26/swap-file-bash-script/main/create_swap.sh
- Make the Script Executable: Once the script is downloaded, make it executable using the following command:
chmod +x create_swap.sh
- Run the Script: Execute the script with superuser privileges using sudo:
sudo ./create_swap.sh
curl -fsSL https://raw.githubusercontent.com/abhranil26/swap-file-bash-script/main/create_swap.sh -o create_swap.sh && sudo sh ./create_swap.sh
What does the Script Do?
- The script uses fallocate to create a swap file. It will ask the user for the sie of the swap file, just enter the number like 2, 4 or 16.
- It sets appropriate permissions for the created swap file using chmod.
- The swap file is formatted for use using mkswap.
- The swap file is activated using swapon.
- An entry is added to /etc/fstab to enable the swap file on system startup.
- The vm.swappiness value in /etc/sysctl.conf is adjusted to control swapping aggressiveness.
- The script applies the changes made to sysctl.conf using sysctl -p.
- Verification of successful swap creation and activation is done using swapon -s.