Java execute Linux commands that require su? - java

What is the best way to run Linux commands that require "su" with Java? I want it to be very reliable, as for security goes I don't care much about it the system is not a production server. No one ever connects to the machine but myself. I thought about logging in as root so I don't have to deal with the su command and its disabled by default. I'm using Debian 6.
The only thing is that one of my requirements is that the program should start after the normal user logs in without any farther user input. I would set auto login for the normal user and when the computer is turned on and after the normal user logs in I would want that Java program to run as sudo, su without any user input. Would that be possible?
Please tell me a simple and reliable solution.
Regards

Use sudo instead.
It can be configed to allow specific command run without typing in password.

For running programs that require root access, they should be run with sudo. The user will be asked to enter a password. As mentioned in the previous answer, sudo can be used to configure commands to be run as root that do not require entering a password. Try looking at the man page for sudo for more information regarding that.
If you want to run programs on system startup, you can write a bash script to run the command gksudo java -jar path when the user logs in. This will prompt the user with a dialog box to enter a password.
I also recommend doing a system call at the beginning of the program to ensure root access.

Related

How does one start a Java server in Linux via console?

So I have a Java program sitting on my DigitalOcean server and I have been using the command,
java -jar IO_Server.jar
To run it. However the problem is that when I type this command in PuTTy, it requires that I keep PuTTy open. If I close PuTTy, the server then shuts down which is not what I want. I need a way to start the server and leave it running even after PuTTy is closed.
I have been trawling the internet for about 2 hours without any luck. I keep coming across the aforementioned command. I have used the command before but I cannot remember what it is nor where I found it.
Help would be appreciated!
You can use nohup. Something like
nohup java -jar IO_Server.jar &
The first line of the linked Wikipedia article says
nohup is a POSIX command to ignore the HUP (hangup) signal.
To add to Elliott's solution, another option is to use screen which allows you to open a "new window" run a command and detach from that window. Then, even when you're logged off, the command is still running, and you can re-connect via ssh and re-attach to that window and continue from where you stopped.
Here's a "getting started guide" to screen.

Start Command Prompt (CMD) from java as Administrator

I am working on a java project. This project need admin privileges to execute some process in the system. For this is there any way to give admin privileges while starting the project? Is there any way to start command prompt with admin privileges from java?
Disclaimer: I assume you are asking this specifically for the Windows platform
There are two ways I'd recommend:
Use the runas program
This is the easiest way and since you actually want a console window the password prompt doesn't look awkward. On the other hand a user might find it suspicious to enter his password into a console window.
Use the CreateProcessWithLogonW API function
This requires you to use a Win32 Java library. (I'd recommend JNA). It is a little more work but it might lead to a better user experience because of the standard Windows login dialog.
You can't simply request admin-privileges and you can't start an admin-command-line from an application that was not launched with admin-privileges. This would be a serious security problem.
Instead, you can check if your app was started with administrative privileges and if not prompt the user to do so (showing a little How-To on how to archive this).
Unfortunately this seams to be a bigger problem since there is no standardized way to do this covering all platforms. However this thread gives some ideas: Detect if Java application was run as a Windows admin

Windows: Running privileged command from non-privileged using CMD (or Java)

I will have a service that runs as administrator and listens on a port. My GUI program will talk to the administrator service for the items that require administrator privileges.
If the service is not yet running, I will need to start it. How can I get my GUI program to run a command as administrator? I assume the user will be asked if they want to continue.
I am hoping there is something I could type in a CMD window because that should fit very well into my Java program. I am thinking something like run-as-admin javaw my-service.jar where run-as-admin is the command that asks whether to continue or not.
Windows contains the tool "runas" which can be used to start any executable with a different user account.
On the commandline you would then use:
runas /user:Administrator "javaw -jar my-service.jar"
The only drawback is, that you need to type in the password, you cannot supply the password as an argument to the runas command
You have a problem here. Non admin processes cannot start services. The very simplest thing to do is to arrange that the service starts automatically. I'd strongly recommend you take that route.
What you are thinking of doing would involve creating a helper application that includes the UAC manifest with requestedExecutionLevel set to requireAdministrator. This helper app could then start the service. The problem with this is that it requires that the logged on user is in the administrators group which you cannot guarantee will be the case.
All in all, starting the service automatically is to be preferred.
You can't execute a batch file as administrator. You need to create a shortcut to that file and then set the flag in the Shortcut 'Execute as Administrator', if this is really what you want to do.
To do this from the desktop, select the Shortcut, right click and Properties->Shortcut tab->Advanced button. Then check Execute as Administrator checkbox.
To do this programmatically, see How to set "Run as administrator" flag on shortcut created by MSI installer
We use the Wrapper library for this:
http://sourceforge.net/projects/wrapper/
The official website seems to be suffering from dynamic dns issues at the moment, so here is the wayback archive version.
From the command line (and by extension, Java) you can install, uninstall, start, and stop your Java application as a service.

Run Java programs from PHP in a sandbox

I have a little question: we have to run Java programs and parts of the code will be uploaded by the users.
So I want to know what's the best way to run them? I know 2 possible ways,
exec("javac Usercode.class") and then run the whole thing with exec("java Main"), but I tried it with exec() and it don't work. maybe because the http is not root? But I don't know exactly why.
http://php-java-bridge.sourceforge.net/pjb/ ?
Any suggestions?
And another question is, how can I run these programs in a sandbox. we have a Debian server and so it's no problem to execute the command with a limited time, but is there a possible way to run the whole code in a sandbox?
Ideas for sandboxing:
Run in a chroot using e.g. Debian's schroot command. Protects against them accessing files outside of the chroot but not against them doing things like opening sockets etc.
Each user has their own Linux username against which they validate. Commands will then be run under the appropriate username (e.g. by using sudo or a set-uid executable).
Maintain a pool of virtual servers - expensive and complicated but gives best isolation.

How to run multiple simultaneous bash shells without a GUI?

Is it possible to have several command prompts running simultaneously and switch between them, without using a GUI?
Background
I have installed CentOS-5.5-i386 without any extras, so I have a bash command prompt with root access but no GUI as far as I know.
I have written a simple Java servlet using Jetty. When I run it, it gets to a couple of commands like this;
server.start();
server.join();
where it waits for incoming requests forever - ie. it never returns to the command prompt.
I want to run a web server without the overhead of a GUI. How can I run my Java program and also continue to use the server from a command prompt?
I apologise for the waffly nature of this question but I am both a Linux newbie and a Java newbie.
Regards,
Nigel
In the general case you want screen or tmux. For running daemons, though, take a look at nohup my-daemon & or even just my-daemon &.
You can switch between consoles using Alt+F1 to Alt+F6. For more shortcuts take a look here: http://linux.about.com/od/linux101/l/blnewbie5_1.htm

Categories