Swap
Increase Swap Size
swapon --show
It will show the current swap available. If you see the type file, it indicates that you are using a swap file.
swapon --showNAME TYPE SIZE USED PRIO/swapfile file 2G 0B -2
Now before you resize the swap file, you should turn the swap off. You should also make sure that you have enough free RAM available to take the data from swap file. Otherwise, create a temporary swap file.
You can disable a given swap file using this command. The command doesn’t produce any output and it may take a few minutes to complete:
sudo swapoff /swapfile
Now use the fallocate command in Linux to change the size of the swap file.
sudo fallocate -l 4G /swapfile
Make sure that you mark this file as swap file:
sudo mkswap /swapfileYou should see an output like this where it warns you that old swap signature is being wiped out.
sudo mkswap /swapfilemkswap: /swapfile: warning: wiping old swap signature.Setting up swapspace version 1, size = 4 GiB (4294967296 bytes)no label, UUID=c50b27b0-a530-4dd0-9377-aa28eabf3957
Once you do that, enable the swap file:
sudo swapon /swapfileThat’s it. You just increased the swap size in Ubuntu from 2 GB to 4 GB. You can check swap size using the free command or the swapon --show command.
free -h total used free shared buff/cache availableMem: 7.7G 873M 5.8G 265M 1.0G 6.3GSwap: 4.0G 0B 4.0GYou see how easy it is to resize swap size thanks to the swap files. You didn’t touch the partition, you didn’t reboot the system. Everything was done on the fly. How cool is that!
-------------------------------X--------------------------------
Increase Swap Space on Linux Ubuntu
Check the System for Swap Information
We will first check the system which already has the swap space. We can have multiple swap files or partitions.
Please note that, we can see the swap available in the system by using the below command –
# sudo swapon --show
[sudo] password for ubuntu:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 1020M 0B -1
Or we can use free -h command to display the swap space.
$ free -h
total used free shared buffers cached
Mem: 975M 254M 720M 4.8M 12M 133M
-/+ buffers/cache: 109M 866M
Swap: 1.0G 0B 1.0G
We can see the Swap row in the output of the machine. Here in this machine we have 1.0 G swap space allocated.
Checking the Available Space on the Hard Disk Partition
The most common way of allocating the swap space on the Linux is to use the separate partitions allocated for the swap. We cannot alter the partition scheme which is impossible, but we can easily create a swap file that resides on the existing partition.
Before we do this we will check the current disk space by using below command.
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 473M 0 473M 0% /devtmpfs 98M 4.9M 93M 5% /run/dev/dm-0 19G 3.1G 15G 18% /tmpfs 488M 0 488M 0% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 488M 0 488M 0% /sys/fs/cgroup/dev/sda1 236M 51M 173M 23% /boottmpfs 98M 0 98M 0% /run/user/1000The device under the /dev is the hard disk drives here we have 15 G space available.
In general, the amount equal to or double the amount of RAM on the machine is recommended for a good starting.
Creating a Swap File
As we know the available hard disk space, we can go head by creating a swap file within our filesystem. Also, note that, a file of the swap size which we are calling as ‘swapfile’ is in our root partition / directory.
The best way to create a swap file is by using a file called ‘fallocate’ program, this command will creates a file of a pre-allocated size instantly.
As we have 1 GB RAM allocated to our machine we will create more 2 GB file to meet the minimum requirement of the Linux.
$ sudo fallocate -l 2G /swapfile
We can verify that using the below command.
$ ls -lh /swapfile
-rw-r--r-- 1 root root 2.0G May 16 12:52 /swapfile
AD
Enabling the Swap File to Use
We have created the swap file of our requirement but it needs to be turned on in this swap space. Before we turn on the swap file, we needed to lock the permissions of the file to only root users privileges who can read the contents of the files which will prevents the normal users from being able to access the file.
We can do this using the below command
$ sudo chmod 600 /swapfile
To verify the permissions we can see using the below command
$ ls -lh /swapfile
-rw------- 1 root root 2.0G May 16 12:52 /swapfile
We can turn on the ''swapfile'' to use as swap space by using the below command
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=049218ad-50b4-4c78-98e4-7a1ea21ca77e
We have to verify that the swap is available with the amount of space allocated. For this, we can use below command –
$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 3068M 0B -1
OR Use the following command –
$ free -h
total used free shared buffers cached
Mem: 975M 255M 720M 4.8M 12M 133M
-/+ buffers/cache: 108M 867M
Swap: 3.0G 0B 3.0G
AD
Making the Swap File Permanent
As we have changed in swap file for the current session, we also need to reboot the server that will not retain the swap settings to this permanant setting. Also, automatically we can add this swap file settings to ‘/etc/fstab’ file.
$ sudo vi /etc/fstab
# /etc/fstab: static file system information.## Use 'blkid' to print the universally unique identifier for a# device; this may be used with UUID= as a more robust way to name devices# that works even if disks are added and removed. See fstab(5).## <file system> <mount point> <type> <options> <dump> <pass>/dev/mapper/server--vg-root / ext4 errors=remount-ro 0 1# /boot was on /dev/sda1 during installationUUID=40f8b7fe-3195-414a-a0e4-a4443cceb78c /boot ext2 defaults 0 2/dev/mapper/server--vg-swap_1 none swap sw 0 0/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0/swapfile none swap sw 0 0