[sudo-users] Running a command every time sudo is used

Michael codejodler at gmx.ch
Sat Nov 5 12:02:17 MDT 2016


Ahmed,

You probably won't need sudo for this. 

Simplest thing would be setting up an alias for "sudo" in users profile (~/.profile).

This alias would call your custom script, for example placed into /opt/myscripts/ or wherever else you like it. Ths script then does its thing, and finally "exec /usr/sbin/sudo $1".

You could as well call your script directly, using something like "snapsudo", maybe adding the path "/opt/myscripts/snapsudo" to PATH variable, also in users profile file.

man alias
cat ~/.profile

If your system is multiuser and security criitical, then be aware that this setup is not at all secure and needs extra-careful permission configuration.

hth mi

> Hi
> 
> Greetings from a newbie :). I wonder if you could help me setting
> things up on a new server I'm currently configuring.
> 
> Ubuntu Server 16.04
> sudo version: 1.8.16
> 
> I would like to run a specific command as root every time a user uses
> sudo. This specific command need to run before the command passed to
> sudo. How can this be achieved?
> 
> Reading the man pages (I still don't fully understand sudo eco system)
> I think this is possible using the sudoer file (or rather sudoer.d
> drectory on Deb/Ubuntu). However I don't know how to do it with EBNF.
> 
> The reason I want to do this:
> I formated the root partition with Btrfs and I would like to take a
> snapshot (using snapper) before every root change imitating OpenSUSE
> behavior. The command I'll be using is:
> 
>     sudo snapper create -d "Description"
> 
> and I would like the description to hold the username and the command
> the user passed to sudo. For example:
> 
>     sudo snapper create -d "jim: sudo apt install mc"
> 
> I appreciate your help.



More information about the sudo-users mailing list