美文网首页
linux007 linux文件权限(常用命令)

linux007 linux文件权限(常用命令)

作者: caoqiansheng | 来源:发表于2020-06-21 15:10 被阅读0次

    1.添加用户 adduser & useradd

    Linux下useradd或adduser命令用来建立用户帐号和创建用户的起始目录,使用权限是超级用户。

    • adduser
    adduser [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
    [--firstuid ID] [--lastuid ID] [--gecos GECOS] [--ingroup GROUP | --gid ID]
    [--disabled-password] [--disabled-login] [--add_extra_groups]
    [--encrypt-home] USER
       Add a normal user
    
    adduser --system [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
    [--gecos GECOS] [--group | --ingroup GROUP | --gid ID] [--disabled-password]
    [--disabled-login] [--add_extra_groups] USER
       Add a system user
    
    adduser --group [--gid ID] GROUP
    addgroup [--gid ID] GROUP
       Add a user group
    
    addgroup --system [--gid ID] GROUP
       Add a system group
    
    adduser USER GROUP
       Add an existing user to an existing group
    
    general options:
      --quiet | -q      don't give process information to stdout
      --force-badname   allow usernames which do not match the
                        NAME_REGEX[_SYSTEM] configuration variable
      --extrausers      uses extra users as the database
      --help | -h       usage message
      --version | -v    version number and copyright
      --conf | -c FILE  use FILE as configuration file
    
    • useradd
    Usage: useradd [options] LOGIN
           useradd -D
           useradd -D [options]
    
    Options:
      -b, --base-dir BASE_DIR       base directory for the home directory of the new account
      -c, --comment COMMENT         GECOS field of the new account
      -d, --home-dir HOME_DIR       home directory of the new account
      -D, --defaults                print or change default useradd configuration
      -e, --expiredate EXPIRE_DATE  expiration date of the new account
      -f, --inactive INACTIVE       password inactivity period of the new account
      -g, --gid GROUP               name or ID of the primary group of the new account
      -G, --groups GROUPS           list of supplementary groups of the new account
      -h, --help                    display this help message and exit
      -k, --skel SKEL_DIR           use this alternative skeleton directory
      -K, --key KEY=VALUE           override /etc/login.defs defaults
      -l, --no-log-init             do not add the user to the lastlog and faillog databases
      -m, --create-home             create the user's home directory
      -M, --no-create-home          do not create the user's home directory
      -N, --no-user-group           do not create a group with the same name as the user
      -o, --non-unique              allow to create users with duplicate (non-unique) UID
      -p, --password PASSWORD       encrypted password of the new account  -r, --system create a system account
      -R, --root CHROOT_DIR         directory to chroot into
      -s, --shell SHELL             login shell of the new account
      -u, --uid UID                 user ID of the new account
      -U, --user-group              create a group with the same name as the user
      -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping
          --extrausers              Use the extra users database
    

    使用
    root下:useradd user1
    其他用户:sudo useradd user1
    查看linux用户列表:cat /etc/passwd

    1. 在CentOs下,useradd与adduser是没有区别的都是在创建用户,在home下自动创建目录,没有设置密码,需要使用passwd命令修改密码。
    2. 在Ubuntu下,useradd与adduser有所不同,useradd在使用该命令创建用户是不会在/home下自动创建与用户名同名的用户目录,而且不会自动选择shell版本,也没有设置密码,需要使用passwd命令修改密码。adduser在使用该命令创建用户是会在/home下自动创建与用户名同名的用户目录,系统shell版本,会在创建时会提示输入密码。

    2.删除用户 userdel & deluser

    • userdel
    Usage: userdel [options] LOGIN
    Options:
      -f, --force                   force removal of files, even if not owned by user
      -h, --help                    display this help message and exit
      -r, --remove                  remove home directory and mail spool
      -R, --root CHROOT_DIR         directory to chroot into
          --extrausers              Use the extra users database
      -Z, --selinux-user            remove any SELinux user mapping for the user
    
    • deluser
    deluser USER
      remove a normal user from the system
      example: deluser mike
      --remove-home             remove the users home directory and mail spool
      --remove-all-files        remove all files owned by user
      --backup                  backup files before removing.
      --backup-to <DIR>         target directory for the backups.
                                Default is the current directory.
      --system                  only remove if system user
    delgroup GROUP
    deluser --group GROUP
      remove a group from the system
      example: deluser --group students
      --system                  only remove if system group
      --only-if-empty           only remove if no members left
    deluser USER GROUP
      remove the user from a group
      example: deluser mike students
    general options:
      --quiet | -q      don't give process information to stdout
      --help | -h       usage message
      --version | -v    version number and copyright
      --conf | -c FILE  use FILE as configuration file
    

    3.passwd

    passwd命令用来更改用户的密码

    Usage: passwd [options] [LOGIN]
    
    Options:
      -a, --all                     report password status on all accounts
      -d, --delete                  delete the password for the named account
      -e, --expire                  force expire the password for the named account
      -h, --help                    display this help message and exit
      -k, --keep-tokens             change password only if expired
      -i, --inactive INACTIVE       set password inactive after expiration to INACTIVE
      -l, --lock                    lock the password of the named account
      -n, --mindays MIN_DAYS        set minimum number of days before password change to MIN_DAYS
      -q, --quiet                   quiet mode
      -r, --repository REPOSITORY   change password in REPOSITORY repository
      -R, --root CHROOT_DIR         directory to chroot into
      -S, --status                  report password status on the named account
      -u, --unlock                  unlock the password of the named account
      -w, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS
      -x, --maxdays MAX_DAYS        set maximum number of days before password change to MAX_DAYS
    

    4.添加用户组 groupadd

    Usage: groupadd [options] GROUP
    
    Options:
      -f, --force                   exit successfully if the group already exists,
                                    and cancel -g if the GID is already used
      -g, --gid GID                 use GID for the new group
      -h, --help                    display this help message and exit
      -K, --key KEY=VALUE           override /etc/login.defs defaults
      -o, --non-unique              allow to create groups with duplicate
                                    (non-unique) GID
      -p, --password PASSWORD       use this encrypted password for the new group
      -r, --system                  create a system account
      -R, --root CHROOT_DIR         directory to chroot into
          --extrausers              Use the extra users database
    
    adduser [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
    [--firstuid ID] [--lastuid ID] [--gecos GECOS] [--ingroup GROUP | --gid ID]
    [--disabled-password] [--disabled-login] [--add_extra_groups]
    [--encrypt-home] USER
       Add a normal user
    
    adduser --system [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
    [--gecos GECOS] [--group | --ingroup GROUP | --gid ID] [--disabled-password]
    [--disabled-login] [--add_extra_groups] USER
       Add a system user
    
    adduser --group [--gid ID] GROUP
    addgroup [--gid ID] GROUP
       Add a user group
    
    addgroup --system [--gid ID] GROUP
       Add a system group
    
    adduser USER GROUP
       Add an existing user to an existing group
    
    general options:
      --quiet | -q      don't give process information to stdout
      --force-badname   allow usernames which do not match the
                        NAME_REGEX[_SYSTEM] configuration variable
      --extrausers      uses extra users as the database
      --help | -h       usage message
      --version | -v    version number and copyright
      --conf | -c FILE  use FILE as configuration file
    

    5.权限修改

    usermod

    Usage: usermod [options] LOGIN
    
    Options:
      -c, --comment COMMENT         new value of the GECOS field
      -d, --home HOME_DIR           new home directory for the user account
      -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
      -f, --inactive INACTIVE       set password inactive after expiration
                                    to INACTIVE
      -g, --gid GROUP               force use GROUP as new primary group
      -G, --groups GROUPS           new list of supplementary GROUPS
      -a, --append                  append the user to the supplemental GROUPS
                                    mentioned by the -G option without removing
                                    him/her from other groups
      -h, --help                    display this help message and exit
      -l, --login NEW_LOGIN         new value of the login name
      -L, --lock                    lock the user account
      -m, --move-home               move contents of the home directory to the
                                    new location (use only with -d)
      -o, --non-unique              allow using duplicate (non-unique) UID
      -p, --password PASSWORD       use encrypted password for the new password
      -R, --root CHROOT_DIR         directory to chroot into
      -s, --shell SHELL             new login shell for the user account
      -u, --uid UID                 new UID for the user account
      -U, --unlock                  unlock the user account
      -v, --add-subuids FIRST-LAST  add range of subordinate uids
      -V, --del-subuids FIRST-LAST  remove range of subordinate uids
      -w, --add-subgids FIRST-LAST  add range of subordinate gids
      -W, --del-subgids FIRST-LAST  remove range of subordinate gids
      -Z, --selinux-user SEUSER     new SELinux user mapping for the user account
    

    groupmod

    Usage: groupmod [options] GROUP
    
    Options:
      -g, --gid GID                 change the group ID to GID
      -h, --help                    display this help message and exit
      -n, --new-name NEW_GROUP      change the name to NEW_GROUP
      -o, --non-unique              allow to use a duplicate (non-unique) GID
      -p, --password PASSWORD       change the password to this (encrypted)
                                    PASSWORD
      -R, --root CHROOT_DIR         directory to chroot into
    

    chmod

    Usage: chmod [OPTION]... MODE[,MODE]... FILE...
      or:  chmod [OPTION]... OCTAL-MODE FILE...
      or:  chmod [OPTION]... --reference=RFILE FILE...
    Change the mode of each FILE to MODE.
    With --reference, change the mode of each FILE to that of RFILE.
    
      -c, --changes          like verbose but report only when a change is made
      -f, --silent, --quiet  suppress most error messages
      -v, --verbose          output a diagnostic for every file processed
          --no-preserve-root  do not treat '/' specially (the default)
          --preserve-root    fail to operate recursively on '/'
          --reference=RFILE  use RFILE's mode instead of MODE values
      -R, --recursive        change files and directories recursively
          --help     display this help and exit
          --version  output version information and exit
    
    Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.
    
    GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
    Report chmod translation bugs to <http://translationproject.org/team/>
    Full documentation at: <http://www.gnu.org/software/coreutils/chmod>
    or available locally via: info '(coreutils) chmod invocation'
    

    chown

    Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
      or:  chown [OPTION]... --reference=RFILE FILE...
    Change the owner and/or group of each FILE to OWNER and/or GROUP.
    With --reference, change the owner and group of each FILE to those of RFILE.
    
      -c, --changes          like verbose but report only when a change is made
      -f, --silent, --quiet  suppress most error messages
      -v, --verbose          output a diagnostic for every file processed
          --dereference      affect the referent of each symbolic link (this is
                             the default), rather than the symbolic link itself
      -h, --no-dereference   affect symbolic links instead of any referenced file
                             (useful only on systems that can change the
                             ownership of a symlink)
          --from=CURRENT_OWNER:CURRENT_GROUP
                             change the owner and/or group of each file only if
                             its current owner and/or group match those specified
                             here.  Either may be omitted, in which case a match
                             is not required for the omitted attribute
          --no-preserve-root  do not treat '/' specially (the default)
          --preserve-root    fail to operate recursively on '/'
          --reference=RFILE  use RFILE's owner and group rather than
                             specifying OWNER:GROUP values
      -R, --recursive        operate on files and directories recursively
    
    The following options modify how a hierarchy is traversed when the -R
    option is also specified.  If more than one is specified, only the final
    one takes effect.
    
      -H                     if a command line argument is a symbolic link
                             to a directory, traverse it
      -L                     traverse every symbolic link to a directory
                             encountered
      -P                     do not traverse any symbolic links (default)
    
          --help     display this help and exit
          --version  output version information and exit
    
    Owner is unchanged if missing.  Group is unchanged if missing, but changed
    to login group if implied by a ':' following a symbolic OWNER.
    OWNER and GROUP may be numeric as well as symbolic.
    
    Examples:
      chown root /u        Change the owner of /u to "root".
      chown root:staff /u  Likewise, but also change its group to "staff".
      chown -hR root /u    Change the owner of /u and subfiles to "root".
    
    GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
    Report chown translation bugs to <http://translationproject.org/team/>
    Full documentation at: <http://www.gnu.org/software/coreutils/chown>
    or available locally via: info '(coreutils) chown invocation'
    

    相关文章

      网友评论

          本文标题:linux007 linux文件权限(常用命令)

          本文链接:https://www.haomeiwen.com/subject/mnfaxktx.html