How to add a swap file on linux

Forgot to create a swap partition? Here’s how you add a swap file (do this with root privileges):

  1. Create the actual file, we’ll go for 1GB (1048576 blocks à 1024 bytes = 1GB):
    dd if=/dev/zero of=/swapfile_1GB bs=1024 count=1048576
  2. Set permissions:
    chmod 0600 /swapfile_1GB
  3. Transform the file into a swap file:
    mkswap /swapfile_1GB
  4. Activate the new swap file:
    swapon /swapfile_1GB
  5. Make the swapfile permanent (surviving reboots):
    echo "/swapfile_1GB none swap sw 0 0" >> /etc/fstab

Leave a Reply

Your email address will not be published. Required fields are marked *