Linux用戶用戶組及權限管理


用戶和用戶組

Linux安全上下文:

  運行中的程序:進程

  進程所能夠訪問資源的權限取決於進程的運行者身份

用戶必須屬於一個且僅有一個主組(主組可以改為其他組)

私有組:創建用戶時默認指定,就是與用戶同名的組

用戶的附加組:一個用戶可以屬於零個或多個附加組

涉及的配置文件

/etc/passwd:保存用戶信息

whatis passwd

sslpasswd (1ssl)     - compute password hashes
passwd (1)           - update user's authentication tokens
passwd (5)           - password file

man 5 passwd

              name:password:UID:GID:GECOS:directory:shell
       The field are as follows:
       name        This is the user's login name.  It should not contain capital letters.
       password    This is either the encrypted user password, an asterisk (*), or the letter 'x'.  (See pwconv(8) for an explanation of 'x'.)
       UID         The privileged root login account (superuser) has the user ID 0.
       GID         This is the numeric primary group ID for this user.  (Additional groups for the user are defined in the system group  file;  see
                   group(5)).
        GECOS       This  field  (sometimes  called the "comment field") is optional and used only for informational purposes.  Usually, it contains
                   the full username.  Some programs (for example, finger(1)) display information from this field.
                   GECOS stands for "General Electric Comprehensive Operating System", which was renamed to GCOS when GE's large  systems  division
                   was  sold  to Honeywell.  Dennis Ritchie has reported: "Sometimes we sent printer output or batch jobs to the GCOS machine.  The
                   gcos field in the password file was a place to stash the information for the $IDENTcard.  Not elegant."
       directory   This is the user's home directory: the initial directory where the user is placed after logging in.  The value in this field  is
                   used to set the HOME environment variable.
       shell       This  is  the  program  to run at login (if empty, use /bin/sh).  If set to a nonexistent executable, the user will be unable to
                   login through login(1).  The value in this field is used to set the SHELL environment variable.

 

/etc/shadow:保存用戶密碼(加密形式)

 Each line of this file contains 9 fields, separated by colons (:), in the following order:
       login name
           It must be a valid account name, which exist on the system.
       encrypted password
           Refer to crypt(3) for details on how this string is interpreted.
           If the password field contains some string that is not a valid result of crypt(3), for instance ! or *, the user will not be able to use
           a unix password to log in (but the user may log in the system by other means).
           This field may be empty, in which case no passwords are required to authenticate as the specified login name. However, some applications
           which read the /etc/shadow file may decide not to permit any access at all if the password field is empty.
           A password field which starts with a exclamation mark means that the password is locked. The remaining characters on the line represent
           the password field before the password was locked.
       date of last password change
           The date of the last password change, expressed as the number of days since Jan 1, 1970.
           The value 0 has a special meaning, which is that the user should change her pasword the next time she will log in the system.
           An empty field means that password aging features are disabled.
       minimum password age
           The minimum password age is the number of days the user will have to wait before she will be allowed to change her password again.
           An empty field and value 0 mean that there are no minimum password age.
       maximum password age
           The maximum password age is the number of days after which the user will have to change her password.
           After this number of days is elapsed, the password may still be valid. The user should be asked to change her password the next time she
           will log in.
           An empty field means that there are no maximum password age, no password warning period, and no password inactivity period (see below).
           If the maximum password age is lower than the minimum password age, the user cannot change her password.
       password warning period
           The number of days before a password is going to expire (see the maximum password age above) during which the user should be warned.
           An empty field and value 0 mean that there are no password warning period.
       password inactivity period
           The number of days after a password has expired (see the maximum password age above) during which the password should still be accepted
           (and the user should update her password during the next login).
           After expiration of the password and this expiration period is elapsed, no login is possible using the current user's password. The user
           should contact her administrator.
           An empty field means that there are no enforcement of an inactivity period.
       account expiration date
           The date of expiration of the account, expressed as the number of days since Jan 1, 1970.
           Note that an account expiration differs from a password expiration. In case of an acount expiration, the user shall not be allowed to
           login. In case of a password expiration, the user is not allowed to login using her password.
           An empty field means that the account will never expire.
           The value 0 should not be used as it is interpreted as either an account with no expiration, or as an expiration on Jan 1, 1970.
        reserved field
           This field is reserved for future use.

/etc/login.defs:用戶屬性

#
# Please note that the parameters in this configuration file control the
# behavior of the tools from the shadow-utils component. None of these
# tools uses the PAM mechanism, and the utilities that use PAM (such as the
# passwd command) should therefore be configured elsewhere. Refer to
# /etc/pam.d/system-auth for more information.
#
# *REQUIRED*
#   Directory where mailboxes reside, _or_ name of file, relative to the
#   home directory.  If you _do_ define both, MAIL_DIR takes precedence.
#   QMAIL_DIR is for Qmail
#
#QMAIL_DIR      Maildir
MAIL_DIR        /var/spool/mail
#MAIL_FILE      .mail
# Password aging controls:
#
#       PASS_MAX_DAYS   Maximum number of days a password may be used.
#       PASS_MIN_DAYS   Minimum number of days allowed between password changes.
#       PASS_MIN_LEN    Minimum acceptable password length.
#       PASS_WARN_AGE   Number of days warning given before a password expires.
#
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
#
# Min/max values for automatic uid selection in useradd
#
UID_MIN                  1000
UID_MAX                 60000
# System accounts
SYS_UID_MIN               201
SYS_UID_MAX               999
#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN                  1000
GID_MAX                 60000
# System accounts
SYS_GID_MIN               201
SYS_GID_MAX               999
#
# If defined, this command is run when removing a user.
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD    /usr/sbin/userdel_local
#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
#
CREATE_HOME     yes
# The permission mask is initialized to this value. If not specified,
# the permission mask will be initialized to 022.
UMASK           077
# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes
# Use SHA512 to encrypt password.
ENCRYPT_METHOD SHA512

