How to fix No space left on device issue on Linux

This post is a technical guide for how to fix the “No space left on device” error that can happen on Linux systems. You’ll learn how to troubleshoot disk space issues so that you can easily find and fix them if they occur on your server.

See what is eating the disk space

To see what is eating the disk space in your Linux system, use the following commands:

  • df command – Displays a table of mounted file systems along with their respective mount point, and the amount of space used on each partition.
  • du command – Lists the contents of directories and displays the disk usage for each directory. * df -h command- Displays a table of mounted file systems along with their respective mount point, and the amount of space used on each partition.
  • du -h command- Lists files or directories within one or more specified directories together with their sizes on disk (human readable format).
  • lsblk command- Lsblk lists information about all available block devices in a tree-like format.

Cleaning up APT package cache

The APT package cache is used to speed up package installation. It can get out of control, so it’s a good idea to clean it up every now and then.

In this example we will be cleaning up the APT package cache on Ubuntu 18.04 LTS (Bionic Beaver). Here are the steps:

  • Open Terminal from Applications -> Accessories -> Terminal or by pressing Ctrl + Alt + T on your keyboard;
  • Run this command to list all files in your .cache folder: ls /var/cache/apt/* | awk ‘{print $2}’;

Cleaning up orphaned packages

Now that you have cleaned up the duplicated files, let’s look at cleaning up orphaned packages.

An orphaned package is a package that has been removed from your system, but is still taking up space on your disk.

Typically, these packages are left behind when you remove a package from your system (this happens when you install or remove an application).

However, sometimes they can be caused by bugs in the packaging process or even patches applied to applications after their initial installation.

Removing unnecessary kernels

You can view a list of the kernels currently installed on your machine by running:

  • `uname -r`
  • `dpkg –get-selections | grep linux-image`

To remove any old kernels, you will need to first find out which ones are currently installed. This can be done by running the following commands:

  • `dpkg -l | grep linux-image`

This will display a list of all packages containing the term “linux-image”, from which you can see what kernel versions are installed on your system. If one or more kernels need to be removed (and thus replaced with another), this is where it should happen.

Using autoremove

autoremove is a tool for removing automatically installed packages. It removes any package that was installed automatically by the package manager. The reason it is called “autoremove” is because it will remove packages that are no longer needed, even if they were not uninstalled manually with the package manager (e.g., apt-get).

You can use this command to free up disk space on your Linux system:

$ sudo apt remove –purge -y –auto-remove pkg1 pkg2 pkg3

Deleting cached logs

  • Finding the log files

In order to troubleshoot this issue, you will need to know where your logs are stored. In general, they can be found in /var/log/ but there may be multiple locations depending on your distribution of Linux and how the system was configured. For example, CentOS stores its logs in /var/logs by default. On Ubuntu or Debian-based systems, the same location would be /var/log/. You can also use this command:

  • [lsb_release -a]

Diskspace is cheap, but it can also be very limited on VPS or cloud instances so it’s important to know how to find and fix problems.

When it comes to managing disk space, there are four main areas that you’ll want to pay attention to:

  • Checking your disk usage (df)
  • Finding out what’s using up all your space (du -hs)
  • Cleaning up the mess (rm -rf and rm -rf/*)
  • Ensuring it doesn’t happen again by setting quotas on users

Conclusion

This is a good example of how a simple command line can fix many problems. If this doesn’t work for you then try running these commands in reverse order and see if that helps.

Categories

Leave a Reply

Your email address will not be published. Required fields are marked *