How to clean up and save space in root directory on a Ubuntu Linux machine
Three ways to clean up space in root directory of Linux
Method 1: Set up logrotate
What is logrotate?
Logrotate
is a tool for managing log files created by system processes. This tool automatically compresses and removes logs to maximize the convenience of logs and conserve system resources. Users have extensive control over how and when log rotation is processed.
Use Logrotate
Logrotate
’s behavior is determined by options set in a configuration file, typically located at /etc/logrotate.conf
:
To check whether the logrotate is installed:
logrotate --version
output
logrotate 3.11.0
Logrotate
is not installed you will get an error. Please install the software using your Linux distribution’s package manager.
To activate the configuration file:
sudo logrotate /etc/logrotate.conf
Run logrotate as a cronjob
Run logrotate as a cronjob to ensures that logs will be rotated as regularly as configured. Logs will only be rotated when logrotate runs, regardless of configuration. For example, if you configure logrotate to rotate logs every day, but logrotate only runs every week, the logs will only be rotated every week.
For most daemon processes, logs should be rotated by the root user. In most cases, logrotate is invoked from a script in the /etc/cron.daily/
directory. If one does not exist, create a script /etc/cron.daily/logrotate
:
#!/bin/sh
logrotate /etc/logrotate.conf
Understand logrotate.conf
The configuration file for log rotation begins with a number of global directives that control how log rotation is applied globally. Most configuration of log rotation does not occur in the /etc/logrotate.conf
file, but rather in files located in the /etc/logrotate.d/
directory. Every daemon process or log file will have its own file for configuration in this directory. The /etc/logrotate.d/
configurations are loaded with the following directive in logrotate.conf
by using the include
command shown as follows:
include /etc/logrotate.d
Configuration settings in logrotate.conf
for rotation of specific logs is instantiated in a block structure:
/var/log/mail.log {
weekly
rotate 5
compress
compresscmd xz
create 0644 postfix postfix
}
The size and rotation of /var/log/mail.log
is managed according to the directives instantiated between the braces. The above configuration rotates logs every week, saves the last five rotated logs, compresses all of the old log files with the xz
compression tool, and recreates the log files with permissions of 0644
and postfix
as the user and group owner. These specific configuration options override global configuration options which are described below.
Remove or Email Old Logs with Rotate Count
rotate 4
The rotate
directive controls how many times a log is rotated before old logs are removed. If you specify a rotation number of 0
, logs will be removed immediately after they are rotated. If you specify an email address using the mail
directive as file, logs are emailed and removed.
mail <username@example.com>
Test Logrotate
After customizing the config to fit your needs and saving it in /etc/logrotate.d
, you can test it by doing a dry run:
sudo logrotate /etc/logrotate.conf --debug
This calls logrotate
, points it to the standard configuration file, and turns on debug mode.
Information will print out about which log files Logrotate is handling and what it would have done to them. If all looks well, you’re done. The standard Logrotate job will run once a day and include your new configuration.
Method 2: Directly clean huge files
Sometimes you may find the /var/log/syslog
file being unbelievably large. A simple way to delete the content of the log file while preserving the file at the same time is:
sudo truncate -s 0 /var/log/syslog
Method3: Remove unused old kernels
Over time you get many Linux kernel packages installed on Ubuntu Linux machine. All unused Linux kernel takes space, hence you must delete them from the system.
Check for old kernels
In order to avoid any mistakes with the current boot kernel of your Ubuntu 18.04 system, make sure to check its version
$ uname -r
5.3.0-40-generic
Also make sure to check if there are old kernels aside from your current one in the command line.
dpkg -l | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
To this end, you can see that you effectively have an old kernel installed marked with ii
. You might see more old kernels if you have done some upgrade or manual installation. There are some status when you check the kernel
ii
: means that the kernel/packages are installed and eligible for removal.rc
: indicates that the kernel has already been removed.iU
: is something like a warning tells to DON’T REMOVE. That means not installed but queued for install in apt.
Make sure to see the status before doing anything. Now that we have found some kernels installed, now let’s see how to remove old kernels.
Remove old kernels
There are some ways in terms of removing old kernels using command and GUI tools.
apt command
The apt command help to uninstall packages including the old kernels that you don’t need in your Ubuntu 18.04 systems. You can check the ones which were automatically installed
apt-mark showauto 'linux-image-.*'
and you can check those that were manually installed
apt-mark showmanual 'linux-image-.*'
Then, you can remove all the old kernel with the remove parameter as below:
apt remove <NAME_OF_KERNEL>
Example:
$ apt remove linux-image-4.15.0-13-generic Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: linux-image-4.15.0-13-generic* linux-image-extra-4.15.0-13-generic .... .... you may need to re-run your boot loader[grub]
After you remove a kernel, make sure to update the grub to see if any error occurs while generating the grub
update-grub
Now you can check whether the old kernel has been removed
dpkg -l | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
GUI tool
-
Ubuntu Cleaner Ubuntu Cleaner is a user-friendly system utility designed to clean browser caches, removed unneeded apps, and get shot of old kernels. Ubuntu can be helpful if you want to remove:
- Application caches which include most major browsers
- Apt cache
- Old kernels
- Unneeded packages
The package is not present by default in the official repository so we need to add the PPA. Remember that on Ubuntu 18.04 you don’t need to update the packages cache after adding the PPA because it’s automatically launched. So add the PPA as below
sudo add-apt-repository ppa:gerardpuig/ppa
Official Ubuntu Cleaner stable repository More info: https://launchpad.net/~gerardpuig/+archive/ubuntu/ppa Press [ENTER] to continue or Ctrl-c to cancel adding it.
Now you can install the tool
sudo apt install ubuntu-cleaner
in the cleaner, select Old Kernel and remove
Conclusion
In this blog we used three ways to clean up the space in log directory of an Ubuntu Machine. To learn more about the command line and configuration options available for Logrotate
, you can read its manual page by running man logrotate
in your terminal.