I'm writing a rake script and would like to detect (using Ruby rather than bash if possible) if the user who executed the rake script has root privileges.
If it is not root then I would like to terminate the script.
The “root” user's UID is always 0 on the Linux systems. Instead of using the UID, you can also match the logged-in user name. The whoami command provides the currently logged user name.
Check root at start of script Some scripts need to be run as root and you may want to check at the start of that script that it is running as root. This can be done by checking the environment variable $EUID. This variable will hold the value 0 if it's being run as root.
Method 2: Check if user is part of the sudo group Another way to find out if a user has sudo access is by checking if the said user is member of the sudo group. If you see the group 'sudo' in the output, the user is a member of the sudo group and it should have sudo access.
Use uid
or euid
in the Process
class:
raise 'Must run as root' unless Process.uid == 0
I don't know Ruby, but what you want to check for is if the user ID is 0. In C, you would do this by checking getuid()
. From the Unix command line, you could also check the output of id -u
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With