Basic permission management based on linux

1. Basic overview of permissions

1. What are permissions?

我们可以把它理解为操作系统对用户能够执行的功能所设立的限制,主要用于约束用户能对系统所做的操作,以及内容访问的范围,或者说,权限是指某个特定的用户具有特定的系统资源使用权力。

2. Why should there be permission?

因为系统中不可能只存在一个root用户,一定会存在多个用户,为了保护每个登陆用户的隐私和工作环境,所以就有了权限。

3. The relationship between permissions and users?

在Linux系统中,针对文件定义了三种身份,分别是属主(owner)、属组(group)、其他人(others),每一种身份又对应三种权限,分别是可读(readable)、可写(writable)、可执行(excutable)。

4. What are the meanings of rwx in permissions?

rwxrwxrwx:
    左三位:定义user(owner)的权限
    中三位:定义group的权限
    右三位:定义other的权限
letter meaning Corresponding permissions
r(read) Read permission 4
w(write) Write permission 2
x(excute) Execution authority 1
-(Permission denied) Permission denied 0
注意:如果权限位不可读、不可写、不可执行,则全部使用-作为占位符

2. Example of permission setting

注意:在Linux中权限对文件和对目录的影响是有不同区别的。
Authority Impact on files Impact on the catalog
Read permission (r) Available file data You can use the ls command to get a list of all files under it
Write permission (w) Modifiable file data You can modify the file list in this directory: create or delete files
Execution authority (x) This file can be run as a process You can cd to this directory and use ls -l to get the detailed attribute information of all files

1. Authority management command: chmod

方式一: 赋权表示法

[root@jiangshen ~]# touch file                    #创建文件
[root@jiangshen ~]# chmod a=rwx file              #给所有人添加读写执行权限
[root@jiangshen ~]# chmod a=-rwx file             #取消所有的权限
[root@jiangshen ~]# chmod u=rwx,g=rw,o=- file     #属主读写执行,属组读写,其他人无权限
[root@jiangshen ~]# chmod ug=rwx,o=r file         #属主属组读写执行,其他人读权限
[root@jiangshen ~]# ll file
-rwxrw-r-- 1 root root 0 Apr 13 03:29 file

方式一: 授权表示法

[root@jiangshen ~]# touch file                    #创建文件
[root@jiangshen ~]# chmod a+r file                #给所有人添加读权限
[root@jiangshen ~]# chmod a-r file                #取消所有人读的权限
[root@jiangshen ~]# chmod u+r file                #给属主添加读权限

方式二:number

#选项:  -R递归修改
[root@jiangshen ~]# touch file
[root@jiangshen ~]# chmod 644 file
[root@jiangshen ~]# chmod 600 file
[root@jiangshen ~]# ll file
-rw------- 1 root root 0 Apr 13 03:29 file
#针对目录设定权限
[root@jiangshen ~]# mkdir dir
[root@jiangshen ~]# chmod 777 dir/    #修改目录允许所有人访问
[root@jiangshen ~]# chmod -R 755 dir/ #修改目录及子目录权限
[root@jiangshen ~]# ll -d dir/
drwxr-xr-x 2 root root 6 Apr 13 03:34 dir/

3. Permission setting case

1. File permission experiment case:

#1.新建文件,并添加内容至文件中,默认文件匿名用户仅有读权限
[root@jiangshen ~]# echo "date" > filename
[root@jiangshen ~]# ll filename
-rw-r--r-- 1 root root 5 Jan 24 08:24 filename

#2.切换liu普通用户
[root@jiangshen ~]# su - liu

#3.对文件拥有读取的权限,但bgx用户对文件没有执行和删除的权限
[liu@jiangshen ~]$ cat  /root/filename
date

#4.使用root增加x执行权限
[root@jiangshen ~]# chmod o+x /root/filename
[root@jiangshen ~]# ll /root/filename
-rw-r--r-x 1 root root 5 Jan 24 08:24 /root/filename

#5.测试x权限是否真的能执行该文件
[liu@jiangshen ~]$ /root/filename
Wed Jan 24 08:28:34 EST 2018

#6.增加w执行权限
[root@jiangshen ~]# chmod o+w /root/filename
[root@jiangshen ~]# ll /root/filename
-rw-r--rwx 1 root root 5 Jan 24 08:24 /root/filename

#7.测试执行权限
[liu@jiangshen ~]$ vim /root/filename

2. Directory permission experiment case:

#示例1: 创建目录,并在该目录下创建文件,匿名用户对目录没有w权限,对文件有777权限 
[root@jiangshen ~]# mkdir /dirname
[root@jiangshen ~]# echo "test" >> /dirname/filename
[root@jiangshen ~]# chmod 777 /dirname/filename

#普通用户验证权限,能正常查看,但无法删除[奇怪]
[liu@jiangshen ~]$ cat /dirname/filename
test
[liu@jiangshen ~]$ rm -f /dirname/filename
rm: cannot remove ‘/dirname/filename’: Permission denied

#示例2: 设置目录777权限,相当于匿名用户对目录有w权限,对文件没有任何权限
[root@jiangshen ~]# chmod 777 /dirname/
[root@jiangshen ~]# chmod 000 /dirname/filename

#普通用户验证权限
[liu@jiangshen ~]$ cat /dirname/filename
cat: /dirname/filename: Permission denied
[liu@jiangshen ~]$ rm -f /dirname/filename
[liu@jiangshen ~]$ touch /dirname/filename_2

4. Ownership group settings

在Linux中如何变更一个文件或者一个资源的属主和属组呢,可以使用chown、chgrp命令实现。

#chown 更改属主以及属组 -R:递归修改

#准备环境,创建文件和目录
[root@jiangshen ~]# mkdir dir/test1 && touch dir/file

#示例1: 修改所属主为bin
[root@jiangshen ~]# chown bin dir/

#示例2: 修改所属组为adm
[root@jiangshen ~]# chown .adm dir/

#示例3: 递归修改目录及目录下的所有文件属主和属组
[root@jiangshen ~]# chown -R root.root dir/

Guess you like

Origin blog.51cto.com/15048360/2561999