zoukankan      html  css  js  c++  java
  • Linux中用户及用户组

    Linux用户只有两个等级:root及非root。Linux中还有一部分用户,如:apache、mysql、nobody、ftp等,这些也都是非root用户,即普通用户。Linux的权限实际是上不同用户所能访问的文件的不同产生的假象。而这些假象的造成,还要涉及到另外一个概念:用户组

    • 一个用户至少要属于一个用户组
    • 一个用户可以属于多个用户组

    用户组存在的原因主要还是方便分配权限。而用户本身和权限的差别不是很大,各个用户之间主要的不同是:

    • 是否拥有密码
    • home目录(普通用户可以有一个以自己用户名命名的home目录,存放的地址是/home/username,root用户的home目录是:/root)
    • shell

    像nobody这样用来执行Nginx的工作进程的用户,一般不分配密码和shell,甚至连home目录都没有。

    为什么不分配密码?如果设置了密码,程序无法自动使用。由于不会有人使用这个用户登录系统,所以就没有必要分配shell。(备注:其实严格上说是有分配shell,只是分配的shell是/sbin/nologin这个特殊的shell,没有任何其他功能,主要功能是防止你登陆。)

    所有用户都可以通过查看/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
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
    systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
    systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:997:995:User for polkitd:/:/sbin/nologin
    tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    qw:x:1000:1000:qw:/home/qw:/bin/bash

    文件的每一行代表着一个用户,每一行由冒号“:”分割成7个字段,其结构如下:

    用户名:密码:UID:GID:用户全名:home目录:shell

    UID:

    • UID 0 root用户
    • UID 1~999 是占坑用户,即一写无法登录的用户(以前是系统是1~499,最近刚改)
    • UID 1000 以上是正常的可登录用户

    GID:前面说了一个用户可以属于多个用户组,但这里只有一个,表示的是专职用户组,即一个用户只有一个专职用户组,其属于其他用户组的关联关系存储在/etc/group 文件中。

    其中比较特殊的是密码字段,统一由x代替了,看/etc/passwd就知道一开始Linux是将密码存在这个文件里的,由于考虑到/etc/passwd可以被所有人查看,所以将统一存储到/etc/shadow文件(只有root权限可以访问)中,具体数据如下:

    root:$6$yp9k7rTFRX76aFP/$RpQ0Q6SIFpKn5jA3vEmpzsMdMDqDjmKQY3qCcVyuVNa1dTOOwJqC0SWCxqsNrZgYiBf5Og94cDR97hGCfd0OX1::0:99999:7:::
    bin:*:16659:0:99999:7:::
    daemon:*:16659:0:99999:7:::
    adm:*:16659:0:99999:7:::
    lp:*:16659:0:99999:7:::
    sync:*:16659:0:99999:7:::
    shutdown:*:16659:0:99999:7:::
    halt:*:16659:0:99999:7:::
    mail:*:16659:0:99999:7:::
    operator:*:16659:0:99999:7:::
    games:*:16659:0:99999:7:::
    ftp:*:16659:0:99999:7:::
    nobody:*:16659:0:99999:7:::
    avahi-autoipd:!!:17031::::::
    systemd-bus-proxy:!!:17031::::::
    systemd-network:!!:17031::::::
    dbus:!!:17031::::::
    polkitd:!!:17031::::::
    tss:!!:17031::::::
    postfix:!!:17031::::::
    sshd:!!:17031::::::
    qw:$6$W1QYF2/.vMB7BPm/$n7GV1D07lcVDhsyGS0xUe68loRTfmFj2vT4TkELQgQGfqDxNn2fGzBYUYj4CgZMhaiNSvvmhW4gubpQxBhr3H0::0:99999:7:::

    其结构如下:

    登录名:加密过的密码(*代表此用户不能用来登录):密码最近更改日期(linux时间戳):最少密码天数(0代表随时可更改):最多密码天数:过期前几天提醒用户:密码不可用期限:账户过期日期:保留位

    再来看看/etc/group文件:

    root:x:0:
    bin:x:1:
    daemon:x:2:
    sys:x:3:
    adm:x:4:
    tty:x:5:
    disk:x:6:
    lp:x:7:
    mem:x:8:
    kmem:x:9:
    wheel:x:10:
    cdrom:x:11:
    mail:x:12:postfix
    man:x:15:
    dialout:x:18:
    floppy:x:19:
    games:x:20:
    tape:x:30:
    video:x:39:
    ftp:x:50:
    lock:x:54:
    audio:x:63:
    nobody:x:99:
    users:x:100:
    utmp:x:22:
    utempter:x:35:
    ssh_keys:x:999:
    avahi-autoipd:x:170:
    input:x:998:
    systemd-journal:x:190:
    systemd-bus-proxy:x:997:
    systemd-network:x:996:
    dbus:x:81:
    polkitd:x:995:
    dip:x:40:
    tss:x:59:
    postdrop:x:90:
    postfix:x:89:
    sshd:x:74:
    qw:x:1000:qw

    其结构如下:

    组名:用户组密码:GID:用户组内的用户名

    正常的使用中很少会用到用户组密码,其存储在/etc/gshadow中。

    用户组文件比较特特殊的是“”用户组内的用户名”,其实就是这个组下的用户列表,每个用户之间用逗号“,”分割;本字段可以为空;如果字段为空表示用户组为GID的用户名

    普通用户的权限非常的低,就连在系统里安装软件的权限都没有,很多时候可以临时给普通用户以特权,就是sudo(在命令前添加sudo)。比如:

    sudo cat /etc/shadow

    完成后需要您输入root的密码,这样 就可以假借root身份了,centos默认普通用户是没有sudo权限的,这与主要以桌面版为主的Ubuntu和Fedora不同,如需给予用户root特权,就需要更改/etc/sudoers文件,修改内容。

    ## Sudoers allows particular users to run various commands as
    ## the root user, without needing the root password.
    ##
    ## Examples are provided at the bottom of the file for collections
    ## of related commands, which can then be delegated out to particular
    ## users or groups.
    ## 
    ## This file must be edited with the 'visudo' command.
     
    ## Host Aliases
    ## Groups of machines. You may prefer to use hostnames (perhaps using 
    ## wildcards for entire domains) or IP addresses instead.
    # Host_Alias     FILESERVERS = fs1, fs2
    # Host_Alias     MAILSERVERS = smtp, smtp2
     
    ## User Aliases
    ## These aren't often necessary, as you can use regular groups
    ## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname 
    ## rather than USERALIAS
    # User_Alias ADMINS = jsmith, mikem
     
     
    ## Command Aliases
    ## These are groups of related commands...
     
    ## Networking
    # Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool
     
    ## Installation and management of software
    # Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
     
    ## Services
    # Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable
     
    ## Updating the locate database
    # Cmnd_Alias LOCATE = /usr/bin/updatedb
     
    ## Storage
    # Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount
     
    ## Delegating permissions
    # Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp 
     
    ## Processes
    # Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
     
    ## Drivers
    # Cmnd_Alias DRIVERS = /sbin/modprobe
     
    # Defaults specification
     
    #
    # Disable "ssh hostname sudo <cmd>", because it will show the password in clear. 
    #         You have to run "ssh -t hostname sudo <cmd>".
    #
    Defaults    requiretty
     
    #
    # Refuse to run if unable to disable echo on the tty. This setting should also be
    # changed in order to be able to use sudo without a tty. See requiretty above.
    #
    Defaults   !visiblepw
     
    #
    # Preserving HOME has security implications since many programs
    # use it when searching for configuration files. Note that HOME
    # is already set when the the env_reset option is enabled, so
    # this option is only effective for configurations where either
    # env_reset is disabled or HOME is present in the env_keep list.
    #
    Defaults    always_set_home
     
    Defaults    env_reset
    Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
    Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
    Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
    Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
    Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
     
    #
    # Adding HOME to env_keep may enable a user to run unrestricted
    # commands via sudo.
    #
    # Defaults   env_keep += "HOME"
     
    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
     
    ## Next comes the main part: which users can run what software on 
    ## which machines (the sudoers file can be shared between multiple
    ## systems).
    ## Syntax:
    ##
    ##     user    MACHINE=COMMANDS
    ##
    ## The COMMANDS section may have other options added to it.
    ##
    ## Allow root to run any commands anywhere 
    root    ALL=(ALL)     ALL
     
    ## Allows members of the 'sys' group to run networking, software, 
    ## service management apps and more.
    # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
     
    ## Allows people in group wheel to run all commands
    %wheel    ALL=(ALL)    ALL
     
     
    ## Same thing without a password
    # %wheel    ALL=(ALL)    NOPASSWD: ALL
     
    ## Allows members of the users group to mount and unmount the 
    ## cdrom as root
    # %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
     
    ## Allows members of the users group to shutdown this system
    # %users  localhost=/sbin/shutdown -h now
     
    ## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
    #includedir /etc/sudoers.d

    给没有用户添加sudo特权,只需参照

    ## Allow root to run any commands anywhere 
    root    ALL=(ALL)     ALL

    添加如下:

    ## Allow root to run any commands anywhere 
    root    ALL=(ALL)     ALL
    qw    ALL=(ALL)     ALL

    如果要给某个用户组添加sudo特权则为:(与给用户不同的是多了一个%)

    ## Allows people in group wheel to run all commands
    %wheel ALL=(ALL) ALL

    另外一种方式是添加不需要输入root密码即有root权限的用户,添加方法如下:

    qw    ALL=(ALL)     NOPASSWD:ALL

    另外还可以设定到底有哪些执行权限,具体的规则如下:(具体可看sudoers配置文件详解

    授权用户 主机=[(切换到哪些用户或用户组)] [是否需要密码验证] 命令1,[(切换到哪些用户或用户组)] [是否需要密码验证] [命令2],[(切换到哪些用户或用户组)] [是否需要密码验证] [命令3]......

    另外默认情况下第一次使用sudo时,需要输入root密码,如果5分钟内再次执行sudo则无需再输入密码,超过5分钟则要重新输入。这个时间也是可以进行配置的,在sudoers中添加如下内容即可:

    Defaults:用户名 timestamp_timeout=20

    其中单位是分钟,如果设为0,则表示每次都要输入密码。

    讲解了这么多,接下来学习下常用的shell命令:

    useradd命令用于Linux中创建的新的系统用户。useradd可用来建立用户帐号。帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号。使用useradd指令所建立的帐号,实际上是保存在/etc/passwd文本文件中。

    语法 : useradd(选项)(参数)

    选项:

    -c<备注>:加上备注文字。备注文字会保存在passwd的备注栏位中
    -d<登入目录>:指定用户登入时的启始目录
    -D:变更预设值
    -e<有效期限>:指定帐号的有效期限
    -f<缓冲天数>:指定在密码过期后多少天即关闭该帐号
    -g<群组>:指定用户所属的群组
    -G<群组>:指定用户所属的附加群组
    -m:自动建立用户的登入目录
    -M:不要自动建立用户的登入目录
    -n:取消建立以用户名称为名的群组
    -r:建立系统帐号
    -s:指定用户登入后所使用的shell
    -u:指定用户id

    案例:

    sudo useradd username -m -s /sbin/nologin -d /home/username -g groupname

    其中:

    • -s /sbin/nologin 设置不能登陆
    • -d 设置用户主目录
    • -g 用户组
    • -m 创建用户目录

    参考资料:

    转自:http://www.biaodianfu.com/linux-user-group.html

  • 相关阅读:
    CodeForces 797D Broken BST
    CodeForces 797C Minimal string
    CodeForces 797B Odd sum
    CodeForces 797A k-Factorization
    CodeForces 772B Volatile Kite
    OpenCV学习笔记二十:opencv_ts模块
    OpenCV学习笔记十九:opencv_gpu*模块
    OpenCV学习笔记十八:opencv_flann模块
    OpenCV学习笔记十七:opencv_bioinspired模块
    OpenCV学习笔记十六:opencv_calib3d模块
  • 原文地址:https://www.cnblogs.com/duhuo/p/5892513.html
Copyright © 2011-2022 走看看