linux中sudo如何读取标准输入作为密码,避免每次都输入密码?
生活随笔
收集整理的這篇文章主要介紹了
linux中sudo如何读取标准输入作为密码,避免每次都输入密码?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求描述:
今天想要在生產環境中,弄自動部署的腳本,但是現在呢,需要sudo權限,每次都要輸入.就想看sudo如何能從標準輸入讀取密碼.
操作過程:
1.原來的方法
[deployer@testvm ~]$ echo "deployer" | sudo netstat -ntlp [sudo] password for deployer: #仍然是需要輸入密碼的.備注:即使使用管道,但是sudo還是沒有讀取到標準輸入的內容.
2.查看sudo幫助文檔,有-S選項,可以從標準輸入中讀入密碼
[deployer@testvm ~]$ echo "deployer" | sudo -S netstat -ntlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1135/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1610/master tcp 0 0 0.0.0.0:54268 0.0.0.0:* LISTEN 995/rpc.statd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 975/rpcbind tcp 0 0 :::16851 :::* LISTEN 1680/modclusterd tcp 0 0 :::22 :::* LISTEN 1135/sshd tcp 0 0 ::1:25 :::* LISTEN 1610/master tcp 0 0 :::3306 :::* LISTEN 1502/mysqld tcp 0 0 :::54539 :::* LISTEN 995/rpc.statd tcp 0 0 :::111 :::* LISTEN 975/rpcbind tcp 0 0 :::80 :::* LISTEN 1621/httpd備注:所以呢,通過給sudo命令,指定-S選項,就能將管道的內容作為密碼使用,也就實現自動腳本的目的.
?
sudo幫助文檔解釋(針對-S選項):
-S The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newlinecharacter.?
文檔創建時間:2018年7月25日15:37:44
轉載于:https://www.cnblogs.com/chuanzhang053/p/9366291.html
總結
以上是生活随笔為你收集整理的linux中sudo如何读取标准输入作为密码,避免每次都输入密码?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 码云git笔记
- 下一篇: .NET 10 首个预览版发布,跨平台开