zoukankan      html  css  js  c++  java
  • 【linux】用户与组

    一、用户和组的基本概念                                              

    1、用户  

      用户:用于获取计算机资源或服务的标识符,比如用户名。计算机处理的是UID,用户名和UID的关系存储在/etc/passwd文件中,每个用户都有权限访问这个文件。

      用户类别:

    • 管理员。管理员的UID为0
    • 普通用户(系统用户和一般用户)。普通用户的UID 1-65535,一般用户500-60000。
    • 系统用户。系统用户1-499 。一登陆就运行的后台进程(服务)以普通用户的身份来运行,叫做系统用户。系统用户只是为了运行某一类进程,这类用户不需要登陆系统。

     2、组

      容器(用户组): 也是一个标识符,用于关联权限,方便地指派权限,每一个组都有一个GID。

      组类别:

    • 普通组(系统组和一般组)
    • 管理员组

     二、/etc/passwd                                                 

    复制代码
    [root@hao ~]# cat /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    ...
    hao:x:500:500:haohao:/home/hao:/bin/bash
    复制代码

    字段解析:

      1、account: 登录名

      2、password:密码。 如果是x则表示是密码占位符,真正的密码在/etc/shadow中

      3、UID:用户ID

      4、GID:基本组ID

      5、GECOS:用户注释信息

      6、directory:家目录

      7、shell:用户默认shell。当前系统上所有的合法shell存在/etc/shells。

    复制代码
    [root@hao ~]# cat /etc/shells
    /bin/sh
    /bin/bash
    /sbin/nologin
    /bin/dash
    /bin/tcsh
    /bin/csh
    复制代码

    三、/etc/group                                                       

    [root@hao ~]# cat /etc/group
    root:x:0:
    bin:x:1:bin,daemon
    ...
    hao:x:500:
    guang:x:501:

    字段解释:

      1.组名

      2.密码占位符

      3.GID

      4.以这个组为附加组的用户列表

    四、用户管理                                                             

    1、useradd(adduser)添加用户

    复制代码
    [root@hao ~]# which useradd
    /usr/sbin/useradd
    [root@hao ~]# ls -l $(which useradd)
    -rwxr-x---. 1 root root 103096 Dec  8  2011 /usr/sbin/useradd
    [root@hao ~]# which adduser
    /usr/sbin/adduser
    [root@hao ~]# ls -l $(which adduser)
    lrwxrwxrwx. 1 root root 7 Jul 26 20:14 /usr/sbin/adduser -> useradd###useradd的软连接符号adduser
    复制代码

    命令格式:useradd [options] username

    复制代码
    [root@hao ~]# useradd guang
    [root@hao ~]# tail -1 /etc/passwd
    guang:x:501:501::/home/guang:/bin/bash
    [root@hao ~]# tail -1 /etc/group
    guang:x:501:
    [root@hao ~]# tail -1 /etc/shadow
    guang:!!:16377:0:99999:7:::
    [root@hao ~]# passwd guang
    Changing password for user guang.
    New password: 
    BAD PASSWORD: it does not contain enough DIFFERENT characters
    BAD PASSWORD: is a palindrome
    Retype new password: passwd: all authentication tokens updated successfully.
    [root@hao ~]# tail -1 /etc/shadow
    guang:$6$0ndd3rOB$OP74KA4BocxxtU87tDCUE/JGHlZbygHML9KnabirIXLJ8yQJAGdP7setenuZP1fVcOK2ZhhK05aUWYqdDBcjG/:16377:0:99999:7:::
    复制代码

    命令选项:

      -u:指定UID,须保证该UID未使用。如果未使用该选项,用户UID默认为/etc/passwd下的最大用户UID的下一个

    [root@hao ~]# useradd -u 1000 testuser
    [root@hao ~]# tail -1 /etc/passwd
    testuser:x:1000:1000::/home/testuser:/bin/bash

      -g:指定基本组,如果不使用该选项则默认添加一个和用户同名的基本组,如果使用该选项则一定保证该组名存在。

    [root@hao ~]# useradd -g mygroup testusergroup
    [root@hao ~]# tail -1 /etc/passwd
    testusergroup:x:1001:502::/home/testusergroup:/bin/bash

      -G:指定额外组(附加组),可以有多个。

    复制代码
    [root@hao ~]# useradd -G mygroup testuser1
    [root@hao ~]# tail -1 /etc/passwd
    testuser1:x:1002:1002::/home/testuser1:/bin/bash
    [root@hao ~]# tail /etc/group
    webalizer:x:67:
    tomcat:x:91:
    sshd:x:74:
    tcpdump:x:72:
    slocate:x:21:
    hao:x:500:
    guang:x:501:
    mygroup:x:502:testuser1
    testuser:x:1000:
    testuser1:x:1002:
    复制代码

      -c:指定注释信息

      -d:指定家目录 默认为在/home/下创建一个跟用户同名的目录为家目录。

    [root@hao ~]# useradd -c "haohao" -d /home/haohao testuser2
    [root@hao ~]# tail -1 /etc/passwd
    testuser2:x:1003:1003:haohao:/home/haohao:/bin/bash

      -s:指定shell路径。该路径需要出现在/etc/shells。默认为/bin/bash

      -r:添加系统用户。即UID在1-500之间,而且系统用户不允许登陆系统,且不存在家目录,即使路径存在。该选项通常单独使用。

    [root@hao ~]# useradd -r testr
    [root@hao ~]# tail -1 /etc/passwd
    testr:x:496:493::/home/testr:/bin/bash
    [root@hao ~]# ls /home
    guang  hao  haohao  testuser  testuser1 

      添加用户的默认信息保存在/etc/default/useradd

    复制代码
    [root@hao ~]# file /etc/default/useradd
    /etc/default/useradd: ASCII text
    [root@hao ~]# cat /etc/default/useradd
    # useradd defaults file
    GROUP=100
    HOME=/home
    INACTIVE=-1
    EXPIRE=
    SHELL=/bin/bash
    SKEL=/etc/skel
    CREATE_MAIL_SPOOL=yes
    复制代码

    2、userdel删除用户

    命令格式:userdel  [option] username 删除用户时,若未指定选项,其家目录不会被删除

    命令选项:

      -r:删除用户同时删除用户的家目录

    复制代码
    [root@hao ~]# ls /home
    guang  hao  haohao  testuser  testuser1  testusergroup
    [root@hao ~]# userdel testuser
    [root@hao ~]# ls /home
    guang  hao  haohao  testuser  testuser1  testusergroup
    [root@hao ~]# userdel -r testusergroup
    [root@hao ~]# ls /home
    guang  hao  haohao  testuser  testuser1
    复制代码

    3、id 查看用户的UID等相关信息

    命令格式:id [option] [username] 

    命令选项:

      -u:查看UID

    [root@hao ~]# id -u testuser1
    1002

      -g:查看基本组

    [root@hao ~]# id -g testuser1
    1002

      -G:查看其附加组和基本组

    [root@hao ~]# id -G testuser1
    1002 502

      -n:显示名字而不是ID

    [root@hao ~]# id -u -n testuser1
    testuser1
    [root@hao ~]# id -g -n testuser1
    testuser1
    [root@hao ~]# id -G -n testuser1
    testuser1 mygroup

     4、finger查看用户账号信息

    命令格式: finger username

    5、usermod修改用户账号属性

    命令格式:usermod [option] username。用法同useradd,

    命令选项:

      -u:更改用户uid

      -g:更改用户GID

      -G:更改用户附加组,即覆盖原有的附加组。一般同-a选项一同使用,即添加用户到新的附加组,原来的附加组不被覆盖。

      -c:更改注释信息

      -d:更改家目录。如果原来家目录下已有该用户的文件夹,原家目录下的文件夹不会被迁移到新的家目录下。一般和-m选项一同使用,则可以使其原家目录下文件  迁移到新的家目录下。

      -s:更改shell。

      -l:更改用户名

      -L:锁定账号

      -U:解锁用户

    6、chsh(change shell)修改用户shell

    复制代码
    root@hao ~]# finger testuser1
    Login: testuser1                  Name: 
    Directory: /home/testuser1              Shell: /bin/bash
    Never logged in.
    No mail.
    No Plan.
    [root@hao ~]# chsh testuser1
    Changing shell for testuser1.
    New shell [/bin/bash]: /bin/tcsh                    
    Shell changed.
    [root@hao ~]# finger testuser1
    Login: testuser1                  Name: 
    Directory: /home/testuser1              Shell: /bin/tcsh
    Never logged in.
    No mail.
    No Plan.
    复制代码

    7、chfn(change finger)修改注释信息

    复制代码
    [root@hao ~]# chfn testuser1
    Changing finger information for testuser1.
    Name []: test
    Office []: test
    Office Phone []: 110
    Home Phone []: 110
    
    Finger information changed.
    [root@hao ~]# tail -2 /etc/passwd
    testuser1:x:1002:1002:test,test,110,110:/home/testuser1:/bin/tcsh
    testuser2:x:1003:1003:haohao:/home/haohao:/bin/bash 
    复制代码

    8、 passwd 修改用户密码

    普通用户只能使用不带参数的该命令更改自己的密码,管理员可以使用该命令更改其他用户的密码

    命令格式:passwd [option][username]

    root@hao:~# passwd user1
    Enter new UNIX password: 
    Retype new UNIX password: 
    passwd: password updated successfully

    命令选项:

      --stdin(standard in ):从标准输入读取密码。经测试Ubuntu没有该参数,可以使用chpasswd命令替换,chpasswd username password,均可以用echo输出指  定的信息,然后管道给这两个命令处理,如:

    root@hao:~# echo "newpassword"|passwd user1 --stdin 
    root@hao:~# ehco "newpassword" |chpasswd user1

      -l:锁定用户账号

      -u:解锁用户

      -d:删除密码

    root@hao:~# tail -1 /etc/shadow
    user1:$6$eKF3mI4G$O3pzjIQiRodxGRw9c4SVUAQ34oi8vOMcguGW4/7LBTYK2ADppH/sPVtqsjKP9ajZamXU8SmZgtm/uWKTtaxam/:16362:0:99999:7:::
    root@hao:~# passwd -d user1
    passwd: password expiry information changed.
    root@hao:~# tail -1 /etc/shadow
    user1::16362:0:99999:7:::

    9、pwck(password check)检查用户完整性

    复制代码
    [root@hao ~]# pwck
    user 'adm': directory '/var/adm' does not exist
    user 'uucp': directory '/var/spool/uucp' does not exist
    user 'gopher': directory '/var/gopher' does not exist
    user 'ftp': directory '/var/ftp' does not exist
    user 'avahi-autoipd': directory '/var/lib/avahi-autoipd' does not exist
    user 'saslauth': directory '/var/empty/saslauth' does not exist
    user 'pulse': directory '/var/run/pulse' does not exist
    pwck: no changes
    复制代码

    五、组管理                                                                  

    1、groupadd创建组

    命令格式:groupadd [options] groupname

    命令选项:

      -g:指定GID

      -r:添加系统组

    [root@hao ~]# groupadd  -r nginx
    [root@hao ~]# tail -1 /etc/group
    nginx:x:492:

    2、groupmod修改组属性

    命令格式:groupmod [options] groupname

    命令选项:

      -g:修改GID

      -n:修改groupname

    3、groupdel删除一个组

    4、gpasswd给组加密码

    命令格式:gpasswd groupname 

    用户创建一个文件,该文件的属主为该文件,属组为该用户所在的基本组

    复制代码
    [root@hao ~]# useradd hadoop
    [root@hao ~]# su - hadoop
    [hadoop@hao ~]$ cd /tmp
    [hadoop@hao tmp]$ touch a.hadoop
    [hadoop@hao tmp]$ ll
    total 40
    -rw-rw-r--. 1 hadoop hadoop    0 Nov  4 12:09 a.hadoop
    drwx------. 2 hao    hao    4096 Aug  6 15:58 keyring-4BcUXT
    drwx------. 2 hao    hao    4096 Jul 27 12:49 keyring-DTEmAL
    drwx------. 2 hao    hao    4096 Jul 27 13:01 keyring-FD7mUX
    drwx------. 2 hao    hao    4096 Aug  4 16:58 keyring-YtvBn9
    drwx------. 2 hao    hao    4096 Jul 26 13:05 keyring-ZCIk4h
    drwx------. 2 root   root   4096 Jul 26 12:48 keyring-l3Td4R
    drwx------. 2 gdm    gdm    4096 Nov  4 10:00 orbit-gdm
    drwx------. 2 gdm    gdm    4096 Nov  4 09:59 pulse-0dTthpTUZIEl
    drwx------. 2 hao    hao    4096 Aug  6 15:58 pulse-1lfMnULTO8sm
    drwx------. 2 root   root   4096 Jul 26 12:48 pulse-u8BNu0nCsMTX
    [hadoop@hao tmp]$ id
    uid=1004(hadoop) gid=1004(hadoop) groups=1004(hadoop) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    复制代码

    5、newgrp:将用户的基本组临时切换到其他组(登陆到其他组)

    只有当用户跟所切换到的组没有任何关系时(即要切换到的组也不是该用户的附加组时),需要输入密码。可以用exit退出

    复制代码
    [root@hao ~]# gpasswd mygroup
    Changing the password for group mygroup
    New Password: 
    Re-enter new password: 
    [root@hao ~]# su - hadoop
    [hadoop@hao ~]$ cd /tmp
    [hadoop@hao tmp]$ newgrp mygroup
    Password: 
    [hadoop@hao tmp]$ id
    uid=1004(hadoop) gid=502(mygroup) groups=1004(hadoop),502(mygroup) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    [hadoop@hao tmp]$ touch b.hadoop [hadoop@hao tmp]$ ll total 40 -rw-rw-r--. 1 hadoop hadoop 0 Nov 4 12:09 a.hadoop -rw-r--r--. 1 hadoop mygroup 0 Nov 4 12:21 b.hadoop drwx------. 2 hao hao 4096 Aug 6 15:58 keyring-4BcUXT drwx------. 2 hao hao 4096 Jul 27 12:49 keyring-DTEmAL drwx------. 2 hao hao 4096 Jul 27 13:01 keyring-FD7mUX drwx------. 2 hao hao 4096 Aug 4 16:58 keyring-YtvBn9 drwx------. 2 hao hao 4096 Jul 26 13:05 keyring-ZCIk4h drwx------. 2 root root 4096 Jul 26 12:48 keyring-l3Td4R drwx------. 2 gdm gdm 4096 Nov 4 10:00 orbit-gdm drwx------. 2 gdm gdm 4096 Nov 4 09:59 pulse-0dTthpTUZIEl drwx------. 2 hao hao 4096 Aug 6 15:58 pulse-1lfMnULTO8sm drwx------. 2 root root 4096 Jul 26 12:48 pulse-u8BNu0nCsMTX
  • 相关阅读:
    30130413 腾讯笔试
    未来网络 SDN
    XAML实例教程系列 XAML传递参数到值转换类实例
    【转】Silverlight MVVM 贴近实战(一)
    XAML实例教程系列 依赖属性和附加属性
    Silverlight开发工具汇总
    XAML实例教程系列 类型转换器(Type Converter)
    XAML实例教程系列 标记扩展(Markup Extensions)
    XAML实例教程系列 事件(Event)
    [转]XAML实例教程系列 命名空间(NameSpace)
  • 原文地址:https://www.cnblogs.com/paulwinflo/p/5407414.html
Copyright © 2011-2022 走看看