当前位置: 首页 > article >正文

Linux基础(四):文件权限与目录配置

1.使用者、群组、其他人概念

linux下每个文件都有三种权限类别,分别为使用者(User)、群组(Group)、其他人(Others)。这三种权限类别针对的是账号,也就是登录这个Linux系统的用户的账号(一个Linux服务器会有多个账号登录使用)。
在这里插入图片描述
举个简单的例子,一台Linux服务器上有5个账号,分别为:王大毛、王二毛、王三毛、张小猪、天神(root)。现在有一个王大毛创建的文件WangDamao.txt,对于这个文件,其User为王大毛,Group为王大毛、王二毛、王三毛,Others为张小猪,当然root可以访问任何文件。WangDamao.txt文件就会定义User、Group、Others对其可以进行的操作,包括读写执行等操作。

2.文件的权限

2.1 文件权限查询

切换到root身份并查看root/路径下的文件权限。
su -:切换到 root 用户的登录 shell,并进入 /root 目录。
su(不加 -):只切换用户,但不改变当前目录,也不会加载新用户的环境变量。

[fle@CentOS7 ~]$ su -
密码:
上一次登录:六 9月 28 20:15:06 CST 2024pts/0 上
[root@CentOS7 ~]# ls -a
.                .bash_logout   .config               .local
..               .bash_profile  .cshrc                .tcshrc
anaconda-ks.cfg  .bashrc        .dbus                 .viminfo
.bash_history    .cache         initial-setup-ks.cfg  .xauthxDdrns
[root@CentOS7 ~]# ls -al
总用量 44
dr-xr-x---.  6 root root  255 9月  28 20:17 .
dr-xr-xr-x. 17 root root  244 6月  21 16:56 ..
-rw-------.  1 root root 2585 5月  18 21:04 anaconda-ks.cfg
-rw-------   1 root root   28 9月  28 20:17 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
drwx------.  4 root root   31 9月  28 20:15 .cache
drwxr-xr-x   3 root root   18 9月  28 20:15 .config
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  3 root root   25 5月  18 21:15 .dbus
-rw-r--r--.  1 root root 2633 5月  18 21:15 initial-setup-ks.cfg
drwxr-xr-x   3 root root   19 9月  28 20:15 .local
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
-rw-------   1 root root 4798 9月  27 21:23 .viminfo
-rw-------   1 root root  104 9月  28 20:17 .xauthxDdrns
[root@CentOS7 ~]# exit
登出
[fle@CentOS7 ~]$ 

使用tree工具查看root/的文件结构

[root@CentOS7 ~]# tree -a
.
├── anaconda-ks.cfg
├── .bash_history
├── .bash_logout
├── .bash_profile
├── .bashrc
├── .cache
│   ├── abrt
│   │   └── lastnotification
│   └── dconf
│       └── user
├── .config
│   └── abrt
├── .cshrc
├── .dbus
│   └── session-bus
│       └── 7bc5fdb34bf34ec2b918a46825779cfc-9
├── initial-setup-ks.cfg
├── .local
│   └── share
├── .pki
│   └── nssdb
├── .tcshrc
├── .viminfo
└── .xauth07cbiw

11 directories, 13 files

下面分析:

dr-xr-x---.  6 root root  255 9月  28 20:17 .
dr-xr-xr-x. 17 root root  244 6月  21 16:56 ..
-rw-------.  1 root root 2585 5月  18 21:04 anaconda-ks.cfg
-rw-------   1 root root   28 9月  28 20:17 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
drwx------.  4 root root   31 9月  28 20:15 .cache
drwxr-xr-x   3 root root   18 9月  28 20:15 .config
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  3 root root   25 5月  18 21:15 .dbus
-rw-r--r--.  1 root root 2633 5月  18 21:15 initial-setup-ks.cfg
drwxr-xr-x   3 root root   19 9月  28 20:15 .local
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
-rw-------   1 root root 4798 9月  27 21:23 .viminfo
-rw-------   1 root root  104 9月  28 20:17 .xauthxDdrns

