Running commands in a specified role with sudo
When a user has been assigned multiple roles, they usually work with their primary role (such as staff_r
) and only selectively execute commands with the other role. This can be accomplished through the sudo
command, as these commands usually also require a different Linux user (which can be root
or the postgresql
account for DBA tasks on the PostgreSQL database server).
How to do it…
In order to configure sudo
to perform the right role and type transition, execute the following steps:
Open up the
sudoers
file throughvisudo
:~# visudo
Define the commands that the user(s) are allowed to execute. For instance, to allow all users in the
dba
group to callinitdb
in thedbadm_r
role, define the commands as follows:%dba ALL=(postgres) ROLE="dbadm_r" TYPE="dbadm_t" /usr/sbin/initdb
The users in the
dba
group can now callinitdb
, andsudo
will automatically switch to thedbadm_r
role and thedbadm_t
user domain wheninitdb
is called:~$ sudo -u postgres...