java - 如何从java程序运行没有密码的sudo命令?

标签 java linux ubuntu

<分区>

Possible Duplicate:
Execute a linux shell command with “sudo” using java, without entering the required password

我想通过 java 程序在 linux 上执行 sudo 命令,但它要求输入密码。 但是我想在没有密码的情况下运行 prog,请告诉我。 我的程序是

public class JavaCommandProg {
    public static void main(String args[]) throws IOException, InterruptedException {
        String command="sudo cat /etc/sudoers";
        Process myProcess =null;
        try {
            Runtime runtime=Runtime.getRuntime();
            File file=new File("/home/users/admin/temp");
            myProcess = runtime.exec(command,null,file);
            myProcess.waitFor();
            InputStreamReader myIStreamReader = new InputStreamReader(myProcess.getInputStream());
            BufferedReader br=new BufferedReader(myIStreamReader);
            String line;
            while((line=br.readLine())!=null){
                System.out.println(line);
            }
        } catch (IOException anIOException) {
            System.out.println(anIOException);
        }
    }
}

Compling like: javac JavaCommandProg.java
and          : java JavaCommandProg

最佳答案

使用 sudo 授予您的用户权限。在这里看看:http://www.ducea.com/2006/06/18/linux-tips-password-usage-in-sudo-passwd-nopasswd/

例如:

/etc/sudoers
admin    ALL = NOPASSWD: ALL

关于java - 如何从java程序运行没有密码的sudo命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732526/

相关文章:

linux - xen dom0 是访客还是主机?

linux - Jenkins maven 构建在 linux 机器中失败

ubuntu - 在 ubuntu 16.04 上安装 pip ...链接不起作用

java - 当我尝试在Jetson TK1上使用协议(protocol)3.0.0 alpha 3和jdk 1.8编译Bazel 0.1.0时,遇到了此JNI链接错误

linux - 将 bash 脚本上传到 MAAS 部署的 Ubuntu 机器上

java - Java有堆和栈吗?

java - Hadoop中最原始方式的数据协调

java - 删除netbeans中的鼠标监听器

java - Java 中的类和泛型

linux - 内核 panic -不同步:VFS:无法将root fs挂载到unknown-bloc(8,2)