zoukankan      html  css  js  c++  java
  • Linux命令-用户、用户组、权限

    参考资料:

    http://www.linuxidc.com/Linux/2014-07/104445.htm    Linux入门教程:如何手动创建一个Linux用户

    http://www.linuxidc.com/Linux/2012-05/60754.htm      Linux用户管理命令

    前言:

      所有用户名会在/etc/passwd文件中保存

    cat /etc/passwd 可以查看所有用户的信息,比如刚才创建的linuxidc用户
    
    
      linuxidc:x:1002:1002:linuxidc,101,186525810**,186525810**:/home/linuxidc:/bin/bash  
    
    基本格式:
      用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell,中间用:隔开
    
    注意:
    
    1.口令 这里用x 表示
    
    真正的密码放在了/etc/shadow文件,这是shadow文件里面linuxidc的密码:
    
    linuxidc:$6$v7uh0ctV$borVraMjOPhMjYV8YCVcmwylKb84djRm5yVWNbKYHxrcFLLqlSsx.hbcW.Ouk2A05CyChx7zZlRV3wZMuH0hE0:15480:0:99999:7:::  
    已经被加密了,所以比较安全。
    
    2.组标识号对应/etc/group中一行
    
    linuxidc:x:1002:
    
    group文件格式参考:
    
        /etc/group 文件文件的格式和 /etc/passwd 文件格式类似,它也是一个纯文本文件,定义了每个组中的用户。每行的格式是:
    
        group_name:passwd:GID:user_list
    
        它们的含义如下:
    
    password:组口令。此域中的口令是加密的。如果此域为空,表明该组不需要口令。

      /home/username  ==>用户主目录。一个用户会在/home/目录下存在一个用户名为文件名的文件夹

      /etc/group (用户组)

      /etc/shadow (密钥文件)(为每个用户的密码使用不同加密方式)

    使用命令创建用用户就是会在

    1、passwd文件创建含用户名的相关配置

    2、为用户生成主目录,主目录中当然也存在一些必要文件

    3、为用户指定用户组(使用adduser会自动创建username组并将username用户加入到username组中)

    4、就是设置密码还有指定登录的shell版本

    -------如果没有指定shell版本,在登陆时候不会显示前缀,只会以$开头(对于强迫症影响很大)

    正常版本  用户名@服务器名:

    创建/删除用户的方式

    adduser: 会自动为创建的用户指定主目录、系统shell版本,会在创建时输入用户密码。(属于傻瓜式操作,输入用户名密码系统会自动为你创建用户,用户组等)

    useradd:需要使用参数选项指定上述基本设置,如果不使用任何参数,则创建的用户无密码、无主目录、没有指定shell版本。

    useradd创建用户

    常用参数:  -d  指定用户主目录,一般在/home/目录下创建主目录

           -m 为用户创建主目录,需要和-d一起使用

           -s 指定登陆时的shell版本

    一般切换为root用户,再创建用户
    useradd abc  创建用户
    passwd abc 为用户设置密码

     上面只创建用户和设置密码,useradd abc 会自动会为用户创建主目录。

    接下来看带参数创建用户

    useradd -md /home/abc -s /bin/bash abc 指定主目录,指定shell版本
    passwd abc
    userdel abc //删除用户 这只是简单删除命令

    userdel删除用户

    userdel: 
    功能:删除用户 
    选项: 
    -r:删除用户的同时、home目录的用户文件一并删除 

    -f: 强制删除用户  ,这个不会删除用户的家目录,但家目录的所有者只会显示为数字id,而不是用户名(这个会导致系统不一致,

      这个选项不会断开 该用户已有的 SSH 链接。因此,即使用户已经不存在,

      但实际上仍是登录状态,并且是活跃用户。但是当用户登出后不可再登录,因为用户已经被删除。)

    删除用户之前不需要将用户从用户组中移除出去
    For example
    删除tom用户并将其家目录一并删除:
    # userdel -r tom

    有些时候用户已登录,会提示删除不了

     userdel: user ceshi is currently used by process 8335
    有多种方法可以解决
    调用  
    ps ax|grep 
    用户名  查看用户所运行的进程
    然后调用kill 进程号,关闭所有进程后在删除就没问题了
     
     

    将用户加入/移除到用户组

    使用usermod命令

    usermod -a -G root abc   将用户abc加入到root用户组中,这样abc用户就具有的该组的用户所具有的权限
    usermod -g root abc   将abc的主要用户组改为root用户组
    gpasswd -d user group 将一个用户从某个组中删除,这个时候需要保证 group 不是 user 的主组。

    注意:

    usermod -G groupname username(这个会把用户从其他组中去掉)

    usermod -a  -G groupname username(加参数a不会移除用户以加入的用户组)

    从组中删除用户

    编辑/etc/group 找到GROUP1那一行,删除 A
    或者用命令
    gpasswd -d A GROUP

    创建/删除用户组

    也可以在新建用户时候新建用户组

    useradd -g 新用户组名 用户名   //使用-g参数来创建用户组

    直接创建/删除用户组

    groupadd 用户组名  //增加用户组
    groupdel 用户组名    //删除用户组

     权限:

    -rw-rw-r-- 1 zwh zwh  1910 Jan  4 03:09 client.py

    1-3位数字代表文件所有者的权限,4-6位数字代表同组用户的权限,7-9数字代表其他用户的权限。 

    权限:r, w, x   对应 读、写、可执行

    r对应数字4,w对应2,x对应1

    通过4、2、1的组合,得到以下几种权限:0(没有权限);4(读取权限);5(4+1 | 读取+执行);6(4+2 | 读取+写入);7(4+2+1 | 读取+写入+执行)

    rwx:可读可学可执行
     r--:只读
     r-x:读和执行
     ---:无权限

    八进制表示:(可忽略。。) 
          0 000 ---:无权限
          1 001 --x: 执行
          2 010 -w-: 写
          3 011 -wx: 写和执行
          4 100 r--: 只读
          5 101 r-x: 读和执行
          6 110 rw-: 读写
          7 111 rwx: 读写执行

    给文件设置权限 chmod 权限 文件名

    chmod也可以用数字来表示权限如 chmod 777 file
    语法为:chmod abc file
    其中a,b,c各为一个数字,分别表示User、Group、及Other的权限。
    r=4,w=2,x=1
    若要rwx属性则4+2+1=7;
    若要rw-属性则4+2=6;
    若要r-x属性则4+1=7。
    范例:
    chmod a=rwx file

    chmod 777 file
    效果相同
    chmod ug=rwx,o=x file

    chmod 771 file
    效果相同
    若用chmod 4755 filename可使此程序具有root的权限

    chmod命令详细用法


    总结 

    用户管理命令:useradd, userdel, usermod, passwd, chsh, chfn, finger, id, chage

    • finger: 查看用户帐号信息
    • usermod:修改用户帐号属性
    • chsh: 修改用户的默认shell
    • chfn:修改注释信息
    • passwd:密码管理

    组管理命令:groupadd, groupdel, groupmod, gpasswd

    • gpasswd:为组设定密码

    权限管理:chown, chgrp, chmod, umask

    • chown: 改变文件属主
    • chgrp:改变文件属组
    • chmod: 修改文件的权限

    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
  • 相关阅读:
    HDU 5912 Fraction (模拟)
    CodeForces 722C Destroying Array (并查集)
    CodeForces 722B Verse Pattern (水题)
    CodeForces 722A Broken Clock (水题)
    CodeForces 723D Lakes in Berland (dfs搜索)
    CodeForces 723C Polycarp at the Radio (题意题+暴力)
    CodeForces 723B Text Document Analysis (水题模拟)
    CodeForces 723A The New Year: Meeting Friends (水题)
    hdu 1258
    hdu 2266 dfs+1258
  • 原文地址:https://www.cnblogs.com/gne-hwz/p/8259421.html
Copyright © 2011-2022 走看看