How to Clean Linux Logs

How to Clean Linux Logs

When you use Linux for a long time, you might find a lot of disk space being used in the folder /var/log/. However, if you plan to clean your Linux logs by deleting all the content inside /var/log/, you might run into new problems.

This is because, even though they contain logs, some systems require those log files to remain in the log folder even if they are empty. If the folder or log file is not found, the system you are using might encounter errors.

How to Clean Linux Logs

To perform this cleanup, you can use two different methods. The easy way is to empty all the content (for example, if the system.log file has 100 lines, the file is kept but the 100 lines of content inside are emptied or deleted), or you can also use logrotate.

Before starting, this method might be a bit risky, so make sure to try it first on a dummy server or development server, and be sure to back up important data on the server.

Cleaning Files Without Deleting

Let's use the easy way first. But before starting, you can check the amount of disk space usage in the /var/log folder first.

1. Check Log Disk Space Usage

du -h /var/log --max-depth=1 | sort -rh

For example, here the disk space usage in the log folder reaches 1008M or about 1GB.

2. Run the Cleanup

Now, if you have already created a backup, you can run the cleanup on the /var/log folder.

for CLEAN in $(find /var/log/ -type f)do    cp /dev/null  $CLEANdone

For cleanup in other folders, you can change /var/log/ according to the log folder you want to clean.

3. Check the Log Folder Again

Finally, you can check the disk space usage in the /var/log folder again and make sure the size has decreased.

du -h /var/log --max-depth=1 | sort -rh

For example, in this case, the disk space usage decreased from 1008M to 17M, or a drop of about 98.31%.

Managing Logs with logrotate

This method can be considered more complicated and you might need to create manual configurations for many existing files. However, once the configuration is finished, logrotate will automatically run to rotate, compress, and delete old log files, thereby reducing disk space usage.

1. Check logrotate Version

Make sure that logrotate is already installed on the server you are using.

logrotate --version

2. Create a logrotate Configuration

If it is confirmed to be installed, you can create a logrotate configuration, for example with the configuration name wpglogrotate.

sudo nano /etc/logrotate.d/wpglogrotate

Inside that file, add the following lines as an example.

/var/log/apt/history.log {    daily    rotate 7    compress    missingok    notifempty    create 0644 root root    postrotate    endscript}/var/log/apt/term.log {    daily    rotate 7    compress    missingok    notifempty    create 0640 root adm    postrotate    endscript}
  • /var/log/apt/history.log and /var/log/apt/term.log: Specifies the path to the log file to be rotated.
  • daily: The log will be rotated every day. Other options you can use:
    • weekly: Every week.
    • monthly: Every month.
    • yearly: Every year.
    • size 10M: Rotate when the log file reaches a size of 10 Megabytes (or k for kilobytes, G for gigabytes).
  • rotate 7: Keeps 7 rotated log versions before the oldest one is deleted.
  • compress: Compresses rotated log files using gzip (adds a .gz extension).
  • missingok: Do not output an error message if the log file does not exist.
  • notifempty: Do not rotate the log file if it is empty.
  • create 0640 root adm and create 0644 root root: Creates a new log file after rotation with 0640 (rw-r----) permissions. Adjust this to the permissions, user, and group already present on the log file.
  • postrotate/endscript: A command block that will be executed after the log rotation is finished.

3. Test the Configuration

Once you have finished saving the configuration, you can test it first.

sudo logrotate -d /etc/logrotate.d/wpglogrotate

4. Run Logrotate

If there are no error messages, you can run your first logrotate. Afterward, logrotate will run automatically according to the cron job located in /etc/cron.daily/logrotate.

You can use the parameter -f to force logrotate to run.

sudo logrotate -f /etc/logrotate.d/wpglogrotate

Those are the two methods you can use to clean Linux logs. If you want an easy way, you can use the first method, and if you want it to run automatically, you can also create a cron job.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...