dongxizhe9755 2015-08-13 18:13
浏览 220
已采纳

sudo:没有tty存在且没有指定askpass程序使用shell_exec时

I have a php page that is trying to run a service restart using:

$list=shell_exec('sudo /sbin/service NetworkManager restart');

I needed to edit my sudoers file to let this happen. Thus:

#Defaults requiretty

and

apache ALL=(ALL) NOPASSWD: /sbin/service

When that failed, as a test I ran:

apache ALL=(ALL) NOPASSWD: ALL

I have run a shell_exec without the sudo command:

$list=shell_exec('whoami');

echo $list;

This returned "apache" as expected. So I ran:

$list=shell_exec('id');

echo $list;

This returned “uid=48(apache) gid=48(apache) groups=48(apache),10(wheel)”

I checked the permissions of the files against a working system that is doing the same thing and they matched up. After that just for testing sake I changed all file permissions to 777. Still nothing. In the apache error log I get the line "sudo: no tty present and no askpass program specified". As I understand it that applies to the #Defaults requiretty line in the sudoers file but as stated that has been commented out. I have done a bit more testing with it and my current entry in the sudoers file is:

#Defaults requiretty

ALL ALL=(ALL) NOPASSWD: ALL

I know not to run the server this way as it is a huge security risk but at this point I am at a total loss for what is locking me down. Selinux is off all permissions are 777 and the ownership of files matches a working system I have. With all of this and all possible security I can think of turned off I still have the “sudo: no tty present and no askpass program specified” line in my /var/log/httpd/error_log every time. The output of:

$list=shell_exec('sudo echo "yes" 2>&1 ');

echo $list;

Is also “sudo: no tty present and no askpass program specified”? I could really use some help on this one. I have read every article for 4 google pages on this every way I can think to google it.

  • 写回答

1条回答 默认 最新

  • doufu4333 2015-08-14 10:13
    关注

    Although my setup is a little different (I'm not trying to achieve it without a password), I use this in the sudoers file:

    apache ALL=(ALL) ALL
    Defaults:apache !requiretty
    

    I then run things as:

    echo '{password}' | sudo -S {command}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。