zoukankan      html  css  js  c++  java
  • 文件权限---II

    一. 高级权限

    高级权限 suid,sgid,sticky

    问题1: 为什么会失败!

    [root@linux-server ~]# chown root.root /root/file1.txt
    [root@linux-server ~]# vim file1.txt
    123
    [root@linux-server ~]# ll /root/file1.txt 
    -rw-r--r--. 1 root root 0 Nov  1 15:30 /root/file1.txt
    
    [root@linux-server ~]# su - alice
    Last login: Sun Nov  3 15:57:41 CST 2019 on pts/0
    [alice@linux-server ~]$ cat /root/file1.txt
    cat: /root/file1.txt: Permission denied
    

    高级权限的类型

    suid ==== 4 提权 (只对二进制命令文件生效,其他不管用)
    sgid ==== 2 组权限继承    (只能对目录设置)
    sticky == 1 (t权限)  权限控制
    

    设置特殊权限

    a、字符---语法:
    chmod u+s file
    chmod g+s dir 
    chmod o+t dir
    
    b、数字
    chmod 4777 file 
    chmod 2770 dir 
    chmod 1770 dir
    

    案例一

    suid   普通用户通过suid提权     <针对文件>
    在进程文件(二进制,可执行的命令文件)上增加suid权限
    [root@linux-server ~]# chmod u+s /usr/bin/cat
    [root@linux-server ~]# chmod u+s /usr/bin/rm
    [root@linux-server ~]# su - alice
    Last login: Wed Nov  6 17:40:40 CST 2019 on pts/0
    [alice@linux-server ~]$ cat /root/file1.txt
    123
    [alice@linux-server ~]$ rm -rf /root/file1.txt
    

    案例二

    首先创建一个用户组,两个用户进行这三个案例操作

    Set UID

    那么这个特殊权限的特殊性的作用是什么呢?
    1、SUID权限仅对命令文件(二进制文件)有效;
    2、启动进程之后,其进程的属主为原程序文件的属主;
    3、执行者将具有该程序拥有者(owner)的权限。
    

    实验

    [root@linux-server ~]# ll /usr/bin/rm
    -rwxr-xr-x. 1 root root 62864 Nov  6  2016 /usr/bin/rm
    [root@linux-server ~]# chmod u+s /usr/bin/rm  #提权
    [root@linux-server ~]# ll /usr/bin/rm
    -rwsr-xr-x. 1 root root 62864 Nov  6  2016 /usr/bin/rm
    此时一旦给rm加上suid权限之后,普通用户相当于root用户。(即提权)
    [root@linux-server ~]# chmod u-s /usr/bin/rm  #取消提权
    

    Set GID

    把s放到文件的所属用户组x位置上的话,就是SGID。那么SGID的功能是什么呢?和SUID一样,只是SGID是获得该程序所属用户组的权限。
    SGID主要用在目录上-----如果用户在此目录下具有w权限的话,若使用者在此目录下建立新文件,则新文件的群组与此目录的群组相同。
    

    案例

    [root@linux-server ~]# mkdir /opt/dir1  #创建目录
    [root@linux-server ~]# groupadd hr  #创建一个组
    [root@linux-server ~]# chmod 775 /opt/dir1/  #设置权限
    [root@linux-server ~]# ll -d /opt/dir1/
    drwxrwxr-x. 2 root root 6 Nov  6 21:26 /opt/dir1/
    [root@linux-server ~]# chown .hr /opt/dir1/  #设置属组
    [root@linux-server ~]# chmod g+s /opt/dir1/  #设置sgid
    [root@linux-server ~]# ll -d /opt/dir1/
    drwxrwsr-x. 2 root hr 6 Nov  6 21:26 /opt/dir1/
    [root@linux-server ~]# touch /opt/dir1/a.txt
    [root@linux-server ~]# ll /opt/dir1/a.txt
    -rw-r--r--. 1 root hr 0 Nov  6 21:33 /opt/dir1/a.txt
    
    [root@linux-server ~]# chmod o+w /opt/dir1/ -R 
    [root@linux-server ~]# su - alice
    Last login: Wed Nov  6 21:34:59 CST 2019 on pts/2
    [alice@linux-server ~]$ touch /opt/dir1/b.txt
    [alice@linux-server ~]$ ll /opt/dir1/b.txt
    -rw-rw-r--. 1 alice hr 0 Nov  6 21:35 /opt/dir1/b.txt
    

    Sticky Bit

    这个就是针对others来设置的了,和上面两个一样,只是功能不同而已。
    SBIT(Sticky Bit)目前只针对目录有效,对于目录的作用是:当用户在该目录下建立文件或目录时,仅有自己与 root才有权力删除。
    

    案例

    [root@linux-server ~]# cd /home/
    [root@linux-server home]# mkdir dir2
    [root@linux-server home]# chmod 777 dir2/
    [root@linux-server home]# chmod o+t dir2/
    [root@linux-server home]# ll -d dir2/
    drwxrwxrwt. 2 root root 52 Oct 31 16:49 dir2/
    [root@linux-server home]# useradd jack  #创建用户
    [root@linux-server home]# su - alice 
    Last login: Wed Nov  6 21:48:12 CST 2019 on pts/2
    [alice@linux-server ~]$ touch /home/dir2/alice.txt  #用户alice创建文件
    [alice@linux-server ~]$ exit
    logout
    [root@linux-server home]# su - jack 
    Last login: Wed Nov  6 21:48:36 CST 2019 on pts/2
    [jack@linux-server ~]$ touch /home/dir2/jack.txt   #用户jack创建文件
    [jack@linux-server ~]$ rm -rf /home/dir2/alice.txt 
    rm: cannot remove ‘/home/dir2/alice.txt’: Operation not permitted
    测试jack删除alice创建的文件,无法删除
    

    目前两种给普通用户提权手段:

    sudo: 有针对性,例如针对某个用户以能够以root的身份执行某些命令。
    suid: 基本针对所有用户,任何用户在执行有suid权限的程序时(例如/usr/bin/rm),都是以root身份在执行。
    

    案例

    [root@linux-server ~]# visudo    #打开配置文件
    90 ##
    91 ## Allow root to run any commands anywhere
    92 root    ALL=(ALL)       ALL
    93 jack    ALL=(ALL)       NOPASSWD: ALL   #添加内容
    94 ## Allows members of the 'sys' group to run networking, software,
    测试
    [root@linux-server ~]# su - jack
    Last login: Wed Nov  6 22:04:46 CST 2019 on pts/2
    [jack@linux-server ~]$ sudo mkdir /test1
    

    文件属性 chattr【拓展】

    文件权限管理之: 隐藏权限防止root误删除

    文件属性添加与查看

    [root@linux-server ~]# touch file1 file2 file3
    1.查看文件属性
    [root@linux-server ~]# lsattr file1 file2 file3 
    ---------------- file1
    ---------------- file2
    ---------------- file3
    2.设置权限
    [root@linux-server ~]# chattr +a file1  #不允许修改,只允许追加
    [root@linux-server ~]# chattr +i file2  #不允许做任何操作
    [root@linux-server ~]# chattr +A file3
    [root@linux-server ~]# lsattr file1 file2 file3
    -----a---------- file1
    ----i----------- file2
    -------A-------- file3
    i:即Immutable,系统不允许对这个文件进行任何的修改
    a:即Append Only,系统只允许在这个文件之后追加数据,不允许任何覆盖或截断这个文件
    A:即Atime,告诉系统不要修改对这个文件的最后访问时间
    

    测试

    [root@linux-server ~]# echo 111 > file1  #覆盖,只允许追加
    -bash: file1: Operation not permitted
    [root@linux-server ~]# rm -rf file1     #不能删除
    rm: cannot remove ‘file1’: Operation not permitted
    [root@linux-server ~]# echo 111 >> file1 #追加
    
    [root@linux-server ~]# echo 111 > file2
    -bash: file2: Permission denied
    [root@linux-server ~]# echo 111 >> file2
    -bash: file2: Permission denied
    [root@linux-server ~]# rm -rf file2
    rm: cannot remove ‘file2’: Operation not permitted
    [root@linux-server ~]# mv file2 file4
    mv: cannot move ‘file2’ to ‘file4’: Operation not permitted
    
    取消权限
    [root@linux-server ~]# chattr -a file1 
    [root@linux-server ~]# chattr -i file2 
    

    进程管理

    关于进程 process =======================================================

    What is a process?  什么是进程
    Process states  进程状态
    

    什么是进程

    进程是已启动的可执行程序的运行实例
    
    程序: 二进制文件,静态 /bin/date, /usr/sbin/httpd,/usr/sbin/sshd, /usr/local/nginx/sbin/nginx
    进程: 是程序运行的过程, 动态,有生命周期及运行状态。
    

    进程状态

    在多任务处理操作系统中,每个CPU在一个时间点上只能处理一个进程。在进程运行时,它对CPU 时间和资源分配的要求会不断变化,从而为进程分配一个状态,它随着环境要求而改变。
    

    查看进程 process

    静态查看进程

    [root@linux-server ~]# ps aux | less
    参数解释:
    ps :process nsapashot
    a 只能查看系统里面运行的所有终端进程
    u 显示进程拥有者
    x 显示系统内所有进程
    f 显示进程之间的父子关系
    -------------------
    less: 可以上下翻页
    
    [root@linux-server ~]# ps aux 
    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    ============================================================================
    USER: 	#运行进程的用户
    PID: 	#进程ID
    %CPU:  #CPU占用率
    %MEM: #内存占用率
    STAT:	#进程状态	 man ps (/STATE)
    ?    表示没有占用终端
    R 	运行
    S 	可中断睡眠 Sleep
    D	不可中断睡眠 (usually IO)
    T 	停止的进程 
    Z 	僵尸进程
    X    死掉的进程
    START:	#进程的启动时间
    TIME:	#进程占用CPU的总时间
    COMMAND: #进程文件,进程名
    
    查看tty的方法:
    [root@linux-server ~]# tty
    ? 表示这个进程开启的时候没有占用终端
    

    查看进程(二)

    [root@linux-server ~]# ps -ef
    参数解释:
    -e 显示所有进程
    -l 长格式显示
    -f 完整格式
    

    按指定字段排序

    [root@linux-server ~]# ps aux --sort %cpu | less  #从小到大
    [root@linux-server ~]# ps aux --sort -%cpu | less #从大到小
    --sort:排序
    

    查看单个PID

    [root@linux-server ~]# yum install -y httpd  #安装apache软件
    [root@linux-server ~]# systemctl start httpd #启动
    [root@linux-server ~]# cat /var/run/httpd/httpd.pid
    1043
    

    查看指定PID

    [root@linux-server ~]# ps aux | grep sshd
    root       1043  0.0  0.2 105996  4120 ?        Ss   01:32   0:00 /usr/sbin/sshd -D
    grep:过滤
    

    查看端口

    [root@linux-server ~]# yum install lsof  #安装软件包
    [root@linux-server ~]# lsof -i:80   #端口号,这能查看带端口的进程
    COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
    httpd   64249   root    4u  IPv6 1373628      0t0  TCP *:http (LISTEN)
    
    #查网络进程和正在监听的端口
    [root@linux-server ~]# netstat -lntp
    参数详解:
    -a  显示全部的进程
    -u  显示udp
    -n  以数字的新式显示协议名称
    -t   tcp
    -p:显示进程的名称和pid
    -l :只显示正在被监听的端口
    
    [root@linux-server ~]# w  #看已经登陆到终端的进程信息,远程登陆会有ip 地址
    
  • 相关阅读:
    Creating a generic Web Parts for hosting ASP.NET User Controls
    Speed Up SQL Server Apps 提高SQL Server应用程序的运行效率 (Part 1)
    How to use CreateChildContorls method inherited from System.Web.UI.Control
    How to quickly access Web Part Management Page
    SQL Script tips for MS SQL Server
    How to enable single signon service on the SPS
    A brief summary of UML & Rational Rose – Use Case Diagram, Part II
    Borland Together for Visual Studio.Net V2.0 安装问题
    Speed Up SQL Server Apps 提高SQL Server应用程序的运行效率 (Part 2)
    体验ReSharper V1.0 for VS.Net 2003 Part I
  • 原文地址:https://www.cnblogs.com/pengpengboshi/p/13235563.html
Copyright © 2011-2022 走看看