要求用户从root模式运行makefile

时间:2016-06-28 04:24:18

标签: linux bash shell makefile

我有一个makefile并在我的项目中配置shell。我编写了代码,要求用户使用以下代码以root模式运行configure shell。

[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"

但是当我跑'make install'时 我需要让用户从root模式运行。 所以我只是从配置shell中复制了代码并将其复制到另一个名为“runasroot.sh”的shell脚本文件中。然后我从make install运行这个shell脚本。

install:
    @echo About to install XXX Project
    ./runasroot.sh
    find . -name "*.cgi" -exec cp {}  $(SCRIPTDEST)/ \;

当我运行上面的代码时,我得到了以下错误。

About to install XXX Project
./runasroot.sh \;
make: *** [install] Error 1

runasroot.sh

#!/bin/bash
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"

3 个答案:

答案 0 :(得分:3)

这里有几个错误。

第一个问题是你要做的事情。要求root作为构建过程的一部分是非常糟糕的形式。对于构建部分,尝试不要求root用于编译任何内容。如果您需要在打包时创建特殊文件,请使用fakerootfakeroot-ng以获得相同的效果,而不会有任何实际的权限提升。

安装时,如果她选择的话,让用户以root用户身份运行整个make文件。许多通常需要root的操作有时不需要。例如,如果安装是用户具有权限的make install,则DESTDIR不需要root。

但是,无论如何你都没有做到这一点,你的流程是完全错误的。虽然runasroot.sh完全按照您的意愿执行,但它只能为自己执行此操作。当你查看收据时:

install:
    @echo About to install XXX Project
    ./runasroot.sh # <<-- this line runs as root
    find . -name "*.cgi" -exec cp {}  $(SCRIPTDEST)/ \;

runasroot.sh行以root身份运行,但find行是一个不同的进程,并且完全不受影响。

对于常规shell脚本来说,情况确实如此,但对于make收据来说,这是真的。在shell脚本中,每个命令都有自己的进程。在生成收据中,每个命令都有自己的 shell 。您的runasroot.sh不会,也不会影响查找运行的权限。

所以,你要做的就是不可能和不想要的。如果您没有足够的权限,只需尝试安装和失败。

答案 1 :(得分:0)

target:
       @if ! [ "$(shell id -u)" = 0 ];then
             @echo "You are not root, run this target as root please"
             exit 1
       fi

答案 2 :(得分:0)

说明

您可以使用const DataInputBox = () => { const inputRef = React.useRef(null) React.useEffect(() => { ReactTooltip.show(inputRef.current); }, []); return ( <div> <ReactTooltip id="title required" efect="solid" place="bottom"> "Title required" </ReactTooltip> <input type="text" ref={inputRef} data-tip data-for="title required" placeholder="Type title" name="title" /> </div> ); } 来检查该用户是否不是root并回显消息,例如,该用户确实是ifneq用户,则不执行实际操作。由于root用户的ID在类似UNIX的操作系统上通常为root,因此我们可以在条件条件下检查用户ID是否为0

提案解决方案

0

输出

install:
ifneq ($(shell id -u), 0)
    @echo "You must be root to perform this action."
else
    @echo "TODO: The action when the user is root here..."
endif

外部资源

Conditional Parts of Makefiles

The shell Function

Recipe Echoing

Man for the id function

What is a root user