使用 sshpass 时出错 "sudo: no tty present and no askpass program specified"

Error "sudo: no tty present and no askpass program specified" while using with sshpass

我想编写一个脚本,我需要在其中从不同的机器 192.168.0.2 获取文件的内容而不输入密码,所以我使用 sshpass 和以下命令:

sshpass -p "password" ssh -o StrictHostKeyChecking=no user@192.168.0.2 " ls /root/path/of/file"

输出:

ls: cannot access '/root/path/of/file': Permission denied

我明白了,因为我没有以 root 用户身份登录。所以我尝试了:

sshpass -p "password" ssh -o StrictHostKeyChecking=no user@192.168.0.2 "sudo ls /root/path/of/file"

输出:

sudo: no tty present and no askpass program specified

我该怎么办?

正如 StackExchange 上 answer 的详细信息,远程主机上的 /etc/sudoers 文件可能不允许您在没有 tty 的情况下执行 运行 sudo 命令。如果您无法解决问题,它还会提供一些解决问题的详细建议。

尝试 ssh -t,它会为您分配一个 tty。