49

Logging into my Ubuntu machine, I get a warning that I am running out of disk space. Tracing back, I find that it is the syslogs, especially the kern.log(s) that are eating up my 1TB disk.

-rw-r----- 1 syslog adm 240G Feb 25 14:22 kern.log
-rw-r----- 1 syslog adm 516G Feb 21 07:59 kern.log.1
-rw-r----- 1 syslog adm 1.1K Feb 15 07:39 kern.log.2.gz
-rw-r----- 1 syslog adm  19K Feb  7 07:56 kern.log.3.gz
-rw-r----- 1 syslog adm  37K Feb  1 07:45 kern.log.4.gz
-rw-r----- 1 syslog adm  23G Feb 25 14:52 syslog
-rw-r----- 1 syslog adm  25G Feb 25 08:11 syslog.1
-rw-r----- 1 syslog adm 1.6G Feb 24 07:49 syslog.2.gz
-rw-r----- 1 syslog adm 1.7G Feb 23 08:18 syslog.3.gz
-rw-r----- 1 syslog adm 3.4G Feb 22 08:19 syslog.4.gz
-rw-r----- 1 syslog adm 3.6G Feb 21 07:59 syslog.5.gz
-rw-r----- 1 syslog adm 6.9G Feb 20 07:38 syslog.6.gz
-rw-r----- 1 syslog adm 7.3G Feb 19 07:36 syslog.7.gz

From the snippet above, you can easily find that kern.log and kern.log.1 is eating up 80% of my 1TB disk. I can get the space by deleting the files, but I think it won't solve the problem.

Does anyone have an idea on what the issue might be? I saw that you can get the logging level by:

cat /proc/sys/kernel/printk

and I get

4    4    1    7
andwjstks
  • 643
  • 1
  • 6
  • 7
  • 4
    I'm voting to close this question as off-topic because it's not a programming question. It is more suitable for [ubuntu.se] instead. – Ken White Aug 09 '18 at 02:30
  • This question seems to have a really nice (and more complete than any was given here) answer at askubuntu: https://askubuntu.com/questions/184949/how-do-i-limit-the-size-of-my-syslog – ntg Nov 10 '22 at 23:01

4 Answers4

143

This is an old question, but neither of the previous two answers are good solutions:

  • The accepted answer doesn't explain why the disk problem goes away if you fix the underlying system issue (the answer is logrotate), plus your system may keep writing to the logs and fill up your disk before you can even figure out the underlying issue.
  • The other answer removes and disables the logs entirely, which is not a good approach as it ignores the underlying issue. Also, you'll probably want those log files later when you're figuring out other system problems -- disabling syslog makes it more difficult to track down future issues!

Instead, here is a safer method that lets you keep the log files while reclaiming disk space while also stopping the log files from doing this again.

  1. Safely clear the logs: after looking at (or backing up) the logs to identify your system's problem, clear them by typing > /var/log/syslog (including the >). You may need to be root user for this, in which case enter sudo su, your password, and then the above command).
  • Then restart the syslog service (either systemctl restart syslog or service syslog restart).
  1. Then, you can force the logs to rotate and delete automatically if they reach a certain size, using logrotate. In this case you can edit the config with sudo nano /etc/logrotate.d/rsyslog and add one line:
/var/log/syslog
{
    rotate 7
    daily
    maxsize 1G # add this line
    missingok
    notifempty
    delaycompress
    compress
    postrotate
        /usr/lib/rsyslog/rsyslog-rotate
    endscript
}
  • This will force your syslog to "rotate" (i.e., create a new log file and archive the previous log file) after either 1 day or when the file becomes 1GB, whichever comes first. Note that rotate 7 means your system will only keep 7 total syslog backups so it can only ever take up 7GB of space
  • Note: you can change maxsize, rotate N, and other settings to customize your logs -- use the command man logrotate to see more.
  1. While you're at it, you may want to add the same setting in the second part of the file, which governs the behavior of other log files (e.g. kern.log for kernel events, auth.log for authentication events, etc.). This setting will make it so that each of these other log files will only take 4GB in total.:
...
{
    rotate 4
    weekly
    maxsize 1G
...
}

This will allow your system to keep logging events without them filling your disk.

For more, see the manual and a similar question.

rubo77
  • 19,527
  • 31
  • 134
  • 226
