To set up a 4 GB swap file on an Azure Debian 12 virtual machine, follow these steps:
1. Check for Existing Swap
Before creating a new swap file, check if swap space is already enabled:
1 |
sudo swapon --show |
If nothing is returned, then no swap space is currently active.
2. Create a Swap File
You can create a swap file using the fallocate
command:
1 |
sudo fallocate -l 4G /swapfile |
If fallocate
is not available or if you want a more secure way, you can use dd
:
1 |
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 |
This command creates a 4 GB swap file at /swapfile
.
3. Secure the Swap File
Set the correct permissions so that only the root user can read and write to the swap file:
1 |
sudo chmod 600 /swapfile |
4. Set Up the Swap Area
Format the file as swap:
1 |
sudo mkswap /swapfile |
5. Enable the Swap File
Activate the swap file so it can be used immediately:
1 |
sudo swapon /swapfile |
Verify that the swap is active:
1 |
sudo swapon --show |
6. Make the Swap Permanent
To ensure that the swap file is used at boot time, add it to the /etc/fstab
file:
1 |
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab |
7. Adjust Swap Settings (Optional)
You can adjust the swappiness value, which defines how often the swap is used. The default value is 60. To change it, edit /etc/sysctl.conf
:
1 |
sudo nano /etc/sysctl.conf |
Add or change the following line to your preferred swappiness value:
1 |
vm.swappiness=10 |
Save and apply the changes:
1 |
sudo sysctl -p |
8. Verify Swap
Finally, check the swap summary to ensure everything is working as expected:
1 |
free -h |
This will display the available swap and RAM on your system. Your 4 GB swap file should be listed here.