目录

一、chattr命令

  chattr命令用来修改文件系统的权限属性,只有 root 用户可以使用,建立凌驾于 rwx 基础权限之上的授权。

PS:chattr 命令不宜对目录 /、/dev/、/tmp/、/var/ 等进行设置,严重者甚至容易导致系统无法启动。

格式:
chattr [+-=] [选项] 文件或目录名

选项:

+    増加权限
- 删除权限
= 等于某权限
i 如果对文件设置属性,不允许对文件进行删除、改名,也不能添加和修改数据;如果对目录设置 i 属性,只能修改目录下文件中的数据,不允许建立和删除文件
a 如果对文件设置 a 属性,只能在文件中増加数据,不能删除和修改数据;如果对目录设置 a 属性,只允许在目录中建立和修改文件,不允许删除文件
e Linux 中大多数文件都默认拥有 e 属性,表示该文件是使用 ext 文件系统进行存储的,而且不能使用"chattr -e"命令取消 e 属性

给文件赋予属性:

# 创建测试文件
[root@VM_0_10_centos ~]# mkdir -p /study
[root@VM_0_10_centos ~]# cd /study/
[root@VM_0_10_centos study]# touch tfile.txt
[root@VM_0_10_centos study]# lsattr tfile.txt
-------------e-- tfile.txt # 添加属性 +i表示加锁,文件不能被删除,修改,移动
[root@VM_0_10_centos study]# chattr +i tfile.txt
[root@VM_0_10_centos study]# lsattr tfile.txt
----i--------e-- tfile.txt
[root@VM_0_10_centos study]# rm -rf tfile.txt
rm: cannot remove ‘tfile.txt’: Operation not permitted
[root@VM_0_10_centos study]# mv tfile.txt tfile2.txt
mv: cannot move ‘tfile.txt’ to ‘tfile2.txt’: Operation not permitted
[root@VM_0_10_centos study]# vi tfile.txt
能输入,但左下方会显示“W10: Warning: Changing a readonly file”
并且也不能保存

给目录赋予权限:

# 创建测试目录
[root@VM_0_10_centos study]# mkdir dtest
[root@VM_0_10_centos study]# touch dtest/test.txt # 添加i属性,加锁,目录不能被删除,也不能在该目录下创建文件和目录,但能修改该目录下文件的内容
[root@VM_0_10_centos study]# chattr +i dtest/
[root@VM_0_10_centos study]# lsattr dtest/
-------------e-- dtest/test.txt # 不能创建目录和文件
[root@VM_0_10_centos study]# mkdir -p dtest/dir2
mkdir: cannot create directory ‘dtest/dir2’: Permission denied
[root@VM_0_10_centos study]# touch dtest/ftest2.txt
touch: cannot touch ‘dtest/ftest2.txt’: Permission denied # 能修改目录下文件内容
[root@VM_0_10_centos study]# echo hello >> dtest/test.txt
[root@VM_0_10_centos study]# cat dtest/test.txt
hello # 不能删除目录及目录下的文件
[root@VM_0_10_centos study]# rm -rf dtest/
rm: cannot remove ‘dtest/test.txt’: Permission denied
[root@VM_0_10_centos study]# rm -rf dtest/test.txt
rm: cannot remove ‘dtest/test.txt’: Permission denied

PS:root用户也不能删除,修改它。如果要修改和删除,需去掉i属性

[root@VM_0_10_centos study]# chattr -i dtest/
[root@VM_0_10_centos study]# lsattr dtest/
-------------e-- dtest/test.txt
[root@VM_0_10_centos study]# chattr -i tfile.txt
[root@VM_0_10_centos study]# lsattr tfile.txt
-------------e-- tfile.txt

案例:比如将备份的日志放入一个只能添加数据,不能删除数据的目录下

# 创建备份目录
[root@VM_0_10_centos study]# mkdir baklog # 赋予a属性
[root@VM_0_10_centos study]# chattr +a baklog/ # 添加数据进去
[root@VM_0_10_centos study]# cp tfile.txt baklog/
[root@VM_0_10_centos study]# ls baklog/
tfile.txt # 不能删除该目录下的数据
[root@VM_0_10_centos study]# rm -rf baklog/tfile.txt
rm: cannot remove ‘baklog/tfile.txt’: Operation not permitted # 能修改该目录的文件内容
[root@VM_0_10_centos study]# echo test >> baklog/tfile.txt
[root@VM_0_10_centos study]# cat baklog/tfile.txt
test

二、lsattr命令

  lsattr命令用于查看文件和目录的属性。

格式:

lsattr [选项]  文件或目录名

选项:

-a    显示所有文件和目录,包括隐藏文件
-d 如果目标是目录,则仅列出目录本身的属性,而不会列出文件的属性
-R  递归列出所有子目录中文件的属性

案例:查看目录属性

# 查看目录属性,需要加上-d
[root@VM_0_10_centos study]# lsattr -d baklog/
-----a-------e-- baklog/

三、sudo命令

  管理员作为特权用户,可授权普通用户协助完成日常管理。现在较为流行的工具是 sudo,几乎所有 Linux 都已默认安装。sudo 的操作对象是系统命令,也就是 root 把本来只能由超级用户执行的命令赋予普通用户执行。简单的说,sudo 是一种权限管理机制,管理员可以授权于一些普通用户去执行一些 root 执行的操作,而不需要知道 root 的密码。

sudo 使用简单,管理员 root 使用 visudo 命令即可编辑其配置文件 /etc/sudoers 进行授权。命令如下:

