3

I found that on my OSX system I have "weird" setup. Java reads my user name as "root" instead of my actual user name. Turns out the code I used uses System.getPropery("user.name") call. So, I made up stand-alone snipped (I save it under test.java) which prints java system user.name property and my UNIX user name environment:

class UserName {
public static void main(String[] args) {
   String name;
   name = System.getProperty("user.name");
   System.out.println("user.name="+name);
   name = System.getenv("USER");
   System.out.println("USER="+name);
}
}

if I compile it as javac test.java and then run it (under my account, not root one) as java UserName it prints the following:

user.name=root
USER=vk

My question is where/how java sets user.name and why it is set to root instead of my actual UNIX name. I'm not java expert and don't really know internals of System.getProperty API.

The second question is it possible to change this settings somehow in UNIX environment that it will read properly my user name.

Finally may be my environment or java configuration is broken and there is some hidden file (properties) which should be fixed.

Valentin
  • 1,492
  • 3
  • 18
  • 27
  • when you run 'whoami' on command line do you get root or vk? – Petros P May 21 '14 at 20:59
  • my account name of course, so I got vk – Valentin May 21 '14 at 21:00
  • this is really strange, i have one more question. Can you try to print user.home property and see if it is root's or your user's. – Petros P May 21 '14 at 21:06
  • Ok, I added to the code: `name = System.getProperty("user.home"); System.out.println("user.home="+name); name = System.getenv("HOME"); System.out.println("HOME="+name);` and it prints correctly **user.home=/Users/vk** and **HOME=/Users/vk** – Valentin May 21 '14 at 21:17
  • What version of OSX and JDK are you running? What does this return when executed from inside your IDE? –  May 21 '14 at 21:28
  • Why do you need to use `getenv()` that is a good way to add unnecessary complexity and external configuration to your programs. –  May 21 '14 at 21:30
  • Is your installed java executable setuid root? – David Conrad May 21 '14 at 22:54
  • @Jarrod, I don't use getenv, I got a program which uses System.getProperty and when I run it it used my account as "root". So I'm trying to understand what wrong with either Java code or my environment. That's why I made stand-alone code and post it here. – Valentin May 21 '14 at 23:04
  • 2
    @David, yes this the **reason**. I checked and indeed my java had setuid bit turned on. Why is that and how it happened is another story. I'll investigate. But once I turn off chmod -s /path/java the user.name reports proper account name. – Valentin May 21 '14 at 23:12

3 Answers3

3

The problem was resolved by turning off setuid bit in java executable. Thanks goes to David Conrad for correct tip. For the reference:

ls -l /usr/bin/java
-rwsr-xr-x ... /usr/bin/java
sudo chmod -s /usr/bin/java
lrwxr-xr-x ... /usr/bin/java

and after that everything was reported properly.

Valentin
  • 1,492
  • 3
  • 18
  • 27
2

You are running java with sudo or after you run su command and become root. (Maybe MacOS by default run your commands as root since your user in an administrators group).

Same output is also obtained on Linux (CentOS) when run after su command with root privileges.

user.name=root
USER=jdiver
jdiver
  • 2,228
  • 1
  • 19
  • 20
  • That looks like a possible explanation so far. I do in admin group on my mac, but I do use my local account. So everything I run is under local account. – Valentin May 21 '14 at 21:06
  • I tried with sudo and I got `user.name=root USER=root` on Mac – Petros P May 21 '14 at 21:07
  • I did check on another mac where I have non-admin account and it seems to confirm that this is the case. For non-admin account the output is correct it prints both user.name and USER correctly. – Valentin May 21 '14 at 21:10
  • @Valentin I have admin account and trying to reproduce your problem, but I am not able to. I tried to sudo and run but I am getting only root for all variables. Can you reproduce this problem on another mac machine? – Petros P May 21 '14 at 21:17
  • @Petros No, you don't need to run under sudo. Everything I did was under my account (compile and run). I'll try to reproduce it on another mac once I'll get access to it where I have admin priv too. – Valentin May 21 '14 at 21:21
0

Java will get the user.name property from the operating system, so if you are not running as root or using sudo to start java, then the java executable must be setuid root.

You can check for this by doing:

$ ls -l /path/to/java

If java is setuid root, you will see something like:

-rwsr-xr-x 1 root root 123456 Apr 1 16:59 /path/to/java

The 's' in the user execute bit indicates that it is a setuid program. You can remove the setuid bit with:

$ sudo chmod -s /path/to/java

David Conrad
  • 15,432
  • 2
  • 42
  • 54