以root身份执行linux命令 [英] Executing linux command as root

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

问题描述

我想以root用户身份从我的C ++ / Qt代码中执行linux命令。
最后一个对话框请求root pass应该被实现,但是没有我可以硬编码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.

这是我到目前为止做的: p>

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.

$ c> void QProcess :: setEnvironment(const QStringList& environment)?

Probably something can be done with void QProcess::setEnvironment ( const QStringList & environment )?

感谢

推荐答案

如果您以普通用户身份运行,则无法提升您的权限,除非您有suid,这可能不需要整个应用程序。
$ b $它的做法是通过外部助手;你想要的东西,sudo不su,在Ubuntu的root帐户被锁定; 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.

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

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