文件基本权限-UGO

来源:互联网 发布:淘宝详情怎么上传 编辑:程序博客网 时间:2024/05/16 06:05


文件权限管理之: UGO设置基本权限(r、w、x)

文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件

rw-r--r-- alice hr install.log

权限对象:
属主: u
属组: g
其他人: o

权限类型:
读:r 4
写:w 2
执行: x 1

设置权限
1. 更改文件的属主、属组
chown:
[root@localhost  ~]# chown alice.hr file1 //改属主、属组
[root@localhost ~]# chown alice file1 //只改属主
[root@localhost ~]# chown .hr file1 //只改属组
[root@localhost ~]# chown -R alice.hr dir1

chgrp:
[root@localhost ~]# chgrp it file1 //改文件属组
[root@localhost ~]# chgrp -R it dir1//改文件属组

2. 更改权限
a. 使用符号
对象 赋值符 权限类型
u + r
chmod g - w file1
o = x
a
[root@localhost ~]# chmod u+x file1 //属主增加执行
[root@localhost ~]# chmod a=rwx file1 //所有人等于读写执行
[root@localhost ~]# chmod a=- file1 //所有人没有权限
[root@localhost ~]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
[root@localhost ~]# ll file1 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果

b. 使用数字
[root@localhost ~]# chmod 644 file1
[root@localhost ~]# ll file1

-rw-r--r-- 1 alice it 17 10-25 16:45 file1

rwx的用法

使用-R选项递归修改目录和目录下已存在的文件的权限的时候可以使用rwX,这个X意味着目录本身和目录下的目录会给x权限,但是目录下的文件不会给x权限

 [root@localhost tmp]# ll -d dir1
drwxrwxrwx 3 jack gougou 4096 7月 22 04:12 dir1
[root@localhost tmp]# ll dir1
总用量 4
drwxr-xr-x 2 root root 4096 7月 22 04:12 dir100
-rw-rw-rw- 1 jack gougou 0 7月 22 03:54 file1
-rw-rw-rw- 1 jack gougou 0 7月 22 03:54 file2
[root@localhost tmp]# chmod -R a=rwX dir1
[root@localhost tmp]# ll -d dir1
drwxrwxrwx 3 jack gougou 4096 7月 22 04:12 dir1
[root@localhost tmp]# ll dir1
总用量 4
drwxrwxrwx 2 root root 4096 7月 22 04:12 dir100
-rw-rw-rw- 1 jack gougou 0 7月 22 03:54 file1
-rw-rw-rw- 1 jack gougou 0 7月 22 03:54 file2


[root@localhost ~]# ll /home/
总用量 24
drwx------ 4 alice alice 4096 7月 22 00:11 alice
drwx------ 4 jack jack 4096 7月 22 19:34 jack
drwx------. 2 root root 16384 7月 11 23:44 lost+found
[root@localhost ~]# chgrp hr /home/jack/
[root@localhost ~]# ll /home/
总用量 24
drwx------ 4 alice alice 4096 7月 22 00:11 alice
drwx------ 4 jack hr 4096 7月 22 19:34 jack
drwx------. 2 root root 16384 7月 11 23:44 lost+found
[root@localhost ~]# id jack
uid=500(jack) gid=500(jack) 组=500(jack)
[root@localhost ~]# su - jack
[jack@localhost ~]$ touch file1
[jack@localhost ~]$ ll
总用量 0
-rw-rw-r-- 1 jack jack 0 7月 22 19:40 file1
[jack@localhost ~]$ logout
[root@localhost ~]# useradd robin -g hr
[root@localhost ~]# id robin
uid=502(robin) gid=505(hr) 组=505(hr)
[root@localhost ~]# ll /home/robin/
总用量 0
[root@localhost ~]# ll /home/robin/ -d
drwx------ 4 robin hr 4096 7月 22 19:41 /home/robin/
[root@localhost ~]# su - robin
[robin@localhost ~]$ touch file1
[robin@localhost ~]$ ll file1
-rw-r--r-- 1 robin hr 0 7月 22 19:42 file1

