Changing the user environment using ProcessBuilder + java

I try to change the user of the child process to a user with minor privileges, but when I run the method to start ProcessBuilder, the subprocess is exec with the same parent user

        LinkedList<String> commands = new LinkedList<String>();
        commands.add("vlc");
        ProcessBuilder builder = new ProcessBuilder(commands);
        Map<String,String> enviroment = builder.environment();
        enviroment.clear();            
        enviroment.put("USER", "otheruser");
        enviroment.put("LOGNAME", "otheruser");
        enviroment.put("PWD", "/home/otheruser");
        enviroment.put("HOME", "/home/otheruser");
        enviroment.put("USERNAME", "otheruser");
        enviroment.put("SHELL", "/bin/false");
        builder.directory(new File("/home/otheruser"));            

        Process process = builder.start();
        process.waitFor();

I work on Linux (Ubuntu)

+3
source share
2 answers

Jim is absolutely right. But if you still want to run your program as another user, you must use tools that depend on the user's platform.

: runas, : runas/user: domain\jamesbond regedt32.exe , runas . , : http://www.windowsnetworking.com/kbase/WindowsTips/WindowsXP/AdminTips/Miscellaneous/RunprogramsasanotheruserinWindows2000WindowsXP.html

VBS java. . : http://weblogs.asp.net/hernandl/archive/2005/12/02/startprocessasuser.aspx

Unix: . su sudo. su , ( root). , script (. http://en.wikipedia.org/wiki/Expect). unix.

!

+3

, USER. Linux ( Unix ), USER ROOT. , , setuid, setuid() ( setuid()).

0

Source: https://habr.com/ru/post/1774990/


All Articles