Tips for batch Deleting Users and adding users in CentOS

Source: Internet
Author: User
Tags crypt

Tools or commands used to manage users:
UseraddNote: Add a user;
AdduserNote: Add a user;
PasswdNote: set a password for the user;
UsermodNote: To modify user commands, you can use usermod to modify the logon name and user's home directory;
PwconvNote: Use the shadow password system to encrypt/etc/passwd and synchronize user information to the/etc/shadow file.
PwckNote: pwck verifies whether the content of the user configuration file/etc/passwd and/etc/shadow is legal or complete;
Pwunconv NOTE: For the traditional password system, it is the vertical Inverse Operation of pwconv. the/etc/passwd file created from/etc/shadow and/etc/passwd will be deleted;

Some problems encountered:
Question 1: The-e Option of adduser sets the account to expire. Will the file information of the Account be automatically deleted after the account expires?

Question 2: Is the adduser-p option used to set the user password? So how can I log on to the system using the password set here?
A: In linux, man adduser can see help. The-p parameter is described as follows:

-P passwd
The encrypted password, as returned by crypt (3). The default is
To disable the account.

It means that the password you entered is encrypted using the crypt command. This account is disabled by default.

Question 3: Are adduser and useradd the same? The functions seem similar. Why are these two commands compiled?

If adduser exists in the/usr/sbin/directory, run the ll/usr/sbin/adduser command to find the following:
Lrwxrwxrwx1 root 1 root 7 Aut 6 20:46/usr/sbin/adduser-> useradd

A: This indicates that adduser is only a symbolic link of useradd. This symbolic connection is created to allow users to hit the command as soon as possible.

Question 4: Why does the/home/directory still exist in the user's directory after userdel deletes the account?

Without parameters, the user directory information is not deleted by default. You can add the parameter-r.

Note:
1. In the case of pwunconv, passwd cannot be used to change the user password for the user to take effect. You can use chpasswd to change the password in batches, or you can use passwd to change the password after executing pwconv.

2. when you use adduser to create a user, use the password set by-p (in the case of pwunconv, it will be changed to a clear code, but). The account is still unavailable and cannot be logged on, you must use passwd after pwconv and then set it. Then, pwuncov is no longer explicitly displayed.

3. You can only delete a groupdel group after all the users in the group are deleted.

Batch Add/delete users:
Method 1: Compile scripts to implement Batch Processing

1. How to add users?

Step 1: Create the account list file useradd
[Root @ denven root] # vi useradd
Adduser stu060101
Adduser stu060102
Adduser stu060103
.....

Note: If you are prompted that the command cannot be executed when executing the script later, use the path/usr/sbin/adduserwin01 or modify the system environment variable.

Step 2: Change the File Permission to make it executable
[Root @ denven root] # chmod 700 userdel

Step 3: Execute the script to add a user
[Root @ denven root] #./useradd

Step 4: Set the password. You can use method 3 to set the password.

2. How to delete users in batches?

Step 1: Create and delete an account file named userdel

[Root @ denven root] # vi userdel
Userdel-r stu060101
Userdel-r stu060102
Userdel-r stu060103
...

Note: The-r option deletes the user's file directory at the same time. If this option is not added, the user does not exist, and the previously created directory still exists.

Step 2: Change the File Permission to make it executable
[Root @ denven root] # chmod 700 userdel

Step 3: Execute the script to delete the user
[Root @ denven root] #./userdel

Note: The above is implemented by adding and deleting USER commands for each user in the script.

Method 2: Use the loop statement in shell to implement

A. Batch add

First, create the account list file users

[Root @ denven root] # vi users
Stu01
Stu02
Stu03
Stu04
...
Step 2: Create the sh file usersadd
[Root @ denven root] # vi usersadd. sh

While read line
Do
Echo $ line# Print the read information
#/Usr/sbin/adduser $ line
/Usr/sbin/adduser $ line# Create a user
Echo "user $ line created! "
Done<Users# Read username from users file

Step 3: Execute the sh file usersadd

[Root @ denven root] # sh usersadd. sh
Some information is displayed on the screen.

Step 4: Set the password. You can use method 3 to set the password.

B. Batch Delete

Write shell to import the above User File users to delete

Step 1: Create the sh file usersadd
[Root @ denven root] # vi usersdel. sh


 
 
  1. While read $ line

  2. Do

  3. Echo $ line # print the read information

  4. #/Usr/sbin/adduser $ line

  5. /Usr/sbin/userdel-r $2 # Delete the read user

  6. Echo "user $ line deleted from system"

  7. Done <users # Read username from users file

Step 3: Execute the sh file usersdel

[Root @ denven root] # sh usersdel. sh

In fact, you can also use the -- stdin command of passwd to pass a value to passwd through a string.


 
 
  1. #!/bin/sh

  2. for i in `more userlist.txt `

  3. do

  4. echo $i

  5. echo $i"123" | passwd –-stdin "$i"

  6. echo; echo "User $username's password changed!"

  7. done


Method 3: Use the system's built-in Batch Processing Command

Step 1: create an account list file
[Root @ denven root] # vi students
Edit the account list in the Vi editor,

Stu01: 1001: 1000: stu:/home/stu01:/bin/bash
Stu02: 1002: 1000: stu:/home/stu02:/bin/bash
Stu03: 1003: 1000: stu:/home/stu03:/bin/bash

Note that this file must correspond to the format of each row in the/etc/passwd file. Seven fields must be separated by six colons.

Step 2: Create the password file for the Account
[Root @ denven root] # vi passwds.txt

Stu01 :******
Stu02 :******
Stu03 :******

* ** Indicates the password set for this user.

Step 3: Add a user
[Root @ denven root] # newusers <students
In this way, a large number of users are created. You can check that they have been written into the user configuration file;
[Root @ denven root] # more/etc/passwd | grep stu

Step 4: add user passwords to accounts in batches
[Root @ denven root] # chpasswd <passwds

Step 5: Synchronize the password to the/etc/shadow file
[Root @ denven root] # pwconv

Comparison of several methods:

The first method is to encapsulate the execution of each command in the script, so you need to write the add and delete commands multiple times in the script;

In the second method, you only need to add the user name list. You do not need to repeatedly write commands in the user file because it places the commands in sh and is implemented through cyclic statements. However, make sure that the shell file is written correctly.

The third method only uses system commands without programming, but is prone to errors when writing user list files;

Note: Sometimes I am used to using text processing software such as notepad or EditPlus in windows, and then upload the compiled list file to the Linux File System for reading, the command identifies some characters in different ways, causing an error in script running and cannot be added successfully. So it is best to be familiar with vi and write it in Linux to prevent errors.

This article is from "Fengyun, it's her ." Blog, please be sure to keep this source http://rfyiamcool.blog.51cto.com/1030776/723670


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.