Java execute Linux commands that require su?

2019-09-19 00:19发布

问题:

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

回答1:

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



回答2:

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.