我们将创建以下三个Shell脚本来锁定和解锁多个帐户和查看账号状态。
列出需要锁定或解锁的用户列表,前提是已经创建了下面的用户:
[root@localhost ~]# cat user-lists.txt u1 u2 u3 u4 u5 [root@localhost ~]# tail -5 /etc/passwd u1:x:1002:1002::/home/u1:/bin/bash u2:x:1003:1003::/home/u2:/bin/bash u3:x:1004:1004::/home/u3:/bin/bash u4:x:1005:1005::/home/u4:/bin/bash u5:x:1006:1006::/home/u5:/bin/bash
脚本一、锁定多个用户的脚本
[root@localhost ~]# vim user-lock.sh #!/bin/bash for user in `cat user-lists.txt` do passwd -l $user done
[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
脚本二、查看用户锁定状态的脚本
[root@localhost ~]# vim user-lock-status.sh #!/bin/bash for user in `cat user-lists.txt` do passwd -S $user done
[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.)
如果上面的输出在用户名后面显示LK
,则表明用户密码已锁定。
脚本三、解锁多个用户的脚本
[root@localhost ~]# vim user-unlock.sh #!/bin/bash for user in `cat user-lists.txt` do passwd -u $user done
[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
如果以上输出在用户名后面显示PS
,则用户密码未锁定。