我有这个:
su $username -c ./script.sh
问题是在脚本中我有“sudo”命令,他们说我
sudo: no tty present and no askpass program specified
如何正确地做到这一点?
UPD:我需要 sudo 和 su。我需要做的是以 USER $username 身份运行脚本,并能够以 root 身份在脚本中运行某些命令(例如,pacman -S)
解决方案:我在运行脚本之前向/etc/sudoers 添加了 NOPASSWD 选项,并在脚本完成后使用 sed 删除了该条目。
最佳答案
首先将 chmod +x
设置为您的脚本
尝试:
#!/bin/bash
echo "hello"
su - <your-user> -c /path/to/script.sh
echo "good bye"
更新:
你应该找到一种方法来强制 bash 使用pseudo-tty
强制伪终端分配。这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如实现菜单服务时。多个 -t 选项强制分配 tty,即使 ssh 没有本地 tty。
如果用户不是 sudoers,请执行以下步骤:
这是您需要在 /etc/sudoers
中执行的操作:
# User privilege specification
root ALL=(ALL:ALL) ALL
newuser ALL=(ALL:ALL) ALL
你还有办法做到: 如果它有密码,你可以输入密码:
echo "yourpassword" | sudo -S
或
您可以运行以下脚本:
#!/usr/bin/expect -f
spawn sudo -s <<EOF
expect "assword for username:"
send -- "user-password\r"
expect eof
你也可以这样做:
sudo -kS bash - << EOF
password
whoami
echo "Not a good idea to have a password encoded in plain text"
EOF
关于linux - su下如何运行sudo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22151361/