bash 以没有密码的特定用户身份运行 sudo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30731782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:09:30  来源:igfitidea点击:

Run sudo as specific user without password

bashunixsudo

提问by andy89ch

I used the following command in my script

我在脚本中使用了以下命令

sudo su - user -c bash <<EOF
cp /home/test.txt /opt/

EOF

If I use the sudo su - useron terminal, Unix don't ask me the Password but if I try to run the script the terminal ask me the Password and if I delete the EOF part the rest of code run when I quit the session. I want to run the command in user mode sudo but the terminal don't Need ask me the Password.

如果我sudo su - user在终端上使用,Unix 不会询问我密码,但是如果我尝试运行脚本,终端会询问我密码,如果我删除 EOF 部分,则在退出会话时会运行其余代码。我想在用户模式下运行命令 sudo 但终端不需要问我密码。

If I use

如果我使用

sudo su - user <<EOF

code

EOF

I have an error in .bash_profile: too many argument

我在 .bash_profile 中有一个错误:参数太多

回答by Jahid

I want to run the command in user mode sudo but the terminal don't Need ask me the Password.

我想在用户模式下运行命令 sudo 但终端不需要问我密码。

The scenario you are experiencing is caused by the users cached credentials for sudo, which allow sudo to maintain a session and any further sudo command will not prompt for passwords.

您遇到的情况是由用户缓存的 sudo 凭据引起的,这允许 sudo 维护会话,并且任何进一步的 sudo 命令都不会提示输入密码。

Check this:

检查这个:

Open a new terminal and run sudo whatever, then close it and open another new terminal and run sudo whatever, you will see that sudoasks for password every time...

打开一个新终端并运行sudo whatever,然后关闭它并打开另一个新终端并运行sudo whatever,您会看到sudo每次都要求输入密码...

If you still need to do that, then you have the following options:

如果您仍然需要这样做,那么您有以下选择:

Prevent sudoto ask for password permanently:

防止sudo永久询问密码

run sudo visudoand look for the line root ALL=(ALL) ALL, then add a line

运行sudo visudo并查找该行root ALL=(ALL) ALL,然后添加一行

username ALL=(ALL) NOPASSWD: ALL 

then save and exit.

然后保存退出。

Note: This is a security risk

注意:这是一个安全风险

Or Prevent sudoto ask for password permanently only for specific script:

或防止sudo仅针对特定脚本永久询问密码

run sudo visudoand look for the line root ALL=(ALL) ALL, then add a line

运行sudo visudo并查找该行root ALL=(ALL) ALL,然后添加一行

username ALL=NOPASSWD: path_to_the_script

then save and exit

然后保存退出

Provide password inside the script, by running your sudocommand like this:

通过sudo像这样运行您的命令,在脚本中提供密码

sudo -S <<< "password" command

Note: This is a security risk too.

注意:这也是一个安全风险。

回答by ColOfAbRiX

I guess you need to execute your command as a different user. This might be your answer: Run a shell script as another user that has no password

我猜您需要以不同的用户身份执行您的命令。这可能是您的答案:以另一个没有密码的用户身份运行 shell 脚本

sudo -H -u otheruser bash -c 'echo "I am $USER, with uid $UID"' 

It is a quote from the link. Probably the following is better for you:

这是来自链接的引用。可能以下对你更好:

sudo -H -u otheruser bash <<EOF
cp /home/test.txt /opt/
EOF

UPDATE:You may wish to create a specific sudo rule to run a specific command without password (inside /etc/sudoers file -remember to use visudo to edit it-):

更新:您可能希望创建一个特定的 sudo 规则来运行一个没有密码的特定命令(在 /etc/sudoers 文件中 - 记得使用 visudo 来编辑它-):

otheruser ALL=NOPASSWD: /full/path/to/your_command.sh

(of course you need root access to edit sudoers, I hope you can do it). And create the script called your_command.shthat contains your logic. You'll then be allowed to run it without password:

(当然你需要root权限来编辑sudoers,我希望你能做到)。并创建your_command.sh包含您的逻辑的脚本。然后您将被允许在没有密码的情况下运行它:

sudo -H -u otheruser your_command.sh

I know, it's not a "single line command" but it is safe as it allows only one specific command without password. And it doesn't require a password, of course!

我知道,它不是“单行命令”,但它是安全的,因为它只允许一个没有密码的特定命令。当然,它不需要密码!

回答by SSD

Then don't use sudo, then it won't ask for password, but you will to be have logged in as root!

然后不要使用sudo,它不会要求输入密码,但您将以root身份登录!