1

I have a program that needs to access a file in my home directory.

Ubuntu will not allow this to happen due to the program not having root privileges. At first I ran this java command (which wouldn't work because the program did not have permission to access the files):

java -jar this.jar

The program was unable to detect the file I needed it to. The path that the program told me that it used was /home/myhome/directory. This path was correct. But then the program needed root privileges to actually access that file so I tried:

sudo java -jar this.jar

Then the directory that the program tried to access was /root/directory this is the problem that I am having.

Is there any way that I can run a program as root with the directory the program views as the home directory being the /home directory instead of the /root directory?

7
  • 1
    Which user is executing the command? If it is you the program should be able to access your home.
    – FelixJN
    Aug 10, 2015 at 14:02
  • I will be executing the command, but i need to give the command root privilages in order for the program to actually work. Ive attempted "sudo -u me mycommand" to set the user as me, but sudo then executes the command without root privilage (it doesnt ask me for my root password)
    – Sock314
    Aug 10, 2015 at 15:56
  • Try : ` $ sudo HOME=/home/your-user-name java -jar this.jar ` your program will be run with sudo privileges, while sudo will perceive its HOME as being whatever path you give it as home. If this is not what you are asking for, then I did not understand your question. Let us know if this works.
    – Cbhihe
    Aug 10, 2015 at 16:41
  • 1
    @Cbhihe That's the same as running sudo without the -H option; try running sudo echo $HOME. If sudo it's not explicitly told to set the environment differently, the environment from which sudo is invoked it's retained during the command execution. That's also the reason why running graphical applications with sudo is discouraged, because root might end up owning files in the home directory, and also the reason why I think the problem is another one.
    – kos
    Aug 10, 2015 at 17:10
  • @kos: yes, I know. OP is rather cryptic about his/her default sudo security policy so I wanted to try this. I do not understand why when invoking sudo, HOME suddenly becomes that of root. My system does not show that behavior at all. At Sock314: did you try this yet ?
    – Cbhihe
    Aug 10, 2015 at 17:47

1 Answer 1

6

Because you are dealing with a Java program, you specify the directory it should see as the home directory by setting the system property user.home:

sudo java -Duser.home=$HOME -jar this.jar

It is confusing that this should be necessary. Since sudo by default retains the value of HOME, one would expect Java to set user.home to $HOME. However, the Java API documentation for java.lang.System states that user.home will be set to User's home directory. It does not state that it will be set to the value of the HOME environment variable -- which indeed may or may not be the user's home directory.

Note, as has been pointed out in the comments, that it is not recommended to run software as root. If you do, then give it a dedicated working directory, i.e. set its user.home to some location reserved for it.

4
  • @Sock314: looks like you went off fishing. Not good form. Please attend people who attend you. @ zwets: +1, most interesting. I did not know that. It would explain the value of HOME being root's when running sudo java -jar ... since root owns the process. Let's see what Socks314 has to say.
    – Cbhihe
    Aug 10, 2015 at 22:12
  • Ah that makes sense! +1. So how is the home directory normally set when running a java program? Based on the user running the program?
    – kos
    Aug 10, 2015 at 22:19
  • @Cbhihe No, HOME will be the home directory of the user invoking sudo. You can check this by executing echo 'public class T1 { public static void main(String[] args) { System.out.println(System.getenv("HOME")); } }' > T1.java && javac T1.java && sudo java T1. This will yield /home/user. The issue is that Java ignores HOME when setting user.home. Now execute echo 'public class T2 { public static void main(String[] args) { System.out.println(System.getProperty("user.home")); } }' > T2.java && javac T2.java && sudo java T2, and this will yield /root.
    – zwets
    Aug 11, 2015 at 6:11
  • @kos The Java API documentation for java.lang.System states that user.home is set to User's home directory. I would then assume that it obtains this via the POSIX standard call getpwuid(getuid())->pw_dir, not via the HOME environment variable, which may or may not be set to the effective user's home directory. This makes sense, really. It is sudo which is the 'odd one out' here.
    – zwets
    Aug 11, 2015 at 6:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .