Shell_unix Linux for users in bulk in Linux

Source: Internet
Author: User
Tags chmod mkdir

Recently, the author used the Linux operating system in the construction of Campus network application platform, providing the users of the campus network with DNS, Apache, POP3, SMTP, FTP and other services. As the number of students in our school is more than 3000, the author pays special attention to ensure the efficiency of user management in the process of building the whole application platform. My basic idea and practice is: using shell for each student to establish a system account-that is, provide an e-mail address (including FTP space account), and then students can use their e-mail to re-register forum users. Based on such a management idea, the author has written a batch of users to build the shell, practice has proved that the use of shell can effectively manage the system users based on the UNIX kernel.

The following is the source code and comments for the entire shell, which are debugged on Redhat 7.2 and Turbo Linux 7.0, and may need to be modified for use in other versions of Linux. where the "@" section is a code comment, "()" part of the author's interpretation of the idea of programming.

@!/bin/sh (definition uses SH shell)

Groupadd users (set up user groups)

@grade code (defines the first argument as grade codes, giving the variable "grade")

Grade=$1

@class code (defines the second argument as class codes, giving the variable "Num_class")

Num_class= ' expr $ + 0 '

if (test $num _class-le 9) (the following code is to ensure that the number of digits in the class code is two bits)

Then

num_class= "0" $num _class

Fi

@max User ID (defines the third parameter as the maximum ID number for the class student, giving the variable "Max_stid")

Max_stid= ' expr $ + 0 '

@init User ID (the class student ID number starts from 1)

Num_stid =1

@mkdir User Home (Create a public directory named "Student" for all students in the "/home" directory)

if [!-x/home/student]

Then

Mkdir/home/student

Fi

if [!-x/home/student/$1$num_class] (in the student public directory for each class to create a directory named for this class code)

Then

Mkdir/home/student/$1$num_class

Fi

while (Test $num _stid-le $max _stid) (loop from user ID number 1 until the maximum ID number)

Todo

if (test $num _stid-le 9) (the following guarantees the number of digits in the student code is two digits)

Then

Num_stid= "0" $num _stid

Fi

User_name= $grade $num_class$num_stid (combining grade, class, student ID number code into user name and assigning variable user_name)

@save user passwd to File user_pwlist (in "name:passwd" format, append to "user_pwlist" file for initialization of user password)

echo $user _name ":" $user _name >> user_pwlist

@add User (set up the user and give the Users group, create the user directory)

Adduser-g users-d/home/student/$1$num_class/$user _name $user _name

@set Quota (Set quotas for this user, limit maximum capacity to 20MB, no file limit)

Setquota-u $user _name 10240 20480 0 0/home

@set Directory mode (permissions to set this user directory to 755)

chmod 755/home/student/$1$num_class/$user _name

@current User ID Add one (username plus 1, prepare for Next loop, build next user)

num_stid= ' expr $num _stid + 1 '

Done

CHPASSWD < User_pwlist (The following two acts have just established all user settings password)

Pwconv

RM user_pwlist-f (delete "user_pwlist" file)

  

About using:

1. Use the VI Editor to write the above Code section by line, and save as a file, such as "AddClass" name save, and then perform the following steps.

2. #chmod +x addclass (set AddClass file as executable)

3. #./addclass GA 1 50 (Establish ga0101-ga0150 user)

Note: #为提示符.

  

The above code is only the author in the use of shell System user management of a little experience. Because the shell can be used in conjunction with the Linux system commands, its function in management is very powerful. As far as the above code is concerned, we can create a shell that deletes a bulk user, a shell that configures user quotas, and a batch initial user password, as long as we change it slightly. If you add a layer of class looping statements, you can generate a shell that builds one grade user at a time.

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.