How To Add Swap Space on Ubuntu 16.04
Check the System for Swap Information
Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.
We can see if the system has any configured swap by typing:
- sudo swapon --show
If you don't get back any output, this means your system does not have swap space available currently.
You can verify that there is no active swap using the
free
utility:
- free -h
Output
total used free shared buff/cache available
Mem: 488M 36M 104M 652K 348M 426M
Swap: 0B 0B 0B
As you can see in the "Swap" row of the output, no swap is active on the system.
Check Available Space on the Hard Drive Partition
The most common way of allocating space for swap is to use a separate partition devoted to the task. However, altering the partitioning scheme is not always possible. We can just as easily create a swap file that resides on an existing partition.
Before we do this, we should check the current disk usage by typing:
- df -h
Output
Filesystem Size Used Avail Use% Mounted on
udev 238M 0 238M 0% /dev
tmpfs 49M 624K 49M 2% /run
/dev/vda1 20G 1.1G 18G 6% /
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/1001
The device under
/dev
is our disk in this case. We have plenty of space available in this example (only 1.1G used). Your usage will probably be different.
Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.
Create a Swap File
Now that we know our available hard drive space, we can go about creating a swap file within our filesystem. We will create a file of the swap size that we want called
swapfile
in our root (/) directory.
The best way of creating a swap file is with the
fallocate
program. This command creates a file of a preallocated size instantly.
Since the server in our example has 512MB of RAM, we will create a 1 Gigabyte file in this guide. Adjust this to meet the needs of your own server:
- sudo fallocate -l 1G /swapfile
We can verify that the correct amount of space was reserved by typing:
- ls -lh /swapfile
- -rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile
Our file has been created with the correct amount of space set aside.
Enabling the Swap File
Now that we have a file of the correct size available, we need to actually turn this into swap space.
First, we need to lock down the permissions of the file so that only the users with
root
privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.
Make the file only accessible to
root
by typing:
- sudo chmod 600 /swapfile
Verify the permissions change by typing:
- ls -lh /swapfile
Output
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
As you can see, only the root user has the read and write flags enabled.
We can now mark the file as swap space by typing:
- sudo mkswap /swapfile
Output
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
After marking the file, we can enable the swap file, allowing our system to start utilizing it:
- sudo swapon /swapfile
We can verify that the swap is available by typing:
- sudo swapon --show
Output
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -1
We can check the output of the
free
utility again to corroborate our findings:
- free -h
Output
total used free shared buff/cache available
Mem: 488M 37M 96M 652K 354M 425M
Swap: 1.0G 0B 1.0G
Our swap has been set up successfully and our operating system will begin to use it as necessary.
Make the Swap File Permanent
Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our
/etc/fstab
file.
Back up the
/etc/fstab
file in case anything goes wrong:
- sudo cp /etc/fstab /etc/fstab.bak
You can add the swap file information to the end of your
/etc/fstab
file by typing:
- echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Comments
Post a Comment