前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >你所不知道Linux下的chattr命令

你所不知道Linux下的chattr命令

作者头像
JiekeXu之路
发布2019-08-15 14:52:26
1.4K0
发布2019-08-15 14:52:26
举报
文章被收录于专栏:JiekeXu之路JiekeXu之路JiekeXu之路

先简单介绍一下怎么和这个命令结缘的,在说明这个命令的用途。因本人公司要在一台Azure云服务器上搭建应用,此服务器是64位centos7.1的linux系统3.0以上内核。原由起初是无法使用XFTP等相关软件上传本地软件。配置sftp后重启服务然后reboot,一个reboot后无法使用CRT等ssh连接,立刻联系原厂解决,通过后台控制登陆进去,将之前搭建SFTP的配置文件/etc/ssh/sshd_config还原,重启sshd服务后可以正常连接;但是XFTP的问题还是依旧在,怎么办呢?通过查看XFTP日志以及secure日志初步判断在建立sftp会话后就出现问题。 Secure部分日志如下:

于是乎:跑去查看sftp-server这个文件,确实有很大发现,不知道之前长啥样,反正现在就长这样了:

看到/usr/libexec/openssh/sftp-server文件的权限变成了000,这样和默认的有出入啊,这就是无法建立sftp会话的原因。一看不对劲啊,这不合适,不要想着是谁干的了,就马上想到了chmod 775 sftp-server,立刻来一打这命令呗,可是出乎意料发现报错了,没有权限啊。怎么整?放大招呗,在Linux ext文件系统中,权限控制除了常规的chmod,还会有文件属性管理,故出现了chattr命令。 在linux系统中,内核在2.6以上的,均可执行此命令。通过chattr命令修改属性能够提高系统的安全性,但是它并不适合所有的目录。chattr命令不能保护/、/dev、/tmp、/var目录。lsattr命令是显示chattr命令设置的文件属性。这两个命令是用来查看和改变文件、目录属性的,与chmod这个命令相比,chmod只是改变文件的读写、执行权限,更底层的属性控制是由chattr来改变的。 先来看看lsattr和chattr的man手册吧.

[root@JiekeXu~]# man lsattr

---------------------------LSATTR(1) LSATTR(1)

NAME

 lsattr - list file attributes on a Linux second extended file systemSYNOPSIS
      lsattr [ -RVadv ] [ files...  ]DESCRIPTION
      lsattr lists the file attributes on a second extended file system.  See chattr(1) for a description of the attributes       and what they mean.OPTIONS
      -R     Recursively list attributes of directories and their contents.      -V     Display the program version.      -a     List all files in directories, including files that start with ‘.’.      -d     List directories like other files, rather than listing their contents.      -v      List the file’s version/generation number.
AUTHOR
      lsattr was written  by  Remy  Card  <Remy.Card@linux.org>.   It  is  currently  being  maintained  by  Theodore  Ts’o       <tytso@alum.mit.edu>.BUGS
      There are none :-).AVAILABILITY
      lsattr is part of the e2fsprogs package and is available from http://e2fsprogs.sourceforge.net.
 [root@JieKuXu etc]# man chattr
------------------------------CHATTR(1)                     General Commands Manual                                CHATTR(1)
NAME
      chattr - change file attributes on a Linux file system
SYNOPSIS
      chattr [ -RVf ] [ -v version ] [ mode ] files...
DESCRIPTION
      chattr changes the file attributes on a Linux file system.
      The format of a symbolic mode is +-=[aAcCdDeijsStTu].
      The  operator  '+'  causes  the  selected  attributes to be added to the existing attributes of the
      files; '-' causes them to be removed; and
      The letters 'aAcCdDeijsStTu' select the new attributes for the files: append  only  (a),  no  atime
      updates  (A), compressed (c), no copy on write (C), no dump (d), synchronous directory updates (D),
      extent format (e), immutable (i), data journalling (j), secure deletion  (s),  synchronous  updates
      (S), no tail-merging (t), top of directory hierarchy (T), and undeletable (u).
      The  following attributes are read-only, and may be listed by lsattr(1) but not modified by chattr:
      compression error (E), huge file (h), indexed directory  (I),  inline  data  (N),  compression  raw       access (X), and compressed dirty file (Z).       Not  all flags are supported or utilized by all filesystems; refer to filesystem-specific man pages
      such as btrfs(5), ext4(5), and xfs(5) for more filesystem-specific details.
OPTIONS
      -R     Recursively change attributes of directories and their contents.
      -V     Be verbose with chattr's output and print the program version.
      -f     Suppress most error messages.
      -v version

