文件基本权限

ll命令输出解析

ticy@ubuntu:~/share/puck2/build/tmp/deploy/images/aud8516-ztk-basic$ ll
drwxr-xr-x 5 nobody nogroup     4096 Sep 10 09:09 ./
- rwxr--r-- 1 nobody nogroup  2172900 Sep  5 17:56 fastboot-darwin*

文件类型(1位)+属主权限(3位)+属组权限(3位)+其他用户权限(3位)

文件的类型

  • -  普通文件
  • d  目录
  • l  符号链接
  • c  字符设备文件
  • b  块设备文件
  • s  套接字
  • p  命名管道

文件的权限

r:4,w:2,x:1,其顺序是(u)(g)(o),如果想让某个文件的属主有“读/写”二种限,只需把4(可读)+2(可写)=6(读/写)。

权限项执行读执行读写写执行读写执行
字符表示rwxrxrwwxrwx
字符示例u+ru+wu+xg+rxg+rwg+wxo+rwx
数字表示4215637
权限分配文件所有者文件所属组用户其他用户

修改文件权限的命令——chmod

chmod [选项] 模式 文件名

选项

  -r  递归,若当前为目录,则目录下的子文件都是该修改权限

模式

  •   [ugoa][+ | - | =][rwx]
  •   [mode=421]

改变文件的拥有者——chown

  • #chown 拥有者 文件名
  • #chown test filepath  //改变所有者,修改file1的拥有者为test
  • #chown test1:test2  filepath  //同时改变所有者为test1,所属组为test2

改变文件的所属组——chgrp 

  • #chgrp 组名 文件名
  • #chgrp user filepath  //改变所属组,修改file1的拥有组为user

添加用户到所属组——chgrp 

  • #useradd test1  //创建一个名为test1的用户
  • #groupadd  share   //创建一个名为share的组
  • #useradd -G share test1 //将用户test1添加到组share

权限对文件和目录的影响

字符权限对文件的含义对目录的含义
r读文件的内容,基本权限列出目录中的文件列表
w编辑、新增、修改、删除文件内容创建、删除、重命名、剪切此目录下文件和子目录,最高权限
x执行执行该文件,最高权限使用cd进入目录及深层次目录,基本权限

应用举例

