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(读/写)。
权限项 | 读 | 写 | 执行 | 读执行 | 读写 | 写执行 | 读写执行 |
字符表示 | r | w | x | rx | rw | wx | rwx |
字符示例 | u+r | u+w | u+x | g+rx | g+rw | g+wx | o+rwx |
数字表示 | 4 | 2 | 1 | 5 | 6 | 3 | 7 |
权限分配 | 文件所有者 | 文件所属组用户 | 其他用户 |
修改文件权限的命令——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-------------具有读写权限的文件,可以编辑文件内容