从tree命令的结果可以看到,.是一个目录,而像anaconda-ks.cfg则是一个文件。以-rw-r--r--. 1 root root 2633 5月 18 21:15 initial-setup-ks.cfg来讲解文件的权限:
在这里插入图片描述

①-rw-r–r–.
这个文件的类型与权限,具体如下:
在这里插入图片描述

档案类型含义
d目录, 例如“.config”的那一行,其是一个目录
-文件, 例如“initial-setup-ks.cfg”那一行,其是一个文件
l链接文件( link file)
b设备文件里面的可供储存的周边设备( 可随机存取设备)
c设备文件里面的序列埠设备, 例如键盘、 鼠标( 一次性读取设备)
权限类型含义
r可读
w可写
x可执行

如对于anaconda-ks.cfg文件来说,只有User可以对其进行读写,Group和Others什么都操作不了。当权限末尾出现一个点 . 时,表示这个文件或目录有 SELinux 的安全上下文设置。这意味着文件系统中的 SELinux 配置可能在控制对该文件的访问。
②1
表示有多少文件名链接到此节点( i-node) 。每个文件都会将他的权限与属性记录到文件系统的i-node中, 不过, 我们使用的目录树却是使用文件名来记录, 因此每个文件名就会链接到一个i-node啰! 这个属性记录的, 就是有多少不同的文件名链接到相同的一个i-node号码去就是了。
③root
这个文件的User
④root
这个文件的Group
⑤2633
文件的容量大小, 默认单位为Bytes
⑥ 5月 18 21:15
文件的创建日期或者是最近的修改日期
⑦initial-setup-ks.cfg
这个文件的文件名

2.2 文件权限更改

文件权限更改主要有三个命令:

chgrp:改变文件所属的Group
chown:改变文件的User/Group
chmod:改变文件的权限, SUID, SGID, SBIT等等的特性

2.2.1 chgrp

chgrp只能更改文件的Group。

[fle@CentOS7 etc]$ chgrp --help
用法:chgrp [选项]... 用户组 文件...
 或:chgrp [选项]... --reference=参考文件 文件...
Change the group of each FILE to GROUP.
With --reference, change the group of each FILE to that of RFILE.

  -c, --changes          like verbose but report only when a change is made
  -f, --silent, --quiet  suppress most error messages
  -v, --verbose          output a diagnostic for every file processed
      --dereference      affect the referent of each symbolic link (this is
                         the default), rather than the symbolic link itself
  -h, --no-dereference   affect symbolic links instead of any referenced file
                         (useful only on systems that can change the
                         ownership of a symlink)
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's group rather than specifying a
                         GROUP value
  -R, --recursive        operate on files and directories recursively

The following options modify how a hierarchy is traversed when the -R
option is also specified.  If more than one is specified, only the final
one takes effect.

  -H                     if a command line argument is a symbolic link
                         to a directory, traverse it
  -L                     traverse every symbolic link to a directory
                         encountered
  -P                     do not traverse any symbolic links (default)

      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

示例:
  chgrp staff /u            将 /u 的属组更改为"staff"。
  chgrp -hR staff /u    将 /u 及其子目录下所有文件的属组更改为"staff"。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告chgrp 的翻译错误
要获取完整文档,请运行:info coreutils 'chgrp invocation'

首先要注意的是,要被改变的群组名称必须要在/etc/group文件内存在才行。查看/etc/group文件的部分内容如下:

root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:fle
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:33:amandabackup
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:

在这里插入图片描述
在这里插入图片描述
chgrp的经典用法:
首先在/etc/group中添加一个测试组,有两种添加方法,一种是用sudo vim /etc/group直接编辑添加,另一种是用sudo groupadd ZhangSan命令直接添加。这里直接编辑group文件添加。
在这里插入图片描述
ZhangSan是一个组名,还没有显式地添加任何账号到这个组。
现在创建一个文件并更改它所属的组:

