How to Create a Swap File on VPS Servers

This article provides guide demonstrating how to create a swap file on VPS servers.

What is a Swap File?

A swap file (also called a swap space or page file) is a special file on a computer’s storage drive (HDD or SSD) that the operating system uses as virtual memory when the system’s physical RAM (random access memory) is full.

Launch 100% SSD VPS from $2.49/mo!

How to Create a Swap File on VPS Servers

To create a swap file on VPS servers, follow the steps outlined below:

  1. Login via SSH as root user.
  2. Ensure the swap file is not already in existence or configured by checking the list of mounted partitions:
    df -h
  3. Create the swap file (our example uses the /var directory):
    sudo touch /var/.swap.img
    sudo chmod 600 /var/.swap.img
  4. Assign the preferred swap file size by writing zeros (ex: 4GB SWAP):
    sudo dd if=/dev/zero of=/var/.swap.img bs=1024k count=4096​
  5. Format the file as swap:
    sudo mkswap /var/.swap.img​
  6. Activate the swap file:
    sudo swapon /var/.swap.img
  7. Make the swap file persist across reboots (optional):
    sudo echo "/var/.swap.img none swap sw 0 0" >> /etc/fstab​
  8. Verify the swap is active:
    swapon --show​

The swap file will now be used if/when needed to supplement the system memory.

Conclusion

Launch 100% SSD VPS from $2.49/mo!

You now know how to creata a swap file on VPS servers.

  • swap
  • 1 Utilizadores acharam útil
Esta resposta foi útil?

Artigos Relacionados

How to Create Sudo User on CentOS

This article provides step-by-step setup guide for adding Sudo user to CentOS system. The sudo...

Set Server Time Zones with Timedatectl

This article provides a guide to setting the server time and server time zone settings using...

What Server Virtualization Are Your Hypervisors?

All VPS hypervisors use KVM virtualization technology. Order here: KVM VPS servers

Using htaccess File to Block IPs

If your webserver is Apache, you may block access to your website from specific IP addresses...

Enable Browser Caching with htaccess File

Enable browser caching to reduce website loading times, by taking advantage of caching of...