Java:在 Ubuntu 上请求 root 权限
如何调用身份验证窗口,从类中为当前应用程序授予 root 一段时间的访问权限。 例如,我正在编写一些应用程序来处理分区,因此它需要 root 权限才能执行某些操作。
How to call the authentication window, to grant access for root for a while for current application, from a class.
for example, I'm writing some app to work with partitions, so it need's root privileges to execute some things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你无法用 Java 做到这一点。最好的方法是,也许您可以检测到该程序没有以
sudo
身份运行,因此您可以要求用户关闭该程序并以超级用户身份重新启动。 (对于这个解决方案,我更喜欢从系统文件中选择一个文件或目录并检查它是否可写。例如new File("/usr/bin/ls").isWritable()
)其他方法可能是启动桌面环境特定的身份验证窗口。例如有 Java-gnome 但正如你所期望的,并非所有用户都应该使用 GNOME。因此,并没有真正的通用解决方案,并且所有解决方案都带有大量的依赖项。 Java 不是为此而设计的。
最好的办法是在启动时通过一个技巧来检查超级用户权限。也许您可以详细说明您的问题,我们可以提出不需要 root 访问权限的解决方案。
Probably you can't do that with Java. The best way to do is, maybe you can detect that program is not run as
sudo
so you can ask users to shutdown the program and relaunch as super user. (for this solution, I'd prefer choosing a file or directory from system files and check whether it is writable or not. e.g.new File("/usr/bin/ls").isWritable()
)Other ways might be launching Desktop Environment-specific authentication windows. e.g. There is Java-gnome but as you might expect, not all users supposed to use GNOME. So there is not really a universal solution and all comes with tons of dependencies. Java is not designed for this.
The best thing is to check super user privileges with a trick on startup. Maybe you can elaborate your problem and we can come up with a solution that does not require root access.