简体   繁体   English

Java执行需要su的Linux命令?

[英]Java execute Linux commands that require su?

What is the best way to run Linux commands that require "su" with Java? 用Java运行要求“ su”的Linux命令的最佳方法是什么? I want it to be very reliable, as for security goes I don't care much about it the system is not a production server. 我希望它非常可靠,因为出于安全性考虑,我不太在乎系统不是生产服务器。 No one ever connects to the machine but myself. 除了我自己,没有人连接到机器。 I thought about logging in as root so I don't have to deal with the su command and its disabled by default. 我考虑过以root用户身份登录,因此不必处理su命令,并且默认情况下已禁用它。 I'm using Debian 6. 我正在使用Debian 6。

The only thing is that one of my requirements is that the program should start after the normal user logs in without any farther user input. 唯一的要求是我的要求之一是该程序应在普通用户登录后启动,而无需任何其他用户输入。 I would set auto login for the normal user and when the computer is turned on and after the normal user logs in I would want that Java program to run as sudo, su without any user input. 我将为普通用户设置自动登录,并且在打开计算机电源后以及普通用户登录后,我希望该Java程序以sudo,su的身份运行,而无需任何用户输入。 Would that be possible? 那有可能吗?

Please tell me a simple and reliable solution. 请告诉我一个简单可靠的解决方案。

Regards 问候

Use sudo instead. 使用sudo代替。 It can be configed to allow specific command run without typing in password. 可以将其配置为允许运行特定命令而无需输入密码。

For running programs that require root access, they should be run with sudo . 对于需要root访问权限的正在运行的程序,应使用sudo运行它们。 The user will be asked to enter a password. 将要求用户输入密码。 As mentioned in the previous answer, sudo can be used to configure commands to be run as root that do not require entering a password. 如上一个答案中所述, sudo可用于将命令配置为以root身份运行,而无需输入密码。 Try looking at the man page for sudo for more information regarding that. 尝试查看sudo的手册页以获取有关此信息的更多信息。

If you want to run programs on system startup, you can write a bash script to run the command gksudo java -jar path when the user logs in. This will prompt the user with a dialog box to enter a password. 如果要在系统启动时运行程序,则可以编写bash脚本以在用户登录时运行命令gksudo java -jar path 。这将提示用户一个对话框,输入密码。

I also recommend doing a system call at the beginning of the program to ensure root access. 我还建议在程序开始时进行系统调用以确保root用户访问权限。

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

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