4.4. Managing sudo rules with IdM API commands
The examples below show common scenarios of how you can manage sudo rules with the IdM API commands.
- Creating a sudo rule
In this example, you create a sudo rule that holds time change commands.
api.Command.sudorule_add("timechange")- Creating a sudo command
In this example, you create the
datesudo command.api.Command.sudocmd_add("/usr/bin/date")- Attaching a sudo command to a sudo rule
In this example, you attach the
datesudo command to thetimechangesudo rule.api.Command.sudorule_add_allow_command("timechange", sudocmd="/usr/bin/date")- Creating and attaching groups of sudo commands
In this example, you create multiple sudo commands, add them to a newly created
timecmdssudo command group, and attach the group to thetimechangesudo rule.api.Command.sudocmd_add("/usr/bin/date") api.Command.sudocmd_add("/usr/bin/timedatectl") api.Command.sudocmd_add("/usr/sbin/hwclock") api.Command.sudocmdgroup_add("timecmds") api.Command.sudocmdgroup_add_member("timecmds", sudocmd="/usr/bin/date") api.Command.sudocmdgroup_add_member("timecmds", sudocmd="/usr/bin/timedatectl") api.Command.sudocmdgroup_add_member("timecmds", sudocmd="/usr/sbin/hwclock") api.Command.sudorule_add_allow_command("timechange", sudocmdgroup="timecmds")- Denying sudo commands
In this example, you deny the
rmcommand to be run as sudo.api.Command.sudocmd_add("/usr/bin/rm") api.Command.sudorule_add_deny_command("timechange", sudocmd="/usr/bin/rm")- Adding a user to a sudo rule
In this example, you add the user
bobto thetimechangesudo rule.api.Command.sudorule_add_user("timechange", user="bob")- Making a sudo rule available only for a specified host
In this example, you restrict the
timechangerule to be available only for theclient.ipa.testhost.api.Command.sudorule_add_host("timechange", host="client.ipa.test")- Setting sudo rules to be run as a different user
By default, sudo rules are run as
root. In this example, you set thetimechangesudo rule to be run as thealiceuser instead.api.Command.sudorule_add_runasuser("timechange", user="alice")- Setting sudo rules to be run as a group
In this example, you set the
timechangesudo rule to be run as thesysadminsgroup.api.Command.sudorule_add_runasgroup("timechange", group="sysadmins")- Setting a sudo option for a sudo rule
In this example, you set a sudo option for the
timechangesudo rule.api.Command.sudorule_add_option("timechange", ipasudoopt="logfile='/var/log/timechange_log'")- Enabling a sudo rule
In this example, you enable the
timechangesudo rule.api.Command.sudorule_enable("timechange")- Disabling a sudo rule
In this example, you disable the
timechangesudo rule.api.Command.sudorule_disable("timechange")