首页 文章

如何检查是否在bash脚本中以root身份运行

提问于
浏览
222

我正在编写一个需要根级别权限的脚本,我想这样做,如果脚本不是以root身份运行,它只是回应“请以root身份运行” . 并退出 .

这是我正在寻找的一些伪代码:

if (whoami != root)
  then echo "Please run as root"

  else (do stuff)
fi

exit

我怎么能最好(干净,安全)完成这个?谢谢!

啊,只是为了澄清:(做东西)部分将涉及运行内部和自身需要root的命令 . 因此,以普通用户身份运行它只会出现错误 . 这只是为了干净地运行需要root命令的脚本,而不在脚本中使用sudo,我只是在寻找一些语法糖 .

17 回答

  • 42

    据我所知,检查它的正确方法是:

    if [ $(id -u) = "0" ]; then
        echo "You are root"
    else
        echo "You are NOT root"
    fi
    

    请参阅此处的“测试根目录”部分:

    http://linuxcommand.org/lc3_wss0080.php

  • 315

    使脚本只能由root运行的一种简单方法是使用以下行启动脚本:
    #!/bin/su root

  • 11

    $ EUID环境变量保存当前用户的UID . Root的UID为0.在脚本中使用以下内容:

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

    Note: 如果你得到 2: [: Illegal number: ,请检查顶部是否有 #!/bin/sh 并将其更改为 #!/bin/bash .

  • 64

    在bash脚本中,您有多种方法可以检查正在运行的用户是否为root用户 .

    作为 warning ,不要使用 root 用户名检查用户是否为root用户 . 没有什么能保证ID为0的用户被称为 root . 这是一个非常强大的惯例,广泛遵循,但任何人都可以重命名超级用户的另一个名称 .

    我认为使用 bash 的最佳方法是使用手册页中的 $EUID

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

    这是比 $UID 更好的方法,可以更改,而不是反映运行脚本的真实用户 .

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

    我处理这种问题的一种方法是在不以root身份运行时在我的命令中注入 sudo . 这是一个例子:

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

    这样我的命令在使用超级用户时由root运行,或者由常规用户运行时由 sudo 运行 .

    如果您的脚本始终由root运行,则只需相应地设置权限( 0500 ) .

  • 1

    给出了一些答案,但似乎最好的方法是使用:

    • id -u

    • 如果以root身份运行,则返回id为0 .

    这似乎比其他方法更可靠,并且它似乎返回id为0,即使脚本运行 sudo .

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

    要么

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

    :)

  • 2

    正如@wrikken在他的评论中提到的, id -u 对于root来说是一个更好的检查 .

    此外,通过正确使用 sudo ,您可以检查脚本并查看它是否以root身份运行 . 如果没有,让它通过 sudo 回忆自己,然后以root权限运行 .

    根据脚本的功能,另一个选项可能是为脚本可能需要的任何专用命令设置 sudo 条目 .

  • 0

    对root用户进行简单检查 .

    [[ stuff ]] 语法是在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
    

    如果失败,这也假设您要退出1 . error 函数是一些将输出文本设置为红色的天赋(不需要,但如果你问我,那就非常优雅) .

  • -1

    0-阅读官方GNU Linux文档,有很多方法可以正确地完成它 .

    1-确保放置shell签名以避免解释错误:

    #!/bin/bash
    

    2-这是我的剧本

    #!/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
    
  • 3

    非常简单的方法就是:

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

    使用它而不是 id 的好处是你可以检查某个非root用户是否也正在运行该命令;例如 .

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

    如果脚本确实需要root访问权限,则其文件权限应该反映出来 . 使非root用户可执行的根脚本将是一个红旗 . 我建议您不要使用 if 检查来控制访问权限 .

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

    尝试以下代码:

    if [ "$(id -u)" != "0" ]; then
        echo "Sorry, you are not root."
        exit 1
    fi
    

    要么

    if [ `id -u` != "0" ]; then
        echo "Sorry, you are not root."
        exit 1
    fi
    
  • 5

    id -uwhoami 要好得多,因为像android这样的系统可能不会提供root这个词 .

    例:

    # whoami
    whoami
    whoami: unknown uid 0
    
  • 7

    检查root,如果失败则退出:

    func_check_for_root() {
            if [ ! $( id -u ) -eq 0 ]; then
                echo "ERROR: $0 Must be run as root, Script terminating" ;exit 7
            fi
        }
        func_check_for_root
    

    然后还有whoami和'我是谁',如果你想知道谁打电话给那些正在运行脚本的脚本:

    it@it-ThinkPad-W510:~$ who am i
        it       pts/19       2014-06-03 12:39 (:0.0)
        it@it-ThinkPad-W510:~$ sudo who am i
        [sudo] password for it: 
        it       pts/19       2014-06-03 12:39 (:0.0)
    

    (看到即使用sudo调用它也会给用户id)

    it@it-ThinkPad-W510:~$ sudo whoami
        root
    

    因此,

    if [ "$(whoami)" == "user" ]; then
                    echo "Executing the installer script"
    
        else
                    echo "user is only allowed to execute the installer script"
        fi
    
  • 29

    在这个答案中,让我们清楚一点,我认为读者能够读取bashPOSIX这样的POSIX shell脚本 .

    我相信没有多少在这里解释,因为高度投票的答案解释了很多 .

    然而,如果有什么需要进一步解释的话,请不要犹豫,我会尽力填补空白 .

    针对性能和可靠性的优化bash解决方案

    #!/bin/bash
    
    AmIRoot()
    {
        ! (( ${EUID:-0} || $(id -u) ))
    }
    
    AmIRoot && echo 'You are root' || echo 'You are NOT root'
    

    解释

    由于读取 $EUID 标准bash变量的有效用户ID号比执行 id -u 命令POSIX -ly找到用户ID要多得多,因此该解决方案将两者结合到一个包装精美的函数中 . 如果且仅当 $EUID 因任何原因不可用时, id -u 命令将被执行,确保无论在什么情况下我们都能获得正确的返回值 .

    为什么我在OP问了这么多年之后发布了这个解决方案

    好吧,如果我看得正确的话,上面似乎确实缺少一些代码 .

    你看,有许多变量需要考虑,其中一个是 combining performance and reliability .


    POSIX解决方案

    #!/bin/sh
    
    AmIRoot()
    {
        [ "$(id -u)" -eq 0 ]
    }
    
    AmIRoot && echo 'You are root' || echo 'You are NOT root'
    

    结论

    尽管你可能不喜欢它,Linux环境也已经多样化了 . 意思是有人喜欢bash这么多,他们甚至没有想到便携性(POSIX贝壳) . 像我这样的人喜欢POSIX贝壳 . 现在是个人选择和需求的问题 .

  • 49

    检查root:

    ROOT_UID=0   # Root has $UID 0.
    
    if [ "$UID" -eq "$ROOT_UID" ]
    then
      echo "You are root."
    else
      echo "You are just an ordinary user."
    fi
    
    exit 0
    

    在root中测试并运行 .

  • -1
    #!/bin/bash
    
    # GNU bash, version 4.3.46
    # Determine if the user executing this script is the root user or not
    
    # Display the UID
    echo "Your UID is ${UID}"
    
    if [ "${UID}" -eq 0 ]
    then
        echo "You are root"
    else
        echo "You are not root user"
    fi
    

    编者注:如果您不需要双括号,请使用单个括号来实现代码可移植性 .

相关问题