How do I add swap after system installation?
You need to edit /etc/fstab
and add the new swap partition.
sudo nano /etc/fstab
You need to add a line that looks like
UUID=735b3be3-779c-4d21-a944-b033225f3ab4 none swap sw 0 0
and you get the UUID using the command
sudo blkid /dev/sda3
(substitute /dev/sda3
with the appropriate device name; in order to see the appropriate device name, we can use lsblk
- as said here).
Related:
- How to edit files in a terminal with nano?
To create a swap partition after installation, create an empty partition. It should have no holes. You can then format this partition with:
sudo mkswap /dev/sdX
replacing /dev/sdX
with your partition. Mount this partition as swap with
sudo swapon -U UUID
where UUID is that of your /dev/sdX
as read from this:
blkid /dev/sdX
Bind your new swap in /etc/fstab
by adding this line:
UUID=xxx none swap sw 0 0
If you want to use your swap for hibernating then you need to update the UUID in /etc/initramfs-tools/conf.d/resume
with this content RESUME=UUID=xxx
. Don't forget to $ sudo update-initramfs -u
.
To complete things: it is also possible to create a swap file if you do not have a spare partition. This answer gives you an idea of how to create a swap file and enable it on boot.
In case you don't want or you're not sure how to create a swap partition, you can create a swap file which will work in the same way as partition. Here are the steps (using terminal):
Create an empty file (1K * 4M = 4 GiB).
sudo mkdir -v /var/cache/swap cd /var/cache/swap sudo dd if=/dev/zero of=swapfile bs=1K count=4M sudo chmod 600 swapfile
Convert newly created file into a swap space file.
sudo mkswap swapfile
Enable file for paging and swapping.
sudo swapon swapfile
Verify by:
swapon -s
ortop
:top -bn1 | grep -i swap
Should display line like:
KiB Swap: 4194300 total, 4194300 free
To disable, use
sudo swapoff swapfile
command.Add it into
fstab
file to make it persistent on the next system boot.echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
Re-test swap file on startup by:
sudo swapoff swapfile sudo swapon -va
Note: Above commands re-checks the syntax of
fstab
file, otherwise your Linux could not boot up properly.