Ubuntu create swap partition


#1

Hello

Is there a way to create additional swap partition on Ubuntu Linux in a way that it will be also available after system reboot?

thank you


#2

Hi SkaterLinux,

The resent Ubuntu Linux systems do not use separate partitions to host swap memory. Instead, the swap memory is stored on a single file located on the root partition in the file called /swapfile. The following linux command will reveal the location of your swap file ( if any ). For an example:

$ sudo swapon -s
[sudo] password for linuxconfig: 
Filename                                Type            Size    Used    Priority
/swapfile                               file            967608  780     -2

In case that you need to add a more Swap memory into your system all what needs to be done is to create a new swap file, format it as a swap filesystem and enable it. The following procedure will show you in the more detail how to create and enable new swap file/memory on Ubuntu.

The below example will create a new swapfile2. Make sure that the you suffix your new swapfile with eg. 2 so you do not accidentally overwrite your existing /swapfile:

Let’s start by making sure that we have enough available space on our root partition:

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  7.6G   11G  41% /

Next, create a new file of eg. 2 GB in size. Change the file size to suit your needs:
$ sudo fallocate -l 2G /swapfile2

Change file permissions:
$ sudo chmod 600 /swapfile2
Failing to change permissions of you new swp file will result in the following error / warning:
swapon: /swapfile2: insecure permissions 0644, 0600 suggested.

Next, create a swap filesystem:
$ sudo mkswap /swapfile2

Enable your new /swapfile2 memory file:
$ sudo swapon /swapfile2

Check whether your new swap is enabled correctly:

$ sudo swapon --show
NAME       TYPE SIZE USED PRIO
/swapfile  file 945M 780K   -2
/swapfile2 file   2G   0B   -3

Lastly ensure that the swap gets mounted after the system reboot. The following command will append instructions to your system to mount your new /swapfile2 after reboot:

sudo bash -c 'echo /swapfile2 none swap sw 0 0 >> /etc/fstab'

The entire swap creation procedure is illustrated below:

In case that you insists that you need a new swap partition instead of new swapfile, the following procedure will create a new partition on disk with block device name /etc/sdb, make swap filesystem and mount the new swap partition.

The steps are the same as with the above swapfile creation, except that instead of swapfile we create and use new swap parition /dev/sdb1:

Create a new partition on /dev/sdX block device disk using fdisk command. If you are not familiar with fdisk then it is recommended to use utility such as cfdisk or gparted. Change below X with your correct block device name:

echo -e "o\nn\np\n1\n\n+2000M\nt\n82\nw" | fdisk /dev/sdX

Same as with the above swapfile procedure, next, we need to format our new swap partition:

$ sudo mkswap /dev/sdb1 
Setting up swapspace version 1, size = 2 GiB (2097147904 bytes)
no label, UUID=f5624480-2ebe-4f96-a208-054237d48baa

Enable a new swap partition:
$ sudo swapon /dev/sdb1

Confirm that new swap partition was enabled:

$ sudo swapon --show
NAME       TYPE      SIZE USED PRIO
/swapfile  file      945M 780K   -2
/swapfile2 file        2G   0B   -3
/dev/sdb1  partition   2G   0B   -4

Add new swap partition to /etc/fstab so it gets mounted after the system reboot:
$ sudo bash -c 'echo /dev/sdb1 none swap sw 0 0 >> /etc/fstab'

If all went well you can now reboot your system. After reboot confirm that your swapfile or swap partition is correctly enabled with:

$ sudo swapon  --show
[sudo] password for linuxconfig: 
NAME       TYPE      SIZE USED PRIO
/swapfile2 file        2G   0B   -3
/swapfile  file      945M   0B   -2
/dev/sdb1  partition   2G   0B   -4

or

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        703M        737M         14M        551M        1.1G
Swap:          4.9G

That is all. Hope this helps.

Lubos