« Previous 1 2
Processing the new sudo logging format
Neat Packaging
Compact JSON for Logfiles
This new format not only saves space but also makes it easier to process the logfiles automatically. Besides the sudo log format, it offers two new formats: json_pretty and json_compact. Whereas json_pretty is identical to the old JSON format, json_compact crams the log messages in JSON format into a single line, much like logging with syslog. You can then either process these locally with a JSON parser (e.g., jq) or forward them to a central logging instance for further analysis.
In the following example, the objective is to evaluate all the logs from a local syslog-ng instance. Alternatively, you could also send the logs from there to some other software or to a central log server. The example is merely intended to illustrate the options the new log format allows. To customize the sudo configuration, you would traditionally call up the visudo tool, which gives you an exclusive lock on the file and prevents any parallel changes to the file from overwriting your settings. Alternatively, you could create a new file in the /etc/sudoers.d/ folder for your own customizations. To edit this file, simply call:
# visudo -f /etc/sudoers.d/logging
In the file, specify that sudo should only write log messages to a text file and not additionally send them by syslog to the local journal. Now enable the new json_compact format. In this example, I'm assuming you also want to log calls to all child processes of the permitted sudo commands:
Defaults !syslog Defaults logfile=/var/log/sudo Defaults log_format=json_compact Defaults log_subcmds
Listing 1 shows a simple syslog-ng configuration file that draws on syslog-ng to evaluate the JSON-formatted /var/log/sudo logfile and convert the logs to a different format to generate the new /var/log/sudo-text logfile. The results are shown in Listing 2. Of course, you could simply forward the JSON-formatted logfile to a different logging instance to analyze the logs there. The syslog-ng website provides some examples [2].
Listing 1
/etc/syslog-ng/conf.d/sudo.conf
# cat sudo.conf
source s_sudojson {
file("/var/log/sudo" flags(no-parse));
};
parser p_json {
json-parser();
};
destination d_sudo-welf {
file("/var/log/sudo"
template("$(format-welf --scope nv_pairs --exclude MESSAGE --exclude accept.submitenv)\n\n")
);
file("/var/log/sudo-text"
template("${DATE} user ${accept.submituser} ran ${accept.command} on host ${HOST} using sudo\n")
);
};
log {
source(s_sudojson);
parser(p_json);
destination(d_sudo);
};
Listing 2
New Logfile Format
May 22 09:38:51 user tscherf ran /usr/bin/vim on host master.ipa.test using sudo May 22 09:38:52 user tscherf ran /usr/bin/who on host master.ipa.test using sudo
Conclusions
Thanks to the new sudo log format json_compact, you can easily generate machine-readable sudo logfiles, which you can then forward to a log management system for analysis downstream. If you prefer to inspect the messages manually instead, you can either use the sudo format or json_pretty if you need more information.
Infos
- Logging sudo child processes: https://www.sudo.ws/posts/2021/08/what-is-coming-in-sudo-1.9.8/
- syslog-ng configuration examples: https://www.syslog-ng.com/community/b/blog/posts/type-support-working-with-sudo-logs-in-syslog-ng-4-0
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

