zoukankan      html  css  js  c++  java
  • centos 特殊权限 各种搜索命令 lsattr ,chattr,suid,sgid,sbit,file,type是否是内置命令,stat文件属性 ,whereis,locate,find,ln 内部命令和外部命令 第五节课

    centos 特殊权限 各种搜索命令 lsattr ,chattr,suid,sgid,sbit,file,type是否是内置命令,stat文件属性 ,whereis,locate,find,ln  内部命令和外部命令 第五节课 

    上半节课

    特殊权限/属性

    隐藏权限

    lsattr
    chattr

    suid

    sgid

    sbit(stickbit)

    下半节课

    file  文件类型(file -i 查看文件的字符集类型,file -s 查看块设备文件的文件系统信息)

    type  是否是 内置命令

    stat    文件属性 比如三个time

    whereis

    locate

    find

    ln

    ln -s

    内部命令和外部命令

    特殊权限/属性

    chattr  :对目录和文件都有效

    lsattr

    lsattr -d  :查看当前目录的特殊权限

    lsattr -R  :针对目录,查看目录下的所有文件是否存在特殊权限,排查 rm -rf 234/ 删除不了的问题,目录下有一个文件存在特殊权限就不能删除整个目录

    chattr +i 2.txt :加权限,禁止对文件进行写入,删除,重命名

    chattr -R +i /usr/bin :整个目录锁定

    chattr -i 2.txt :去掉权限

    chattr -R -i /usr/bin :整个目录解除锁定

    chattr  +a  2.txt  只能追加内容 echo 'xx' >>2.txt 不能重命名,不能删除,对目录可以创建文件

    chattr  -a  2.txt 


    隐藏权限

    suid,sgid,sbit

    rwsr-xr-x. 临时以命令的属主去执行命令

    rwSr-xr-x. 无x执行权限,就算有命令的属主权限也无用,你执行不了

    set_uid

    chmod u+s cd

    chmod u-s cd

    -rwsrwxrwx 

    suid:针对可执行文件,二进制文件,不能针对目录

    set_gid

    sgid:针对文件和目录

    chmod g+s 12/

    chmod g-s 12/

    drwxrwsrwx 

    父目录下创建的所有子目录或文件拥有父目录的属组,而不是当前有效用户,而原有的子目录和文件属组不变

    子目录也会继承sgid

    stick_bit

    stick_bit:针对目录,作用于文件无意义

    chmod o+t 12/

    chmod o-t 12/

    drwxrwxrwt

    other位无x权限,会显示为drwxrwxrwT,无x权限根本无用

    只有root用户和文件的创建者可以删文件,其他人不能删

    drwxrwxrwx :这个目录下的文件,任何人都可以删,因为other位为w,x

    用户有父目录的other位写入w权限,那么就可以删除重命名父目录下的文件和目录,即使那个目录和文件的other位无w权限 rm -f 1.txt

    目录没有x权限只有w权限也不能删除,因为用户不能进去目录,不能进去意味着他也不能写目录

    set_uid 4
    set_gid 2
    stick_bit 1

    /tmp/  :1777


    下半节课

    file命令

    file  文件类型(file -i 查看文件的字符集类型,file -s 查看块设备文件的文件系统信息)

    file 33.txt
    33.txt: ASCII text

    file linc-870-0-7c0cd77f61c19     #默认只显示文件的类型
    linc-870-0-7c0cd77f61c19: socket

    file -i 33.txt    #查看文件的编码格式
    33.txt: text/plain; charset=us-ascii


    file -i  linc-870-0-7c0cd77f61c19
    linc-870-0-7c0cd77f61c19: application/x-socket; charset=binary

    file -s /dev/vdb1    #查看块设备文件的文件系统信息
    没有安装文件系统会只显示data

    file -s /dev/vdb1
    /dev/vdb1: data
    安装了文件系统会显示文件系统类型
    整个磁盘

    file -s /dev/vda
    /dev/vda: x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, boot drive 0x80, 1st sector stage2 0xf0800, GRUB version 0.94; partition 1: ID=0x83, active, starthead 32, startsector 2048, 16775168 sectors, code offset 0x48
    整个分区

    file -s /dev/vda1
    /dev/vda1: Linux rev 1.0 ext3 filesystem data (needs journal recovery) (large files)
    /dev/vdc是swap分区,可以识别swap分区,标签label是lswap,

    file -s /dev/vdc
    /dev/vdc: Linux/i386 swap file (new style) 1 (4K pages) size 524287 pages Label lswap
    file -s /dev/vdc1
    /dev/vdc1: Linux/i386 swap file (new style) 1 (4K pages) size 524277 pages

    which 命令路径

    which ls

    系统内核内置命令

    非系统内核内置命令查不出

    type 查看是否是内置命令

    type cd

    type ls

    whereis

    whereis  ls

    locate 

    locate 不会搜索/tmp/目录

    yum install -y  mlocate
    /var/lib/mlocate/mlocate.db

    updatedb
    locate  ls

    find
    find /root -type f
    find /root -type d
    find /root -name "linux"
    find /root -name "linux*"
    find /root -name "*.linux"
    find /root -name "*.linux" -mtime +5 5天以前
    find /root -name "*.linux" -mtime -5 5天以内
    find /root -name "*.linux" -mmin -5 5分钟以内
    find /root -name "*.linux" -mmin -60 60分钟以内
    find /path -name '*.txt' -perm 644 有644权限且txt扩展名的文件 find / -type f -perm -4000 -o -perm -2000 -print | xargs ls –al 所有含“s”位的程序
    find /tmp -size 2M 查找在/tmp 目录下等于2M的文件
    find /tmp -size +2M 查找在/tmp 目录下大于2M的文件
    find ./ -inum 11883412 inode号为11883412
    find /etc/init.d ! -type d 除了目录之外的其他类型文件 取反
    find /etc/ -type d -o -mtime -1 -o 或者
    find /etc/ -type d -name "cron.d"
    find / -lname 67.txt 查找67.txt有哪些残留的快捷方式 软连接
    find / -iname Abc 忽略大小写
    find /root -name "*.linux" -mmin -60 |xargs ls -l
    find /root -name "11.txt" -mtime -60 -print |xargs rm -f -print打印出匹配的文件默认是不会显示匹配的文件的
    find /root -name "*.linux" -mmin -60 -exec ls -l {} ;
    find /root -uid 502 -gid 502   uid和gid都是502的文件
    find /  -user root    查找属主是root的文件
    find / -nouser      查找文件缺失了属主的文件
    find / –nogroup    查找文件缺失了属组的文件
    find /root  -type l  查找当前目录下的所有软链接文件

    根据inode号删除文件

    $ ls -li
    11883412-rw-r--r-- 1 oracle dba  254 Jan 31 19:21 
    find ./  -inum 11883412 |xargs  rm -f

    find的运算符 OPERATORS  not  and   or(!  a  o)
    以优先级高低顺序排列:
    ( expr )
    强制为优先
    ! expr 如果 expr 是false则返回true
    -not expr
    与 ! expr 相同
    expr1 expr2
    与 (隐含的默认运算符);如果 expr1 为false则不会执行 expr2
    expr1 -a expr2
    expr1 -and expr2
    与 expr1 expr2 相同
    expr1 -or expr2
    与 expr1 -o expr2 相同
    expr1 , expr2列表;expr1 和 expr2 都会被执行。expr1 的值被忽略,列表的值是 expr2的值


    ;: ls ;ls  执行两次

    atime:访问文件

    ctime:更改属性时间 更改inode   改了内容就会变c

    mtime:创建或更改文件内容   ls -l看到的就是mtime

    权限不够的时候 错误信息放入黑洞,或者将find命令加入sudo

    sudo find / -type d -mtime -1  

    find: `/etc/sudoers.d': Permission denied

    find / -type d -mtime -1   2>/dev/null

    stat 3.txt  查看3个time

    atime:访问文件,挂载分区的时候一般去掉atime,noatime ctime:更改属性时间 更改inode mtime:更改文件内容

    vi会改变三个time

    vi打开文件并访问改变atime,写文件改变mtime,写完后文件大小会变改变ctime

    stat linc-870-0-7c0cd77f61c19
      File: `linc-870-0-7c0cd77f61c19'
      Size: 0             Blocks: 0          IO Block: 4096   socket
    Device: fd00h/64768d    Inode: 265249      Links: 1
    Access: (0755/srwxr-xr-x)  Uid: (   42/     gdm)   Gid: (   42/     gdm)
    Access: 2017-01-14 20:26:07.400519385 +0800
    Modify: 2017-01-14 20:26:07.400519385 +0800
    Change: 2017-01-14 20:26:07.400519385 +0800

    stat 33.txt
      File: `33.txt'
      Size: 6             Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 69697       Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2017-03-22 11:23:46.435897639 +0800
    Modify: 2017-03-22 11:19:18.491664028 +0800
    Change: 2017-03-22 11:19:18.491664028 +0800

    echo "xxxx" >>3.txt

    ctime和mtime都改变了

    软链接

    -f : 链接时先将目标文件同名的文件删除

    一定要用绝对路径

    针对文件和目录

    ln
    ln -s
    ln -s  /root/3.txt  /tmp/12.txt   软链接文件
    ln -s /root/aming  /tmp/12/   软链接目录

    ln -fs /root/test  /tmp/test   强制删除目标文件然后创建软链接

    unlink  解除软链接
    22.sh -> 333.sh  
    333.sh是被链接文件
    22.sh是链接文件

    unlink 333.sh  会删除333.sh这个被链接文件 比较危险
    unlink 22.sh  会删除22.sh这个链接文件

    不加链接来源,ln会自动在当前目录生成与目标同名的链接来源文件,并链接到目标,如果链接目标没有则报错

    #cd
    #touch 1qa
    #cd 123/
    #ln -s /root/1qa
    #ll

     

    实例

    ln -s log2013.log link2013
    ll
    lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
    -rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log
    说明:
    为log2013.log文件创建软链接link2013,如果log2013.log丢失,link2013将失效
    link2013是快捷方式,右边那个文件是快捷方式

    pwd  

    显示当前路径
    pwd -L 默认
    pwd -P

    硬链接

    -f : 链接时先将目标文件同名的文件删除

    不是创建一个新文件,inode引用计数+1,不是创建一个新文件

    不能作用于目录, 不能跨分区

    一定要用绝对路径

    ln 3.txt  4.txt

    ls -li  3.txt  4.txt   看inode是一样的

    ln -f /root/test  /tmp/test  强制删除目标文件然后创建硬链接


    如何屏蔽部分find输出,非root用户那种 

    --http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4188094&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline

    find:

    `/var/cache/tomcat7': Permission denied

    find: `/var/cache/lightdm': Permission denied

    find: `/var/cache/ldconfig': Permission denied

    find: `/var/tmp/systemd-private-9bea128bab724d788b8137f442823142-rtkit-daemon.service-6apFCE': Permission denied

    find: `/var/lib/udisks2': Permission denied ^C

    shell@debian:~$

    shell@debian:~$

    shell@debian:~$

    -------------------------------------

    find xxx 2>/dev/null

    ---------------------------

    ---------------------------


    链接注意

    一定要注意链接的方向,不管软链接还是硬链接

    1、方向

    ln -s  被链接文件   链接

    ln   被链接文件   链接

    #ln -s /etc/sysconfig/network-scripts/ifcfg-eth0  /root/eth1 

    # ll eth1 
    lrwxrwxrwx 1 root root 41 12月 19 08:14 eth1 -> /etc/sysconfig/network-scripts/ifcfg-eth0

    /etc/sysconfig/network-scripts/ifcfg-eth0:被链接文件

    /root/eth1 :链接文件

    2、软硬链接不能已经存在,不然会报:ln: 创建符号链接 "/root/eth1": 文件已存在,软链接串接创建一个新的inode,也就是软链接会新建一个新文件,不影响inode引用计数

    #touch  /root/eth1 

    #ln -s /etc/sysconfig/network-scripts/ifcfg-eth0  /root/eth1 

    /root/eth1 文件不能已经存在

    3、一定要用绝对路径 在/etc/sysconfig/network-scripts/路径下

    [root@steven1 network-scripts]#ln -s  ifcfg-eth0  /root/eth1 

    #cat /root/eth1 

    失败

    要用绝对路径

    [root@steven1 network-scripts]# ln -s /etc/sysconfig/network-scripts/ifcfg-eth0  /root/eth1 


    内部命令和外部命令

    内部命令:由shell自带的命令就叫内部命令,shell为了完成一些自己必须要完成的任务
    外部命令:在系统的某个路径下,有一个和命令同名的可执行程序

    type type
    type is a shell builtin


    find  -perm

    https://mp.weixin.qq.com/s?__biz=MjM5NTU2MTQwNA==&mid=402022683&idx=1&sn=6d403ab4472e8c6fb5615e3694ef1abf&scene=0&key=710a5d99946419d997addab69cf0313c7ced31d81e1b0f726bba0a78487df51caa8962eee72d85ca945bb3b71f1c7447&ascene=7&uin=MTY5MDMyNDgw&devicetype=android-19&version=26030f33&nettype=WIFI&pass_ticket=x9GuVQLT8uG%2BQUJ37Z%2Bo858blZxUKVrFZJ6BJCSv5HA%3D

    http://blog.csdn.net/xysoul/article/details/44956269


    find -perm mode 精确匹配 4667
    find -perm -mode 模糊匹配
    find -perm +mode 模糊匹配

    检查系统中所有suid及sgid文件
    find / -user root -perm -2000 -print -exec md5sum {} ;
    find / -user root -perm -4000 -print -exec md5sum {} ;

    查找系统中任何用户都有写权限的文件或目录
    查找文件:find / -type f -perm -2 -o -perm -20 |xargs ls -al
    查找目录:find / -type d -perm -2 -o -perm -20 |xargs ls –ld


    按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。

    如在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:

    de>$ find . -perm 755 -printde>
    还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-006相当于666

    de># ls -l
    -rwxrwxr-x 2 sam adm 0 10月 31 01:01 http3.conf
    -rw-rw-rw- 1 sam adm 34890 10月 31 00:57 httpd1.conf
    -rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
    drw-rw-rw- 2 gem group 4096 10月 26 19:48 sam
    -rw-rw-rw- 1 root root 2792 10月 31 20:19 temp

    # find . -perm 006
    # find . -perm -006
    ./sam
    ./httpd1.conf
    ./tempde>
    -perm mode:文件许可正好符合mode

    -perm +mode:文件许可部分符合mode

    -perm -mode: 文件许可完全符合mode

    我们首先创建下面的一个例子

    #ls -l ./testdir
    ——S— 1 root root 0 2008-05-06 10:39 2000
    —S—— 1 root root 0 2008-05-06 10:39 4000
    —S–S— 1 root root 0 2008-05-06 10:39 6000
    -rwS–S— 1 root root 0 2008-05-06 10:39 6600


    我创建了4个文件,都具有suid/sgid位。
    假定我执行
    find . -type f -perm 6000
    那么我们显然可以得到下面的结果
    ./60000
    这属于完全匹配。

    如果执行find . -type f -perm -6000呢,其结果是:

    ./6000
    ./6600

    这里的-号表示有1的位置一定要正确匹配,其他无所谓。这里要转成二进制来说,首先6000转成三位的二进制是下面这个样子
    110 000 000 000
    这表示前面两个1一定要匹配,其他无所谓。那能匹配的就是6000,6600两个文件,其权限二进制转换过来分别是

    110 000 000 000
    110 110 000 000


    而如果是执行find . -type f -perm +6000呢,结果会怎样?

    我们看看结果

    ./6000
    ./2000
    ./4000
    ./6600

    +号的意思是,只要有一个1匹配就行了,也就是说前2位中,只要有一个1就行,所以上面的都符合要求,因为翻译成二进制变成

    110 000 000 000
    010 000 000 000
    100 000 000 000
    110 110 000 000


    所以,从上面的结果可以看出,命令find . -type f -perm +6000就是指定目录下所有具有suid/sgid的程序。

    这是在做安全维护的时候,应该常使用的一个指令。我们首先创建下面的一个例子

    f

  • 相关阅读:
    linux时间同步ntp服务的安装与配置
    linux系统的初化始配置
    透明手机
    linux桌面的安装
    Linux中FTP服务器的搭建教程
    Linux怎样创建FTP服务器--修改用户默认目录-完美解决
    linux下vsftpd的安装及配置
    vsftpd快速部署_2018_lcf
    史上最详细的vsftpd配置文件详解2
    史上最详细的vsftpd配置文件讲解
  • 原文地址:https://www.cnblogs.com/MYSQLZOUQI/p/4802951.html
Copyright © 2011-2022 走看看