리눅스에서 chattr 는 해당 파일을 일반 사용자가 수정할수 없도록 읽기 전용으로 만들어 주는 역할을 한다.
chattr 명령어는 루트만 사용 가능하다.

[root@ruo91 ~]# chattr
Usage: chattr [-RV] [-+=AacDdijsSu] [-v version] files…

간단하게 사용하는 방법은 chattr +i 파일 하면 읽기 전용으로 속성이 변경 된다.
읽기 전용 속성을 해제 하기 위해서는 chattr -i 파일 로 – 를 사용하여 해제 해주면 된다.

hello.c 라는 소스 파일을 수정할수 없도록 chattr 명령어로 사용하고 lsattr 로 속성이 변경 되었는지 확인 한다.

[root@ruo91 ~]# cat > hello.c
#include <stdio.h>
int main(void)
{
printf(“hello? world!\n”);
return 0;
}
[root@ruo91 ~]# lsattr hello.c
————- hello.c
[root@ruo91 ~]# chattr +i hello.c
[root@ruo91 ~]# lsattr hello.c
—-i——– hello.c

 

읽기 전용으로 변경 되어 있는지 vi 에디터로 확인 해본다.

[root@ruo91 ~]# vi hello.c
#include <stdio.h>
int main(void)
{
printf(“hello? world!\n”);
return 0;
}
~
~
“hello.c” [readonly] 6L, 81C

 

vi 에디터에서 a 나 i 를 눌러 편집 모드로 변경해보면 아래와 같이 읽기전용이라 수정할수 없다고 나온다.

[root@ruo91 ~]# vi hello.c
#include <stdio.h>
int main(void)
{
printf(“hello? world!\n”);
return 0;
}
~
~
— INSERT — W10: Warning: Changing a readonly file

+i 모드가 적용된 상태에서 해당 파일을 삭제 시도 해보면 삭제를 할수 없다.
이럴떄는 반드시 -i 로 읽기 속성을 빼줘야한다.

 

US-UTF8 환경 쉘

[root@ruo91 ~]# rm -f hello.c
rm: cannot remove `hello.c’: Operation not permitted

 

eucKR 환경 쉘

[root@ruo91 ~]# rm -f hello.c
rm: cannot remove `hello.c’: 명령이 허용되지 않음

 

-i 모드로 읽기 전용 해제 후 삭제

[root@ruo91 ~]# chattr -i hello.c
[root@ruo91 ~]# lsattr hello.c
————- hello.c
[root@ruo91 ~]# rm -f hello.c