1.1 Related Commands
There are two commands Useradd and passwd used during scripting, but the man command is used to view the help.
Man passwd--stdinthis option was used to indicate this passwd should read the new password from standard input, which can B e a pipe. #可以使用标准输入的方式获取用户的新密码, and you can use pipe commands. -e This is a quick-to-expire a password for the account. The user is forced to the password during the next login attempt. Available to root only. #用户下次登录时必须修改用户密码, and only administrators can use it. Useradd username# administrators can add username users using the commands above.
1.2 Creating a user script
Requirements: create User1 to User5 5 user with the same initial user password and user name. The command does not output during execution but needs to prompt the user to create a success, and the user created must change the password at logon.
[[email protected] ~]# vi createuser.sh #!/bin/bashfor (( i=1 ; i <=5 ; i++ ) { username=user${i} #用户名赋值到变量中 useradd $ username echo $username | passwd --stdin $username | >/dev/null #使用标准输入创建用户密码, Output output of the execution process to/dev/null passwd -e $username | >/dev/null #下次登录时必须修改密码 echo "the user $username is created" #userdel -r $username #删除用户} #编辑完成后保存并退出 [[ email protected] ~]# chmod u+x createuser.sh [[email protected] ~]# ./createuser.sh# Add permissions and Execute the user user1 is createdthe user user2 is createdthe user user3 is createdThe user user4 is createdThe user user5 is created
1.3 Results validation
[[email protected] ~]# tail-5/etc/passwd# view/etc/passwd at the end of 5 lines user1:x:1002:1002::/home/user1:/bin/bashuser2:x : 1003:1003::/home/user2:/bin/bashuser3:x:1004:1004::/home/user3:/bin/bashuser4:x:1005:1005::/home/user4:/bin/ Bashuser5:x:1006:1006::/home/user5:/bin/bash[[email protected] ~]$ su-user2# switch to user user2password:you is required to Change your password immediately (root enforced) changing password for user2. (current) UNIX Password: #提示输入新密码
Linux Batch Create users