How can i verify if a user is root in a java application?
如何验证用户是否是java应用程序的root用户?
Thanks
谢谢
7 个解决方案
#1
8
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. If the current user is root-like, the output will be 0
.
请记住,系统上的“root”用户可能不会被称为root(尽管很少更改它),并且还可以将其别名为另一个用户名。如果当前用户是root用户,则输出将为0。
#2
6
Easy. Just use
简单。只是用
System.getProperty("user.name")
#3
2
run a native command? like whoami
运行本机命令?像whoami
#4
2
You can call
你可以打电话
Process p = Runtime.getRuntime.exec("whoami")
method. Then you can process p's stdout to read output of command.
方法。然后你可以处理p的stdout来读取命令的输出。
#6
0
The best way is to run
最好的方法是跑步
Process p = Runtime.getRuntime.exec("groups `whoaim`");
and try to parse string to get group call root. Your JVM process could be run by user not call root but i.e. moderator but this user could be in root group and you have root privileges.
并尝试解析字符串以获取组调用root。您的JVM进程可以由用户运行,而不是调用root,但是主持人,但是该用户可以在root组中,并且您具有root权限。
#7
0
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.
只是“另一个”修改后的解
#1
8
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. If the current user is root-like, the output will be 0
.
请记住,系统上的“root”用户可能不会被称为root(尽管很少更改它),并且还可以将其别名为另一个用户名。如果当前用户是root用户,则输出将为0。
#2
6
Easy. Just use
简单。只是用
System.getProperty("user.name")
#3
2
run a native command? like whoami
运行本机命令?像whoami
#4
2
You can call
你可以打电话
Process p = Runtime.getRuntime.exec("whoami")
method. Then you can process p's stdout to read output of command.
方法。然后你可以处理p的stdout来读取命令的输出。
#5
#6
0
The best way is to run
最好的方法是跑步
Process p = Runtime.getRuntime.exec("groups `whoaim`");
and try to parse string to get group call root. Your JVM process could be run by user not call root but i.e. moderator but this user could be in root group and you have root privileges.
并尝试解析字符串以获取组调用root。您的JVM进程可以由用户运行,而不是调用root,但是主持人,但是该用户可以在root组中,并且您具有root权限。
#7
0
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.
只是“另一个”修改后的解