โ06-13-2024 04:24 AM
Hello Admin Friends!
We're almost at the end of H1'24, and I hope everything has gone well so far. I wish and pray that everything will be even better in the next half ๐ฅ. With that said, today I'm here to show you how to grant sudo access to JumpCloud users for specific commands only on macOS and Linux, leveraging JumpCloud Commands.
As an IT administrator, at times, there may be a need to grant sudo permissions to the end user to perform certain privileged tasks or execute critical scripts. Instead of granting them full blown sudo access, we can grant them sudo access only to specific commands. This access is typically granted to facilitate efficient workflow and empower users to carry out necessary actions without requiring the direct intervention of administrators.
This can be achieved by adding custom rules for the JumpCloud user to /etc/sudoers.d folder. This folder allows the addition of multiple configuration files to manage users sudo privileges, often used to modularize and simplify the management of sudo.
First, we begin by reviewing the existing files in sudoers.d directory by sending the below command on Mac via JumpCloud Commands, set to run as โrootโ:
cd /etc/sudoers.d && ls
Output of the command displays the contents of this folder, displaying the sudo file of the target JumpCloud user.
In my case nyc is the username of my JC user, bound to my test Mac. This user does not have sudo permissions granted from JumpCloud. So it's a standard user account on the device.
Next, we create a new file with a custom rule in /etc/sudoers.d/ directory, using the below command :
sudo echo "USERNAME ALL=(ALL) /bin/COMMAND1, /usr/bin/COMMAND2, /usr/sbin/COMMAND3" >> /etc/sudoers.d/01-username
We need to modify the USERNAME to match the username of the target user account, and replace COMMAND with commands such as cat, grep, chown,md5 etc along with their absolute path, as needed. We can add as many commands as we wish by seperating them with a comma (,).
Different macOS binaries reside at different locations based on the nature of the command. Absolute path to the binary needs to be carefully added, based on the commands we're looking to modify. Hereโs a quick summary of different command paths on macOS -
We can locate the full path to the binary using which command, aโla:
|
With the above command configured in JumpCloud Commands and assigned to target Mac, we can execute the command. The output from this command returns blank, as we are simply adding a file in the sudoers.d directory. Result would look like this and it's expected:
As we can see in my example, I've granted permissions to the user nyc to execute cat, md5 and syslog commands as sudo.
We can run cat on the newly created userโs sudoer file to verify it's contents:
Now the enduser can verify their sudo access on these limited commands from the Terminal. Here's proof for my user nyc, who is able to sudo on cat but not on touch:
Similar to Mac, we begin by reviewing the existing files in sudoers.d directory by sending the below command on Linux via JumpCloud Commands, set to run as โrootโ:
cd /etc/sudoers.d && ls
Output of the command displays the contents of this folder with the sudo file of the target JumpCloud user.
In my case sfo is the username of my JumpCloud user, bound to my test Linux device. This user does not have sudo permissions granted from JumpCloud. So it's a standard user account on the device.
Next, we create a new file with a custom rule in /etc/sudoers.d/ directory, using the below command :
sudo echo -e "USERNAME ALL=(ALL) /bin/COMMAND1, /usr/bin/COMMAND2, /usr/sbin/COMMAND3" >> /etc/sudoers.d/01-username
NOTE - While we can skip adding '-e' parameter, we're adding it to enable intepretation of backslash escapes. When '-e' is used, sequences like \n (newline), \t (tab), and others are processed. |
We need to modify the USERNAME to match the username of the target user account, and replace COMMAND with commands such as systemctl, cat, chown etc along with their absolute path, as needed. We can add as many commands as we wish by seperating them with a comma (,).
On a Linux system, commands (executables) reside in various directories, which are part of the system's PATH environment variable. At these directories are searched in order when a command is executed in the terminal, we can reduce the time in split seconds by adding the absolute path to the command, based on the commands we're looking to modify. Hereโs a quick summary of different command paths in Linux -
One can locate the full path to the binary using which command, aโla:
|
With the above command configured in JumpCloud Commands and assigned to target Linux device, we can execute the command. The output from this command returns blank, as we are simply adding a file in the sudoers.d directory. Result would look like this and it's expected:
In my example, I've granted permissions to the user sfo to be able to execute cat, apt and fdisk commands as sudo.
We can run cat on the newly created userโs sudoer file to verify it's contents:
Now the enduser can verify his sudo access on these limited commands from the Terminal. Here's proof for my user sfo, who is able to sudo on fdisk but not on useradd:
That's it, we've successfully granted sudo access to macOS or Linux users to execute only certain commands by leveraging JumpCloud Commands.
In case, administrators would like to revoke this sudo access granted to the users, it is as simple as deleting the custom rule sudoer file of the target user from the /etc/sudoers.d folder. Execute the below command to delete the custom file created:
rm /etc/sudoers.d/01-username
Once I removed this file for my test user sfo, user was unable to execute any commands as sudo.
I hope this was informative and helpful. Until next time! ๐
โ06-13-2024 11:11 AM
Brilliantly Done! Excellent writeup and super helpful!
New to the site? Take a look at these additional resources:
Ready to join us? You can register here.