如果没有给普通用户配置过 NOPASSWD,那么每次非root用户在命令行终端执行需要root权限的操作时都需要需要密码然后回车,在一般情况下没什么问题,但是如果在脚本中就不行了。
如果希望在脚本中执行一个需要输入密码后回车的命令,我们可以使用管道
echo 密码 | sudo 命令
示例,如下
[admin@BizMsTestAlpha test]$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied
[admin@BizMsTestAlpha test]$
[admin@BizMsTestAlpha test]$
[admin@BizMsTestAlpha test]$
[admin@BizMsTestAlpha test]$ echo "DKaeTKx6$6ka" | sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bbea84f9a686 core:latest "/bin/sh -c 'java -s…" 47 hours ago Up 47 hours core.1.ia7kml1ugrj9fqeq2p28jp8
12c2f3a6275d zookeeper "/docker-entrypoint.…" 5 weeks ago Up 5 weeks 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp docker-zookeeper
ebae23411616 registry:2 "/entrypoint.sh /etc…" 2 months ago Up 5 weeks 0.0.0.0:5000->5000/tcp registry
[admin@BizMsTestAlpha test]$