简体   繁体   English

检查 Bash 中的 sudo(带有 if 语句的脚本)

[英]Checking sudo in Bash (script with if statements)

I've been working with bash for not more than 6 hours, and now I'm trying to create a menu that allows you to do some "fun" stuff:D.我使用 bash 的时间不超过 6 个小时,现在我正在尝试创建一个菜单,让您可以做一些“有趣”的事情:D。

My problem is with the if statement that check if you're in sudo mode.我的问题是检查您是否处于 sudo 模式的if 语句 I want to have 3 conditions:我想有3个条件:

  1. If I execute the script with sudo mode, I'll be able to enter the path of the folder to be copied.如果我以 sudo 模式执行脚本,我将能够输入要复制的文件夹的路径。
  2. If I execute the script without sudo mode, it'll ask me to insert the password, if I do that correctly the script will show me the echo and read op that allows me to write the path of the folder to be copied.如果我在没有 sudo 模式的情况下执行脚本,它会要求我插入密码,如果我这样做正确,脚本将向我显示echoread 操作,允许我编写要复制的文件夹的路径。
  3. The same as the point 2, but if I fail the authentication the application will be closed automatically.与第 2 点相同,但如果我未能通过身份验证,应用程序将自动关闭。

Create a copy创建副本

2)
    if [ "$EUID" -ne 1 ]
      then 
            echo "Checking if you are in sudo mode..."
            echo "Error, please insert your password:"
            sudo ls /root
            if [ "$EUID" -ne 1 ]
                then
                    echo -e "\nCould not authenticate the user."
                    echo -e "For security reasons the application will be closed."
                    exit    
            else
                echo "==============================================================="
                echo -e "ALL COPIES HAVE A DEFAULT ROUTE: /home/patryk/Desktop/a/"
                echo "==============================================================="
                echo -e "Enter the path of the folder to be copied: "
                read origin
                rsync -avzh $origin /home/patryk/Desktop/a/`date-I`
            fi
    else
        echo "==============================================================="
        echo -e "ALL COPIES HAVE A DEFAULT ROUTE: /home/patryk/Desktop/a/"
        echo "==============================================================="
        echo -e "Enter the path of the folder to be copied: "
        read origin
        rsync -avzh $origin /home/patryk/Desktop/a/`date -I`    
    fi;;    

If you have sudo credentials caching enabled (that is, after a successful sudo, you don't have to enter the password again for subsequent sudos) you could use the following trick:如果您启用了 sudo 凭据缓存(也就是说,在成功 sudo 之后,您不必为后续的 sudo 再次输入密码),您可以使用以下技巧:

Execute sudo true and check the return status.执行sudo true并检查返回状态。 If the correct password was entered, the exit code will always be 0 .如果输入了正确的密码,退出代码将始终为0 Otherwise the exit code will be different.否则退出代码会有所不同。

if [[ "$EUID" = 0 ]]; then
    echo "(1) already root"
else
    sudo -k # make sure to ask for password on next sudo
    if sudo true; then
        echo "(2) correct password"
    else
        echo "(3) wrong password"
        exit 1
    fi
fi
# Do your sudo stuff here. Password will not be asked again due to caching.

At least on my system, root has the UID 0, not 1, so I adapted the if.至少在我的系统上,root 的 UID 是 0,而不是 1,所以我调整了 if。

I think what you really want to do is to check whether your script runs as root or not.我认为您真正想要做的是检查您的脚本是否以 root 身份运行。 If security considerations are not super duper important you could just throw this at the beginning of your script:如果安全考虑不是超级重要的,你可以在脚本的开头抛出这个:

if [ "$USER" != "root" ]
then
    echo "Please run this as root or with sudo"
    exit 2
fi

This will work and also cover the case where sudo is used to run command as another user (who is not root)这将起作用,并且还涵盖了使用 sudo 作为另一个用户(不是 root)运行命令的情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM