如何设置SUDO_ASKPASS环境变量? [英] How to setup a SUDO_ASKPASS environment variable?

查看:155
本文介绍了如何设置SUDO_ASKPASS环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置SUDO_ASKPASS环境变量是什么意思?你怎么做呢?我在哪里声明它?

What does setting up a SUDO_ASKPASS environment variable mean? How do you do it? Where do I declare it?

我试图使用shell脚本在几分钟后关闭我的电脑并用java启动这个脚本。

I am trying to use a shell script to shutoff my computer after a couple of minutes and initiate this script with java.

推荐答案

所以我不确定我是否会使用 $ SUDO_ASKPASS 。基本上, $ SUDO_ASKPASS 的值是一个可执行文件,在调用时会将密码吐出标准输出。所以,如果您的密码是'foo',您可以编写一个shell脚本:

So I am not sure I'd use $SUDO_ASKPASS for this. Basically the value of $SUDO_ASKPASS is to be an executable that will spit your password to standard out when invoked. So, if your password was 'foo', you could write a shell script as:

#!/bin/bash
echo 'foo'

并将其放入〜/ bin / pw .SH 。然后你将设置环境变量并执行命令:

and place it in ~/bin/pw.sh. Then you would set the environment variable and execute the command as so:

SUDO_ASKPASS=${HOME}/bin/pw.sh sudo shutdown -h now

这个例子假设你在达尔文; shutdown 在不同的操作系统上表现不同。

that example assumes that you're on Darwin; shutdown behaves differently on different operating systems.

更智能的方法(更安全)是在 / etc / sudoers 中使用 NOPASSWD 参数。我们会添加这样一行:

A more intelligent way of doing this (and more secure) is to use the NOPASSWD argument in /etc/sudoers. We would add a line like this:

jane ALL=NOPASSWD: /sbin/shutdown

这再次假设您使用的是Mac。你的名字是简。改变这一点。这样,当您发出命令 / sbin / shutdown 时,sudo不会要求输入密码。 (正确)编辑sudoers 的命令是 visudo

This again assumes you're on a Mac. And that your name is Jane. Change that. This way sudo will not ask for a password when you issue the command /sbin/shutdown. The command to (properly) edit sudoers is visudo.

这篇关于如何设置SUDO_ASKPASS环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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