chattr命令的用法:chattr [ -RVf ] [ -v version ] [ mode ] files… 最关键的是在[mode]部分,[mode]部分是由+-=和[ASacDdIijsTtu]这些字符组合的,这部分是用来控制文件的属性。 + :在原有参数设定基础上,追加参数。 - :在原有参数设定基础上,移除参数。 = :更新为指定参数设定。 A:文件或目录的 atime (access time)不可被修改(modified), 可以有效预防例如手提电脑磁盘I/O错误的发生。 S:硬盘I/O同步选项,功能类似sync。 a:即append,设定该参数后,只能向文件中添加数据,而不能删除,多用于服务器日志文件安全,只有root才能设定这个属性。 c:即compresse,设定文件是否经压缩后再存储。读取时需要经过自动解压操作。 d:即no dump,设定文件不能成为dump程序的备份目标。 i:设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容。i参数对于文件 系统的安全设置有很大帮助。 j:即journal,设定此参数使得当通过mount参数:data=ordered 或者 data=writeback 挂 载的文件系统,文件在写入时会先被记录(在journal中)。如果filesystem被设定参数为 data=journal,则该参数自动失效。 s:保密性地删除文件或目录,即硬盘空间被全部收回。 u:与s相反,当设定为u时,数据内容其实还存在磁盘中,可以用于undeletion。 各参数选项中常用到的是a和i。a选项强制只可添加不可删除,多用于日志系统的安全设定。而i是更为严格的安全设定,只有superuser (root) 或具有CAP_LINUX_IMMUTABLE处理能力(标识)的进程能够施加该选项。 应用举例: 1、用chattr命令防止系统中某个关键文件被修改: # chattr +i /etc/resolv.conf 然后用mv /etc/resolv.conf等命令操作于该文件,都是得到Operation not permitted 的结果。vim编辑该文件时会提示W10: Warning: Changing a readonly file错误。要想修改此文件就要把i属性去掉: chattr -i /etc/resolv.conf lsattr /etc/resolv.conf #会显示如下属性 —-i——– /etc/resolv.conf 2、让某个文件只能往里面追加数据,但不能删除,适用于各种日志文件: # chattr +a /var/log/messages

好啦,补充知识就到这里,继续我们的话题,回归正题,查看我们的文件发现确实是这样:

[root@JiekeXu openssh]# lsattr sftp-server

----i--------e- sftp-server

证实了此文件被保护了,无法对其进行修改,所以只能撤掉对其的保护,OK,来一波试试,chattr –I sftp-server

[root@JiekeXu67 openssh]# chattr –i sftp-server

-bash: chattr: command not found

好玩啊,找不到命令,命令别人干掉了!!!怎么办?装呗,通过软件官网中进行下载即可:https://pkgs.org/download/e2fsprogs,网上找到了一个对应的源码包,进行安装了。(怎么上传上去的?大家很想知道吧,我偷偷告诉你,通过上传到同一网段的另一服务器,使用scp e2fsprogs-1.42.9-7.el7.x86_64 100.1.0.0:/ygsoft/下)

[root@JiekeXu ygsoft]# rpm -qf /usr/bin/chattr

e2fsprogs-1.42.9-7.el7.x86_64

[root@JiekeXu ygsoft]# rpm -ivh e2fsprogs-1.42.9-7.el7.src.rpm

Updating / installing...1:e2fsprogs-1.42.9-7.el7

################################# [100%]

[root@JiekeXu ygsoft]# chattr

-bash: chattr: command not found

[root@JiekeXu ygsoft]# which chattr

/usr/bin/which: no chattr in (/usr/java/jdk1.6.0_45/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

[root@JiekeXu ygsoft]# rpm -qf /usr/bin/chattr

e2fsprogs-1.42.9-7.el7.x86_64

看到这里,是不是很无语了,貌似没问题吧,确实是没问题,但命令就是找不到,神奇吧!没办法,配置yum源吧,换一种方法玩玩。通过协调原厂重配网络yum源,使用命令安装好了。

yum install chattr -y

[root@JiekeXu openssh]# which chattr/usr/bin/chattr

[root@JiekeXu67 openssh]# chattr -i sftp-server

[root@JiekeXu openssh]# lsattr sftp-server

-------------e- sftp-server

[root@JiekeXu openssh]# chmod 775 sftp-server

[root@JiekeXu openssh]# ll sftp-server

-rwxrwxr-x. 1 root root 63544 May 4 2015 sftp-server

[root@JiekeXu67 openssh]# lsattr sftp-server

-------------e- sftp-server

这样就完美了呗,然后使用XFTP连接此服务器就可以上传下载文件,部署程序了,心情一下愉快了好多。如要继续保护此文件,使用 chattr +i 文件即可。 总结:在日常的运维中,数据是很重要的,增加保护机制很重要,此过程主要是为了介绍一个不怎么常使用的命令chattr,但是对于文件、数据等保护性很高的一个命令,希望大家都能get到。

作者:JiekeXu,北漂者,一个初出茅庐,混迹于IT的单身屌丝男,想要了解的更多,请添加微信:xxq1426321293

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 JiekeXu之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档