Operating system Knowledge (4)--linux User and user group management

Source: Internet
Author: User
Tags readable

Linux system is a multi-user multi-tasking time-sharing system, any user who wants to use system resources must first request an account from the system administrator and then enter the system as this account.

On the one hand, the user's account can help the system administrator to track the users who use the system and control their access to system resources, and on the other hand can help users organize files and provide security protection for users.

Each user account has a unique user name and a separate password.

When a user types the correct user name and password at logon, they are able to enter the system and their home directory.

To achieve the management of user accounts, the main tasks to be completed are as follows:

  • Add, delete and modify user accounts.
  • Management of user passwords.
  • Management of user groups.
  • I. Management of Linux system user accounts

    The management of user accounts mainly involves the addition, modification and deletion of user accounts.

    To add a user account is to create a new account in the system, and then assign the user number, user group, home directory, and login shell resources to the new account. The account you just added is locked and cannot be used.

    1. Add a new user account using the Useradd command with the following syntax:

    useradd 选项 用户名

    Parameter description:

    Options:

  • -C Comment Specifies an annotative description.
  • The-D directory specifies the home directory, and if this directory does not exist, the-m option can be used to create the master directory.
  • The-G user group specifies the user group to which the user belongs.
  • -G user Group, user group specifies the additional group to which the user belongs.
  • -S Shell file specifies the user's login shell.
  • -u user number specifies the user's user number, and if you have the-o option, you can reuse the other user's identification number.
  • User name:

  • Specify the login name for the new account.
  • Example 1

    # useradd –d /usr/sam -m sam

    This command creates a user Sam, where the-D and-m options are used to generate a home directory for the login Sam/usr/sam (/usr is the parent directory where the default user home directory resides).

    Example 2

    # useradd -s /bin/sh -g group –G adm,root gem

    This command creates a new user gem, which is the login shell of/bin/sh, which belongs to the group user groups, and also to the ADM and root user groups, where group user groups are their primary groups.

    There may be a new group: #groupadd Group and Groupadd ADM

    Add user account is in the/etc/passwd file to add a record for the new user, while updating other system files such as/etc/shadow,/etc/group and so on.

    Linux provides an integrated system management tool, userconf, which can be used to manage user accounts uniformly.

    2. Delete Account

    If a user's account is no longer in use, it can be removed from the system. Deleting a user account is to delete the user record in the system files such as/etc/passwd, and delete the user's home directory if necessary.

    Delete an existing user account using the Userdel command, in the following format:

    userdel 选项 用户名

    The common option is-r, which is to remove the user's home directory.

    For example:

    # userdel sam

    This command removes the user Sam's records in the system files (mainly/etc/passwd,/etc/shadow,/etc/group, etc.) while deleting the user's home directory.

    3, modify the account

    Modify user account is based on the actual situation to change the user's relevant attributes, such as user number, home directory, user group, login shell and so on.

    Modify the information for an existing user using the Usermod command, which is in the following format:

    usermod 选项 用户名

    Common options include-C,-D,-M,-G,-G,-S,-u, and-O, etc., which have the same meaning as the options in the Useradd command, specifying a new resource value for the user.

    In addition, some systems can use the option:-L New user name

    This option specifies a new account that will change the original user name to the new one.

    For example:

    # usermod -s /bin/ksh -d /home/z –g developer sam

    This command modifies the user Sam's login shell to Ksh, the home directory to/home/z, and the user group to developer.

    4, the User password management

    An important part of user management is the management of user passwords. The user account has just been created without a password, but is locked by the system, cannot be used, it must be given a password before it can be used, even if a blank password is specified.

    The shell command that specifies and modifies the user's password is passwd. A superuser can specify a password for himself and another user, and a normal user can only use it to modify his or her password. The format of the command is:

    passwd 选项 用户名

    Options available for use:

  • -L locks the password, which disables the account.
  • -u password to unlock.
  • -D make the account no password.
  • -F forces the user to modify the password the next time they log on.
  • If the default user name, the password for the current user is modified.

    For example, assuming the current user is Sam, the following command modifies the user's own password:

    $ passwd Old password:****** New password:******* Re-enter new password:*******

    If you are a superuser, you can specify the password for any user in the following form:

    # passwd sam New password:******* Re-enter new password:*******

    Ordinary users to modify their own password, the passwd command will first ask the original password, verify and then ask the user to enter two times the new password, if the password two times the same, the password is assigned to the user, and the superuser to specify a password for the user, you do not need to know the original password.

    For system security, the user should choose a more complex password, for example, preferably with a 8-bit long password, the password contains uppercase, lowercase letters and numbers, and should be different from name, birthday, and so on.

    When you specify an empty password for a user, the following forms of command are executed:

    # passwd -d sam

    This command removes the password for the user Sam so that the next time the user Sam logs on, the system will no longer ask for the password.

    The passwd command can also lock a user with the-l (lock) option so that it cannot log on, for example:

    # passwd -l sam
    II. Management of Linux system user groups

    Each user has a user group, and the system can centrally manage all users in a single user group. Different Linux systems provide a different set of user groups, as users under Linux belong to a user group with the same name, which is created at the same time as the user is created.

    The management of user groups involves adding, deleting, and modifying user groups. The addition, deletion, and modification of a group is actually an update to the/etc/group file.

    1. Add a new user group using the Groupadd command. The format is as follows:

    groupadd 选项 用户组

    The options you can use are:

  • -G GID Specifies the group identification number (GID) of the new user group.
  • -O is commonly used with the-G option, which means that the GID of the new user group can be the same as the GID of the user group already in the system.
  • Example 1:

    # groupadd group1

    This command adds a new group group1 to the system, and the group identification number of the new group is added 1 on the basis of the currently existing maximum group identification number.
    Example 2:

    # groupadd -g 101 group2

    This command adds a new group group2 to the system, specifying that the group identification number for the new group is 101.

    2. If you want to delete an existing user group, use the Groupdel command, which has the following format:

    groupdel 用户组

    For example:

    # groupdel group1

    This command removes the group group1 from the system.

    3. Modify the properties of the user group using the groupmod command. Its syntax is as follows:

    groupmod 选项 用户组

    The common options are:

  • -G GID Specifies a new group identification number for the user group.
  • The-O is used in conjunction with the-G option, and the user group's new GID can be the same as the GID of the user group already in the system.
  • -N New user Group change user group name to new name
  • Example 1:

    # groupmod -g 102 group2

    This command modifies the group ID number of group group2 to 102.

    Example 2:

    # groupmod –g 10000 -n group3 group2

    This command changes the identification number of the group group2 to 10000 and the group name to Group3.

    4. If a user belongs to more than one user group at the same time, the user can switch between groups of users in order to have permissions from other user groups.

    After logging in, the user can switch to another user group using the command Newgrp, which is the target user group. For example:

    $ newgrp root

    This command switches the current user to the root user group, provided that the root user group is really the user's primary or additional group. Similar to the management of user accounts, the management of user groups can also be done through integrated system management tools.

    Third, the user account related system files

    There are many ways to accomplish user-managed work, but each of these methods actually modifies the system files concerned.

    Information related to users and user groups is stored in some system files, including/etc/passwd,/etc/shadow,/etc/group, etc.
    The contents of these files are described below.

    1./etc/passwd file is one of the most important files involved in user management work.

    Each user in the Linux system has a corresponding record line in the/etc/passwd file, which records some of the basic properties of the user.

    This file is readable for all users. Its content is similar to the following example:

    # CAT/ETC/PASSWDRoot:x:0:0: Superuser:/:Daemon:x:1:1: System daemons:/etc:Bin:x:2:2: Ownerof systemCommands:/Bin:SYS:x:3:3: Ownerof systemFiles:/usr/SYS:adm:x:4:4: System Accounting:/usr/adm:UUCP:x:5:5: UUCP Administrator:/usr/lib/UUCP:Auth:x:7: +: Authentication Administrator:/tcb/files/Auth:Cron:x:9: -: Cron Daemon:/usr/spool/Cron:Listen:x:Panax Notoginseng:4: Network Daemon:/usr/net/NLS:LP:x: in: -:P Rinter Administrator:/usr/spool/LP:Sam:x: $: -: Sam San:/usr/Sam:/bin/sh

    As we can see from the above example, one row of records in/etc/passwd corresponds to a user, and each row of records is separated by a colon (:) into 7 fields, with the following format and meaning:

    用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell

  • 1) "User name" is a string representing the user account.
  • It is usually not more than 8 characters in length and consists of uppercase and lowercase letters and/or numbers. The login name cannot have a colon (:), because the colon is the delimiter here.

    For compatibility purposes, it is best not to include the dot character (.) in the login name, and to begin with a hyphen (-) and a plus sign (+).

  • 2) "Password" in some systems, stored in the encrypted user password Word.
  • Although this field contains only the user password of the encrypted string, not clear text, but because the/etc/passwd file is readable to all users, so this is still a security risk. As a result, many Linux systems (such as SVR4) now use Shadow technology to store real encrypted user passwords into/etc/shadow files, while storing only a special character, such as "X" or "*", in the password field of the/etc/passwd file.

  • 3) The user ID number is an integer that is used internally by the system to identify the user.
  • In general, it corresponds to a user name of one by one. If several user names correspond to the same user ID number, the system will treat them as the same user, but they can have different passwords, different home directories, and different login shells.

    Typically, the range of user identification numbers is 0~65 535. 0 is the root identification number of Superuser , 1~99 is reserved by the system, as an administrative account, the identification number of ordinary users starts from 100. In a Linux system, this limit is 500.

  • 4) The Group Identification Number field records the user group to which the user belongs.
  • It corresponds to a record in the/etc/group file.

  • 5) The "Annotative description" field records some of the user's personal circumstances.
  • For example, the user's real name, phone number, address, etc., this field does not have any practical use. In different Linux systems, the format of this field is not uniform. In many Linux systems, this field holds an arbitrary annotated descriptive text that is used as the output of the finger command.

  • 6) "Home Directory", which is the user's starting working directory.
  • It is the directory where the user is located after logging on to the system. In most systems, each user's home directory is organized under the same specific directory, and the user's home directory name is the user's login name. Each user has read, write, execute (search) permissions on his or her home directory, and other users ' access to this directory is set according to the specific circumstances.

  • 7) After the user logs in, to start a process, is responsible for the user's actions to the kernel, the process is the user log on to the system run after the command interpreter or a specific program, the shell.
  • The shell is the interface between the user and the Linux system. There are many types of Linux shells, each of which has different characteristics. Commonly used are sh (Bourne shell), csh (c shell), Ksh (Korn Shell), tcsh (tenex/tops-20 type C shell), bash (Bourne Again shell), etc.

    System administrators can assign a shell to a user based on system conditions and user habits. If you do not specify a shell, then the system uses SH as the default login shell, that is, the value of this field is/bin/sh.

    The user's login shell can also be specified as a specific program (this program is not a command interpreter).

    With this feature, we can restrict the user from running only the specified application, and the user exits the system automatically after the application has finished running.

    Some Linux systems require only those programs that are registered in the system to appear in this field.

  • 8) There is a class of users in the system called Pseudo-user (Psuedo users).
  • These users also occupy a record in the/etc/passwd file, but cannot log on because their logon shell is empty. Their existence is mainly to facilitate the system management, to meet the corresponding system process of the document owner requirements.

    Common pseudo-users are as follows:

    伪 用 户 含 义 bin 拥有可执行的用户命令文件 sys 拥有系统文件 adm 拥有帐户文件 uucp UUCP使用 lp lp或lpd子系统使用 nobody NFS使用
    Owning an account file

    1, in addition to the pseudo-users listed above, there are many standard pseudo-users, such as: Audit, cron, mail, Usenet, etc., they are also related to the process and files required.

    Because the/etc/passwd file is readable by all users, if the user's password is too simple or the rule is more obvious, a common computer can easily crack it, so the security requirements of the Linux system is encrypted after the password word separated out, stored in a file alone, This file is/etc/shadow file. A superuser has the ability to read the file, which guarantees the security of the user's password.

    2. The record line in/etc/shadow corresponds to one by one in/etc/passwd, which is automatically generated by the Pwconv command according to the data in the/etc/passwd.

    Its file format is similar to/etc/passwd and consists of several fields, separated by ":" Between the fields. These fields are:

    登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时间:失效时间:标志

  • "Login Name" is a user account that matches the login name in the/etc/passwd file
  • The password field holds the encrypted user password Word with a length of 13 characters. If null, the corresponding user does not have a password, the password is not required at logon, and if it contains characters that are not part of the collection {./0-9a-za-z}, the corresponding user cannot log on.
  • Last modified time represents the number of days from the time the user last modified the password. The beginning of time may not be the same for different systems. For example, in SCO Linux, the starting point for this time is January 1, 1970.
  • "Minimum time interval" refers to the minimum number of days required between changing the password two times.
  • Maximum time interval refers to the maximum number of days that a password remains valid.
  • The warning Time field represents the number of days from the beginning of the system warning user to the official expiration of the user's password.
  • "Inactivity Time" represents the maximum number of days that a user does not have a login activity but the account remains valid.
  • The "Expiration Time" field gives an absolute number of days, and if this field is used, the lifetime of the corresponding account is given. After expiry, the account is no longer a legitimate account, and can no longer be used to log on.
  • Here is an example of/etc/shadow:

    # cat /etc/shadowroot:Dnakfw28zf38w:8764:0:168:7:::daemon:*::0:0::::bin:*::0:0::::sys:*::0:0::::adm:*::0:0::::uucp:*::0:0::::nuucp:*::0:0::::auth:*::0:0::::cron:*::0:0::::listen:*::0:0::::lp:*::0:0::::sam:EkdiSECLWPdSa:9740:0:0::::

    3, all the information of the user group is stored in the/etc/group file.

    Grouping users is a means of managing and controlling access to users in a Linux system.

    Each user belongs to a group of users, a group can have multiple users, and a user can belong to a different group.

    When a user is a member of more than one group at the same time, the primary group that the user belongs to is recorded in the/etc/passwd file, which is the default group to which the login belongs, and the other groups are called additional groups.

    When a user accesses a file that belongs to an additional group, you must first use the NEWGRP command to make yourself a member of the group you want to access.

    All the information for the user group is stored in the/etc/group file. The format of this file is also similar to the/etc/passwd file, separated by a colon (:) several fields, which are:

    组名:口令:组标识号:组内用户列表

  • Group name is the name of the user group, consisting of letters or numbers. As with logins in/etc/passwd, group names should not be duplicated.
  • The password field holds the password word after the user group is encrypted. The user groups in the General Linux system do not have a password, that is, the field is generally empty or *.
  • The group ID is similar to the user identification number and is an integer that is used internally by the system to identify the group.
  • The "group user list" is a list of all users belonging to this group/b], separated by commas (,) between different users. This user group may be the user's primary group, or it may be an additional group.
  • An example of the/etc/group file is as follows:

    root  ::0   : Root  bin  ::2  : Root , bin  sys  ::3  :root , UUCP  adm  ::4  :root , adm  daemon  ::5  :root , daemon  LP  ::7  :root , LP  users  ::20  :root , sam 
    Iv. Add Volume User Batch

    Adding and removing users to every Linux system administrator is a breeze, the tricky thing is that if you want to add dozens of, hundreds or even thousands of users, we are not likely to use Useradd to add one by one, it is necessary to find a simple way to create a large number of users. The Linux system provides the tools to create a large number of users, allowing you to create a large number of users immediately, as follows:

    (1) Edit a text user file first.

    Each column according to the format of the/etc/passwd password file, note that each user's user name, UID, host directory can not be the same, where the password bar can be left blank or enter the X number. An example file user.txt the contents as follows:

    user001:: -: -: User:/home/user001:/bin/bashuser002::601: -: User:/home/user002:/bin/bashuser003::602: -: User:/home/user003:/bin/bashuser004::603: -: User:/home/user004:/bin/bashuser005::604: -: User:/home/user005:/bin/bashuser006::605: -: User:/home/user006:/bin/bash

    (2) Execute the command/usr/sbin/newusers as root, import the data from the user file user.txt that you just created, create the user:

    # newusers < user.txt

    You can then execute the command VIPW or vi/etc/passwd to check whether the/etc/passwd file already has data for those users, and whether the user's host directory has been created.

    (3) Execute command/usr/sbin/pwunconv.

    Decode the shadow password generated by/etc/shadow, then write back to/etc/passwd and delete the shadow password bar of the/etc/shadow. This is to facilitate the next step of the password conversion work, that is, the first cancellation shadow password function.

    # pwunconv

    (4) Edit the password control file for each user.

    Sample file Passwd.txt content is as follows:

    user001:密码user002:密码user003:密码user004:密码user005:密码user006:密码

    (5) Execute command/usr/sbin/chpasswd as root.

    To create a user password, CHPASSWD writes the password encoded by the/USR/BIN/PASSWD command to the/etc/passwd password bar.

    # chpasswd < passwd.txt

    (6) After you have determined that the password has been encoded into the/etc/passwd password bar.

    Executes the command/USR/SBIN/PWCONV encodes the password to shadow password and writes the result to/etc/shadow.

    # pwconv

    This completes the creation of a large number of users, after which you can check whether the permissions settings for these user host directories are correct, and log on to verify that the user's password is correct.

    Operating system Knowledge (4)--linux User and user group management

    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.