[root@VM_0_10_centos study]# visudo

格式说明:

user ALL=(ALL) ALL
%userg ALL=(ALL) ALL
user ALL=(ALL) NOPASSWD: ALL
%userg ALL=(ALL) NOPASSWD: ALL # 第一行:允许用户youuser执行sudo命令(需要输入密码).
# 第二行:允许用户组youuser里面的用户执行sudo命令(需要输入密码).
# 第三行:允许用户youuser执行sudo命令,并且在执行的时候不输入密码.
# 第四行:允许用户组youuser里面的用户执行sudo命令,并且在执行的时候不输入密码.

【linux命令】权限管理命令(chattr、lsattr、sudo)的更多相关文章

  1. linux笔记:linux常用命令-权限管理命令

    一个文件的权限只有root和所有者可以更改. 权限管理命令:chmod(改变文件或目录的权限) 权限的数字表示: 用权限加减的方式改变权限(u代表所有者,g代表所属组,o代表其他人,a代表所有人): ...

  2. [Linux] 010 权限管理命令 chmod

    1. 权限管理命令:chmod 命令名称:chmod 命令英文原意:change the permissions mode of a file 命令所在路径:/bin/chmod 执行权限:所有用户 ...

  3. Linux基本命令 权限管理命令

    1.权限管理命令chmod ================================================================================== 命令名 ...

  4. Linux 基础——权限管理命令chown、chgrp

    一.chown命令与chgrp命令的作用 有时你需要改变文件或目录的属主,比如有人离职或开发人员创建了一个在测试或生产环境中需要归属在系统账户下的应用.Linux提供了两个命令来实现这个功能:chow ...

  5. Linux 基础——权限管理命令chmod

    一.Linux中的文件权限与目录权限 Linux中定义了3种访问权限,分别是r.w.x.其中r表示对象是可读的,w表示对象是可写的,x表示对象是可执行的,这3种权限组成一组rwx分别对应对象的3个安全 ...

  6. linux中权限管理命令(chmod/chown/chgrp/unmask)

    目录 chmod chown chgrp umask chmod 解释 命令名称:chmod 命令英文原意:change the permissions mode of a file 命令所在路径:/ ...

  7. linux常用命令-权限管理命令

    chmod  [{ugoa}{+-=}{rwx}] [文件或目录] [mode=421] [文件或目录] -R 递归修改 例:chmod g+w,o-r 文件或目录 但是一般用数字配置权限,例:chm ...

  8. Linux经常使用命令-权限管理命令-其它权限管理命令

    命令名称:chown 英文: change file ownership 命令所在路径:/bin/chown 语法:chown [用户][文件或者文件夹] 功能描写叙述:改变文件或者文件夹的全部者 范 ...

  9. Linux命令-权限管理命令:chown

    选项:-R 处理指定目录以及其子目录下的所有文件 useradd wangyunpeng 创建一个用户名为wangyunpeng的用户 passwd wangyunpeng 给wangyunpeng这 ...

  10. Linux命令-权限管理命令:umask

    umask -S 显示用户创建目录或文件时的默认权限 mkdir shuaige 创建一个shuaige目录 ls -ld shuaige 查看shuaige目录当前的权限(和上面默认的权限是一样的) ...

随机推荐

  1. Django django-cors-headers实现防跨域

    安装 pip install django-cors-headers 注册应用 INSTALLED_APPS = ( ... 'corsheaders', ... ) 中间层设置 MIDDLEWARE ...

  2. JSON对象转JAVA对象--com.alibaba.fastjson.JSONObject

    打印结果:

  3. 关于 Comparable 的使用

    作为一名刚上路的超初级程序员,今天终于可以迈开自己的第一步,写一篇博客.把我自己都感动哭了. 今天看面试题时看到了一个Comparable 的使用,才发现自己好像并没有使用过这个接口,具体这个接口是怎 ...

  4. 02-Nginx配置

    一.Nginx配置 1.创建Nginx运行使用的用户 www: / usr / sbin / groupadd www / usr / sbin / useradd -g www www 2.检查配置 ...

  5. java8-计算时间差的方法

    一.简述 在Java8中,我们可以使用以下类来计算日期时间差异: 1.Period 2.Duration 3.ChronoUnit 二.Period类 主要是Period类方法getYears(),g ...

  6. python访问Apollo获取配置

    操作系统 : CentOS7.3.1611_x64 Python 版本 : 3.6.8 Apollo源码地址: https://github.com/ctripcorp/apollo 访问Apollo ...

  7. 查看SpringBoot应用中的嵌入式tomcat的版本

    第一种,在启动springboot项目的时候,日志中可以看到 第二种,直接在maven依赖文件中查看 地址在:你的maven库文件夹/org/springframework/boot/spring-b ...

  8. 即将是史上最全的meta大全

    本文的目的是搜集当前主流的meta配置,方便开发者快速开发调试.在这里不会做各种meta的深入分析,只是简单的介绍,让大家知道有这个东西. meta简述 meta用于描述 HTML 文档的元数据.通常 ...

  9. ABAP - AT END OF 的使用

    TYPES: begin of ty_tab , num() type i, str() type c, end of ty_tab. data: gw_tab TYPE ty_tab , gt_ta ...

  10. jvm虚拟机笔记<一> 内存区域

    运行时数据区域: 程序计数器:字节码的行号指示器. 虚拟机栈:为每个方法创建一个栈帧(存放方法中的局部变量,变量引用等). 本地方法栈:存放本地方法. ------------------------ ...