在Qt中以root身份执行Linux命令 [英] Executing Linux command as root in Qt

查看:1412
本文介绍了在Qt中以root身份执行Linux命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的C ++/Qt代码中以root用户身份执行linux命令. 最终,应该实现一个对话框,要求输入root密码,但是我不能硬编码root密码.

I want to execute a linux command as a root user, from my C++/Qt code. Ultimately a a dialog requesting root pass should be implemented, but for no I can hard-code root password.

这是我到目前为止所做的:

This is what I done so far:

QProcess p;
p.start( "dmidecode" );
p.waitForFinished(-1);

QString p_stdout = p.readAllStandardOutput();
QString p_stderr = p.readAllStandardError();

它适用于不要求root特权的命令.但是我想实现诸如"zypper up"或"dmidecode"之类的命令,这些命令如果没有root密码就无法执行.

And it is working for commands that do not request root privileges. But I want to implement commands like "zypper up" or "dmidecode" which I can't execute without a root password.

也许可以用void QProcess::setEnvironment ( const QStringList & environment )做些什么?

谢谢

推荐答案

如果您以普通用户身份运行,除非您是suid,否则您无法提升特权.您可能不需要整个应用程序.
它的完成方式是通过外部帮助程序.您想要执行sudo not su的操作,因为在Ubuntu中根帐户已被锁定; gksudo或kdesudo取决于运行的环境.其中之一应预先安装在任何现代linux发行版上,否则您可以随应用程序一起部署副本.
当然,如果当前用户没有权限通过sudo运行内容,那将无法正常工作,但这是一种正常的安全措施,您不应该解决它.

You cannot elevate your privileges if you run as a normal user unless you're suid, which you probably don't want for the whole application.
The way it's done is through an external helper; you want something that does sudo not su as in Ubuntu the root account is locked; gksudo or kdesudo depending on the running environment. One of those should be preinstalled on any modern linux distro, or you could deploy a copy with your application.
Of course it will not work if the current user does not have the rights to run stuff through sudo, but this is a normal security measure and you shouldn't work around it.

这篇关于在Qt中以root身份执行Linux命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