美文网首页
linux用户和用户组权限

linux用户和用户组权限

作者: 深圳邱道长 | 来源:发表于2020-02-17 19:33 被阅读0次

    由于Linux是一个多用户操作系统(因为它允许不同计算机或终端上的多个用户访问单个系统),因此需要知道如何执行有效的用户管理:如何添加、编辑、挂起或删除用户帐户,以及授予他们执行分配任务所需的权限。

    测试在Ubuntu18下进行

    添加用户

    adduser [xxx]
    useradd [xxx]
    
    image.png

    第一种可以,第二种还需要其它事情。

    将新用户帐户添加到系统时,将执行以下操作。
    1 创建其主目录(默认情况下为/home/username)。

    .bash_logout
    .bash_profile
    .bashrc
    
    image.png

    2 以下隐藏文件将复制到用户的主目录中,并将用于为其用户会话提供环境变量。

    image.png

    3 为用户在/var/spool/mail/username下创建一个假脱机邮件

    4 创建一个组,并为其赋予与新用户帐户相同的名称。

    了解 /etc/passwd

    完整的帐户信息存储在/etc/passwd文件中。

    cat /etc/passwd
    
    root:x:0:0:root:/root:/bin/bash
    daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
    bin:x:2:2:bin:/bin:/usr/sbin/nologin
    sys:x:3:3:sys:/dev:/usr/sbin/nologin
    sync:x:4:65534:sync:/bin:/bin/sync
    games:x:5:60:games:/usr/games:/usr/sbin/nologin
    man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
    lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
    mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
    news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
    uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
    proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
    www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
    backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
    list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
    irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
    gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
    nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
    _apt:x:100:65534::/nonexistent:/usr/sbin/nologin
    u1:x:1000:1000:u1full,,,:/home/u1:/bin/bash
    u2:x:1001:1001::/home/u2:/bin/sh
    

    它的格式是

    [username]:[x]:[UID]:[GID]:[Comment]:[Home directory]:[Default shell]
    

    比如我们刚刚创建的账号u1

    u1:x:1000:1000:u1full,,,:/home/u1:/bin/bash
    
    • u1是名字
    • x 就是x
    • 1000 uid
    • 1000 gid
    • 注释这些就省略了。
    • 然后是home目录 /home/u1
    • 默认的Shell是 /bin/bash

    了解 /etc/group

    它的形式

    [Group name]:[Group password]:[GID]:[Group members]
    
    cat /etc/group
    
    root:x:0:
    daemon:x:1:
    bin:x:2:
    sys:x:3:
    adm:x:4:
    tty:x:5:
    disk:x:6:
    lp:x:7:
    mail:x:8:
    news:x:9:
    uucp:x:10:
    man:x:12:
    proxy:x:13:
    kmem:x:15:
    dialout:x:20:
    fax:x:21:
    voice:x:22:
    cdrom:x:24:
    floppy:x:25:
    tape:x:26:
    sudo:x:27:
    audio:x:29:
    dip:x:30:
    www-data:x:33:
    backup:x:34:
    operator:x:37:
    list:x:38:
    irc:x:39:
    src:x:40:
    gnats:x:41:
    shadow:x:42:
    utmp:x:43:
    video:x:44:
    sasl:x:45:
    plugdev:x:46:
    staff:x:50:
    games:x:60:
    users:x:100:
    nogroup:x:65534:
    u1:x:1000:
    u2:x:1001:
    

    还是以u1:x:1000:分析

    • u1 是组名
    • x表示未使用组密码
    • 然后是组id1000
    • [组成员]:以逗号分隔的用户列表。

    我们现在都是单兵作战,所有看不到效果。

    修改账号

    如果你创建的账号有信息要改

    usermod [options] [username]
    

    比如要改密码

    usermod -p "123123" u1
    

    删除用户

    userdel
    
    image.png
    userdel -r qiudaozhang
    

    此时会把 mail spool 以及 主目录删除。 如果本身咩有 mail spool,会打印日志提示你。

    添加用户组

    查看帮助

    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
    

    eg

    groupadd -g 1001 gjames
    

    删除组

    Usage: groupdel [options] GROUP
    
    Options:
      -h, --help                    display this help message and exit
      -R, --root CHROOT_DIR         directory to chroot into
      -f, --force                   delete group even if it is the primary group of a user
    

    eg

    groupdel gjames
    

    修改组

    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
    

    eg

    groupmod -g 1002 gjames
    

    添加用户到用户组

    在usermod中有一个参数-a

    image.png

    现在有一个用户wade,我们加入到gjames组

    usermod -a -G gjames wade
    

    我们check下group

    cat /etc/group
    
    image.png

    用户组里面移除某个用户

    Usage: gpasswd [option] GROUP
    
    Options:
      -a, --add USER                add USER to GROUP
      -d, --delete USER             remove USER from GROUP
      -h, --help                    display this help message and exit
      -Q, --root CHROOT_DIR         directory to chroot into
      -r, --remove-password         remove the GROUP's password
      -R, --restrict                restrict access to GROUP to its members
      -M, --members USER,...        set the list of members of GROUP
      -A, --administrators ADMIN,...
                                    set the list of administrators for GROUP
    Except for the -A and -M options, the options cannot be combined.
    

    现在我们移除wade

    gpasswd -d  wade gjames
    
    image.png image.png

    相关文章

      网友评论

          本文标题:linux用户和用户组权限

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