If you want to use a Beaglebone Black or Raspberry Pi as a small server, it makes sense to give him a little swap space.
But if you you are not able to use an own swap-partition, you can use a swap-file.

This can be done in a few steps

  1. Create a file in the desired size
    The easiest way is to use the universal tool dd. The command is:
    dd if=/dev/zero of=/Path-to-File/swapfile bs=1M count=size of the paging file

    So if you want to have a 128 (256, 512) megabyte swap file, the command would be:

     dd if=/dev/zero of=/Path-to-File/swapfile bs=1M count=128

    .. or 256 , 512 etc.

    • if=/dev/zero - input, so read from /dev/zero. This means that the file is written with zeros
    • of=/Path-to-File/swapfile - output, so write to .. the swap file
    • bs=1M - write in blocks of 1 Megabyte
    • count=128 - write 128 blocks
  2. Change the file to a swap file
    That goes with the mkswap command.
    mkswap /Path-to-File/swapfile
  3. Set the correct permissions for the file
     chown root: root /Path-to-File/swapfile
    chmod 0600 /Path-to-File/swapfile 
    This ensures that the file is owned by the user root and only he is allowed to read or write to the file .
  4. Turn the swap on and test
    The swap can now be switched on easily with
     swapon /Path-to-File/swapfile

    To test if everything works, enter the command free. With the free command, the system displays the available memory.

    # free -m
                 total       used       free     shared    buffers     cached
    Mem:           495        344        151          0         15        199
    -/+ buffers/cache:        129        366
    Swap:          256          0        256

    Where Mem is the physical memory and Swap is our swap file. The -m causes dd to print the information in megabytes.

  5. Make the swap space permanently
    So if the swap is displayed, you must still ensure that it is switched on automatically at boot. This can be done in the file /etc/fstab.
    So open the file with a text editor of choice and append the line
    /Path-to-File/swapfile swap swap defaults 0 0

And that's it .

Add comment

Please insert your mail adress. Your mail address will not be displayed.