[fle@CentOS7 桌面]$ touch file
[fle@CentOS7 桌面]$ ls -l file
-rw-rw-r-- 1 fle fle 0 9月  29 15:57 file
[fle@CentOS7 桌面]$ sudo chgrp ZhangSan file
[fle@CentOS7 桌面]$ sudo chgrp LiSi file
chgrp: 无效的组:"LiSi"

可以看到,由于LiSi没有在/etc/group中存在,所以sudo chgrp LiSi file是无效的。

2.2.2 chown

chown命令既可以改变文件的User,也可以改变文件的Group。

[fle@CentOS7 桌面]$ chown --help
用法:chown [选项]... [所有者][:[组]] 文件...
 或:chown [选项]... --reference=参考文件 文件...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those of RFILE.

  -c, --changes          like verbose but report only when a change is made
  -f, --silent, --quiet  suppress most error messages
  -v, --verbose          output a diagnostic for every file processed
      --dereference      affect the referent of each symbolic link (this is
                         the default), rather than the symbolic link itself
  -h, --no-dereference   affect symbolic links instead of any referenced file
                         (useful only on systems that can change the
                         ownership of a symlink)
      --from=当前所有者:当前所属组
                         	只当每个文件的所有者和组符合选项所指定时才更改所
				有者和组。其中一个可以省略,这时已省略的属性就不
				需要符合原有的属性。
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's owner and group rather than
                         specifying OWNER:GROUP values
  -R, --recursive        operate on files and directories recursively

The following options modify how a hierarchy is traversed when the -R
option is also specified.  If more than one is specified, only the final
one takes effect.

  -H                     if a command line argument is a symbolic link
                         to a directory, traverse it
  -L                     traverse every symbolic link to a directory
                         encountered
  -P                     do not traverse any symbolic links (default)

      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

Owner is unchanged if missing.  Group is unchanged if missing, but changed
to login group if implied by a ':' following a symbolic OWNER.
OWNER and GROUP may be numeric as well as symbolic.

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

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告chown 的翻译错误
要获取完整文档,请运行:info coreutils 'chown invocation'

首先要注意的是,要被改变的用户名称必须要在/etc/passwd文件内存在才行。查看/etc/passwd文件的部分内容如下:

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
ods:x:999:999:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:997:User for polkitd:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin

在这里插入图片描述
chown的经典用法:
首先在/etc/passwd中添加一个测试用户,有两种添加方法,一种是用sudo vim /etc/passwd直接编辑添加,另一种是用sudo useradd ZhangSan_U命令直接添加。这里采用useradd命令进行添加,添加后可查看passwd文件如下:
在这里插入图片描述

[fle@CentOS7 桌面]$ rm file
[fle@CentOS7 桌面]$ touch file
[fle@CentOS7 桌面]$ ls -l file
-rw-rw-r-- 1 fle fle 0 9月  29 16:58 file
[fle@CentOS7 桌面]$ chown ZhangSan_U file
chown: 正在更改"file" 的所有者: 不允许的操作
[fle@CentOS7 桌面]$ sudo chown ZhangSan_U file
[fle@CentOS7 桌面]$ ls -l file
-rw-rw-r-- 1 ZhangSan_U fle 0 9月  29 16:58 file
[fle@CentOS7 桌面]$ sudo chown ZhangSan_U:ZhangSan file
[fle@CentOS7 桌面]$ ls -l file
-rw-rw-r-- 1 ZhangSan_U ZhangSan 0 9月  29 16:58 file

从上面可以看到chown可以通过一个:来同时更改User和Group。
Tips:使用cp命令进行复制操作是不会改变文件的所属User、

2.2.3 chmod

chgrp和chown都是用来改变User或者Group的,而chmod则是用来更改User、Group、Others各自的权限。

[fle@CentOS7 桌面]$ chmod --help
用法:chmod [选项]... 模式[,模式]... 文件...
 或:chmod [选项]... 八进制模式 文件...
 或:chmod [选项]... --reference=参考文件 文件...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.

  -c, --changes          like verbose but report only when a change is made
  -f, --silent, --quiet  suppress most error messages
  -v, --verbose          output a diagnostic for every file processed
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's mode instead of MODE values
  -R, --recursive        change files and directories recursively
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告chmod 的翻译错误
要获取完整文档,请运行:info coreutils 'chmod invocation'

