如何在 Bash 脚本中运行 sudo 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18364714/
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 06:20:57  来源:igfitidea点击:

How can i run a sudo command in Bash script?

bashshell

提问by Shivam Agrawal

I want to run the following sample bash script which needs sudo password for a command

我想运行以下示例 bash 脚本,该脚本需要命令的 sudo 密码

#!/bin/bash
kinit #needs sudo password
vi hello.txt  

while running the above script it is asking for password.
How can i pass the username and password in the command itself or is there any better way i can skip passing my password in the script ?

在运行上述脚本时,它要求输入密码。
我如何在命令本身中传递用户名和密码,或者有什么更好的方法可以跳过在脚本中传递我的密码?

回答by Malcolm Jones

So if you have access to your full system, you can change your sudoers file to allow certain sudo commands to be run w/o a password.

因此,如果您可以访问整个系统,则可以更改 sudoers 文件以允许在不使用密码的情况下运行某些 sudo 命令。

  1. On the command line run visudo

  2. Find your user and change the line to look something like this:

  1. 在命令行运行visudo

  2. 找到您的用户并将该行更改为如下所示:

pi ALL=(ALL) NOPASSWD: /path/to/kinit, /path/to/another/command

pi ALL=(ALL) NOPASSWD: /path/to/kinit, /path/to/another/command

That should do it. Give it another shot!

那应该这样做。再试一次!

Hope that helps

希望有帮助

回答by Todd A. Jacobs

TL;DR

TL; 博士

You can't—at least, not the way you think.

你不能——至少,不是你想的那样。

Longer Answer with Alternatives

更长的替代方案

You have a couple of options:

您有几个选择:

  1. Authenticate interactively with sudobefore running your script, e.g. sudo -v. The credentials will be temporarily cached, giving you time to run your script.
  2. Add a specific command such as /usr/lib/klibc/bin/kinitto your sudoers file with the NOPASSWD option. See sudoers(5) and and visudo(8) for syntax.
  3. Use gksudo(1) or kdesu(1) with the appropriate keyring to cache your credentials if you're using a desktop environment.
  1. 在运行脚本之前使用sudo进行交互验证,例如sudo -v. 凭据将被临时缓存,让您有时间运行脚本。
  2. 使用 NOPASSWD 选项将特定命令(例如/usr/lib/klibc/bin/kinit)添加到您的 sudoers 文件中。有关语法,请参阅 sudoers(5) 和 visudo(8)。
  3. 如果您使用的是桌面环境,请使用带有适当密钥环的 gksudo(1) 或 kdesu(1) 来缓存您的凭据。

One or more of these will definitely get you where you want to go—just not the way you wanted to get there.

其中一个或多个肯定会让您到达您想去的地方——只是不是您想要的方式。

回答by Aleks-Daniel Jakimenko-A.

You shouldn't pass username and password. This is not secure and it is not going to work if the password is changed.

您不应该传递用户名和密码。这是不安全的,如果更改密码,它将无法正常工作。

You can use this:

你可以使用这个:

gksudo kinit # This is going to open a dialog asking for the password.
#sudo kinit # or this if you want to type your password in the terminal
vi hello.txt

Or you can run your script under root. But note that vi is going to be ran as root as well, which means that it will probably create files that belong to root, that might be not what you want.

或者您可以在 root 下运行您的脚本。但请注意,vi 也将作为 root 运行,这意味着它可能会创建属于 root 的文件,这可能不是您想要的。