You can use the log_output
option in the sudoers
file to send an e-mail when the sudo
command is run on a UNIX or Linux system. The log_output
option allows you to specify a command or script that will be run whenever the sudo
command is executed, and can be used to send an e-mail or perform other actions.
To configure the log_output
option, you will need to edit the sudoers
file using the visudo
command. The sudoers
file is typically located at /etc/sudoers
or /etc/sudoers.d/
.
To send an e-mail when the sudo
command is run, you can use a command like the following in the log_output
option:
echo "sudo command run on $(hostname) by $(whoami)" | mail -s "Sudo command run" user@example.com
This command will send an e-mail to the specified address (e.g., user@example.com
) with the subject "Sudo command run" and the body "sudo command run on $(hostname) by $(whoami)". The $(hostname)
and $(whoami)
variables will be expanded to the hostname of the system and the username of the user who ran the sudo
command, respectively.
You can customize this command to suit your specific needs and requirements. For example, you can use a different mail command (e.g., sendmail
) or specify different subject and body text.
It's worth noting that the log_output
option has many other options and features that can be used to control how it handles the output it receives. You can use the man sudoers
command to learn more about these options and how to use the log_output
option.