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

问题描述:

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

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

#!/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 ?

因此,如果您有权访问整个系统,则可以更改sudoers文件,以允许某些不带密码的sudo命令运行.

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.

  1. 在命令行上运行 visudo

找到您的用户并更改行,使其看起来像这样:

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

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

应该这样做.再试一次!

That should do it. Give it another shot!

希望有帮助