linux中sudo如何读取标准输入作为密码,避免每次都输入密码?
生活随笔
收集整理的這篇文章主要介紹了
linux中sudo如何读取标准输入作为密码,避免每次都输入密码?
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
需求描述:
今天想要在生產(chǎn)環(huán)境中,弄自動(dòng)部署的腳本,但是現(xiàn)在呢,需要sudo權(quán)限,每次都要輸入.就想看sudo如何能從標(biāo)準(zhǔn)輸入讀取密碼.
操作過(guò)程:
1.原來(lái)的方法
[deployer@testvm ~]$ echo "deployer" | sudo netstat -ntlp [sudo] password for deployer: #仍然是需要輸入密碼的.備注:即使使用管道,但是sudo還是沒(méi)有讀取到標(biāo)準(zhǔn)輸入的內(nèi)容.
2.查看sudo幫助文檔,有-S選項(xiàng),可以從標(biāo)準(zhǔn)輸入中讀入密碼
[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備注:所以呢,通過(guò)給sudo命令,指定-S選項(xiàng),就能將管道的內(nèi)容作為密碼使用,也就實(shí)現(xiàn)自動(dòng)腳本的目的.
?
sudo幫助文檔解釋(針對(duì)-S選項(xiàng)):
-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.?
文檔創(chuàng)建時(shí)間:2018年7月25日15:37:44
轉(zhuǎn)載于:https://www.cnblogs.com/chuanzhang053/p/9366291.html
總結(jié)
以上是生活随笔為你收集整理的linux中sudo如何读取标准输入作为密码,避免每次都输入密码?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 码云git笔记
- 下一篇: .NET 10 首个预览版发布,跨平台开