简体   繁体   English

Java system.getProperty问题

[英]Java system.getProperty issue

I found that on my OSX system I have "weird" setup. 我发现在我的OSX系统上我有“怪异”的设置。 Java reads my user name as "root" instead of my actual user name. Java将我的用户名读作“root”而不是我的实际用户名。 Turns out the code I used uses System.getPropery("user.name") call. 原来我使用的代码使用System.getPropery(“user.name”)调用。 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: 所以,我编写了独立剪切(我将它保存在test.java下),它打印了java系统user.name属性和我的UNIX用户名环境:

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: 如果我将其编译为javac test.java然后运行它(在我的帐户下,而不是root用户)作为java UserName,它将打印以下内容:

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. 我的问题是java在哪里/如何设置user.name以及为什么将它设置为root而不是我的实际UNIX名称。 I'm not java expert and don't really know internals of System.getProperty API. 我不是Java专家,也不知道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. 第二个问题是可以在UNIX环境中以某种方式更改此设置,以便正确读取我的用户名。

Finally may be my environment or java configuration is broken and there is some hidden file (properties) which should be fixed. 最后可能是我的环境或java配置被破坏,并且有一些应该修复的隐藏文件(属性)。

The problem was resolved by turning off setuid bit in java executable. 通过关闭java可执行文件中的setuid位解决了该问题。 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. 之后一切都报告得当。

You are running java with sudo or after you run su command and become root. 您正在使用sudo运行java或运行su命令后成为root用户。 (Maybe MacOS by default run your commands as root since your user in an administrators group). (默认情况下,MacOS默认以root身份运行您的命令,因为您的用户位于管理员组中)。

Same output is also obtained on Linux (CentOS) when run after su command with root privileges. 在具有root权限的su命令之后运行时,在Linux(CentOS)上也可以获得相同的输出。

user.name=root
USER=jdiver

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. Java将从操作系统获取user.name属性,因此如果您不是以root身份运行或使用sudo启动java,那么java可执行文件必须是setuid root。

You can check for this by doing: 您可以通过执行以下操作来检查:

$ ls -l /path/to/java

If java is setuid root, you will see something like: 如果java是setuid root,你会看到类似的东西:

-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. 用户执行位中的's'表示它是一个setuid程序。 You can remove the setuid bit with: 您可以使用以下命令删除setuid位:

$ sudo chmod -s /path/to/java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM