How to check if running as root in a bash script

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I'm writing a script that requires root level permissions, and I want to make it so that if the script is not run as root, it simply echoes "Please run as root." and exits.

Here's some pseudocode for what I'm looking for:

if (whoami != root)   then echo "Please run as root"    else (do stuff) fi  exit

How could I best (cleanly and securely) accomplish this? Thanks!

Ah, just to clarify: the (do stuff) part would involve running commands that in-and-of themselves require root. So running it as a normal user would just come up with an error. This is just meant to cleanly run a script that requires root commands, without using sudo inside the script, I'm just looking for some syntactic sugar.

回答1:

The $EUID environment variable holds the current user's UID. Root's UID is 0. Use something like this in your script:

if [ "$EUID" -ne 0 ]   then echo "Please run as root"   exit fi


回答2:

In a bash script, you have several ways to check if the running user is root.

As a warning, do not check if a user is root by using the root username. Nothing guarantees that the user with ID 0 is called root. It's a very strong convention that is broadly followed but anybody could rename the superuser another name.

I think the best way when using bash is to use $EUID, from the man page:

EUID   Expands to the effective user ID of the current  user,  initialized        at shell startup.  This variable is readonly.

This is a better way than $UID which could be changed and not reflect the real user running the script.

if (( $EUID != 0 )); then     echo "Please run as root"     exit fi

A way I approach that kind of problem is by injecting sudo in my commands when not run as root. Here is an example:

SUDO='' if (( $EUID != 0 )); then     SUDO='sudo' fi $SUDO a_command

This ways my command is run by root when using the superuser or by sudo when run by a regular user.

If your script is always to be run by root, simply set the rights accordingly (0500).



回答3:

if [[ $(id -u) -ne 0 ]] ; then echo "Please run as root" ; exit 1 ; fi

or

if [[ `id -u` -ne 0 ]] ; then echo "Please run as root" ; exit 1 ; fi

:)



回答4:

As @wrikken mentioned in his comments, id -u is a much better check for root.

In addition, with proper use of sudo, you could have the script check and see if it is running as root. If not, have it recall itself via sudo and then run with root permissions.

Depending on what the script does, another option may be to set up a sudo entry for whatever specialized commands the script may need.



回答5:

A few answers have been given, but it appears that the best method is to use is:

  • id -u
  • If run as root, will return an id of 0.

This appears to be more reliable than the other methods, and it seems that it return an id of 0 even if the script is run through sudo.



回答6:

There is a simple check for a user being root.

The [[ stuff ]] syntax is the standard way of running a check in bash.

error() {   printf '\E[31m'; echo "$@"; printf '\E[0m' }  if [[ $EUID -eq 0 ]]; then     error "This script should not be run using sudo or as the root user"     exit 1 fi

This also assumes that you want to exit with a 1 if you fail. The error function is some flair that sets output text to red (not needed, but pretty classy if you ask me).



回答7:

Very simple way just put:

if [ "$(whoami)" == "root" ] ; then     # you are root else     # you are not root fi

The benefit of using this instead of id is that you can check whether a certain non-root user is running the command, too; eg.

if [ "$(whoami)" == "john" ] ; then     # you are john else     # you are not john fi


回答8:

0- Read official GNU Linux documentation, there are many ways to do it correctly.

1- make sure you put the shell signature to avoid errors in interpretation:

 #!/bin/bash

2- this is my script

#!/bin/bash   if [[ $EUID > 0 ]]; then # we can compare directly with this syntax.   echo "Please run as root/sudo"   exit 1 else   #do your stuff fi


回答9:

If the script really requires root access then its file permissions should reflect that. Having a root script executable by non-root users would be a red flag. I encourage you not to control access with an if check.

chown root:root script.sh chmod u=rwx,go=r script.sh


回答10:

try the following code:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!