LoginSignup
0
0

CentOS8(XFSのファイルシステム)でlsattrコマンド、chattrコマンドを実行

Posted at

シェルのテストで、ファイルを削除するときにエラーを発生させたい、というのがあり、lsattrコマンドとchattrコマンドを使ってみました。

manでコマンドを調べてみると、

  • lsattr - Linux第2拡張ファイルシステム(ext2fs)上にあるファイルの属性(attribute)を表示する
  • chattr - Linux第2拡張ファイルシステム(ext2fs)上にあるファイルの属性(attribute)を変更する

とあったので、CentOS8のXFSのファイルシステムでも使用できるか試してみました。

環境

  • OS : CentOS Linux release 8.5.2111
  • ファイルシステム : XFS
[root@centos85 ~]# cat /etc/redhat-release
CentOS Linux release 8.5.2111
[root@centos85 ~]#
[root@centos85 ~]# df -T
ファイルシス        タイプ   1K-ブロック    使用   使用可 使用% マウント位置
devtmpfs            devtmpfs      993228       0   993228    0% /dev
tmpfs               tmpfs        1012180       0  1012180    0% /dev/shm
tmpfs               tmpfs        1012180    8608  1003572    1% /run
tmpfs               tmpfs        1012180       0  1012180    0% /sys/fs/cgroup
/dev/mapper/cl-root xfs         14034944 1905464 12129480   14% /
/dev/sda1           xfs          1038336  187364   850972   19% /boot
tmpfs               tmpfs         202436       0   202436    0% /run/user/0
[root@centos85 ~]#

試した内容

touchコマンドで空のファイルを作成します。

実行結果
[root@centos85 ~]# touch sample.txt
[root@centos85 ~]# ls -l sample.txt
-rw-r--r--. 1 root root 0  7月  2 17:28 sample.txt
[root@centos85 ~]#

lsattrコマンドでファイルの属性を確認します。

実行結果
[root@centos85 ~]# lsattr sample.txt
-------------------- sample.txt
[root@centos85 ~]#

chattrコマンドでi属性(変更不可の属性)を付与します。

実行結果
[root@centos85 ~]# chattr +i sample.txt
[root@centos85 ~]# lsattr sample.txt
----i--------------- sample.txt
[root@centos85 ~]#

i属性(変更不可の属性)が付与された状態でrmコマンドでsample.txtを削除してみます。

実行結果
[root@centos85 ~]# rm -f sample.txt
rm: 'sample.txt' を削除できません: 許可されていない操作です
[root@centos85 ~]# echo $?
1
[root@centos85 ~]#

しっかり削除できないようになりました。rmコマンドの戻り値も「0」ではなく「1」が返ってきます。

chattrコマンドでi属性(変更不可の属性)を外します。

実行結果
[root@centos85 ~]# chattr -i sample.txt
[root@centos85 ~]# lsattr sample.txt
-------------------- sample.txt
[root@centos85 ~]#

i属性(変更不可の属性)を外した状態でrmコマンドでsample.txtを削除してみます。

実行結果
[root@centos85 ~]# rm -f sample.txt
[root@centos85 ~]# echo $?
0
[root@centos85 ~]#

i属性(変更不可の属性)を外すと削除でき、rmコマンドの戻り値も「0」でが返ってきました。

ちょっと試したところ、XFSのファイルシステムでも、lsattrコマンド、chattrコマンドは使えそうです。

参考

Linux コマンド 一覧表(manページ一覧)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0