Adding a Linux general user as a system administrator is very simple in a powerful and complete desktop environment such as GNOME or KDE, and in general there are options directly in the User-Set dialog box. However, for the sake of brevity and efficiency, I do not currently use these high-end but memory-eating "heavyweight" desktop environments, using a combination of the most basic X Windows +sawfish window manager. In this environment, user management is done through the command line. For example, use the useradd command to add a new user. However, the user added by the useradd command only has the privileges of the general user and does not have the ability to administer the system. This makes it inconvenient for common operations, such as using the sudo command to temporarily upgrade to an administrator, burn a disc, access a Bluetooth device, and so on. This behavior is caused by the fact that the user generated by the useradd command does not belong to some critical system management groups by default, such as:
ADM dialout Fax cdrom floppy tape sudo audio dip video plugdev Netdev bluetooth lpadmin fuse scanner Powerdev burning
To do this, you can simply add users to these groups by using the usermod command. But every time this operation is more inconvenient, so you can write a script to do this automatically. The script design features are:
- Define the groups listed above as the list of system administrators groups.
- The script program can add the user specified on the command line to each group. If you do not specify a user name, the currently logged on user is added to the group. Note that the currently logged on user name can be queried using the whoami command.
- The function add_to_groupsis defined in the script. The first parameter is the user name to be added to the administrator, and the second and all subsequent parameters are listed above for the Administrators group. The function checks whether the specified user is already in the Administrators group. If not, use the usermod command to add it to the group.
Based on your own bash script template, the script written in add_admin.sh is as follows:
#!/bin/Bashscript_name="add_admin.sh"Script_usage=$(Cat<<Eof$script_name [USER name]eof] Script_function=$(Cat<<eofthis script is used to add the current or specified users as system administrator. EOF) Script_doc=$(Cat<<EOF-h Display this help. EOF) Script_examples=$(Cat<<eofeof) State_prefix="==="Warning_prefix="***"Error_prefix="!!!"functionDisplay_help () {if[-N"$script _usage"]; Then Echo-E"Usage: $script _usage" fi if[-N"$script _function"]; Then Echo-E"$script _function" fi if[-N"$script _doc"] ; Then Echo-E"\n$script_doc" fi if[-N"$script _examples"]; Then Echo-E"\nexamples" Echo-E"$script _examples" fi}functionadd_to_groups () {The_user=" $" Shift 1 forThe_groupinch "[email protected]"; Do if[-N"' cat/etc/group | grep $the _group '"]; Then if[-N"' groups $the _user | grep $the _group | cut-d ': '-F 2 '"]; Then Echo "$warning _prefix User ' $the _user ' have already been in the group ' $the _group '!" Else sudoUsermod-a-G $the _group $the _userEcho "$state _prefix User ' $the _user ' have been added to the group ' $the _group '!" fi Else Echo "$warning _prefix The group ' $the _group ' does not exist!" fi Done}# Process Command Options whileGetopts": H"Opt Do Case$optinchh) display_help exit0 ;; \?) Display_help Exit1 ;; Esac DoneShift$ ($OPTIND-1)) Admin_groups="adm dialout Fax cdrom floppy tape sudo audio dip video plugdev Netdev bluetooth lpadmin fuse scanner Powerdev burning< /c0>"# Start Execute the commandif[$OSTYPE ='Linux-gnu']; Then# Get The user nameif[-N"$*"]; Then forThe_userinch "[email protected]"; Do if[-N"' cat/etc/passwd | grep $the _user | cut-d ': '-F 1 '"]; Thenadd_to_groups $the _user $admin _groupsEcho "$state _prefix User ' $the _user ' has been set as administrator!" Else Echo "$warning _prefix ' $the _user ' is not a valid user!" fi Done ElseThe_user=`WhoAmI` Echo "$state _prefix The current logged-on user ' $the _user ' would be set as administrator!"add_to_groups $the _user $admin _groupsEcho "$state _prefix User ' $the _user ' has been set as administrator!" fiExit0fiEcho "$warning _prefix Operating system or host name is not supported!"
Use bash scripts to add Linux normal users as system administrators