Java:在 Ubuntu 上请求 root 权限

发布于 2024-11-30 14:12:55 字数 89 浏览 2 评论 0原文

如何调用身份验证窗口,从类中为当前应用程序授予 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亢潮 2024-12-07 14:12:55

也许你无法用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文