chmod有两种使用方式,一种是使用数字,另一种是使用字母。
①数字形式

权限数字
r4
w2
x1

则-rwxrwx—的权限可以表示为4+2+1 4+2+1 0+0+0=770

[fle@CentOS7 桌面]$ touch file
[fle@CentOS7 桌面]$ ls -l file
-rw-rw-r-- 1 fle fle 0 9月  29 20:39 file
[fle@CentOS7 桌面]$ chmod 777 file
[fle@CentOS7 桌面]$ ls -l file
-rwxrwxrwx 1 fle fle 0 9月  29 20:39 file

②字母形式
chmod命令中用u表示User、g表示Group、o表示Others、a表示User、Group和Others。则为file赋予-r-xrwx–x权限如下:

[fle@CentOS7 桌面]$ rm file
rm:是否删除有写保护的普通空文件 "file"?y
[fle@CentOS7 桌面]$ touch file
[fle@CentOS7 桌面]$ ls -l file
-rw-rw-r-- 1 fle fle 0 9月  29 20:51 file
[fle@CentOS7 桌面]$ chmod u=rx,g=rwx,o=x file
[fle@CentOS7 桌面]$ ls -l file
-r-xrwx--x 1 fle fle 0 9月  29 20:51 file

值得注意的是,用字母赋予权限的时候,只需要写有的权限,如User的r-x表示为u=rx而不是u=r-x。
如果不需要知道file文件的具体权限,只是需要确保任何人都可以读取file文件且不可以写file,那么就可以使用a:

[fle@CentOS7 桌面]$ ls -l file
-r-xrwx--x 1 fle fle 0 9月  29 20:51 file
[fle@CentOS7 桌面]$ chmod a+r file
[fle@CentOS7 桌面]$ ls -l file
-r-xrwxr-x 1 fle fle 0 9月  29 20:51 file
[fle@CentOS7 桌面]$ chmod a-w file
[fle@CentOS7 桌面]$ ls -l file
-r-xr-xr-x 1 fle fle 0 9月  29 20:51 file
[fle@CentOS7 桌面]$ chmod a-r+w file
[fle@CentOS7 桌面]$ ls -l file
--wx-wx-wx 1 fle fle 0 9月  29 20:51 file

3.目录权限

文件的权限针对的是文件的内容,如r为可读取文件内容、w为可写文件内容、x为可执行文件内容。而目录的权限针对的则是文件名。目录的权限定义为:

权限含义
r可以查询该目录下的文件名数据,比如可以利用 ls 这个指令将该目录的内容列表显示出来
w具有异动该目录结构清单的权限, 如:创建新的文件与目录;删除已经存在的文件与目录( 不论该文件的权限为何! );将已存在的文件或目录进行更名;搬移该目录内的文件、 目录位置。 总之, 目录的w权限就与该目录下面的文件名异动有关就对了啦!
x使用者可以进入该目录成为工作目录,如cd命令
[fle@CentOS7 桌面]$ mkdir dir
[fle@CentOS7 桌面]$ ls -ld dir
drwxrwxr-x 2 fle fle 6 9月  29 21:29 dir
[fle@CentOS7 桌面]$ cd dir
[fle@CentOS7 dir]$ ls -a
.  ..
[fle@CentOS7 dir]$ touch file
[fle@CentOS7 dir]$ ls -a
.  ..  file

使用fle这个账号,则对dir文件夹具有rwx权限,r用ls命令来验证,w用touch命令来验证,x用cd命令来验证。
切换为ZhangSan_U用户(登录密码在/etc/shadow文件中),再次验证。首先查看shadow文件:
在这里插入图片描述
其实我们并没有为ZhangSan_U这个账号设置密码,但是用其他账号切换到ZhangSan_U时需要输入ZhangSan_U账号的密码,所以我们先为其设置一个密码:

