Managing swap:

List partitions:

  1. $ lsblk $ lsblk --list /dev/sda

  • Lists storage blocks and their mount points

  1. Lists all the blocks and sectors and other information: $ sudo fdisk --list /dev/sda $ sudo fdisk --list

  2. Opens a TUI on which we can interactively managing partitions: $ sudo cfdisk /dev/sda

Best tool to use is cfdisk because of its interactivity and easyness.

Managing Swap:

  1. Show if the system has swap: $ sudo swapon --show This will show us the swap if it is configured or not if it isnt.

  2. List all thepartitions: $ lsblk

  3. Create swap on empty available space: sudo mkswap /dev/sdv This will format the mentioned block device and make it swap.

  4. Now we need to mount the swap at a mount point: $ sudo swapon --verbose /dev/sdv This will mount newly created swap on the mentioned block device and the --verbose flag is used just see all the details of the process.

  5. Now to verify: $ sudo swapon --show This will show the newly mounted swap in the system.

  6. To stop using a partition as swap: sudo swapoff /dev/sdv

  7. We can also create a swap file and that as swap: $ sudo dd if=/dev/zero of=/swap bs=1M count=2048 status=progress This will create an outputfile [of=/swap] at /swap with inputfile [if=/dev/zero] with block size of 1M 2048 times, so when the command ends running we will have a swap file of 2Gigs.

  8. Now we also dont want to expose this file to all users on this system so we will change the permissions: $ sudo chmod 600 /swap This will change all permissions to only usable by root user.

  9. Now we can use this file as swap: sudo mkswap /swap This will use /swap file as swap.

  10. Now we will mount the swap: $ swapon /swap

  11. Again verify the swap setup: swapon --show

  12. To keep the changes across reboots: Append this to the /etc/fstab file: /swapfile swap swap defaults 0 0

  13. Adjusting the swappiness:

    • Swappiness means how often a system will use the swap with a value between 0 to 100, higher the value more often the swap will be used. In ubuntu by default it is set to 60.

    • Swappiness value is stored at: $ cat proc/sys/vm/swappiness

      60

    • Changing the swappiness value: $ sudo sysctl vm.swappiness=10

    • To make this change persistant across reboots add this to the /etc/sysctl.conf: vm.swappiness=10

Last updated