“usermod”和“passwd”⽤于⼀次锁定或解锁⼀个⽤户帐户,但也可以使⽤ 循环⽤于⼀次设置多个⽤户账号。我们将创建以下三个Shell来锁定和解锁多个帐户和查看账号状态。
创建锁定⽤户的
创建查看⽤户状态的脚本创建解锁⽤户的脚本
列出需要锁定或解锁的⽤户列表,前提是已经创建了下⾯的⽤户:
[root@localhost ~]# cat user-lists.txt u1u2u3u4u5
[root@localhost ~]# tail -5 /etc/passwdu1:x:1002:1002::/home/u1:/bin/bashu2:x:1003:1003::/home/u2:/bin/bashu3:x:1004:1004::/home/u3:/bin/bashu4:x:1005:1005::/home/u4:/bin/bashu5:x:1006:1006::/home/u5:/bin/bash
脚本⼀、锁定多个⽤户的脚本
使⽤以下Shell脚本在中锁定多个⽤户帐户。
[root@localhost ~]# vim user-lock.sh #!/bin/bash
for user in `cat user-lists.txt`do
passwd -l $userdone
最后,运⾏脚本锁定⽂件中的⽤户列表。
[root@localhost ~]# sh user-lock.sh Locking password for user u1.passwd: Success
Locking password for user u2.passwd: Success
Locking password for user u3.passwd: Success
Locking password for user u4.passwd: Success
Locking password for user u5.passwd: Success
脚本⼆、查看⽤户锁定状态的脚本
使⽤以下shell脚本来检查锁定的⽤户帐户的状态:
[root@localhost ~]# vim user-lock-status.sh#!/bin/bash
for user in `cat user-lists.txt`do
passwd -S $userdone
最后运⾏该脚本,查看⽤户锁定状态:
[root@localhost ~]# sh user-lock-status.sh
u1 LK 2021-02-28 0 99999 7 -1 (Password locked.)u2 LK 2021-02-28 0 99999 7 -1 (Password locked.)u3 LK 2021-02-28 0 99999 7 -1 (Password locked.)u4 LK 2021-02-28 0 99999 7 -1 (Password locked.)u5 LK 2021-02-28 0 99999 7 -1 (Password locked.)
脚本三、解锁多个⽤户的脚本
使⽤以下Shell脚本来解锁多个⽤户帐户:
[root@localhost ~]# vim user-unlock.sh#!/bin/bash
for user in `cat user-lists.txt`
do
passwd -u $userdone
运⾏该脚本解锁⽤户密码:
[root@localhost ~]# sh user-unlock.sh Unlocking password for user u1.passwd: Success
Unlocking password for user u2.passwd: Success
Unlocking password for user u3.passwd: Success
Unlocking password for user u4.passwd: Success
Unlocking password for user u5.passwd: Success
总结
在本教程中,学习了如何使⽤Shell脚本在Linux中锁定和解锁多个⽤户帐户。
因篇幅问题不能全部显示,请点此查看更多更全内容