"echo "password" | sudo -S <command>" 要求输入密码

Jo *_*erd 6 linux sudo passwords

我尝试在不成为 su 用户的情况下运行脚本,并为此使用此命令:

echo "password" | sudo -S <command>
Run Code Online (Sandbox Code Playgroud)

如果我将此命令用于“scp”、“mv”、“whoami”命令,则该命令运行良好,但是当我用于“chmod”时,该命令会要求我的用户输入密码。我不输入密码,命令有效。我的问题是系统向我询问密码。我不希望系统要求输入密码。

问题ss是这样的:

[myLocalUser@myServer test-dir]$ ls -lt
total 24
--wx-wx-wx 1 root root 1397 May 26 12:12 file1
--wx-wx-wx 1 root root  867 May 26 12:12 script1
--wx-wx-wx 1 root root 8293 May 26 12:12 file2
--wx-wx-wx 1 root root 2521 May 26 12:12 file3

[myLocalUser@myServer test-dir]$ echo "myPassw0rd" | sudo -S chmod 111 /tmp/test-dir/*
[sudo] password for myLocalUser: I DONT WANT ASK FOR PASSWORD

[myLocalUser@myServer test-dir]$ ls -lt
total 24
---x--x--x 1 root root 1397 May 26 12:12 file1
---x--x--x 1 root root  867 May 26 12:12 script1
---x--x--x 1 root root 8293 May 26 12:12 file2
---x--x--x 1 root root 2521 May 26 12:12 file3
Run Code Online (Sandbox Code Playgroud)

use*_*274 4

sudostderr在读取管道密码之前打印此提示。重定向标准错误以避免看到它:

echo "password" | sudo -S command 2> /dev/null
Run Code Online (Sandbox Code Playgroud)