linux如何通过脚本来修改用户的密码?脚本自动化修改用户密码?

需求描述:

  linux环境中在创建用户的时候,涉及到修改用户的密码,一般是通过passwd命令进行修改,需要多次的确认,这里考虑通过一条命令直接对密码进行修改

  不需要进行交互的方式.在此记录.

操作过程:

1.通过echo命令结合passwd命令来实现

[root@testvm ~]# echo "mytest" | passwd --stdin mytest
Changing password for user mytest.
passwd: all authentication tokens updated successfully.

备注:标记为红色的部分就是密码.passwd使用--stdin选项的意思是告诉passwd命令从标准输入读取新的密码.通过前面的管道读取标准输入.

2.通过切换进行验证密码是否修改成功

[root@testvm ~]# su - mytest    #通过root切换到普通用户不需要密码
[mytest@testvm ~]$ su - mytest  #再次进行用户的切换,需要输入密码
Password: 
[mytest@testvm ~]$              #密码正确,可以正常的执行切换

备注:已经可以使用之前设置的密码成功的进行登录.

 

passwd的--stdin解释如下:

--stdin
              This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.

 

文档创建时间:2018年7月2日13:14:12

posted @ 2018-07-02 13:16  Zhai_David  阅读(9044)  评论(0编辑  收藏  举报