0319 centos7权限设置和文件查找

1.隐藏权限lsattr和chattr:

常用参数:
+:添加参数
-:去掉参数
i:文件不能被“删除、改名、写入和添加数据
a:文件只能添加数据,不能删除和修改数据
-R:连同子目录的数据也列出来
-d:将目录本身列出来
对于文件:

[root@ligen ~]# ls -l               #
总用量 12
drwxrwx---. 2 root root   19 3月  18 12:32 1
-rw-r--r--. 1 root root    0 3月  18 12:26 2.txt
-rw-rw-r--. 1 root root   71 3月  18 17:33 4.txt
drwxrwxr-x. 2 root root    6 3月  18 12:49 5
-rw-------. 1 root root 6494 3月  16 19:39 anaconda-ks.cfg
[root@ligen ~]# chattr +i 4.txt 
[root@ligen ~]# rm 4.txt 
rm:是否删除普通文件 "4.txt"?y
rm: 无法删除"4.txt": 不允许的操作
[root@ligen ~]# mv 4.txt 5.txt
mv: 无法将"4.txt" 移动至"5.txt": 不允许的操作
[root@ligen ~]# head -n 2 /etc/passwd >> 4.txt 
-bash: 4.txt: 权限不够
[root@ligen ~]# lsattr 4.txt 
----i----------- 4.txt
[root@ligen ~]# chattr -i 4.txt 
[root@ligen ~]# head -n 2 /etc/passwd >> 4.txt 
[root@ligen ~]# cat 4.txt 
ligen
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
  • 对于目录:
    在这里插入图片描述
    chattr +a 参数对于目录来说,是可以增加文件

特殊权限

  • set uid

:该权限针对二进制可执行文件,使文件在执行阶段具有文件所有者的权限。

[aaa@ligenkelong ~]$ ls /root          #普通用户查看root目录
ls: 无法打开目录/root: 权限不够            #失败
[aaa@ligenkelong ~]$ su - root            #切换到root目录
密码:
上一次登录:三 3月 20 10:44:42 CST 2019从 192.168.247.1pts/2 上
[root@ligenkelong ~]# chmod u+s /usr/bin/ls          #为/usr/bin/ls执行文件加上特殊权限
[root@ligenkelong ~]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117680 10月 31 03:16 /usr/bin/ls        #所有者权限位上变成rws
[root@ligenkelong ~]# su - aaa                                               #切换到普通用户
上一次登录:三 3月 20 10:48:08 CST 2019pts/2 上
[aaa@ligenkelong ~]$ ls /root/                      #普通用户查看root目录成功
2  2.txt  3.txt  anaconda-ks.cfg
  • set gid:
    该权限可以作用在文件上(二进制可执行文件),也可以作用在目录上。当作用在文件上时,其功能和set uid一样。作用在目录上时,任何用户在此目录下创建的文件都具有和该目录所属组相同的组。
    [root@ligenkelong ~]# which cat      #查看执行文件cat位置
    /bin/cat
    [root@ligenkelong ~]# chmod g+s /bin/cat      #加上特殊权限set gid
    [root@ligenkelong ~]# ls -l /bin/cat       
    -rwxr-sr-x. 1 root root 54160 10月 31 03:16 /bin/cat     #用户组权限原来的x变成了s
    [root@ligenkelong ~]# su - aaa                   
    上一次登录:三 3月 20 10:48:56 CST 2019pts/2 上
    [aaa@ligenkelong ~]$ cat /root                 #普通用户可以使用cat命令查看/root目录了
    cat: /root: 是一个目录

为/root/2/目录加上set gid:

[root@ligenkelong ~]# ls -ld /root/     
dr-xr-xr-x. 4 root root 182 3月  20 08:54 /root/     #首先上级目录/root其他人最少应该要有执行的权限
[root@ligenkelong ~]# ls -ld /root/2/
drwxrwsr-x. 2 root root 19 3月  20 11:10 /root/2/    #然后/roo/2/其他人要有w权限
[root@ligenkelong ~]# ls -al /root/2/
总用量 0
drwxrwsr-x. 2 root root  19 3月  20 11:10 .
dr-xr-xr-x. 4 root root 182 3月  20 08:54 ..
-rw-rw-r--. 1 aaa  root   0 3月  20 11:10 2.txt    #创建的/root/2/2.txt所属组为root
  • sticky bit:
    可以理解为防删除位。为父目录设置该权限时,目录下的用户只能够删除自己的文件,不能够删除其他人的文件
    [root@ligenkelong ~]# cd /tmp       
    [root@ligenkelong tmp]# ls -ld
    drwxrwxrwt. 8 root root 232 3月  20 11:04 .  #/tmp目录是共享目录,默认设置sticky bit权限
    [root@ligenkelong tmp]# chmod o+t /root/2/     设置sbit权限
    [root@ligenkelong tmp]# ls -ld /root/2/ 
    drwxrwsr-t. 2 root root 19 3月  20 11:10 /root/2/

软链接:

软链接是建立在一个独立的文件,当读取这个链接文件时,它会把读取的行为转发到该文件所链接的文件上,链接到文件名。

