Linux Bulk Add user and group shell scripts

Source: Internet
Author: User
Tags ftp stdin

Recently, when customizing ISO, there is a need for specific projects to run with a specific user, to remove root permissions. This requires creating a group of users by default based on the project. So I wrote a simple script to automatically add users after the system setup is complete.

Simple example

Umask 0002

#批量创建用户组
For groups in AA BB cc DD
Todo
Groupadd $groups
Done

#批量创建相同用户组的用户

For the users in AA BB
Todo
USERADD-G AA $users
Done
Useradd-g CC CC
USERADD-G DD DD


Example

Example: Let the system automatically add a B C D four users with the same name as the password and user name, the script is as follows:
#!/bin/bash
#自动添加用户和密码 with the same name
For UU in a B c D
Todo
Useradd $UU
echo $UU | passwd--stdin $UU
Done
Automatically add a B C D four users, and the password is 123:
#!/bin/bash
#自动添加用户和密码, and the password is all 123.
For UU in a B c D
Todo
Useradd $UU
Echo 123 | passwd--stdin $UU
Done
How do I change the password for an existing user?
echo "NewPassword" | Passwd–stdin username
One: Create a file to add a list of users
#ee username.list
Usr1
Usr2
Usr3
Save exit
Two: Write the shell script implementation to add the user automatically (same password and user name)
#ee useradd.sh
#!/bin/sh
For USER in $ (cat username.list)
Todo
mkdir/home/$USER
echo $USER | PW Useradd $USER-H 0
home=/home/$USER
Done
Save exit
#chmod a+x haha.sh
#./haha.sh
Note: Echo $USER | PW Useradd $USER-H 0
The first $user is the user password ($USER is the contents of the Usrname.list)
The second $user is the user name


Example


newusers+chpasswd Bulk Add Users

1, first we create user files and password files;

We want to create the file userfile.txt that contains the new user, and the other is to set the password for the newly added user userpwdfile.txt;

[Root@localhost ~]# Touch Userfile.txt
[Root@localhost ~]# Touch Userpwdfile.txt

Then use the text editor to open the file Userfile.txt, add the following content;

Win00:x:520:520::/home/win00:/sbin/nologin
Win01:x:521:521::/home/win01:/sbin/nologin
Win02:x:522:522::/home/win02:/sbin/nologin
Win03:x:523:523::/home/win03:/sbin/nologin
Win04:x:524:524::/home/win04:/sbin/nologin
Win05:x:525:525::/home/win05:/sbin/nologin
Win06:x:526:526::/home/win06:/sbin/nologin
Win07:x:527:527::/home/win07:/sbin/nologin
Win08:x:528:528::/home/win08:/sbin/nologin
Win09:x:529:529::/home/win09:/sbin/nologin

The Userfile.txt file content format is the same as the/etc/passwd format and must be written in strict accordance with the/etc/passwd format; The user added above cannot log on to the system, but it is fully available for FTP logins. However, you have to open the configuration file in the appropriate FTP server so that local users have read and write permissions; If you want to allow some of the above users to log on to the system, you can change the shell like a change, such as/bin/bash;

We'll write the new user's password file userpwdfile.txt content, the user name in the content of the file to be the same as the Userfile.txt username and in strict accordance with the "Username: password" format, a user line That is, we first added win00 to win09 users, and now we want to update the passwords for those users, like the following;

win00:123456
win01:654321
win02:123321
Win03:qweewq
Win04:google
Win05:adadwc
Win06:wsscee
Win07:xxec32
Win08:543wew
Win09:3ce3wf

2, with NewUsers batch add users, at this time the user is no password

[Root@localhost ~]# newusers Userfile.txt

2, the implementation of the command/usr/sbin/pwunconv, will/etc/shadow generated shadow password decoding, and then write back to/etc/passwd, and/etc/shadow Shadow password column deleted. This is to facilitate the next step of the password conversion work, that is, first cancel the shadow password function, close shadow files.

[Root@localhost ~]# Pwunconv

3, with chpasswd batch change password
[Root@localhost ~]# chpasswd < Userpwdfile.txt

4, finally do not forget to restore shadow files, to ensure security, you can use the following command to map to the/etc/shadow file name

[Root@localhost ~]# Pwconv

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.