Change Swap Size in Ubuntu

29/12/2020
In the case of Linux, the swap file is an important part. It’s not a must-have option but having one is crucial for a number of purposes. Generally, when installing the system, you have to declare the size of the swap file. The recommended size is 4GB but you can either decrease or increase it depending on your demand. Today, let’s have a look at changing the swap size in Ubuntu.

Note – Running all these commands require root privilege, so I recommend running a “root” terminal.

sudo -s

Finding out available swap file(s)

Before we get to change the swap, let’s find out how much swap size we have.

swapon -s

According to the result, the system has a swap file at “/swapfile”.

For manipulating the swap file, we have to disable it first.

swapoff -a

Now, change the size of the swap file –

dd if=/dev/zero of=/swapfile bs=1M count=4096

Here, the total size of the swap file will be bs*count = 1M x 4096 = 4GB

Make the “/swapfile” usable again –

mkswap /swapfile

Turn on the swapfile –

swapon /swapfile

After restarting your system, check out the result –

swapon -s

Deleting the swap file

If your RAM space is high enough, then you probably want to get rid of the swap file. If that’s your case, then let’s get rid of the swap file!

Don’t worry! I’ll also show you how to set a completely fresh swap file.

For deleting the swap file, run the following commands –

swapoff -v /swapfile

Remove the swap file entry from “/etc/fstab” file. It should be the last line of the file.

gedit /etc/fstab

Now, time to remove the actual swap file.

rm -v /swapfile

Don’t forget to restart your system!

For getting back the swap file, use the following commands –

dd if=/dev/zero of=/swapfile bs=1M count=4096

# Adding permission where only root user can read the swap file

chmod 600 /swapfile

# Setup the file for swap space

mkswap /swapfile

swapon -v /swapfile

Edit the “/etc/fstab” file and add the following line at the end of the file –

/swapfile swap swap defaults 0 0

Restart your system.

Voila! Your swap file is back!

What if you had a swap partition? You have to perform actions on the partition itself. That’s more troublesome than just simply managing your swap files directly using the methods above, right?

I recommend deleting the “swap” partition and use the above method to create a new swap file.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to install Qt 5.9.1 (Qt Creator 4.3.1 included) on Ubuntu 17.04

Qt 5.9.1 the most recent release, is a cross-platform application framework that is widely used for developing apps with...
28/12/2020

Install Adapta Theme Ubuntu

On Ubuntu, theming is one of the most interesting things you can do to make your system appear COOL. It’s been ages since...
29/12/2020

22 Best Ubuntu Apps Recommended for 2017

As a regular Linux user, one daunting task I sometimes face is finding the right Linux Applications to install on my linux...
12/02/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024