在Linux中,如何在Qt Creator中添加使用su权限的构build步骤,而不会在构build中获取错误的密码尝试?

我的目标是成功链接一个文件夹到/ opt /目录,这需要使用sudo来运行。

我试过这个:

system(sudo ln -s $$OUT_PWD/xampp /opt/lampp):message("You should manually link LAMPP") 

但是从qt-creator构build它不会提示inputsudo密码,所以我无法获取它链接到的文件夹。 编译器输出中出现“错误的密码尝试…”错误。 然后我尝试了这些构build步骤:

 make sudo make install 

看看它是否会提示我那里,但它没有make install步骤与相同的错误,这是详细的:

 00:31:20: Starting: "/usr/bin/sudo" make install sudo: no tty present and no askpass program specified sudo: no tty present and no askpass program specified Sorry, try again. sudo: no tty present and no askpass program specified sudo: no tty present and no askpass program specified Sorry, try again. sudo: no tty present and no askpass program specified sudo: no tty present and no askpass program specified Sorry, try again. sudo: 3 incorrect password attempts 00:31:20: The process "/usr/bin/sudo" exited with code 1. 

在qmake脚本中的system()函数工作正常,当我从terminal使用qmake && make && make install ,因为它会在sudo ln... command之前提示我; 但为了快速testing目的,我觉得在Qt Creator中使用CTRL-B编译也应该使用sudo命令。

有没有办法从Qt Creator提示,或解决方法? 就像在Qt Creator中存储sudo pass一样(虽然这是有风险的),或者也许让它在terminal上运行构build步骤,它会提示我? qmake中的prompt()函数是否可以收集sudo密码?

任何build议,欢迎…

经过大量的研究:

 ssh-askpass Sudo Password | sudo -S bash ./script 

像一个魅力工作,它要求在一个单独的窗口/提示框的密码。 我在自定义构建步骤中添加它。 重要的是让sudo知道它会通过-S选项来输入密码。

 echo "password" | sudo -S bash ./script 

也可以使用bash脚本,但是您可能认为它不安全地在脚本中存储sudo密码。 据说读取权限应该可以解决这个问题。

@Logan你可以使sudo直接使用ssh-askpass (GUI口令请求对话框)和-A选项,这可以使用SUDO_ASKPASS环境变量来覆盖/usr/bin/ssh-askpass值,可以在/etc/sudo.conf [1] (如@herophuong的gksudokdesudo的建议)。 这会在答案中解决第二个解决方案的安全风险,让任何可以观察命令行参数的"password"可见。 相当于你的第一个解决方案变成:

 sudo -A -s ./script 

不要忘记, sudo可以提供凭证缓存,如果它最近已经使用(默认5分钟?),重复的调用将使用缓存的密码值,而不是发出另一个密码请求。 如果出于理智/安全原因,对于脚本(或其他地方)不希望使用这个脚本,则使用sudo -K而不使用其他参数来立即清除任何缓存的凭据(该用法不需要​​密码),以便任何后续的sudo使用需要认证。

1 – 如果你想编辑/etc/sudo.conf来添加:

 # Path to askpass helper program Path askpass /usr/bin/ssh-askpass 

我想你应该使用gksudo (在GNOME中)或kdesudo (在KDE中)。 这些命令将使用一个窗口来提示输入密码。