ascendants
  • 2,123
  • 3
  • 11
  • 22
  • 5
    If the size is generated during one day, this is not a solution, because logrotate only runs once a day. To run logrotate hourly instead of daily, you can move it: `mv /etc/cron.daily/logrotate /etc/cron.hourly/` – rubo77 Mar 01 '21 at 06:28
  • 2
    Is `maxsize` a typo for `size`? According to [the man page for logrotate](https://linux.die.net/man/8/logrotate), under the section "Configuration File", it states that `size` and `minsize` are both valid directives, but there is no mention of `maxsize` (and furthermore, setting `maxsize` in `/etc/logrotate.d/rsyslog` did not solve my problem with large log files, I am going to try setting `size` instead and time will tell if this works or not) – Jake Levi Sep 27 '21 at 12:04
  • 1
    @JakeLevi It's not a typo (I have `maxsize` successfully working with my local logrotate config), but you're correct that it's not specified in the online manpage. It still shows up on my system when running `man logrotate` so I'd expect it to be a version issue, though I do not see anything about this in the [logrotate changelog](https://github.com/logrotate/logrotate/blob/master/ChangeLog.md). I will post my local manual entry for `maxsize`, which suggests that the log would be rotated even before the time interval (avoiding the issue mentioned by @rubo77) – ascendants Sep 28 '21 at 18:51
  • 3
    **maxsize** _size_: `Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval (daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive with the time interval options, and it causes log files to be rotated without regard for the last rotation time. When maxsize is used, both the size and timestamp of a log file are considered.` – ascendants Sep 28 '21 at 18:52
  • I checked running `man logrotate` locally and you're right, there is a `maxsize` directive that's not on the online manpage for some reason – Jake Levi Oct 06 '21 at 10:34
  • 1
    `maxsize` didn't have the desired behaviour for me for some reason (using `logrotate` version 3.14.0), but I managed to get the desired behaviour by using `size 100M`, and removing all time-based directives (`daily`, `weekly` etc) from the `/var/log/syslog` and `/var/log/kern.log` configurations in `/etc/logrotate.d/rsyslog`. I also used `sudo mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate` to make rotation happen hourly instead of daily, as described [here](https://serverfault.com/a/851038/620693) – Jake Levi Oct 06 '21 at 10:39
31

Have you checked the content of those files? There's obviously something going on with your server causing events to be generated. Resolve whatever issue is causing that, and your logs should return to their normal size.

To temporary solve the issue, type

echo "" > /var/log/kern.log
echo "" > /var/log/syslog
service syslog restart
journalctl --vacuum-size=50M

You need to be root user for this: enter sudo su, your password, and then the above commands

rubo77
  • 19,527
  • 31
  • 134
  • 226
Tom Damon
  • 658
  • 5
  • 10
9
  • Rotation of log files (EG system logs, kernel logs) is handled by logrotate
  • Enter the following command to modify the logrotate configurations:
sudo nano /etc/logrotate.d/rsyslog
  • Under the entries for the log files that are reaching problematic sizes (EG syslog, kern.log), if there is no configuration then add the configuration shown below, otherwise modify the existing configuration to look like the configuration shown below
  • A configuration consists of one or more lines of directives enclosed in curly braces, type man logrotate and scroll down to the DIRECTIVES section for a description of these directives
  • In particular, make sure to include the size 100M line, where 100M can be modified according to the maximum size you want your log files to take up, and make sure that there are no time-based rotation directives, EG daily, weekly, etc
{
        rotate 7
        size 100M
        missingok
        ifempty
        delaycompress
        compress
        postrotate
                /usr/lib/rsyslog/rsyslog-rotate
        endscript
}
  • Rotation of log files can be scheduled by cron, and by default happens daily
  • You can modify this behaviour to schedule rotation of log files to happen hourly instead of daily using the following command:
sudo mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate
  • It is possible that the cron script for logrotate is disabled in favour of the systemd timer
  • You can make sure that the cron script for logrotate is not disabled in favour of the systemd timer as follows:
    • Enter the command sudo nano /etc/cron.hourly/logrotate to view the contents of the cron script for logrotate (or sudo nano /etc/cron.daily/logrotate if you didn't previously move the script)
    • Check to see if the following four lines are present, and if they are, either comment them out by placing a # at the beginning of each line, or delete those lines entirely:
# skip in favour of systemd timer
if [ -d /run/systemd/system ]; then
    exit 0
fi
  • You can also manually force rotation of log files using the following command:
sudo logrotate --force --verbose /etc/logrotate.conf
  • To simply see what actions would be performed by the above command, without actually rotating or removing any log files, use the following command:
sudo logrotate --force --debug /etc/logrotate.conf
  • If you find that the /var/log/journal folder is also getting very big, according to this answer, you can clear it with the following command:
sudo journalctl --vacuum-size=100M
  • To make this happen automatically every time logrotate is called by cron, enter the command sudo nano /etc/cron.hourly/logrotate (or sudo nano /etc/cron.daily/logrotate if you didn't previously move the script) and insert the line journalctl --vacuum-size=100M (NB not including sudo)
Jake Levi
  • 1,329
  • 11
  • 16
  • 1
    I realize this is closed but this should be the accepted answer, it is comprehensive and includes elimnating the systemd timer issue that trips most people up. – jmatthews Jan 18 '23 at 17:48
1

As ascendants suggests: "Safely clear the logs: after looking at (or backing up) the logs to identify your system's problem"

The real problem was found in the logsys file: millions of lines with the following message "PCIe Bus Error severity Corrected".

The "PCIe Bus Error severity Corrected" error is basically a Linux report indicating that there is some problem, in my case hardware compatibility.

This problem caused several files or folders to grow out of proportion (30 GB or more): /var/log/kern.log, /var/log/syslog, /var/log/journal/, etc.

On this site they offer four solutions that offer a workaround, in my case only the last option worked for me.

wmora2
  • 119
  • 3