设置权限示例

针对hr部门的访问目录设置权限,要求如下:
1. root用户和hr组的员工可以读、写、执行
2. 其他用户没有任何权限

[root@localhost ~]# groupadd hr
[root@localhost ~]# mkdir /home/hr
[root@localhost ~]# chgrp hr /home/hr
[root@localhost ~]# chmod 770 /home/hr
[root@localhost ~]# ll -d /home/hr/
drwxrwx---. 2 root hr 4096 3月 13 14:26 /home/hr/

重要: r、w、x权限对文件和目录的意义


  对于文件:
r----------读 cat head tail less more
w----------写 vim > >>
x----------执行 ./ 绝对路径 普通执行(rx) 管理员(x)
对于目录:
r----------ls r 只能读文件名 r-x 文件详细信息
w----------touch rm -wx 创建文件 rwx 删除所有文件

x----------cd

rwx的权限对于文件的影响的验证

r权限验证

[root@localhost tmp]# touch file
[root@localhost tmp]# ll file
-rw-r--r-- 1 root root 0 7月 22 07:25 file
[root@localhost tmp]# echo 111 > file
[root@localhost tmp]# cat file
111
[root@localhost tmp]# su - alice
[alice@localhost ~]$ cat /tmp/file
111
[alice@localhost ~]$ echo 222 >> /tmp/file
-bash: /tmp/file: Permission denied
[alice@localhost ~]$ logout

w权限验证

[root@localhost tmp]# chmod o=w file
[root@localhost tmp]# ll file
-rw-r---w- 1 root root 4 7月 22 07:27 file
[root@localhost tmp]# su - alice
[alice@localhost ~]$ cat /tmp/file
cat: /tmp/file: Permission denied
[alice@localhost ~]$ echo 111 >> /tmp/file
[alice@localhost ~]$ cat /tmp/file
cat: /tmp/file: Permission denied
[alice@localhost ~]$ logout
[root@localhost tmp]# cat file
111
111
[root@localhost tmp]# ll file
-rw-r---w- 1 root root 8 7月 22 07:28 file

x权限验证

[root@localhost tmp]# chmod o=x file
[root@localhost tmp]# echo "ls /home" > file
[root@localhost tmp]# cat file
ls /home
[root@localhost tmp]# su - alice
[alice@localhost ~]$ /tmp/file
bash: /tmp/file: Permission denied
[alice@localhost ~]$ cat /tmp/file
cat: /tmp/file: Permission denied
[alice@localhost ~]$ logout
[root@localhost tmp]# chmod o=rx file
[root@localhost tmp]# ll file
-rw-r--r-x 1 root root 9 7月 22 07:29 file
[root@localhost tmp]# su - alice
[alice@localhost ~]$ /tmp/file
alice gougou jack maomao robin user01
[alice@localhost ~]$ cat /tmp/file
ls /home
[alice@localhost ~]$ echo 333 >> /tmp/file
-bash: /tmp/file: Permission denied

rwx的权限对于目录的影响的验证

x权限验证

[root@localhost tmp]# mkdir dir100
[root@localhost tmp]# ll -d dir100/
drwxr-xr-x 2 root root 4096 7月 22 07:32 dir100/
[root@localhost tmp]# chmod o=x dir100/
[root@localhost tmp]# ll -d dir100/
drwxr-x--x 2 root root 4096 7月 22 07:32 dir100/
[root@localhost tmp]# su - alice
[alice@localhost ~]$ cd /tmp/dir100/
[alice@localhost dir100]$ ls
ls: cannot open directory .: Permission denied

r权限验证

