Shell commands as root - java

I was trying to execute Runtime.getRuntime().exec("taskkill /PID ... /F");, but I keep getting permission denied on error stream. I know it normally requires opening cmd as root, but I was wondering if there's a way to achieve that with code (something in Windows like adding sudo in the beginning).

I'd suggest you to use the runas functionality in Windows.
Runas allows a user to run specific tools and programs with different
permissions than the user's current logon provides.
Please try something like:
Runtime.getRuntime().exec("cmd /c \"runas /savecred /user:theDomain\\administrator yourCommand\"")
Runas Documentation

You can open CMD with administrator privileges and run your Java program with Administrator privileges too. When invoking to shell it should inherit those permissions and be able to exec with Administrator privileges.

Related

Run batch file as administrator in java [duplicate]

I am developing a small shutdown scheduler project in which i have to put the computer in "Stand By" mode. The command that i am using is
Runtime.getRuntime().exec("cmd /c Powrprof.dll,SetSuspendState ");
This command requires Admin rights which i don't know how to get. Also while searching for previous answers i found i can use elevate.exe as
Runtime.getRuntime().exec("c:/elevate Rundll32.exe Powrprof.dll,SetSuspendState ");
Elevate.exe is doing the task but is consuming too much of time i.e. making the software slow. Is there any other speedy way? I am using Netbeans IDE.
Runtime.getRuntime().exec("runas /profile /user:Administrator \"cmd.exe /c Powrprof.dll,SetSuspendState\"");
Also plz see comments
Running as admin without Admin rights
You have a few options
A. Create a shortcut with admin priv.
The shortcut will run cmd /c Rundll32.exe Powrprof.dll,SetSuspendState
Your Java code will run the shortcut:
Runtime rt = Runtime.getRuntime();
rt.exec("cmd /c start \"\" \"myshortcut.lnk\"")
Right click the shortcut icon > properties > advanced > run as administrator
B. Run the java process as administrator
Again, create a shortcut and set to run as administrator. Any processes spawned will also have admin privileges. Your java code will run:
rt.exec("cmd /c Powrprof.dll,SetSuspendState")
C. Use JNA to directly call SetSuspendState routine. The Java process will require admin priv (like B), but you won't have to spawn a process. If you like this, I can provide source code.
D. Use wizmo utility: wizmo quiet standby
Add parameter /savecred
runas /profile /user:Administrator /savecred
Input the password one times. In future OS will not ask you password.
I'm using Windows 10. IDK why but runas isn't working and isn't reporting any errors.
I found this answer on superuser.com:
powershell -Command "Start-Process 'cmd.exe /c Powrprof.dll,SetSuspendState ' -Verb runAs"
No password required if you have permission to elevate.
No shortcut required on client machine
No dependency on runas
Requires powershell
Powershell is installed by default on Windows since Windows 8 and Windows Server 2008 R2, according to an answer found on serverfault.com.

running application from cmd as administrator through java code

I executed the netsh command from the CMD that was manually opened by me by right clicking the CMD icon from the start and then selecting run as administrator from the options.It worked fine.Now I tried to run the netsh command through my java code,then it is not working.Nothing is happening when i run that code.I want to ask that I can run applications like notepad.exe from the cmd by calling appropriate methods of the runtime class from my java code,But how can I open the same application with the administrator priviliges from my java code.r.exec("notepad"); where r is an object reference to the runtime class will run the application,but the notepad so opened will not be in administrator mode.Actually I guess that learning to run the application in administrator mode from CMD will be enough to do the work done as The corresponding CMD command will be passed as the argument to the exec() method of the Runtime class.So my questions are:
How to run any application from CMD in windows 8 with administrator privilliges?
The way i want to implement the use of netsh is a good thing to practise or there is some other way out i must use these commands from my java code.
I have seen some commands while googling but they where not working out for me,like runas /user:administrator "notepad.exe" etc.
Thanks
You cannot use the runas /user:administrator approach, as that requires a password input which you cannot provide from an external source (such as a Java application) for security reasons.
I had a similar issue to you in the past, and I solved it using PsExec, running the process on localhost with an administrator username and password allowed me to execute external applications as an administrator.
Using your example you could run:
PsExec.exe \\\\127.0.0.1 /accepteula -u USER -p PASSWORD notepad.exe
The "/accepteula" flag prevents the requirement to accept the EULA interactively when run on a machine for the first time.
This approach may require a bit of tweaking to get working with your setup, but hope it gives you a starting point.

How to execute shell commands as root in Java

I'm working on a cross-platform installation utility written in Java. I would like to be able to execute shell commands, for example "apt-get ..." on Linux. I'm using the ProcessBuilder API for this purpose.
Unfortunately, some commands require root privileges. I would like to execute the following shell command as root:
bash -c apt-get install -y [some package]
If I try to add "sudo" before "apt-get" nothing works because there is no terminal "sudo" can get the password from.
What should I do in order to ask the user for a password and then execute the above mentioned shell command?
A short code snippet would be very appreciated!
Thanks in advance!
Max
You could run gksudo <your command> but this ties your application to a certain UI, e.g. gnome, kde, etc.
You could write all the the root-tasks to a scriptfile and ask the user to execute it with root privileges. I know this sounds clumsy, but its done that way by quite a lot of "enterprise level" software vendors (e.g. the (Java-) installer of the Oracle RDBMS at least up to 11g).

Call terminal and run command through Java App

I'm using the followig code to run a command from my Java App:
String cmd[] = {"sh","-c", "sudo chmod 777 -R " + path};
Terminal.runCommand(cmd);
I'd like to execute the .jar just by click in it and choose "Open with.." -> "Java";
The problem is that the app keep wating for a password because of the "sudo" command, but no terminal is called, the user can't give the password..
So, how could I call the coomand above AND a terminal to give the user a chance to insert the password and the application finally keep going?
Thank very much!
This is a sudo question, not a Java one; sudo does things as root. Your process doesn't have root priviledges, so sudo needs to authenticate the human being. That's a feature, not a bug. The system is not supposed to allow you to run root commands.
One option might be to use gksu instead, which is shipped by default on some distributions. It works similarly, but will pop up the password dialog in the GUI instead of on the (in this case non-existent) terminal.
Another might be to simply run your Java process as root, with all the security implications that might have. In some situations that can be a valid choice, but be careful.
Or you can check the man page for sudo and sudoers -- it's possible to configure accounts not to require a password, and to limit them to particular commands when they do.

executing java with another id

I was wondering How to execute java Application with different userName and password.
For example:
Right now: When I do,
System.getProperty("user.name");
I get user1.
But I want to program in such a way that it says user2.
Can somebody help me How to accomplish this with Java or bat files.
Any kind of help is appreciated.
The java system property user.name is set by the operating system. So if you login as a different user and start you're java program, it will run under that username.
But You can also change the user under which you execute an program (if you have sufficient rights for it!).
Linux: use the sudo command
sudo -u user2 java yourprogram
(but you need to have sudo rights, for example by being root)
Windows use the runas command:
runas /user:domain\user2 java yourprogram
You can override this value, the same as any other system property with
java -Duser.name=my-new-user
or
System.setProperty("user.name", "my-new-user");
Note: neither solution changes the user-id of the process, just the value returned by System.getProperty("user.name");
You need to switch to user2 (su user2 on linux), then run your program.
If you're doing this on Windows, you can use the runas command in a batch file to run in the context of a different user. On a Unix/Linux system, you can use the su command.

Categories