[root@ligenkelong ~]# ln -s /etc/yum.conf ./5.txt    #做一个yum.conf的链接文件,文件名5.txt     被指向文件最好用绝对路径
[root@ligenkelong ~]# ls -al
总用量 28
dr-xr-xr-x.  4 root root  195 3月  20 11:51 .
dr-xr-xr-x. 17 root root  245 3月  16 01:27 ..
drwxrwsr-t.  2 root root   19 3月  20 11:10 2
-rwx------.  1 root root    0 3月  19 21:30 2.txt
-rw----r--.  1 root root    0 3月  20 08:54 3.txt
lrwxrwxrwx.  1 root root   13 3月  20 11:51 5.txt -> /etc/yum.conf     #成功
-rw-------.  1 root root 1418 3月  15 18:56 anaconda-ks.cfg
-rw-------.  1 root root 3018 3月  20 11:11 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  2 root root  103 3月  15 21:00 .ssh
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
[root@ligenkelong ~]# mv 5.txt ./2/5.txt     #尝试移动5.txt位置
[root@ligenkelong ~]# cd ./2
[root@ligenkelong 2]# ls -al   
总用量 0
drwxrwsr-t. 2 root root  32 3月  20 11:53 .
dr-xr-xr-x. 4 root root 182 3月  20 11:53 ..
-rw-rw-r--. 1 aaa  root   0 3月  20 11:10 2.txt
lrwxrwxrwx. 1 root root  13 3月  20 11:51 5.txt -> /etc/yum.conf     #还是可以
 [root@ligenkelong 2]# mv 5.txt 4.txt   #尝试改名
 [root@ligenkelong 2]# ls -al
 总用量 0
drwxrwsr-t. 2 root root  32 3月  20 11:57 .
dr-xr-xr-x. 4 root root 182 3月  20 11:53 ..
-rw-rw-r--. 1 aaa  root   0 3月  20 11:10 2.txt
lrwxrwxrwx. 1 root root  13 3月  20 11:51 4.txt -> /etc/yum.conf    #还是可以链接到
[root@ligenkelong 2]# 

硬链接:

当系统要读取一个文件时,会先读inode号,然后再根据inode中的信息到块区域将数据取出来。硬链接直接再建立一个inode号链接到文件放置的区域,只是增加了一个指向文件的inode

[root@ligenkelong ~]# ln /etc/yum.conf ./4.txt     #创建一个硬链接
[root@ligenkelong ~]# ls -i ./4.txt 
16992153 ./4.txt
[root@ligenkelong ~]# ls -i /etc/yum.conf    #inode号相同
16992153 /etc/yum.conf
[root@ligenkelong ~]# mv 4.txt 5.txt    #更改名字依然不变
[root@ligenkelong ~]# ls -al
总用量 32
dr-xr-xr-x.  4 root root  195 3月  20 12:33 .
dr-xr-xr-x. 17 root root  245 3月  16 01:27 ..
drwxrwsr-t.  2 root root   32 3月  20 11:57 2
-rwx------.  1 root root    0 3月  19 21:30 2.txt
-rw----r--.  1 root root    0 3月  20 08:54 3.txt
-rw-r--r--.  2 root root  970 11月  5 09:53 5.txt
-rw-------.  1 root root 1418 3月  15 18:56 anaconda-ks.cfg
-rw-------.  1 root root 3018 3月  20 11:11 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  2 root root  103 3月  15 21:00 .ssh
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
[root@ligenkelong ~]# 
  • 一些常用远程终端快捷键:

ctrl+l: 光标定位到第一行,清屏
ctrl+d:推出终端,exit
ctrl+c:结束命令
ctrl+u:把当前命令光标前的删除
ctrl+a:命令最前面
ctrl+e:命令最后面

find命令用法:

[root@ligenkelong ~]# 
[root@ligenkelong ~]# find /root/ -type f -mtime -1    #找出/root/下的 一天以内更改内容或新建的文件
/root/.bash_history
/root/2.txt
/root/2/2.txt
/root/3.txt
[root@ligenkelong ~]# find /root/ -type f -atime -1    #找出一天以内的被访问的文件
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.bash_history
/root/2.txt
/root/2/2.txt
/root/3.txt
/root/5.txt
[root@ligenkelong ~]# find /root/ -type f -ctime -1     # 找出一天以内更改权限的文件
/root/.bash_history
/root/2.txt
/root/2/2.txt
/root/3.txt
/root/5.txt
[root@ligenkelong ~]# find /root/ -type f -ctime -1 -o -mtime -1    #-o是或者的意思
/root/
/root/.bash_history
/root/2.txt
/root/2
/root/2/2.txt
/root/2/4.txt
/root/3.txt
/root/5.txt
[root@ligenkelong ~]# find /root/ -type f -mmin -60 -exec ls -l {} \;    #-mmin 表示分钟 -exec {} \;表示选项
-rw-------. 1 root root 3377 Mar 20 12:57 /root/.bash_history
[root@ligenkelong ~]# find /root/ -type f -size -10k -exec ls {} \;     #-size表示文件大小
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.bash_history
/root/.ssh/known_hosts
/root/.ssh/id_rsa
/root/.ssh/id_rsa.pub
/root/.ssh/authorized_keys
/root/2.txt
/root/2/2.txt
/root/3.txt
/root/5.txt

windows和linux互传文件:

[root@ligenkelong ~]# yum install -y lrzsz
[root@ligenkelong ~]# sz 3.txt  
[root@ligenkelong ~]# rz

课堂笔记:

inode:
inode包含文件的元信息,有
文件的字节数
文件拥有者的userid
文件的group id
文件的读、写、执行权限
文件的时间戳:ctime、atime、mtime,inode改动,则ctime必改。mtime改则ctime必改。
链接数
文件数据的block位置

文件名包含在目录这个文件下面,更改文件名---->ctime会改
inode也会消耗磁盘空间

查看每个磁盘分区的inode总数和已经使用的数量
df -i

删除inode:
find 文件范围 -inum inode号 | xargs rm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值