2

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?

4
  • 1
    Is this a Mac OS X only thing? Commented Oct 11, 2011 at 6:59
  • 2
    Is that the best way to limit access to websites? Surely a proxy server approach is far more flexible and easier to run? Commented Oct 11, 2011 at 7:12
  • the question is missing information about target operating system Commented Oct 11, 2011 at 7:27
  • I'm sure a proxy server would be more effective, but this is a very quick, home baked approach. Commented Oct 11, 2011 at 8:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.