如何在Bash脚本中运行sudo命令? [英] How can i run a sudo command in Bash script?

查看:146
本文介绍了如何在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!

希望有帮助

这篇关于如何在Bash脚本中运行sudo命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