• L12.linux命令每日一练 -- 第二章 文件和目录操作命令 -- dirname和chattr命令


    2.17 dirname:显示文件或目录路径

    2.17.1 命令详解

    【命令星级】 ★★★☆☆

    【功能说明】

    ​ dirname命令用于显示文件或目录路径。

    【语法格式】

    dirname [name]
    dirname [<文件或目录>]
    
    • 1
    • 2

    ​ **说明:**dirname命令以及后面的选项和文件,每个元素之间都至少要有一个空格。

    2.17.2 使用范例

    ​ **范例2-83:**显示文件或目录路径。

    [root@centos7 ~]# dirname /data/dir1/file1.txt 	#只显示文件所在的路径。
    /data/dir1
    [root@centos7 ~]# cd /data/dir1/
    [root@centos7 /data/dir1]# dirname file1.txt 	
    .	#给dirname命令一个相对路径,它也会返回相对路径,当前目录(.)。
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.18 chattr:改变文件的扩展属性

    2.18.1 命令详解

    【命令星级】 ★★★☆☆

    【功能说明】

    ​ chattr命令用于改变文件的扩展属性。与chmod这个命令相比,chmod只是改变了文件的读、写、执行权限,更底层的属性控制是由chattr来改变的。

    【语法格式】

    chattr [option] [mode] [files]
    chattr [选项] [模式] [<文件或目录>]
    
    • 1
    • 2

    ​ **说明:**chattr命令以及后面的选项和文件,每个元素之间都至少要有一个空格。

    【选项说明】

    ​ 表2-19针对chattr命令的参数选项进行了说明。

    ​ 表2-19 chattr命令的参数选项及说明

    在这里插入图片描述

    2.18.2 使用范例

    ​ **范例2-84:**设置只能往文件里追加内容,但不能删除文件。

    [root@centos7 ~]# touch test
    [root@centos7 ~]# lsattr test	#lsattr查看文件的扩展属性。
    ---------------- test
    [root@centos7 ~]# chattr +a test	#+a添加追加属性。
    [root@centos7 ~]# lsattr test
    -----a---------- test
    [root@centos7 ~]# rm -f test 	#即使是root用户也无法删除。
    rm: cannot remove ‘test’: Operation not permitted
    [root@centos7 ~]# echo 111 >> test	#可以追加文本。
    [root@centos7 ~]# cat test
    111
    [root@centos7 ~]# echo 111 > test	#但是不能清空文件。
    -bash: test: Operation not permitted
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ​ **范例2-85:**对文件加锁,使其只能被执行读操作。

    [root@centos7 ~]# touch file1.txt
    [root@centos7 ~]# echo test >file1.txt 
    [root@centos7 ~]# cat file1.txt 
    test
    [root@centos7 ~]# chattr +i file1.txt 	#使用+i参数为文件加锁。
    [root@centos7 ~]# lsattr file1.txt 
    ----i----------- file1.txt
    [root@centos7 ~]# rm file1.txt	#root用户也无法删除文件。 
    rm: remove regular empty file ‘file1.txt’? y
    rm: cannot remove ‘file1.txt’: Operation not permitted
    [root@centos7 ~]# echo 111 > file1.txt 	#不能清空。
    -bash: file1.txt: Permission denied
    [root@centos7 ~]# echo 111 >> file1.txt 	#也不能追加。
    -bash: file1.txt: Permission denied
    [root@centos7 ~]# cat file1.txt 
    test	#可以查看。
    [root@centos7 ~]# chattr -i file1.txt	#使用-i参数解锁。
    [root@centos7 ~]# rm file1.txt 	
    rm: remove regular empty file ‘file1.txt’? y	#解锁后就可以删除了。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.18.3 安全优化实战

    ​ 下面利用-a和-i参数为大家讲解chattr在企业中的实战应用。

    ​ 为了避免恶意删除.bash_history历史记录文件或者重定向到/dev/null,又因为系统需要向这个文件写入历史记录,因此采用追加模式,只增不减。命令如下:

    [root@centos7 ~]# chattr +a .bash_history 	#为历史记录文件加上只能追加的属性。
    [root@centos7 ~]# lsattr .bash_history 
    -----a---------- .bash_history
    [root@centos7 ~]# chattr -a .bash_history 
    [root@centos7 ~]# lsattr .bash_history 
    ---------------- .bash_history
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ​ 如果系统锁定的文件不能被删除或修改,那么可以使用下面的命令来实现:

    [root@centos7 ~]# chattr +i /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/inittab 	#锁定系统关键文件。
    [root@centos7 ~]# lsattr /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/inittab 
    ----i----------- /etc/passwd
    ----i----------- /etc/group
    ----i----------- /etc/shadow
    ----i----------- /etc/gshadow
    ----i----------- /etc/inittab
    [root@centos7 ~]# useradd xx	#添加用户的命令,后面会详细讲解。
    useradd: cannot open /etc/passwd
    #说明:做完上面实验的读者,请立即解锁这些文件,因为以后学习都需要修改这些文件。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ​ 解锁命令如下:

    [root@centos7 ~]# chattr -i /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/inittab 
    [root@centos7 ~]# lsattr /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/inittab 
    ---------------- /etc/passwd
    ---------------- /etc/group
    ---------------- /etc/shadow
    ---------------- /etc/gshadow
    ---------------- /etc/inittab
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ​ **提示:**对于chattr功能的使用,黑客如果知道也能操作,因此,使用chattr的安全性只是相对的。

  • 相关阅读:
    [附源码]java毕业设计教务系统
    AcWing第 80 场周赛
    Web server failed to start. Port 8080 was already in use
    高德面试:为什么Map不能插入null?
    Week5
    使用Docker部署开源分布式任务调度系统DolphinScheduler
    MySQL数据库基本使用
    SpringCloud复习:(7)@EnableZuulProxy注解的作用
    8.0、C语言数据结构——线性表 (4)
    【爬虫】7.2. JavaScript动态渲染界面爬取-Selenium实战
  • 原文地址:https://blog.csdn.net/qq_25599925/article/details/125351559