Managing swap:
List partitions:
$ lsblk
$ lsblk --list /dev/sda
Lists storage blocks and their mount points
Lists all the blocks and sectors and other information:
$ sudo fdisk --list /dev/sda
$ sudo fdisk --list
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:
Show if the system has swap:
$ sudo swapon --show
This will show us the swap if it is configured or not if it isnt.List all thepartitions:
$ lsblk
Create swap on empty available space:
sudo mkswap /dev/sdv
This will format the mentioned block device and make it swap.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.Now to verify:
$ sudo swapon --show
This will show the newly mounted swap in the system.To stop using a partition as swap:
sudo swapoff /dev/sdv
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.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.Now we can use this file as swap:
sudo mkswap /swap
This will use /swap file as swap.Now we will mount the swap:
$ swapon /swap
Again verify the swap setup:
swapon --show
To keep the changes across reboots: Append this to the
/etc/fstab
file:/swapfile swap swap defaults 0 0
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