簡體   English   中英

“回聲”密碼“| 須藤 -S<command> ” 要求輸入密碼

[英]“echo ”password“ | sudo -S <command>” asks for password

我嘗試在不成為 su 用戶的情況下運行腳本,並為此使用此命令:

echo "password" | sudo -S <command>

如果我將此命令用於“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

您可以使用位於 /etc/sudoers 中的 sudoers 文件來允許特定用戶以 root 身份執行命令而無需密碼。

myLocalUser ALL=(ALL) NOPASSWD: /bin/chmod

通過這一行,用戶 myLocalUser 可以在不需要密碼的情況下以 root 身份執行 chmod。

但這也破壞了系統安全的一部分,因此請注意不要允許太多並盡可能地屏蔽任務。

用戶信息

sudo -S提示打印到stderr

如果您不想看到它,請將stderr重定向到/dev/null

  • 以下命令在本地主機上重定向 stderr:
echo <password> | ssh <server> sudo -S ls 2>/dev/null

相當於echo <password> | ssh <server> "sudo -S ls" 2>/dev/null echo <password> | ssh <server> "sudo -S ls" 2>/dev/null

  • 以下命令重定向遠程服務器上的 stderr:
echo <password> | ssh <server> "sudo -S ls 2>/dev/null"
  • 如果您需要保留stderr ,但隱藏[sudo] password for ...那么您可以在本地或遠程機器上使用進程替換。 由於sudo提示符沒有換行符,我使用sed來剪掉sudo提示符。 我這樣做是為了保存所創建進程的第一行stderr
# local filtering
echo <password> | ssh <server> "sudo -S ls" 2> >(sed -e 's/^.sudo[^:]\+: //')
#remote filtering
echo <password> | ssh <server> "sudo -S ls 2> >(sed -e 's/^.sudo[^:]\+: //')"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM