Ryan F
Ryan F

Reputation: 175

Edit /etc/hosts/ From Within a Java Application

So, it's a little unconventional, but I'm basically producing a Java App which I need to have access the /etc/hosts file.

This file obviously cannot be edited without root privs. The program is for parents so they can disable their kids from viewing certain sites. How can I make this root access happen?

I have read somewhere that I might be able to open the application as root inside of MacOSX's terminal line, but the reason I'm doing this program in Java is so I can distribute it to some of my close friends who are not computer savy, and they can easily run it.

Is there anyway I can request root privs at the beginning of the app?

Upvotes: 2

Views: 3093

Answers (2)

A.H.
A.H.

Reputation: 66213

You can let the user start the app with superuser privilges using the sudo command.

sudo your-app

The user must be allowed to use superuser privileges. He willl be prompted for his password before executing the command. Look around this site for more infos about sudo.

Note: Another possibility would be using the SUID bits directly. This is not very clever, because anyone starting the app would have superuser privileges. `sudo`` is the wrapper of choice for granting access for exactly that reason.

Upvotes: 1

lc2817
lc2817

Reputation: 3742

I assume that you use a unix-based system.

You can ask the users to make the program be owned by root and put the SUID bit on it (chmod 4555 on the file for example). This way any user can launch it and the program will be executed with root privileges and will be easy to use.

Correction: Apparently you can't apply the SUID a java program according to this link (it is barely the same for scripts).

Just do a wrapper in C instead with the SUID bit:

#include <unistd.h>

int main(){  
system("java main"); 
return 0; }

For more details concerning the SUID bit check this out.

Upvotes: 0

Related Questions