Java – restart jars with root privileges on most, if not all, Linux distributions
I'm developing a Java application that requires root / administrator privileges to run properly If the user does not start the application with this permission, I want to notify the user and restart the application with these permissions I've figured out how to do this on windows and OS X, but I can't find a way on Linux On windows, I found a program to improve any command you pass to it. On OS X, it can be realized by running a simple Applescript, but on Linux, there is no portable method at all
My plan is to use gksu, but I recently found that it is not installed by default on most Linux systems and Debian is not installed, so I can't install it Is there any portable way to restart applications with root privileges, if not all, use the Linux distribution and version in Java?
Edit: I can check whether the program runs as an administrator on all platforms
Solution
You can't do this neatly on Linux * * because the idea of upgrading privileges doesn't exist in Linux Sudo is the de facto answer to privileged execution, but all it really does is run a single command as root, which is conceptually different from running a command as the same user with upgrade permission
Consider that sudo (not even gksudo or similar) is just a user space application, not the core component of Linux For example, if someone on arch likes to do things with Su, he may not have installed it
This concept is in sharp contrast to OS X and windows, which are desktop centric, so it is easy to blur the boundary between management and regular use Because Linux uses the same kernel for desktop and server distributions, the high security requirements of server environment often dominate around these types of policies at the kernel level, so you won't get an unspecified solution Some desktop - oriented distribution
There are also cultural differences; Linux users tend to be those who want fine-grained control over how things run, and most people may look at things restarted with root privileges as a virus: P
Simple answer: an error message is thrown, indicating that the application needs root permission. When exiting, use a non-zero error code and call it every day This is more or less the "expected" behavior in the Linux world
Hack-y answer: if you want to run in a graphical environment, link gksudo as a dependency with GTK (or kdesudo, if you use QT or other, etc.), and then use it to prompt for upgrade Even if it is not installed by default, you can easily install it with the exquisite package management of Linux distribution without any extra work for your users In a non - graphical environment, you're just unlucky
**At least, its value is not easy for an application, especially considering that your application can become an attack medium for anyone trying to write a new rootkit