Linux CentOS7 文件属性
目录
一、简介
二、应用
2.1、概述
2.2、linux文件属主和属组
2.3、更改文件属性
2.3.1、chgrp:更改文件属组
2.3.2、chgrp --help
2.3.3、chown:更改文件属主,也可以同时更改属组
2.3.4、chown --help
2.4、chmod:更改文件9个属性
2.1.4、概述
2.4.2、案例
2.4.3、符号类型改变文件权限
一、简介
- Linux是一种典型的多用户系统,不同的用户拥有不同的权限。
- 为了保护系统安全性,Linux系统对不同用户访问同一文件的权限做了不同的规定。
- Linux中通常使用两个命令来修改文件或目录的所属用户与权限:
- chown(change ownerp):修改所属用户与组。
- chmod(change mode):修改用户的权限。
二、应用
2.1、概述
- 使用chown授权用户,通过chmod为用户设置权限。
- 使用ll或ls -l来显示一个文件的属性以及文件所属的用户和组。
[root@localhost /]# ls -l
总用量 32
lrwxrwxrwx. 1 root root 7 11月 24 19:31 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 11月 24 19:41 boot
drwxr-xr-x. 2 root root 6 11月 25 00:20 cangls
drwxr-xr-x. 19 root root 3280 11月 25 17:16 dev
drwxr-xr-x. 2 root root 6 11月 25 01:43 dir
drwxr-xr-x. 144 root root 8192 11月 26 17:41 etc
drwxr-xr-x. 3 root root 17 4月 10 2018 home
lrwxrwxrwx. 1 root root 7 11月 24 19:31 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 11月 24 19:31 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 4月 10 2018 media
drwxr-xr-x. 3 root root 17 11月 24 19:35 mnt
drwxr-xr-x. 2 root root 6 11月 26 17:45 newtest1
drwxr-xr-x. 3 root root 15 4月 10 2018 opt
dr-xr-xr-x. 216 root root 0 11月 25 17:15 proc
drwxr-xr-x. 2 root root 6 11月 24 19:42 rich
dr-xr-x---. 9 root root 4096 11月 26 20:22 root
drwxr-xr-x. 42 root root 1240 11月 26 18:29 run
lrwxrwxrwx. 1 root root 8 11月 24 19:31 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 4月 10 2018 srv
dr-xr-xr-x. 13 root root 0 11月 25 17:15 sys
drwxr-xr-x. 5 root root 41 11月 26 18:26 TEST
drwxr-xr-x. 2 root root 6 11月 26 17:45 test1
drwxrwxrwt. 33 root root 4096 11月 26 20:22 tmp
drwxr-xr-x. 13 root root 4096 11月 24 19:31 usr
drwxr-xr-x. 21 root root 4096 11月 24 19:31 var
[root@localhost /]#
- bin文件的第一个属性用L表示,L在linux中代表该文件是一个链接文档
- 在linux中第一个字符代表文件的类别,说明该文件是目录、文件或链接文件等
- 当为 d 则是目录。
- 当为 - 则是普通文件。
- 当前为 l(L) 则表示为链接文档。
- 当前为 b 则表示为装置文件里面的可提供储存的接口设备(可随机存取装置)。
- 若为 c 则表示为装置文件里面的串行端口设备,例如:键盘、鼠标(一次性读取装置)。
图片来源:https://www.runoob.com/linux/linux-file-attr-permission.html
- 从左至右用 0-9 这些数字来表示。
- 第 0 位确定文件类型,第 1-3 位确定属主(该文件的所有者)拥有该文件的权限。
- 第4-6位确定属组(所有者的同组用户)拥有该文件的权限,第7-9位确定其他用户拥有该文件的权限。
- 1、4、7 位表示读权限,如果用 r 字符表示,则有读权限,如果用 - 字符表示,则没有读权限。
- 2、5、8 位表示写权限,如果用 w 字符表示,则有写权限,如果用 - 字符表示没有写权限。
- 3、6、9 位表示可执行权限,如果用 x 字符表示,则有执行权限,如果用 - 字符表示,则没有执行权限。
2.2、linux文件属主和属组
[root@localhost /]# ls -l
总用量 32
lrwxrwxrwx. 1 root root 7 11月 24 19:31 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 11月 24 19:41 boot
drwxr-xr-x. 2 root root 6 11月 25 00:20 cangls
drwxr-xr-x. 19 root root 3280 11月 25 17:16 dev
drwxr-xr-x. 2 root root 6 11月 25 01:43 dir
drwxr-xr-x. 144 root root 8192 11月 26 17:41 etc
drwxr-xr-x. 3 root root 17 4月 10 2018 home
lrwxrwxrwx. 1 root root 7 11月 24 19:31 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 11月 24 19:31 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 4月 10 2018 media
drwxr-xr-x. 3 root root 17 11月 24 19:35 mnt
drwxr-xr-x. 2 root root 6 11月 26 17:45 newtest1
drwxr-xr-x. 3 root root 15 4月 10 2018 opt
dr-xr-xr-x. 217 root root 0 11月 25 17:15 proc
drwxr-xr-x. 2 root root 6 11月 24 19:42 rich
dr-xr-x---. 9 root root 4096 11月 26 20:22 root
drwxr-xr-x. 42 root root 1240 11月 26 18:29 run
lrwxrwxrwx. 1 root root 8 11月 24 19:31 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 4月 10 2018 srv
dr-xr-xr-x. 13 root root 0 11月 25 17:15 sys
drwxr-xr-x. 5 root root 41 11月 26 18:26 TEST
drwxr-xr-x. 2 root root 6 11月 26 17:45 test1
drwxrwxrwt. 35 root root 4096 11月 26 22:57 tmp
drwxr-xr-x. 13 root root 4096 11月 24 19:31 usr
drwxr-xr-x. 21 root root 4096 11月 24 19:31 var
- 在linux中,每一个文件都有一个特定的所有者。即对该文件有所有权的用户。
- 系统中的用户是按组划分的,一个用户属于一个或多个组。
- 文件所有者以外的用户可以分为文件所有者同组用户和其他用户。
- linux为文件分配不同的用户,具体为所有者用户,所有者同组用户与其他用户。它们对该文件有不同的访问权限。
- 文件所有者用户称为属主,与属主同组的用户称为属组,其他用户。
- 属主对文件有可读、可写、可执行的权限;属组对文件有可读、可执行的权限;其他用户对文件也有可读、可执行的权限。
- root用户,一般情况下,文件的权限对其不起作用。
2.3、更改文件属性
2.3.1、chgrp:更改文件属组
- 语法:chgrp[-R] 属组名 文件名
- -R:递归更改文件属组,就是在更改某个目录文件的属组时,如果加上-R的参数,那么该目录下的所有文件的属组都会更改。
[root@localhost ~]# useradd wang 创建一个账号 wang
[root@localhost ~]# passwd 1234 密码:1234
passwd:未知的用户名 1234。
[root@localhost ~]# 1234
bash: 1234: 未找到命令...
[root@localhost ~]# ls -l 查看/root目录下的文件信息
总用量 4
-rw-------. 1 root root 2745 11月 24 19:19 anaconda-ks.cfg
drwxr-xr-x. 2 bin bin 6 11月 25 00:28 dir1
drwxr-xr-x. 2 root jack 6 11月 25 00:29 file1
drwxr-xr-x. 3 root root 18 11月 25 00:29 file2
drwxr-xr-x. 2 root root 6 11月 25 00:30 file3
drwxr-xr-x. 2 root root 6 11月 26 23:31 test.log
[root@localhost ~]# chgrp wang file3 将file3的属组改为wang
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 2745 11月 24 19:19 anaconda-ks.cfg
drwxr-xr-x. 2 bin bin 6 11月 25 00:28 dir1
drwxr-xr-x. 2 root jack 6 11月 25 00:29 file1
drwxr-xr-x. 3 root root 18 11月 25 00:29 file2
drwxr-xr-x. 2 root wang 6 11月 25 00:30 file3
drwxr-xr-x. 2 root root 6 11月 26 23:31 test.log
[root@localhost ~]#
2.3.2、chgrp --help
[root@localhost user]# 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 isthe 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 theownership 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 aGROUP value-R, --recursive operate on files and directories recursivelyThe 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 linkto a directory, traverse it-L traverse every symbolic link to a directoryencountered-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'
2.3.3、chown:更改文件属主,也可以同时更改属组
语法:
chown [–R] 属主名 文件名
chown [-R] 属主名:属组名 文件名
chown [-R] 属主名 文件名user@localhost ~]$ su root
密码:
[root@localhost user]# cd ~
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 2745 11月 24 19:19 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 11月 26 23:31 test.log[root@localhost ~]# chown wang test.log
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 2745 11月 24 19:19 anaconda-ks.cfg
drwxr-xr-x. 2 wang root 6 11月 26 23:31 test.log
chown [-R] 属主名:属组名 文件名
[root@localhost ~]# ll 显示/root目录下的文件信息
总用量 4
-rw-------. 1 root root 2745 11月 24 19:19 anaconda-ks.cfg
drwxr-xr-x. 2 bin bin 6 11月 25 00:28 dir1
drwxr-xr-x. 2 root jack 6 11月 25 00:29 file1
drwxr-xr-x. 3 root root 18 11月 25 00:29 file2
drwxr-xr-x. 2 root wang 6 11月 25 00:30 file3
drwxr-xr-x. 2 wang root 6 11月 26 23:31 test.log[root@localhost ~]# chown wang:jack file2
[root@localhost ~]# ls-l
总用量 4
-rw-------. 1 root root 2745 11月 24 19:19 anaconda-ks.cfg
drwxr-xr-x. 2 bin bin 6 11月 25 00:28 dir1
drwxr-xr-x. 2 root jack 6 11月 25 00:29 file1
drwxr-xr-x. 3 wang jack 18 11月 25 00:29 file2
drwxr-xr-x. 2 root wang 6 11月 25 00:30 file3
drwxr-xr-x. 2 wang root 6 11月 26 23:31 test.log
2.3.4、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 isthe 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 theownership 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 thanspecifying OWNER:GROUP values-R, --recursive operate on files and directories recursivelyThe 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 linkto a directory, traverse it-L traverse every symbolic link to a directoryencountered-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'
2.4、chmod:更改文件9个属性
2.1.4、概述
Linux文件属性有两种设置方法:数字、符号。
linux有9种基本权限,分别为owner/group/others(拥有者/组/其他),三种身份各有自己的read/write/execute权限。文件的权限字符-rwxrwxrwx,这9个权限是三个三个一组的,用数字表示各个权限为:
- r:4
- w:2
- x:1
每种身份(owner/group/others)各自的三个权限(r/w/x)分数需要累加,例如当权限为:-rwxrwx---分数则是
owner=rwx=4+2+1=7group=rwx=4+2+1=7others=---=0+0+0=0该文件的权限数字为770,变更为chmod的语法为:
chmod [-R] xyz 文件或目录
选项与参数:
xyz : 就是刚刚提到的数字类型的权限属性,为 rwx 属性数值的相加。
-R : 进行递归(recursive)的持续变更,亦即连同次目录下的所有文件都会变更
2.4.2、案例
将dir1文件所有权限都设定为启用
[root@localhost ~]# ls -al dir1
总用量 4
drwxr-xr-x. 2 bin bin 6 11月 25 00:28 .
dr-xr-x---. 10 root root 4096 11月 27 00:25 ..
[root@localhost ~]# cmdod 550 dir1[root@localhost ~]# chmod 550 dir1
[root@localhost ~]# ls -al dir1
总用量 4
dr-xr-x---. 2 bin bin 6 11月 25 00:28 .
dr-xr-x---. 10 root root 4096 11月 27 00:25 ..
2.4.3、符号类型改变文件权限
- user:用户
- group:组
- others:其他
我们用u,g,o代表以上三种身份的权限。
另外a代表all,即全部身份,读写的权限为r,w,x
chmod |
u g o a |
+(加入) -(除去) =(设定) |
r w x |
文件或目录 |
假设将文件权限设置为 -rwxr-xr-- ,可以使用 chmod u=rwx,g=rx,o=r 文件名 来设定:
[root@localhost ~]# touch test0
[root@localhost ~]# ls -al test0
-rw-r--r--. 1 root root 0 11月 27 02:17 test0
[root@localhost ~]# chmod u=rwx,g=rx,0=r test0
chmod: 无效模式:"u=rwx,g=rx,0=r" --错误 零=r
Try 'chmod --help' for more information.
[root@localhost ~]# chmod u=rwx,g=rx,o=r test0
[root@localhost ~]# ls -al test0
-rwxr-xr--. 1 root root 0 11月 27 02:17 test0
[root@localhost ~]#
去除所有用户的执行权限
[root@localhost ~]# chmod a -x test0 -- 正确的语法为:chmod a-x test0
chmod: 无法访问"a": 没有那个文件或目录
[root@localhost ~]# chmod a-x test0
[root@localhost ~]# ls -al test0
-rw-r--r--. 1 root root 0 11月 27 02:17 test0
Linux CentOS7 文件属性相关推荐
- LINUX CentOS7安装字体库
LINUX CentOS7安装字体库 2017年12月26日 17:06:07 q260996583 阅读数:4866更多 个人分类: linux JAVA画图时常用到Font 类对象 这样的对象依赖 ...
- Linux Centos7.x 安装部署Mysql5.7几种方式的操作手册
简述 Linux Centos7.x 操作系统版本下针对Mysql的安装和使用多少跟之前的Centos6之前版本有所不同的,下面介绍下在centos7.x环境里安装mysql5.7的几种方法: 一. ...
- linux Centos7下安装python3及pip3
linux Centos7下安装python3及pip3 先去python官网下载python3安装包 执行命令: wget https://www.python.org/ftp/python/3.6 ...
- Linux/Centos7系统管理之进程与计划任务管理
Linux/Centos7系统管理之进程与计划任务管理查看进程--静态和动态查看控制进程--条件和全部at 一次性任务设置crontab 周期性任务设置一.程序和进程的关系(1)概念程序:保存在硬盘. ...
- linux把目录下的文件设置属性为rx,LINUX的文件属性与目录配置
LINUX的文件属性与目录配置 发布时间:2008-03-20 10:28:46来源:红联作者:cnbtoo -rwxrwxrwx 1 root root 293 Oct 19 21:24 test ...
- Linux CentOS7 中 完美解决VMTools失效,windows 与 Liunx间完美复制文件,无报错的解决方案
Linux CentOS7 中 完美解决VMTools失效,windows 与 Liunx间完美复制文件,无报错的解决方案 DreamFollowerss 2020-05-25 12:03:08 3 ...
- linux centos7 安装最新版git 教程
2019独角兽企业重金招聘Python工程师标准>>> linux centos7 安装最新版git 教程 2018年08月22日 11:53:59 alisen169 阅读数:13 ...
- linux centos7 mysql_Linux centos7环境下安装MySQL的步骤详解
Linux centos7环境下安装MySQL的步骤详解 安装MySQL mysql 有两个跟windows不同的地方 1).my.ini 保存到/etc/my.ini 2).用户权限,单独用户执行 ...
- mysql linux centos7_MySQL在Linux centos7环境下安装教程详解(图)
这篇文章主要为大家详细介绍了Linux centos7环境下MySQL安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 Linux centos7环境下安装MySQL步骤的详细介绍 安装My ...
最新文章
- 阿里钉钉陈航发布10亿“春雨计划”,推进企业级市场服务创新
- 微信小程序-canvas绘制文字实现自动换行
- 中小企业IT不可错过的十大虚拟机软件
- 【SignalR学习系列】7. SignalR Hubs Api 详解(JavaScript 客户端)
- input file HTML控件控制
- linux中的权限管理,Linux中的用户和权限管理
- cookie知识总结
- 噪声的频谱分析的重要意义_噪声测试及频谱分析
- 【openGauss 1.1.0测试:全量备份和恢复增量备份和恢复】
- 消防报警图形显示装置linux,中级消防设施操作员考点:消防控制室图形显示装置...
- 通过EverEdit工具连接Linux系统远程操作文件
- 多因子量化选股(1)
- 【Mysql进阶-3】大量实例悟透EXPLAIN与慢查询
- 初一计算机下册理论知识,有关七年级信息技术下册的教学计划
- windows服务器443端口被占用---云锁
- 怎样学习嵌入式软件开发
- 如何制作Mountain Lion系统镜像
- Linux中使用iptables记录网络访问日志
- dispatch_source
- 兄弟Brother MFC-8520DN 驱动