zoukankan      html  css  js  c++  java
  • linux 简单记录5--用户身份与文件权限

    用户身份与文件权限

    --用户身份和能力
    --文件权限与归属
    --文件的特殊权限
    --文件的隐藏权限
    --文件访问控制列表
    --su命令与sudo服务

    linux是一个多用户、多任务的操作系统。

    在linux中,用户的UID是唯一的,UID=0,是超级管理员
    在centos7 中
    --管理员UID=0
    --系统用户UID=1~999
    --普通用户UID=1000开始

    useradd [ ] 用户名
    使用该命令创建用户时,默认的用户家目录会是被存放在/home目录中,模式shell解释器为/bin/bash,而且默认会创建
    一个与该用户同名的基本用户组
    -d 指定用户的家目录(默认为/home/username)
    -e 账户的到期时间,格式为 YYYY-MM-DD.
    -u 指定该用户的默认 UID
    -g 指定一个初始的用户基本组(必须已存在)
    -G 指定一个或多个扩展用户组
    -N 不创建与用户同名的基本用户组
    -s 指定该用户的默认 Shell 解释器

    如果使用/sbin/nologin,用户不能登录到系统中

    [root@iscsi opt]# id yhq123
    id: yhq123: no such user
    [root@iscsi opt]# useradd -d /home/yhq123 -u 9999 -s /sbin/nologin yhq123
    [root@iscsi opt]# id yhq123
    uid=9999(yhq123) gid=9999(yhq123) groups=9999(yhq123)
    
    groupadd [ ] 群组名
    [root@iscsi opt]# groupadd yhq123
    groupadd: group 'yhq123' already exists
    [root@iscsi opt]# groupadd yhq121
    usermod [ ] 用户名 修改已经创建的用户信息
    -c 填写用户账户的备注信息
    -d -m 参数-m 与参数-d 连用,可重新指定用户的家目录并自动把旧的数据转移过去
    -e 账户的到期时间,格式为 YYYY-MM-DD
    -g 变更所属用户组
    -G 变更扩展用户组
    -L 锁定用户禁止其登录系统
    -U 解锁用户,允许其登录系统
    -s 变更默认终端
    -u 修改用户的 UID

    用户的信息保存在/etc/passwd中

    [root@iscsi opt]# id yhq123
    uid=9999(yhq123) gid=9999(yhq123) groups=9999(yhq123)
    [root@iscsi opt]# usermod -G root yhq123
    [root@iscsi opt]# id yhq123
    uid=9999(yhq123) gid=9999(yhq123) groups=9999(yhq123),0(root)
    [root@iscsi opt]# usermod -u 8888 yhq123
    [root@iscsi opt]# id yhq123
    uid=8888(yhq123) gid=9999(yhq123) groups=9999(yhq123),0(root)
    passwd [] [用户名] passwd命令用于修改用户密码、过期时间、认证信息等。
    -l 锁定用户,禁止其登录
    -u 解除锁定,允许用户登录
    --stdin 允许通过标准输入修改用户密码,如 echo "NewPassWord" | passwd --stdin Username
    -d 使该用户可用空密码登录系统
    -e 强制用户在下次登录时修改密码
    -S 显示用户的密码是否被锁定,以及密码所采用的加密算法名称
    [root@iscsi ~]# passwd yhq123
    Changing password for user yhq123.
    New password: 
    BAD PASSWORD: The password is shorter than 8 characters
    Retype new password: 
    passwd: all authentication tokens updated successfully.
    [root@iscsi ~]# passwd -l yhq123 #锁用户
    Locking password for user yhq123.
    passwd: Success
    [root@iscsi ~]# passwd -S yhq123
    yhq123 LK 2020-06-24 0 99999 7 -1 (Password locked.)
    [root@iscsi ~]# passwd -u yhq123 #解锁用户
    Unlocking password for user yhq123.
    passwd: Success
    userdel [] 用户名 用于删除用户,在执行删除操作时,该用户的家目录默认会被保留,可以加-r 将其删除
    -f 强制删除用户
    -r 同时删除用户及用户家目录
    [root@iscsi opt]# id yhq123
    uid=8888(yhq123) gid=9999(yhq123) groups=9999(yhq123),0(root)
    [root@iscsi opt]# userdel -r yhq123
    [root@iscsi opt]# id yhq123
    id: yhq123: no such user
    [root@iscsi opt]# ll /home/yhq123
    ls: cannot access /home/yhq123: No such file or directory

    2 文件权限与归属

    linux系统使用不同的字符来区分文件
    - 普通文件
    d 目录文件
    l 链接文件
    b 块设备文件
    c 字符设备文件
    p 管道文件

    linux中,每个文件都有所属的所有者和所有组,规定了文件的所有者、所有组以及其他人对文件所拥有的的r、w、x权限
    对目录文件来说,“可读”表示能够读取目录内的文件列表;“可写”能够在目录内新增、删除、重命名文件;“可执行”表示能够进入该目录。
    文件权限
    -rwxrwxrwx
    -421 421 421
    文件所有者 文件所属组 其他用户

    文件的特殊权限

    SUIDSGIDSBIT特殊权限位
    SUID是一种对二进制程序进行设置的特殊权限,可以让二进制程序的执行者临时用于属主的权限(仅对用于执行权限的二进制程序有效)
    如:所有用户都可以执行passwd命令来修改自己的密码,用户密码保存在/etc/shadow文件中
    [root@iscsi opt]# ll /etc/shadow
    ---------- 1 root root 1479 Jun 24 15:14 /etc/shadow
    该文件的默认权限是000,除了root管理员外,所有用户都没有查看或编辑该文件的权限。
    使用passwd命令加上SUID特殊权限位,可以让普通用户临时获得程序所有者的身份,把变更密码的信息写入到/etc/shadow中。
    [root@iscsi opt]# ll /bin/passwd
    -rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd
    #x变成s,意味着该文件被赋予了SUID权限
    如果文件是rw-权限,没有x执行权限,被赋予了SUID后,变成rwS

    SGID 主要实现
    --让执行者临时拥有所属组的权限(对拥有执行权限的二进制程序进行设置)
    --在某个目录中创建的文件自动继承该目录的用户组(只可以对目录进行设置)

    [root@iscsi opt]# cd /tmp/
    [root@iscsi tmp]# mkdir testdir
    [root@iscsi tmp]# ls -ald testdir/
    drwxr-xr-x 2 root root 6 Jun 24 16:03 testdir/
    [root@iscsi tmp]# chmod -Rf 777 testdir/
    [root@iscsi tmp]# chmod -Rf g+s testdir/
    [root@iscsi tmp]# ls -ald testdir/
    drwxrwsrwx 2 root root 6 Jun 24 16:03 testdir/
    [root@iscsi tmp]# su - hong
    [hong@iscsi ~]$ cd /tmp/testdir/
    [hong@iscsi testdir]$ echo "yhq.abc">test
    [hong@iscsi testdir]$ ll -al test 
    -rw-rw-r-- 1 hong root 8 Jun 24 16:04 test ##test文件的所属组是root
    [root@iscsi tmp]# cd testdir/
    [root@iscsi testdir]# chmod 760 test
    [root@iscsi testdir]# ll -l test 
    -rwxrw---- 1 hong root 8 Jun 24 16:04 test
    chown chmod 用于修改文件属性和权限的常用命令。
    SBIT 确保用户只能删除自己的文件
    [hong@iscsi tmp]$ echo "abc.123" >test
    [hong@iscsi tmp]$ ll -al test
    -rw-rw-r-- 1 hong hong 8 Jun 24 16:18 test
    [hong@iscsi tmp]$ chmod 777 test
    [hong@iscsi tmp]$ exit
    logout
    [root@iscsi testdir]# su - tang
    Last login: Wed Jun 24 16:16:40 CST 2020 on pts/0
    [tang@iscsi ~]$ cd /tmp/
    [tang@iscsi tmp]$ rm -rf test
    rm: cannot remove ‘test’: Operation not permitted
    [root@iscsi testdir]# cd ..
    [root@iscsi tmp]# mkdir yh1
    [root@iscsi tmp]# chmod -R o+t yh1 ##设置目录的SBIT特殊权限位
    [root@iscsi tmp]# ls -ld yh1/
    drwxr-xr-t 2 root root 6 Jun 24 16:24 yh1/

    3 文件的隐藏属性

    chattr 用于设置文件的隐藏权限,chattr [参数] 文件,+ 追加功能,- 移除功能
    i 无法对文件进行修改;若对目录设置了该参数,则仅能修改其中的子文件内容而不能新建或删除文件
    a 仅允许补充(追加)内容,无法覆盖/删除内容(Append Only)
    S 文件内容在变更后立即同步到硬盘(sync)
    s 彻底从硬盘中删除,不可恢复(用 0 填充原文件所在硬盘区域)
    A 不再修改这个文件或目录的最后访问时间(atime)
    b 不再修改文件或目录的存取时间
    D 检查压缩文件中的错误
    d 使用 dump 命令备份时忽略本文件/目录
    c 默认将文件或目录进行压缩
    u 当删除该文件后依然保留其在硬盘中的数据,方便日后恢复
    t 让文件系统支持尾部合并(tail-merging)
    X 可以直接访问压缩文件中的内容
    [root@iscsi tmp]# echo "i love "> chattrtest
    [root@iscsi tmp]# rm chattrtest 
    rm: remove regular file ‘chattrtest’? y
    [root@iscsi tmp]# echo "i love "> chattrtest
    [root@iscsi tmp]# chattr +a chattrtest 
    [root@iscsi tmp]# rm chattrtest 
    rm: remove regular file ‘chattrtest’? y
    rm: cannot remove ‘chattrtest’: Operation not permitted
    [root@iscsi tmp]# ll -al chattrtest 
    -rw-r--r-- 1 root root 8 Jun 24 16:30 chattrtest
    
    lsattr 用于显示文件的隐藏权限 lsattr [参数] 文件
    [root@iscsi tmp]# ll -al chattrtest 
    -rw-r--r-- 1 root root 8 Jun 24 16:30 chattrtest
    [root@iscsi tmp]# lsattr chattrtest 
    -----a---------- chattrtest

    4 访问控制列表

    setfacl 用于管理文件的ACL规则 setfacl [参数] 文件名称
    使用 setfacl 命令可以针对单一用户或用户组、单一文件或目录来进行读/写/执行权限的控制。
    针对目录文件需要使用-R 递归参数;针对普通文件则使用-m 参数;
    如果想要删除某个文件的 ACL,则可以使用-b 参数。
    [root@iscsi ~]# setfacl -Rm u:hong:rwx /root
    [root@iscsi ~]# su - hong
    Last login: Wed Jun 24 16:17:21 CST 2020 on pts/0
    [hong@iscsi ~]$ cd /root
    [hong@iscsi root]$ ls
    anaconda-ks.cfg  initial-setup-ks.cfg  original-ks.cfg  pgrouting_10-2.6.3-1.rhel7.x86_64.rpm  postgis25_10-2.5.3-6.rhel7.x86_64.rpm
    [hong@iscsi root]$ cat anaconda-ks.cfg 
    #version=DEVEL
    # System authorization information
    [hong@iscsi root]$ exit
    logout
    [root@iscsi ~]# ls -ld /root
    dr-xrwx---+ 9 root root 4096 Jun 24 15:06 /root #文件的权限最后一个(.)变成了(+),表示该文件设置了ACL
    [root@iscsi ~]# ls -ld /opt
    drwxr-xr-x. 7 root root 4096 Jun 24 15:06 /opt
    getfacl 用于显示文件上设置的ACL信息
    [root@iscsi ~]# getfacl /root
    getfacl: Removing leading '/' from absolute path names
    # file: root
    # owner: root
    # group: root
    user::r-x
    user:hong:rwx
    group::r-x
    mask::rwx
    other::---

    5 su命令与sudo服务

    [root@iscsi ~]# id
    uid=0(root) gid=0(root) groups=0(root)
    [root@iscsi ~]# su - yang
    su: user yang does not exist
    [root@iscsi ~]# useradd yang
    [root@iscsi ~]# su - yang ##- 号,把环境变量也变更为新用户的相应信息
    [yang@iscsi ~]$ id
    uid=1009(yang) gid=1009(yang) groups=1009(yang)
    sudo命令用于给普通用户提供额外的权限来完成原本root管理员才能完成的任务
    -h 列出帮助信息
    -l 列出当前用户可执行的命令
    -u 用户名或 UID 值 以指定的用户身份执行命令
    -k 清空密码的有效时间,下次执行 sudo 时需要再次进行密码验证
    -b 在后台执行指定的命令
    -p 更改询问密码的提示语
    sudo命令具有的功能:
    --限制用户执行指定的命令
    --记录用户执行的每一条命令
    --配置文件/etc/sudoers 提供集中的用户管理、权限与主机等参数
    --验证密码的后5分钟(default)无须再让用户再次验证密码
    只有root管理员才可以使用visudo命令边界sudo服务的配置文件
    #visudo 增加如下,加%代表用户组, ALL=(ALL)表示登录者的来源主机名,最后的 ALL 代表可执行的命令。 
    NOPASSWD 代表不需要密码直接可运行 Sudo,限制多命令一定要写绝对路径,用逗号分开,多行用‘’,用!代表不能执行
    
    [root@iscsi ~]# useradd yh
    [root@iscsi ~]# id yh
    uid=1002(yh) gid=1002(yh) groups=1002(yh)
    [root@iscsi ~]# visudo
    [yh@iscsi sbin]$ sudo cp nginx.2020042311 nginx.202004231234
    [yh@iscsi sbin]$ rm nginx.2020042311 
    rm: remove write-protected regular file ‘nginx.2020042311’? y
    rm: cannot remove ‘nginx.2020042311’: Permission denied
    [yh@iscsi sbin]$ sudo rm nginx.2020042311 
    Sorry, user yh is not allowed to execute '/bin/rm nginx.2020042311' as root on iscsi.
    [yh@iscsi sbin]$ logout
    [root@iscsi ~]# visudo
    [root@iscsi ~]# su - yh
    Last login: Mon Apr 27 02:47:07 PDT 2020 on pts/0
    [yh@iscsi ~]$ cd /usr/local/nginx-1.8.1/sbin/
    [yh@iscsi sbin]$ sudo mv nginx.202004231
    nginx.202004231     nginx.2020042311    nginx.202004231234  
    [yh@iscsi sbin]$ sudo mv nginx.202004231
    nginx.202004231     nginx.2020042311    nginx.202004231234  
    [yh@iscsi sbin]$ sudo mv nginx.2020042311 nginx.20200423111111
    Sorry, user yh is not allowed to execute '/bin/mv nginx.2020042311 nginx.20200423111111' as root on iscsi.
    [yh@iscsi sbin]$ sudo cat /etc/sudoers
    yh      ALL=(ALL)       NOPASSWD:ALL,!/usr/bin/rm,!/usr/bin/mv,!/usr/bin/passwd root
    #NOPASSWD 参数,使用户执行sudo命令时不需要再密码验证
    [yh@iscsi sbin]$ sudo -l
    User yh may run the following commands on iscsi:
        (ALL) NOPASSWD: ALL, !/usr/bin/rm, !/usr/bin/mv, !/usr/bin/passwd root, !/usr/sbin/fdisk, !/usr/sbin/visudo
    [yh@iscsi ~]$ sudo visudo
    Sorry, user yh is not allowed to execute '/sbin/visudo' as root on iscsi.
    [yh@iscsi ~]$ sudo vim /etc/sudoers
    ##
    ## Allow root to run any commands anywhere
    root    ALL=(ALL)       ALL
    yh      ALL=(ALL)       NOPASSWD:ALL,!/usr/bin/rm,!/usr/bin/mv,!/usr/bin/passwd root,!/usr/sbin/fdisk,!/usr/sbin/visudo
    [yh@iscsi ~]$ sudo systemctl restart named
    [yh@iscsi ~]$ sudo systemctl status named
    ● named.service - Berkeley Internet Name Domain (DNS)
       Loaded: loaded (/usr/lib/systemd/system/named.service; disabled; vendor preset: disabled)
       Active: active (running) since Tue 2020-06-30 10:08:04 CST; 6s ago
      Process: 80784 ExecStop=/bin/sh -c /usr/sbin/rndc stop > /dev/null 2>&1 || /bin/kill -TERM $MAINPID (code=exited, status=0/SUCCESS)
      Process: 80800 ExecStart=/usr/sbin/named -u named -c ${NAMEDCONF} $OPTIONS (code=exited, status=0/SUCCESS)
      Process: 80798 ExecStartPre=/bin/bash -c if [ ! "$DISABLE_ZONE_CHECKING" == "yes" ]; then /usr/sbin/named-checkconf -z "$NAMEDCONF"; else echo "Checking of zone files is disabled"; fi (code=exited, status=0/SUCCESS)
     Main PID: 80804 (named)
       CGroup: /system.slice/named.service
               └─80804 /usr/sbin/named -u named -c /etc/named.conf
    [yh@iscsi ~]$ sudo head -n 10 /etc/shadow
    root:$1$KkFN1vnx$.I/eypvST9m/k0Y74dkUd.::0:99999:7:::
    bin:*:17110:0:99999:7:::
    daemon:*:17110:0:99999:7:::
  • 相关阅读:
    grunt in webstorm
    10+ Best Responsive HTML5 AngularJS Templates
    响应式布局
    responsive grid
    responsive layout
    js event bubble and capturing
    Understanding Service Types
    To add private variable to this Javascript literal object
    Centering HTML elements larger than their parents
    java5 新特性
  • 原文地址:https://www.cnblogs.com/yhq1314/p/13212331.html
Copyright © 2011-2022 走看看