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

编程入门 行业动态 更新时间:2024-10-26 20:21:16
本文介绍了如何在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.

  • 在命令行上运行 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命令?

    本文发布于:2023-10-09 10:28:12,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1475489.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:脚本   命令   如何在   Bash   sudo

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!