How to fix 'sudo: no tty present and no askpass program'以及硬盘序列号的读写
Posted 万载一梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How to fix 'sudo: no tty present and no askpass program'以及硬盘序列号的读写相关的知识,希望对你有一定的参考价值。
在调用system命令读写硬盘序列号的过程中遇到问题,报错如下:
sudo: no tty present and no askpass program
发现此问题是由于帐号并没有开启免密码导致的 。
查找资料,解决方法如下:
方法一:
sudo isudo
然后在最末尾添加:
username ALL = NOPASSWD: /fullpath/to/command,/fullpath/to/othercommand
eg:
john ALL = NOPASSWD: /sbin/poweroff,/sbin/start,sbin/stop
会允许用户John在进行poweroff,start,stop的时候不需要密码。这里我使用了最简单的方式,如下:
john ALL = NOPASSWD: ALL
添加完成以后就可以无须密码进行sudo操作了。(亲测可用)
方法二:
1 打开sudoers
vi /etc/sudoers
2 添加免密码
john ALL = NOPASSWD: ALL
读取硬盘序列号的方法:
if(system("sudo hdparm -I /dev/sda | grep Serial >> tmpGenuine.txt")>=0) { QFile *tmpFile = new QFile("tmpGenuine.txt");//临时存放文件 if(tmpFile->open(QIODevice::ReadOnly|QIODevice::Text)) { while(!tmpFile->atEnd()) { QString tmpLine; tmpLine = tmpFile->readLine(); tmpLine.indexOf(rx2); genuineNum += rx2.cap(1); } qDebug()<<"genuineNum:"<<genuineNum; } tmpFile->close(); } else { qDebug()<<"can not get the Serial Num"; return 0; }
以上是关于How to fix 'sudo: no tty present and no askpass program'以及硬盘序列号的读写的主要内容,如果未能解决你的问题,请参考以下文章
error fixE: Package 'oracle-java8-installer' has no installation candidate
What's Assembly - CSharp - Editor - first pass.dll? Best How to Fix Assembly - CSharp - Editor -
Java中使用HttpRequest调用RESTfull的DELETE方法接口提示:How to fix HTTP method DELETE doesn't support output
How to solve Nissan Consult 3 plus can't see no vehicle in list