16. Linux 文件目录权限

# 文件参数

d:表示是一个目录
-:表示这是一个普通的文件
l: 表示这是一个符号链接文件,实际上它指向另一个文件
b、c:分别表示区块设备和其他的外围设备,是特殊类型的文件
s、p:这些文件关系到系统的数据结构和管道
x  :执行文件或者进入目录的权限

一、将一个文件设置成其他人不能读

# 先用 root 用户修改一个文件的权限:

[root@Demon data]# touch 1.txt
[root@Demon data]# ls -lh
总用量 4.0K
-rw-r--r--. 1 root root    0 1月  15 19:54 1.txt
[root@Demon data]# chmod 600 1.txt         
[root@Demon data]# ls -l
总用量 4
-rw-------. 1 root root    0 1月  15 19:54 1.txt

# 用demon 用户打开文件:

demon@Demon data]$ cat 1.txt 
cat: 1.txt: 权限不够

二、将一个目录设置成其它人不可进入

# 还是先用 root 对 /data/test 目录进行操作

[root@Demon data]# chmod 744 test
[root@Demon data]# ls -l
总用量 8
drwxr--r--. 2 root root 4096 1月  15 20:00 test

# 用 demon 用户进入 /data/test 目录

[demon@Demon data]$ cd test/
bash: cd: test/: 权限不够

三、将一个脚本设置成不可执行

[demon@Demon data]$ ls -l test.sh 
-rwxrwxr-x. 1 D D 26 1月  15 20:07 test.sh
[demon@Demon data]$ ./test.sh 
Demon
[demon@Demon data]$ chmod 666 test.sh 
[demon@Demon data]$ ./test.sh
bash: ./test.sh: 权限不够

四、改变文件 / 目录的所有者

  chown root /u 将 /u 的属主更改为"root"。
  chown root:staff /u 和上面类似,但同时也将其属组更改为"staff"。
  chown -hR root /u         将 /u 及其子目录下所有文件的属主更改为"root"。

1、改变文件的所有者

[root@Demon data]# ls -l 1.txt 
-rw-------. 1 root root 0 1月  15 19:54 1.txt
[root@Demon data]# chown demon  1.txt 
[root@Demon data]# ls -l 1.txt 
-rwxrw-rw-. 1 demon  root 0 1月  15 19:54 1.txt

2、改变目录的所有者

[root@Demon data]# ls -l
总用量 12
drwxr--r--. 2 root root 4096 1月  15 20:00 test
[root@Demon data]# chown demon test
[root@Demon data]# ls -l
总用量 12
drwxr--r--. 2 demon     root 4096 1月  15 20:00 test
posted @ 2016-03-25 17:06  Demon-咖啡  阅读(240)  评论(0编辑  收藏  举报