Commands for batch adding users and groups in Linux

Source: Internet
Author: User
Tags ftp ftp login

Recently, when customizing ISO, a specific project needs to run with a specific user to cancel the root permission. In this way, you need to create a batch of users by default based on the project. Therefore, a simple script is written to automatically add users after the system is installed.

Umask 0002
 
# Batch create user groups
For groups in aa bb cc dd
Do
Groupadd $ groups
Done
 
# Batch create users of the same user group
 
For users in aa bb
Do
Useradd-g aa $ users
Done
Useradd-g cc
Useradd-g dd


Newusers + chpasswd batch add users

1. First, create the user file and password file;

Create the userfile.txt file containing the new user, and set the userpwdfile.txt password for the new user;

[Root @ localhost ~] # Touch userfile.txt
[Root @ localhost ~] # Touch userpwdfile.txt

Then, open the userfile.txt file with the administrative editor and 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 content format of the userfile.txt file is the same as that of/etc/passwd, which must be written in strict accordance with the/etc/passwd format. All users added above cannot log on to the system, but it can be used for ftp login, but you have to open it in the configuration file of the corresponding ftp server to allow local users to 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/bin/bash;

Let's write the userpwdfile.txt content of the new password file. The username in the file must be the same as the userfile.txt username and be written in strict accordance with the format of "username: password", one line for the user; that is to say, we first add the users from win00 to win09, and now we need to update the passwords for these users; for example, the following;

Win 00: 123456
Win01: 654321
Win02: 123321.
Win03: qweewq
Win04: google
Win05: adadwc
Win06: wsscee
Win07: xxec32
Win08: 543wew
Win09: 3ce3wf

2. Use newusers to add users in batches. At this time, the user does not have a password.

[Root @ localhost ~] # Newusers userfile.txt

2. Run the/usr/sbin/pwunconv command to decode the shadow password generated by/etc/shadow and write it back to/etc/passwd, delete the shadow password column of/etc/shadow. This is to facilitate the next step in password conversion, that is, to cancel the shadow password function and disable the shadow file.

[Root @ localhost ~] # Pwunconv

3. Use chpasswd to change passwords in batches
[Root @ localhost ~] # Chpasswd <userpwdfile.txt

4. Do not forget to restore the shadow file to ensure security. You can use the following command to map to the/etc/shadow file name.

[Root @ localhost ~] # Pwconv

 

How can I delete them in batches?


As shown in the following figure, you only need to change useradd to userdel. If we confirm that the user does not need it, we can add rm-rf/home/$ user to delete the user's home directory.
 

When we look at it again, the user has no more
 


User1 and user2 in the home directory are gone.
 

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.