2018/08/25-linux

chattr: change file attributes on a linux file system.

The format of a symbolic mode is +-=[ai]

+:the selected attributes to be added to the existing attributes of the files;

-:the selected attributes to be removed

=:the selected attributes to be the only attributes that the files have.

the letters 'ai' select the new attribute for the files:

a:append only(只能添加)

i:immutable(不可改变的)

lsattr:list file attributes on a linux second extended file system.

root用户操作:

mkdir chapter7_1

ls -ld chapter7_1

drwxr-xr-x 2 root root 4096 Aug 25 15:14 chapter7_1/

chown myuser:mygroup chapter7_1

ls -ld chapter7_1
drwxr-xr-x 2 myuser mygroup 4096 Aug 25 15:14 chapter7_1

chmod 755 chapter7_1

ls -ld chapter7_1
drwxr-xr-x 2 myuser mygroup 4096 Aug 25 15:14 chapter7_1

root用户操作:

cd /tmp

touch attrtest

ls -l attrtest
-rw-r--r-- 1 root root 0 Aug 25 15:37 attrtest

chattr +i attrtest

ls -l attrtest
-rw-r--r-- 1 root root 0 Aug 25 15:37 attrtest 

rm attrtest

rm:cannot remove 'attrtest':Operation not permitted

chattr -i attrtest

rm attrtest

ls -l attrtest

ls:cannot access attrtest:No such file or directory (说明,已经删除attrtest)

root 用户操作:

cd /tmp

touch attrtest

ls -l attrtest

-rw-r--r-- 1 root root 0 Aug 25 15:46 attrtest

lsattr attrtest

--------------- attrtest

chattr +i attrtest

ls -l attrtest

-rw-r--r-- 1 root root 0 Aug 25 15:46 attrtest(使用ls,无法查看a、i这些隐藏属性)
lsattr attrtest
----i---------- attrtest                (使用lsattr,才能查看a、i这些隐藏属性)

rm attrtest

rm:cannot remove 'attrtest':Operation not permitted

chattr -i attrtest

rm attrtest

ls -l attrtest

ls:cannot access attrtest:No such file or directory(说明已经删除attrtest)

a(append only),i(immutable)等等这些隐藏属性,ls是无法查看的,lsattr才能查看。

使用chattr/lsattr时要小心,否则很容易造成困扰。

比如,给/ect/shadow添加了i属性,某天想要添加新用户却总添加不成,这时就要回去把/etc/shadow的i属性去掉了。

ls -ld /tmp

drwxrwxrwt 35 root root 585728 Aug 25 16:38 /tmp
ls -l /usr/bin/passwd

-rwsr-xr-x 1 root shadow 81856 Feb  1  2012 /usr/bin/passwd

set UID(SUID):s出现在文件所有者的x权限上。
 

ls -l /etc/shadow

-rw-r----- 1 root shadow 1115 Aug 23 20:11 shadow

ls -l /usr/bin/passwd
-rwsr-xr-x 1 root shadow 81856 Feb  1  2012 /usr/bin/passwd

所有账号的密码都记录在/etc/shadow 中,ls后可知,仅root用户对/etc/shadow具有可读可写权限。

但是,一般用户,比如myuser,是可以使用passwd来修改自己的密码的。

SUID仅对二进制程序有效,SUID对目录无效;

      ------>/usr/bin/passwd是二进制程序

运行者需要对该二进制程序具有x的可运行权限;

      ------>myuser作为others,对/usr/bin/passwd具有x的可运行权限

SUID权限仅在运行该二进制程序的过程中有效;

运行者在运行该二进制程序的过程中将获取该二进制程序的所有者权限;

      ------>/usr/bin/passwd的所有者是root,myuser运行passwd时,myuser可以短暂获得root权限,

              所以myuser运行passwd可以修改/etc/shadow(仅root对/etc/shadow具有可读可写权限)

  
 


 


 


 


 


 


 

猜你喜欢

转载自blog.csdn.net/qzw752890913/article/details/82051776