[英]Check if a user is root in a java application
How can i verify if a user is root in a java application? 如何验证用户是否是java应用程序的root用户?
Thanks 谢谢
Process p = Runtime.getRuntime().exec("id -u")
Keep in mind that the "root" user on a system may not be called root
(although it's rare to change it), and it's also possible to alias it to another username. 请记住,系统上的“root”用户可能不会被称为
root
(尽管很少更改它),并且也可以将其别名为另一个用户名。 If the current user is root-like, the output will be 0
. 如果当前用户是root用户,则输出将为
0
。
Easy. 简单。 Just use
只是用
System.getProperty("user.name")
run a native command? 运行本机命令? like whoami
像whoami
You can call 你可以打电话
Process p = Runtime.getRuntime.exec("whoami")
method. 方法。 Then you can process p 's stdout to read output of command.
然后你可以处理p的stdout来读取命令的输出。
检查一下: 在java中获取登录用户名 。
String userName = System.getProperty("user.name");
Process p = Runtime.getRuntime().exec("groups " + userName);
String output = read(p.getInputStream());
String error = read(p.getErrorStream());
And here is a read function: 这是一个读取功能:
public static String read(InputStream input) throws IOException {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
}
Just "another" modified solution. 只是“另一个”修改后的解决方
The best way is to run 最好的方法是跑步
Process p = Runtime.getRuntime.exec("groups `whoaim`");
and try to parse string to get group call root. 并尝试解析字符串以获取组调用root。 Your JVM process could be run by user not call root but ie moderator but this user could be in root group and you have root privileges.
您的JVM进程可以由用户运行,而不是调用root,但是调度程序,但该用户可以在root组中,并且您具有root权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.