root@ubuntu:/home/ticy# mkdir dir
root@ubuntu:/home/ticy# mkdir dir_x
root@ubuntu:/home/ticy# mkdir dir_w
root@ubuntu:/home/ticy# mkdir dir_r
root@ubuntu:/home/ticy# chmod 751 dir_x
root@ubuntu:/home/ticy# chmod 752 dir_w
root@ubuntu:/home/ticy# chmod 754 dir_r
root@ubuntu:/home/ticy# ls -l
drwxr-xr-x 2 root root 4096 Dec 29 00:26 dir-----------目录的默认权限755
drwxr-xr-- 2 root root 4096 Dec 29 00:27 dir_r---------其他用户具有目录的读权限
drwxr-x-w- 2 root root 4096 Dec 29 00:26 dir_w-------其他用户具有目录的写权限
drwxr-x--x 2 root root 4096 Dec 29 00:26 dir_x--------其他用户具有目录的执行权限
root@ubuntu:/home/ticy# useradd tp-------------------------创建一个其他用户并访问
root@ubuntu:/home/ticy# su tp
tp@ubuntu:/home/ticy$ ls -l-------------------------查看其他用户对目录的权限
drwxr-xr-x 2 root root 4096 Dec 29 00:26 dir
drwxr-xr-- 2 root root 4096 Dec 29 00:37 dir_r
drwxr-x-w- 2 root root 4096 Dec 29 00:34 dir_w
drwxr-x--x 2 root root 4096 Dec 29 00:31 dir_x
tp@ubuntu:/home/ticy$ cd dir_w-------------------------其他用户具有目录的读写权限,无法进入目录
bash: cd: dir_w: Permission denied
tp@ubuntu:/home/ticy$ cd dir_r---------------------------其他用户具有目录的读权限,无法进入目录
bash: cd: dir_r: Permission denied
tp@ubuntu:/home/ticy$ cd dir_x-------------------------其他用户具有目录的执行权限,可以进入目录
tp@ubuntu:/home/ticy/dir_x$ ls -------------------------不能列出文件列表
ls: cannot open directory '.': Permission denied
tp@ubuntu:/home/ticy/dir_x$ cat file_r -------------------------可以访问具有读权限的文件,必须知道文件名
rrrrrrrrr
tp@ubuntu:/home/ticy/dir_x$ cd ..
tp@ubuntu:/home/ticy$ exit-------------------------执行权限是目录的基本权限,回到root下创建具有读执行、写执行权限的目录
exit
root@ubuntu:/home/ticy# mkdir dir_xr
root@ubuntu:/home/ticy# mkdir dir_xw
root@ubuntu:/home/ticy# chmod 753 dir_xw
root@ubuntu:/home/ticy# chmod 755 dir_xr
root@ubuntu:/home/ticy# ls -l
drwxr-xr-x 2 root root 4096 Dec 29 00:57 dir_xr
drwxr-x-wx 2 root root 4096 Dec 29 00:57 dir_xw
root@ubuntu:/home/ticy# cd dir_xr-------------------------在读执行权限目录下创建的文件
root@ubuntu:/home/ticy/dir_xr# touch file
root@ubuntu:/home/ticy/dir_xr# cd ..
root@ubuntu:/home/ticy# cd dir_xw-------------------------在写执行权限目录下创建文件
root@ubuntu:/home/ticy/dir_xw# touch file
root@ubuntu:/home/ticy# cd ..
root@ubuntu:/home/ticy# su tp
tp@ubuntu:/home/ticy$ cd dir_xr
tp@ubuntu:/home/ticy/dir_xr$ ls -l-----------------------在读执行权限目录下,可以列出文件列表,不可以创建文件
-rw-r--r-- 1 root root 0 Dec 29 01:05 file
tp@ubuntu:/home/ticy/dir_xr$ touch test1
touch: cannot touch 'test1': Permission denied
tp@ubuntu:/home/ticy/dir_xr$ cd ..
tp@ubuntu:/home/ticy$ cd dir_xw
tp@ubuntu:/home/ticy/dir_xw$ ls -l-----------------------在写执行权限目录下,可以创建文件,不可以查看文件
ls: cannot open directory '.': Permission denied
tp@ubuntu:/home/ticy/dir_xw$ touch test1 
tp@ubuntu:/home/ticy/dir_xw$ exit----------------------写权限是目录的最高权限,在root下创建具有读写执行权限的目录
exit
root@ubuntu:/home/ticy# mkdir dir_xrw
root@ubuntu:/home/ticy# chmod 647 dir_xrw
root@ubuntu:/home/ticy# ls -l
drw-r--rwx 2 root root 4096 Dec 29 01:33 dir_xrw
root@ubuntu:/home/ticy# cd dir_xrw-------------在具有读写执行权限的目录下,创建具有读、写、执行权限的文件
root@ubuntu:/home/ticy/dir_xrw# touch file
root@ubuntu:/home/ticy/dir_xrw# touch file_r
root@ubuntu:/home/ticy/dir_xrw# touch file_w
root@ubuntu:/home/ticy/dir_xrw# touch file_x
root@ubuntu:/home/ticy/dir_xrw# chmod 642 file_w
root@ubuntu:/home/ticy/dir_xrw# chmod 641 file_x
root@ubuntu:/home/ticy/dir_xrw# ls -l
-rw-r--r-- 1 root root 0 Dec 29 01:39 file-----------文件的默认权限644
-rw-r--r-- 1 root root 0 Dec 29 01:39 file_r---------其他用户具有文件的读权限
-rw-r---w- 1 root root 0 Dec 29 01:39 file_w---------其他用户具有文件的写权限
-rw-r----x 1 root root 0 Dec 29 01:39 file_x---------其他用户具有文件的写权限
root@ubuntu:/home/ticy/dir_xrw# su tp
tp@ubuntu:/home/ticy/dir_xrw$ ls -l
-rw-r--r-- 1 root root 0 Dec 29 01:39 file
-rw-r--r-- 1 root root 7 Dec 29 01:44 file_r
-rw-r---w- 1 root root 6 Dec 29 01:44 file_w
-rw-r----x 1 root root 6 Dec 29 01:45 file_x
tp@ubuntu:/home/ticy/dir_xrw$ cat file_w-------------具有写权限的文件,不能读取文件内容
cat: file_w: Permission denied
tp@ubuntu:/home/ticy/dir_xrw$ cat file_x-------------具有执行权限的文件,不能读取文件内容
cat: file_w: Permission denied 
tp@ubuntu:/home/ticy/dir_xrw$ cat file_r-------------具有读权限的文件,可以读取文件内容
rrrrrrtp
tp@ubuntu:/home/ticy/dir_xrw$ exit-------------------------读权限是文件的基本权限,回到root下创建具有读执行、读写权限的文件
exit
root@ubuntu:/home/ticy/dir_xrw# touch file_rw
root@ubuntu:/home/ticy/dir_xrw# touch file_rx
root@ubuntu:/home/ticy/dir_xrw# chmod 646 file_rw
root@ubuntu:/home/ticy/dir_xrw# chmod 645 file_rx
root@ubuntu:/home/ticy/dir_xrw# ls -l
-rw-r--rw- 1 root root 0 Dec 29 02:20 file_rw
-rw-r--r-x 1 root root 0 Dec 29 02:20 file_rx
tp@ubuntu:/home/ticy/dir_xrw$ vim file_rx-------------具有读执行权限的文件,不能编辑文件内容,但可以执行文件
E45: 'readonly' option is set (add ! to override) 
tp@ubuntu:/home/ticy/dir_xrw$ vim file_rw-------------具有读写权限的文件,可以编辑文件内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值