[fle@CentOS7 dir]$ sudo passwd ZhangSan_U
[sudo] fle 的密码:
更改用户 ZhangSan_U 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[fle@CentOS7 dir]$ su ZhangSan_U
密码:
[ZhangSan_U@CentOS7 dir]$ 

切换到ZhangSan_U这个账号之后,进行验证:

[ZhangSan_U@CentOS7 桌面]$ ls -ld dir
drwxrwxr-x 2 fle fle 18 9月  29 21:33 dir
[ZhangSan_U@CentOS7 桌面]$ cd dir
[ZhangSan_U@CentOS7 dir]$ ls
file
[ZhangSan_U@CentOS7 dir]$ touch file2
touch: 无法创建"file2": 权限不够

ZhangSan_U对于dir目录来说是Other,所以ZhangSan_U对file的权限只有r-x,因此cd和ls仍然有效,而touch无效。
Tips:目录有权限而目录内的文件没有权限,目录的权限仍然可以生效

[ZhangSan_U@CentOS7 dir]$ su - ZhangSan_U
密码:
上一次登录:日 9月 29 21:56:42 CST 2024pts/1 上
[ZhangSan_U@CentOS7 ~]$ pwd
/home/ZhangSan_U
[ZhangSan_U@CentOS7 ~]$ mkdir dir
[ZhangSan_U@CentOS7 ~]$ cd dir
[ZhangSan_U@CentOS7 dir]$ su
密码:
[root@CentOS7 dir]# pwd
/home/ZhangSan_U/dir
[root@CentOS7 dir]# touch file
[root@CentOS7 dir]# su ZhangSan_U
[ZhangSan_U@CentOS7 dir]$ ls -l file
-rw-r--r-- 1 root root 0 9月  29 22:17 file
[ZhangSan_U@CentOS7 dir]$ rm file
rm:是否删除有写保护的普通空文件 "file"?y
[ZhangSan_U@CentOS7 dir]$ ls
[ZhangSan_U@CentOS7 dir]$ cd ..
[ZhangSan_U@CentOS7 ~]$ ls -ld dir
drwxrwxr-x 2 ZhangSan_U ZhangSan_U 6 9月  29 22:18 dir

从上面可以看到,对于file文件,ZhangSan_U是Others,只有读的权限,但是ZhangSan_U对dir目录有rwx的权限,所以ZhangSan_U仍然可以删除file文件。


http://www.kler.cn/news/325877.html

相关文章:

  • vulhub Jboss 漏洞攻略
  • 华为OD真题机试-英文输入法(Java)
  • MySQL9个连接:left join、inner join等
  • RabbitMQ常用管理命令及管理后台
  • 深度学习推理的技术实现与优化策略
  • 达梦数据库导入导出统计信息
  • 【tower-boot 系列】开源RocketMQ和阿里云rockerMq 4.x和5.x集成 (一)
  • C#中实现压缩包(如ZIP)的解压功能
  • 源2.0全面适配百度PaddleNLP,大模型开发开箱即用
  • 弹射型蜂群巡飞无人机技术详解
  • 光电开关应用设计讨论
  • 0x09 瑞友 应用虚拟化系统 GetBSAppUrl SQL注入漏洞 - 复现
  • 【学习笔记】手写 Tomcat 七
  • MySQL 8 临时关闭缓存
  • 安卓主板_MTK4G/5G音视频记录仪整机及方案定制
  • Python酷库之旅-第三方库Pandas(127)
  • ASPICE的商业价值:提升汽车软件开发竞争力与质量保障
  • Qt——如何创建一个项目
  • vulhub weblogic 靶场攻略
  • ElementPlus---Timeline 时间线组件使用示例
  • 分布式选举 - Paxos、Zab 和 Raft 选举协议的逐步优化与对比分析
  • 数据库 - python操作MySQL
  • Swagger配置且添加小锁(asp.net)(笔记)
  • VUE 整合 ECharts
  • Qt多线程与数据库
  • Vue.js组件开发
  • Django 和 ASP.NET Zero 的对比分析
  • Python 复制PDF中的页面
  • 基于Vue3内置的lodash函数库实现防抖节流
  • centos9 nginx 版本