SSH sudo inside script 不同的行为

SSH sudo inside script different behaviour

我正在尝试在本地网络中设置一些自动化并开始使用一些 shell 脚本和我看到的东西 - 根据脚本 运行(with或不使用 sudo):

我有: 计算机 A 和计算机 B。

里面 ComputerA:

一个shell脚本script.sh:

cp /dir1/file1 /dir2/file2
ssh username@ComputerB "sudo reboot"

/etc/ssh/ssh_config 文件,其中包含一些无需 ssh 密钥即可工作的配置(它们在 ComputerB 上总是会发生变化):

StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
GlobalKnownHostsFile=/dev/null

里面 ComputerB:

/etc/sudoers 文件中:

username ALL=(ALL:ALL) NOPASSWD:ALL

当我通过 SSH 连接到 ComputerA 并且 运行 script.sh 没有 sudo 时,我得到写入 /dir2 的权限错误(没问题)并且ComputerB 上的下一个命令正常执行(重新启动 ComputerB),但我是 运行 sudo script.sh。它复制文件然后我很奇怪 - SSH 询问我用户名密码。尝试了不同的变体将 ssh 命令更改为类似:

ssh -t username@ComputerB "echo user_pass | sudo -S reboot"

但没有任何帮助。 所以我需要帮助来弄清楚会发生什么以及如何执行 sudo script.sh 而无需在内部输入 ssh 命令的密码。 谢谢!

不要 运行 script.sh 与计算机 A 上的 sudo;而是像这样修改脚本:

sudo cp /dir1/file1 /dir2/file2
ssh username@ComputerB "sudo reboot"

您看到 奇怪行为 的原因是您实际上在 computerA 上成为 root(我假设您有为您的普通用户设置密钥对并期望连接到 computerB 无密码?),以及 computerA[ 上的 root =24=] 没有 computerB 知道的密钥对。