To send an email when the sudo
command is executed on a UNIX/Linux system, you can set up an email notification using the sudoers
configuration file.
Here are the steps to do it:
- Edit the
sudoers
configuration file by running the following command:
sudo visudo
This will open the sudoers
file in the system’s default text editor.
- Scroll down to the bottom of the file and add the following line:
Defaults mail_always
This line tells sudo
to send an email every time a user executes a sudo
command.
- Save and close the
sudoers
file. - Configure the email settings. You will need to have a working email server and have the
mail
ormailx
package installed on your system. You can configure the email settings by adding the following lines to thesudoers
file:
Defaults mailto="user@example.com"
Defaults mailfrom="sudo@example.com"
Defaults mailhub="mail.example.com"
Replace user@example.com
with the email address where you want to receive notifications, sudo@example.com
with the email address that will appear in the “From” field, and mail.example.com
with the hostname or IP address of your email server.
- Save and close the
sudoers
file.
That’s it! Now, every time a user executes a sudo
command, an email will be sent to the specified email address. The email will contain information about the command that was executed, as well as the user and host that executed it.