[root@localhost tmp]# chmod o=r dir100/
[root@localhost tmp]# touch dir100/file1
[root@localhost tmp]# touch dir100/file2
[root@localhost tmp]# ll -d dir100/
drwxr-xr-- 2 root root 4096 7月 22 07:33 dir100/
[root@localhost tmp]# su - alice
[alice@localhost ~]$ cd /tmp/dir100/
-bash: cd: /tmp/dir100/: Permission denied
[alice@localhost ~]$ ls /tmp/dir100/
ls: cannot access /tmp/dir100/file1: Permission denied
ls: cannot access /tmp/dir100/file2: Permission denied
file1 file2
[alice@localhost ~]$ logout
[root@localhost tmp]# chmod o=rx dir100/
[root@localhost tmp]# su - alice
[alice@localhost ~]$ cd /tmp/dir100/
[alice@localhost dir100]$ ll
total 0
-rw-r--r-- 1 root root 0 Jul 22 07:33 file1
-rw-r--r-- 1 root root 0 Jul 22 07:33 file2

w权限验证

[root@localhost tmp]# chmod o=w dir100/
[root@localhost tmp]# ll -d dir100/
drwxr-x-w- 2 root root 4096 7月 22 07:33 dir100/
[root@localhost tmp]# su - alice
[alice@localhost ~]$ cd /tmp/dir100/
-bash: cd: /tmp/dir100/: Permission denied
[alice@localhost ~]$ touch /tmp/dir100/file333
touch: cannot touch `/tmp/dir100/file333': Permission denied
[alice@localhost ~]$ logout
[root@localhost tmp]# chmod o=wx dir100/
[root@localhost tmp]# ll -d dir100/
drwxr-x-wx 2 root root 4096 7月 22 07:33 dir100/
[root@localhost tmp]# su - alice
[alice@localhost ~]$ cd /tmp/dir100/
[alice@localhost dir100]$ ll
ls: cannot open directory .: Permission denied
[alice@localhost dir100]$ touch file111
[alice@localhost dir100]$ rm -rf file1
[alice@localhost dir100]$ logout
[root@localhost tmp]# ll dir100/
总用量 0
-rw-rw-r-- 1 alice alice 0 7月 22 07:35 file111
-rw-r--r-- 1 root root 0 7月 22 07:33 file2

示例1: 对文件的影响
[root@localhost ~]# mkdir /dir10
[root@localhost ~]# touch /dir10/file1
[root@localhost ~]# chmod 777 /dir10/file1

[root@localhost ~]# ll -d /dir10/
drwxr-xr-x. 2 root root 4096 3月 11 18:37 /dir10/
[root@localhost ~]# ll /dir10/file1
-rwxrwxrwx. 1 777 root 0 3月 11 18:37 /dir10/file1

[alice@localhost ~]$ cat /dir10/file1
[alice@localhost ~]$ rm -rf /dir10/file1
rm: 无法删除"/dir10/file1": 权限不够

想要删除目录下的文件就必须要对文件所在的目录有写的权限。
示例2: 对目录有w权限
[root@localhost ~]# chmod 777 /dir10/
[root@localhost ~]# chmod 000 /dir10/file1
[root@localhost ~]# ll -d /dir10/
drwxrwxrwx. 2 root root 4096 3月 11 18:37 /dir10/
[root@localhost ~]# ll /dir10/file1
----------. 1 root root 0 3月 11 18:37 /dir10/file1

[alice@localhost ~]$ cat /dir10/file1
cat: /dir10/file1: 权限不够
[alice@localhost ~]$ rm -rf /dir10/file1
[alice@localhost ~]$ touch /dir10/file2

如果对上一级目录有写的权限,那么是可以删掉目录下的文件的,但是,如果对文件本身没有什么权限,那么就没有办法看这个文件。如果对文件本身有r的权限,那么就可以看文件了。

练习:
查以下目录或者文件的权限 并用数字表示
/ r-xr-xr-x 555
/etc rwxr-xr-x 755
/var rwxr-xr-x 755
/tmp rwxrwxrwt 1777
/etc/shadow --------- 000
/etc/passwd rw-r--r-- 644
root用户家目录 r-xr-x--- 550
普通用户家目录 rwx------ 700
root用户创建文件默认权限 rw-r--r-- 644
root用户创建目录默认权限 rwxr-xr-x 755
普通用户创建文件默认权限 rw-rw-r-- 664
普通用户创建目录默认权限 rwxrwxr-x 775

文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件
0 0
原创粉丝点击