/etc/defaults/useradd:默認的useradd配置文件(使用useradd時進行的默認操作)

# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

/etc/group

The /etc/group file is a text file that defines the groups on the system.  There is one entry per line, with the following format:
              group_name:password:GID:user_list
       The fields are as follows:
       group_name  the name of the group.
       password    the (encrypted) group password.  If this field is empty, no password is needed.
       GID         the numeric group ID.
       user_list   a list of the usernames that are members of this group, separated by commas.

/etc/gshadow

新建用戶之后,系統默認進行的操作

 在/etc/passwd中添加用戶信息

如使用passwd命令創建密碼,密碼會被加密保存在/etc/shadow中

為用戶hjm創建家目錄/home/hjm

將/etc/skel/下的所有文件復制到hjm的家目錄

創建與用戶名相同的hjm組,hjm用戶默認屬於hjm同名組

hjm組的信息保存到/etc/group中

 

useradd命令

 useradd [options] LOGIN


       -b, --base-dir BASE_DIR
           The default base directory for the system if -dHOME_DIR is not specified.  BASE_DIR is concatenated with the account name to define the home directory.
           The BASE_DIR must exist otherwise the home directory cannot be created.
           If this option is not specified, useradd will use the base directory specified by the HOME variable in /etc/default/useradd, or /home by default.

       -d, --home-dir HOME_DIR
           The new user will be created using HOME_DIR as the value for the user's login directory. The default is to append the LOGIN name to BASE_DIR and use
           that as the login directory name.

       -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
           A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace.
           The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group.

       -s, --shell SHELL
           The name of the user's login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by
           the SHELL variable in /etc/default/useradd, or an empty string by default.
 -r, --system
           Create a system account.
           System users will be created with no aging information in /etc/shadow, and their numeric identifiers are chosen in the SYS_UID_MIN-SYS_UID_MAX range,
           defined in /etc/login.defs, instead of UID_MIN-UID_MAX (and their GID counterparts for the creation of groups).
           Note that useradd will not create a home directory for such an user, regardless of the default setting in /etc/login.defs (CREATE_HOME). You have to
           specify the -m options if you want a home directory for a system account to be created.

       -N, --no-user-group
           Do not create a group with the same name as the user, but add the user to the group specified by the -g option or by the GROUP variable in
           /etc/default/useradd.
           The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs.
 
常見用法:
批量創建用戶
cat > users.txt << EOF
>hjm01
>hjm02
>hjm03
>EOF
newusers users.txt
批量修改口令
cat > pass.txt << EOF
>hjm01:001 (用戶名:密碼)
>hjm02:002
>hjm03:003
>EOF
cat pass.txt | chpasswd
getent = cat /etc/passwd
創建hjm用戶,並加入到group1,group2附加組中,指定新的家目錄,指定登錄的shell
useradd -G group1,group2 -d /tmp -s /bin/sh hjm
修改用戶名
usermod -l newname oldname
將hjm用戶加入到group3附屬組中,原組保留
usermod -a -G group3 hjm
鎖定用戶;解鎖用戶
usermod -L hjm;usermod -U hjm
刪除用戶及其家目錄
userdel -r hjm
 權限管理
-rw-r--r--. 1 root root 0 Oct 20 11:50 f1
令牌token
用戶在登錄Linux系統時,輸入賬號密碼驗證成功后,系統會返回一個token令牌,當用戶在訪問系統資源時(比如文件),系統會根據token所擁有的權限來進行識別。
用戶登錄是,獲取令牌,如果想要得到新的令牌,要重新登陸系統
 
對於文件
按照屬主、屬組、other的順序匹配,一旦匹配到,就不再看后面的權限(匹配到屬主,則不看屬組、other的權限)
對於root用戶,rw權限沒有限制,x權限有限制
對於普通用戶,rwx權限都有嚴格限制
普通用戶可以修改組的權限,前提是這個用戶在組里
刪除文件的權限由文件所在的文件夾的寫權限決定
對於目錄
r:可以進入目錄,並看到文件列表
w:可以刪除文件,前提是有x權限
x:在有r權限的前提下,可以cd進去,可以查看文件屬性,不能查看內容
參考f1的權限來設置f2的權限
chmod --reference=f1 f2
默認權限及掩碼
對於目錄
公式:umask+default=777
對於文件
公式:umask+default=666 得到的結果是奇數則加一,偶數不變
放在配置文件中永久生效
 特殊權限:SUID(4) SGID(2) Sticky(1)
SUID:繼承二進制程序所有者的權限
 
chmod u+s /usr/bin/passwd
chmod 4777 /usr/bin/passwd
 
 
SGID:
(1)繼承二進制程序所有組的權限
(2)作用於目錄,此目錄新建的文件繼承目錄的所屬組
chmod g+s /usr/bin/passwd
chmod 2777 /usr/bin/passwd
Sticky:作用於目錄,此目錄的文件只有被所有者刪除(自己只能刪除自己的文件)
chmod o+t /dir1
chmod 1777 /dir1
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM