Recently, the author uses the Linux operating system in the construction platform of campus network to provide campus network users.
DNS, Apache, POP3, SMTP,
FTP and other services. Since the number of students in our school is more than 3,000, the author pays special attention to ensuring user management efficiency in the process of building the entire application platform. My basic ideas and practices are: use shell to build a system account for each student - providing an E-mail address (including FTP space account), and then students can re-register for the forum users with their E-mail. Based on such management ideas, the author wrote a batch of shell, practicing, proves, using Shell to effectively manage system users based on UNIX kernel.
The following is the source code and comments of the entire shell, this code is debugged on Redhat 7.2 and Turbo Linux 7.0, and uses in other versions of Linux to do appropriate modifications. Where "@" is the code annotation, "()" section is the explanation of the author for programming ideas.
@! / bin / sh (definition using a sh shell)
GroupAdd Users (establishing user group)
@grade code (defined the first parameter as the grade code, give the variable "grade")
Grade = $ 1
@class code (defined the second parameter as a class code, give the variable "num_class")
Num_class = `EXPR $ 2 0`
IF (TEST $ Num_Class -le 9) (The following code is two digits to ensure the number of classes))
THEN
Num_class = "0" $ NUM_CLASS
Fi
@max user ID (Define the third parameter for the maximum ID number of the class, give the variable "max_stid")
MAX_STID = `EXPR $ 3 0`
@INIT User ID (The class student user ID number starts from 1)
Num_stid = 1
@mkdir user home (in the "/ home" directory, building a public directory named "Student")
IF [! -X / home / student]
THEN
MKDIR / HOME / STUDENT
Fi
IF [! -X / home / student / $ 1 $ Num_Class] (established a directory named by this class in the student public directory)
THEN
MKDIR / HOME / STUDENT / $ 1 $ NUM_CLASS
Fi
While (TEST $ NUM_STID -LE $ MAX_STID) (start cycling from the user ID number until the maximum ID number)
DO
IF (Test $ Num_Stid -le 9) (The following to ensure the number of bits of student code is two)
THEN
Num_stid = "0" $ NUM_STID
Fi
User_name = $ grade $ NUM_CLASS $ Num_Stid (grade the grade, class, student ID number into user name, and assigning variable user_name)
@save user passwd to file user_pwlist (press "Name: Passwd" to add the "user_pwlist" file by line, for initialization user password)
Echo $ user_name ":" $ user_name >> user_pwlist @ add user (establish the user, 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 the user, limit the maximum capacity of 20MB, no file limit)
Setquota -u $ USER_NAME 10240 20480 0 0 / HOME
@Set Directory Mode (settings for this user directory is 755)
CHMOD 755 / HOME / STUDENT / $ 1 $ NUM_CLASS / $ USER_NAME
@Current User ID Add One (User ID number plus 1, ready next loop, establish the next user)
Num_stid = `expr $ NUM_STID 1`
DONE
Chpasswd PWCONV RM user_pwlist -f (Delete "User_Pwlist" file) About usage: 1. Write the above code section by line with the VI editor, and saved in the form of a file, such as named "addClass", and then perform the following steps. 2. #CHMOD X AddClass (Set the AddClass file as executable) 3. #. / Addclass GA 1 50 (establish a GA0101-GA0150 user) Note: # for the prompt. The above code is just a little experience when the author uses the shell to manage system users. Since the shell can be used in conjunction with Linux system commands, it is very powerful in management. For the above code, we can establish a shell of a bulk user with a slight change, we can establish the shell of the bulk user, and configure the shell of the user quota and the shell of the bulk initial user password. If you add a layer of class loop statement, you can generate a shell for establishing a grade user.