文件及目录的权限,属性,查找(find,locate)

Linux系统之所以更安全,是因为对文件权限有着非常严格的控制。今天我们就来了解一下。

查看文件或目录的权限

ls -al    : -l  列出每个文件的详细信息    -a  列出隐藏文件

root@guowei ~]# ls -al        
total 64
dr-xr-x---. 14 root root 4096 Mar  3 10:23 .
dr-xr-xr-x. 17 root root  224 Feb 25 00:56 ..
-rw-------.  1 root root 1705 Feb 25 00:56 anaconda-ks.cfg
-rw-------.  1 root root 1018 Mar  3  2019 .bash_history
...
//文件类别和权限  连接数(文件为1 目录为包含其他目录个数+2) 所有人 所有组  文件大小 创建时间或者最近修改时间  文件名 

注:第一列的第一个字符含义 ——d 目录, - 普通文件,l 链接文件,b 块文件 ,c 字符文件 ,s socket文件,p 管道文件

文件的隐藏属性

隐藏属性,必须使用lsattr来显示,默认情况下,文件的隐藏属性都是没有设置的。

[root@guowei ~]# lsattr test.txt 
---------------- test.txt

设置隐藏属性,使用chattr

 a属性:这种属性的文件只能在尾部增加数据而不能被删除

[root@guowei ~]# chattr +a test.txt 
[root@guowei ~]# lsattr test.txt 
-----a---------- test.txt
[root@guowei ~]# rm -rf test.txt 
rm: cannot remove ‘test.txt’: Operation not permitted

i属性:这种属性的文件将无法写入、改名、删除,即便是root用户也不行。常用于设置在系统或者关键服务中的配置文件,这对提升系统安全性有较大的帮助。

[root@guowei ~]# echo "1111"  > test.txt 
[root@guowei ~]# cat test.txt 
1111
[root@guowei ~]# chattr +i test.txt 
[root@guowei ~]# lsattr  test.txt 
----ia---------- test.txt
[root@guowei ~]# echo "2222" > test.txt 
-bash: test.txt: Permission denied

默认权限和umask

默认权限

扫描二维码关注公众号,回复: 5940291 查看本文章
[root@guowei ~]# touch file1.txt
[root@guowei ~]# mkdir dir2 
...
drwxr-xr-x. 2 root root    6 Mar  3 15:18 dir2               //权限为755
drwxr-xr-x. 2 root root    6 Feb 25 01:08 Documents
drwxr-xr-x. 2 root root    6 Feb 25 01:08 Downloads
-rw-r--r--. 1 root root    0 Mar  3 15:17 file1.txt          //权限为644
...

[guowei@guowei ~]$ touch file2.txt                           //切换普通用户
[guowei@guowei ~]$ mkdir dir3
[guowei@guowei ~]$ ls -l
...
drwxrwxr-x. 2 guowei guowei 6 Mar  3 15:20 dir3               //权限为775
drwxr-xr-x. 2 guowei guowei 6 Feb 25 01:02 Documents
drwxr-xr-x. 2 guowei guowei 6 Feb 25 01:02 Downloads
-rw-rw-r--. 1 guowei guowei 0 Mar  3 15:20 file2.txt          //权限为664

我们可以看到,当我们分别以root和普通用户guowei创建文件和目录的时候,权限都是不一样的。

这就涉及到了:

umask 中文称为“遮罩”   定义目录权限的值为“umask遮罩777后的权限” 定义文件的权限为“umask遮罩666后的权限”

[root@guowei ~]# vim /etc/profile        //这里就对umask进行了设置,默认为如下。所以我们创建文件和目录的时候,会得到相应的权限
...
 59 if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
 60     umask 002
 61 else
 62     umask 022
 63 fi
...

注:很多人用“同位相减”的做法来计算遮罩后的值,比如说777-022同位相减得到755,666-022同位相减得到644,这种看似正确的结果其实只是一种巧合,并不是正确方式。假设有个文件的权限为666,在遮罩值为011的情况下,采用该“同位相减”的方法计算出的权限值为655,但实际上正确的权限值应该是666。

[root@guowei ~]# umask 011
[root@guowei ~]# mkdir dir4
[root@guowei ~]# touch file3
...
drwxrw-rw-. 2 root root    6 Mar  3 15:32 dir4
...
-rw-rw-rw-. 1 root root    0 Mar  3 15:33 file3        //权限为666
...

file 可以查看文件类型和相关信息

[root@guowei ~]# file  test.txt 
test.txt: ASCII text
[root@guowei ~]# file dir2
dir2: directory

find 查找文件 

find常见参数
参数 含义 参数 含义
-name filename 查找文件名为filename的文件 -perm 根据文件权限查找
-user username 根据用户名查找 -newer filename 查找更改时间比filename新的文件
-mtime -n/+n 查找n天内/n天前更改过的文件

-type b/d/c/p/l/f/s

查找块/目录/字符/管道/链接/普通/套接字文件
-atime -n/+n 查找n天内/n天前访问过的文件 -size  +/-k/M/G 根据文件大小查找
-ctime -n/+n 查找n天内/n天前创建过的文件 -maxdepth  n 最大的查找目录深度

常用格式  find ....  -exec  处理命令  {} \;      这里用{}代替每一个结果,逐个处理,遇\;结束

[root@guowei ~]# find /etc  -user root   -type f -maxdepth 1
...
/etc/fstab
/etc/crypttab
/etc/resolv.conf
/etc/issue
...
[root@guowei ~]# find /etc/  -mtime +15  -size 20k  -exec ls -hn {}  \;
-rw-r--r--. 1 0 0 20K Jul 10  2017 /etc/selinux/targeted/active/modules/100/samba/hll
-rw-r--r--. 1 0 0 20K Jul 10  2017 /etc/selinux/targeted/active/modules/100/ssh/hll
[root@guowei ~]# find /etc/  -mtime -15  -size 20k  -exec ls -hn {}  \;
-rw-r--r--. 1 0 0 20K Feb 25 00:52 /etc/gconf/gconf.xml.defaults/%[email protected]

locate  数据库查找 

locate命令依赖于一个数据库文件,Linux系统默认每天会检索一下系统中的所有文件,然后将检索到的文件记录到数据库中。在运行locate命令的时候可直接到数据库中查找记录并打印到屏幕上,所以使用locate命令要比find命令反馈更为迅速。在执行这个命令之前一般需要执行updatedb命令(这个不是必须,系统每天会更新,但是有时候没再次更新,需要手动操作)

[root@guowei ~]# touch  test.txt 
[root@guowei ~]# locate test.txt      //找不到,因为没有更新
[root@guowei ~]# updatedb             //更新一下,就可以了
[root@guowei ~]# locate test.txt 
/root/test.txt

which 和 whereis 查找执行文件

从系统的PATH变量所定义的目录中查找可执行文件的绝对路径

[root@guowei ~]# which  passwd 
/bin/passwd
[root@guowei ~]# whereis passwd              //不但有二进制文件 还有相关的man文件
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

猜你喜欢

转载自blog.csdn.net/weixin_43800781/article/details/88086715