Batch create a user's shell in Linux

Source: Internet
Author: User

Recently, I have adopted a Linux operating system in the construction of campus network application platform to provide campus network users with DNS, Apache, POP3, SMTP, FTP and other services. Since the number of students in our school is more than 3000, I pay special attention to ensuring user management efficiency during the construction of the entire application platform. My basic idea and practice are: use shell to create a system account for each student-that is, provide an e-mail address (including an FTP space account ), then, students can use their e-mails to register Forum users. Based on this management idea, I have compiled a shell for batch creation of users. Practice has proved that using shell can effectively manage Unix kernel-based system users.

The source code and comments of the entire shell are as follows:CodeDebugging on Redhat 7.2 and Turbo Linux 7.0 is successful. You may need to modify it in other versions of Linux. The "@" part is the code comment, and the "()" part is the author's explanation of programming ideas.

@! /Bin/sh (definition uses sh shell)

Groupadd users (create a user group)

@ Grade code (define the first parameter as the grade code and assign the variable "Grade ")

Grade = $1

@ Class code (defines the second parameter as the class code and assigns the variable "num_class ")

Num_class = 'expr $2 + 0'

If (test $ num_class-le 9) (The following Code ensures that the number of digits of the class code is two digits)

Then

Num_class = "0" $ num_class

Fi

@ Max user ID (define the third parameter as the maximum ID of the students in the class and assign the variable "max_stid ")

Max_stid = 'expr $3 + 0'

@ Init user ID (the student ID 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] (create a directory named after the class code in the public directory of the student)

Then

Mkdir/home/student/$1 $ num_class

Fi

While (test $ num_stid-Le $ max_stid) (starts from user ID 1 until the maximum ID)

Do

If (test $ num_stid-le 9) (The following ensures that 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)

@ Save User passwd to file user_pwlist (append the "user_pwlist" file row by row in the format of "Name: passwd" for initializing the user password)

Echo $ user_name ":" $ user_name> user_pwlist

@ Add user (create the user and assign the "users" group to create the user directory)

Adduser-G users-D/home/student/$1 $ num_class/$ user_name

@ Set quota (set the quota for this user. The maximum capacity is 20 mb and there is no limit on the number of files)

Setquota-U $ user_name 10240 20480 0 0/home

@ Set Directory mode (set the permission of the user directory to 755)

Chmod 755/home/student/$1 $ num_class/$ user_name

@ Current user ID add one (add 1 to the user ID, prepare for the next cycle, and create the next user)

Num_stid = 'expr $ num_stid + 1'

Done

Chpasswd <user_pwlist (the following two actions are used to set passwords for all users just created)

Pwconv

Rm user_pwlist-F (delete the "user_pwlist" file)

Usage:

1. Use the VI editor to write the above code line by line and save it as a file. For example, save it in the name of "addclass", and then perform the following steps.

2. # chmod + x addclass (set the execution permission for the addclass file)

3 .. #./addclass Ga 1 50 (build ga0101-ga0150 user)

Note: # indicates the prompt.

The above code is just a bit of experience from the author when using shell for system user management. Because shell can be used in combination with Linux system commands, It is very powerful in management. As far as the above Code is concerned, we only need to slightly change it to create a shell for deleting batch users, a shell for bulk user quota configuration, and a shell for batch initial user passwords. If you add a class loop statement, you can create a shell for a 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.