如何从java程序运行没有密码的sudo命令? [英] How to run sudo command without password from java program?

查看:126
本文介绍了如何从java程序运行没有密码的sudo命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用“ sudo”执行linux shell命令使用java,无需输入所需密码

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

I want to execute sudo command on linux through java program but it ask for password. But I want to run prog without password,please tell me. My prog is

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/

Grant your user the permissions by using sudo. Have a look here: http://www.ducea.com/2006/06/18/linux-tips-password-usage-in-sudo-passwd-nopasswd/

例如:

/etc/sudoers
admin    ALL = NOPASSWD: ALL

这篇关于如何从java程序运行没有密码的sudo命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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