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 newline
                 character.

文档创建时间:2018年7月25日15:37:44

猜你喜欢

转载自www.cnblogs.com/chuanzhang053/p/9366291.html