swap.sh
· 865 B · Bash
Eredeti
#!/bin/bash
# Define the swap file location and size
SWAPFILE="/swapfile"
SWAPSIZE="64G"
# Check if the script is being run as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Ensure util-linux is installed for mkswap and swapon commands
apt-get update
apt-get install -y util-linux
# Create the swap file with the defined size
fallocate -l $SWAPSIZE $SWAPFILE
# Set the correct permissions for the swap file
chmod 600 $SWAPFILE
# Set up the swap area
/sbin/mkswap $SWAPFILE
# Enable the swap file
/sbin/swapon $SWAPFILE
# Add the swap file to /etc/fstab for automatic mounting at boot
echo "$SWAPFILE none swap sw 0 0" >> /etc/fstab
# Verify the swap file is active
/sbin/swapon --show
# Confirm the swap file entry in /etc/fstab
grep swapfile /etc/fstab
echo "Swap file created and activated successfully."
1 | #!/bin/bash |
2 | |
3 | # Define the swap file location and size |
4 | SWAPFILE="/swapfile" |
5 | SWAPSIZE="64G" |
6 | |
7 | # Check if the script is being run as root |
8 | if [ "$(id -u)" -ne 0 ]; then |
9 | echo "This script must be run as root" |
10 | exit 1 |
11 | fi |
12 | |
13 | # Ensure util-linux is installed for mkswap and swapon commands |
14 | apt-get update |
15 | apt-get install -y util-linux |
16 | |
17 | # Create the swap file with the defined size |
18 | fallocate -l $SWAPSIZE $SWAPFILE |
19 | |
20 | # Set the correct permissions for the swap file |
21 | chmod 600 $SWAPFILE |
22 | |
23 | # Set up the swap area |
24 | /sbin/mkswap $SWAPFILE |
25 | |
26 | # Enable the swap file |
27 | /sbin/swapon $SWAPFILE |
28 | |
29 | # Add the swap file to /etc/fstab for automatic mounting at boot |
30 | echo "$SWAPFILE none swap sw 0 0" >> /etc/fstab |
31 | |
32 | # Verify the swap file is active |
33 | /sbin/swapon --show |
34 | |
35 | # Confirm the swap file entry in /etc/fstab |
36 | grep swapfile /etc/fstab |
37 | |
38 | echo "Swap file created and activated successfully." |
39 |