How to create swap file in ubuntu linux

Step 1 – Checking the System for Swap Information: Before proceeding, it’s essential to check if the system already has swap space available. You can do this by typing the following command:

sudo swapon --show

If you don’t get any output, it means your system currently does not have any swap space configured.

You can also verify the absence of active swap by using the free utility:

free -h

If the output shows “Swap: 0B,” it confirms that no swap is currently active on the system.

Step 2 – Checking Available Space on the Hard Drive Partition: Before creating a new swap file, ensure that your current disk usage provides enough space. To do this, enter the command:

df -h

Review the output and identify the device with “/” in the “Mounted on” column. You should have enough free space on this partition to create a swap file.

Step 3 – Creating a Swap File: Now that we have verified available space, we can create a swap file in the root (“/”) directory. We’ll use the fallocate program for this purpose, as it efficiently allocates the required space:

sudo fallocate -l 1G /swapfile

The above command creates a 1GB swap file. Adjust the size according to your system’s needs.

Step 4 – Enabling the Swap File: After creating the swap file, we need to set it up as actual swap space. To ensure security, we’ll restrict access to the file to only the root user:

sudo chmod 600 /swapfile

Next, mark the file as swap space:

sudo mkswap /swapfile

Finally, enable the swap file, allowing the system to start using it:

sudo swapon /swapfile

Verify that the swap is active by running:

sudo swapon --show

Step 5 – Making the Swap File Permanent: To retain the swap settings after a reboot, add the swap file information to the “/etc/fstab” file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

This ensures that the swap file is automatically activated on system startup.

Step 6 – Tuning your Swap Settings: You can optimize your swap behavior by adjusting the swappiness and vfs_cache_pressure parameters. Lowering swappiness will reduce swapping to disk, while adjusting vfs_cache_pressure allows better caching of filesystem data.

To view the current swappiness value:

cat /proc/sys/vm/swappiness

To set a new swappiness value (e.g., 10):

sudo sysctl vm.swappiness=10

To make the change persistent, add this line to “/etc/sysctl.conf”:

vm.swappiness=10

Similarly, to check the current vfs_cache_pressure value:

cat /proc/sys/vm/vfs_cache_pressure

To set a new value (e.g., 50):

sudo sysctl vm.vfs_cache_pressure=50

For persistence, add this line to “/etc/sysctl.conf”:

vm.vfs_cache_pressure=50

Conclusion: Following these steps will set up and optimize swap space on your system, providing additional memory resources when needed. Remember that swap space is useful in avoiding out-of-memory issues, but it’s essential to optimize your applications and consider server upgrades if you encounter frequent memory problems.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply