Linux学习笔记 第五课

一、特殊权限 (lsattr,chattr)

二、文件隐藏权限(suid,sgid,stick)

三、查看命令(which,type,whereis,locate,find

四、ln链接文件 (软链接,硬链接)


一、特殊权限 (lsattr,chattr)

     1、lsattr  查看隐藏权限 i,a

[root@localhost dir2]# lsattr
-------------e- ./file.txt

-a 显示所有文件的特殊权限,

-V 显示lsattr 命令版本信息和当前目录文件特殊权限

-d 当前目录本身的权限

-R 递归显示目录及子目录文件

     2、chattr   (a,i)

[root@localhost ~]# lsattr +i  file1   不能修改,移动,重命名,删除文件或目录,甚至root也不可以

[root@localhost ~]# lsattr +a file1   不能修改,重命名,只能追加文件和目录

+i 实验

[root@localhost ~]# touch 1.txt
[root@localhost ~]# lsattr 1.txt
-------------e- 1.txt
[root@localhost ~]# chattr +i 1.txt

[root@localhost ~]# lsattr 1.txt
----i--------e- 1.txt
[root@localhost ~]# mv 1.txt 11.txt
mv: 无法将"1.txt" 移动至"11.txt": 不允许的操作
[root@localhost ~]# rm -rf 1.txt
rm: 无法删除"1.txt": 不允许的操作
[root@localhost ~]# echo abcd >>1.txt
-bash: 1.txt: 权限不够

+a实验

[root@localhost ~]# mkdir dir1

[root@localhost ~]# ll dir1
总用量 0
[root@localhost ~]# lsattr dir1
[root@localhost ~]# lsattr -d dir1
-------------e- dir1
[root@localhost ~]# chattr +a dir1
[root@localhost ~]# lsattr -d dir1     
-----a-------e- dir1
[root@localhost ~]# mv dir1 dir11    // 不允许重命名 
mv: 无法将"dir1" 移动至"dir11": 不允许的操作
[root@localhost ~]# chmod 777 dir1     // 不允许chmod,chown
chmod: 更改"dir1" 的权限: 不允许的操作
[root@localhost ~]# rm -rf dir1     // 不允许删除
rm: 无法删除"dir1": 不允许的操作
[root@localhost ~]# touch dir1/1.txt     // 运行创建文件
[root@localhost ~]# ls dir1
1.txt


二、隐藏权限

suid: 权限4,让普通用户拥有root权限执行命令和二进制可执行文件。之作用于文件

sgid: 权限2,对文件,与suid的作用相同。对目录,递归继承上级目录所属组

stick:权限1,防删除目录或文件,作用于其他用户。 只有所有者才可以删除文件或目录。其他人无权限删除


*******************************

小s代表有执行权限,大S表示无执行权限  *

*******************************


1、suid举例(取消普通用户passwd 命令)

passwd 命令作用于/etc/shadow 文件。其文件权限000。

[root@localhost ~]# which passwd
/usr/bin/passwd
[root@localhost ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 2月  22 2012 /usr/bin/passwd   
[root@localhost ~]# chmod u-s  /usr/bin/passwd 
[root@localhost ~]# ll /usr/bin/passwd
-rwxr-xr-x. 1 root root 30768 2月  22 2012 /usr/bin/passwd
[root@localhost ~]# ll /etc/shadow
---------- 1 root root 888 3月  19 09:38 /etc/shadow

[root@localhost ~]# su - user2
[user2@localhost ~]$ passwd            普通用户无法执行passwd命令
更改用户 user2 的密码 。
为 user2 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:
重新输入新的 密码:
passwd: 鉴定令牌操作错误


2、sgid举例(继承上级目录权限)

不使用g+s

[root@localhost ~]# chmod :user1 dir1

[root@localhost ~]# ll -d dir1      -d显示目录本身信息
drwxr-xr-x  3 root user1  4096 3月  19 09:52 dir1

[root@localhost ~]# mkdir dir1/d1

[root@localhost ~]# ll dir1
drwxr-xr-x 2 root root 4096 3月  19 09:55 d1

使用g+s

[root@localhost ~]# chmod g+s dir1
[root@localhost ~]# ll -d dir1
drwxr-sr-x 3 root user1 4096 3月  19 09:55 dir1  x变成了s
[root@localhost ~]# mkdir dir1/d2
[root@localhost ~]# ll dir1
drwxr-xr-x 2 root root  4096 3月  19 09:55 d1
drwxr-sr-x 2 root user1 4096 3月  19 09:58 d2

g+s 与chown的区别。chown只作用于现有目录,而g+s不能作用于现有目录或文件,作用于新建

[root@localhost ~]# chown -R :user1 dir1   
[root@localhost ~]# ll dir1
drwxr-xr-x 2 root user1 4096 3月  19 09:55 d1
drwxr-sr-x 2 root user1 4096 3月  19 09:58 d2
[root@localhost ~]# mkdir dir1/d3
[root@localhost ~]# ll dir1
drwxr-xr-x 2 root user1 4096 3月  19 09:55 d1
drwxr-sr-x 2 root user1 4096 3月  19 09:58 d2
drwxr-xr-x 2 root root  4096 3月  19 10:00 d3


3、t举例(防止删除)

[root@localhost ~]# ll -d /tmp    默认/tmp有t权限
drwxrwxrwt. 4 root root 4096 3月  19 03:13 

用user1创建一个文件

[root@localhost ~]# su - user1
[user1@localhost ~]$ echo  123456 >>/tmp/user1.txt
[user1@localhost ~]$ ll /tmp/user1.txt
-rw-rw-r-- 1 user1 user1 7 3月  19 10:05 /tmp/user1.txt
切换成user2,发现不能对user1.txt 删除

[user1@localhost ~]$ su - user2
密码:
[user2@localhost ~]$ echo abcdef >> /tmp/user1.txt
-bash: /tmp/user1.txt: 权限不够
[user2@localhost ~]$ rm -rf /tpm/user1.txt
[user2@localhost ~]$ rm -f /tmp/user1.txt
rm: 无法删除"/tmp/user1.txt": 不允许的操作

取消t权限
[user2@localhost ~]$ su - root
密码:
[root@localhost ~]# chmod o-t /tmp
[root@localhost ~]# ll -d /tmp
drwxrwxrwx. 5 root root 4096 3月  19 10:05 /tmp
[root@localhost ~]# su - user2
[user2@localhost ~]$ rm -rf /tmp/user1.txt
[user2@localhost ~]$ ll /tmp/  已经删除user1.txt


三、查看命令  (which,type,whereis,locate,find)

1、which ls  查看命令路径或别名

     which cd (内核内置的)

2、type cd  查看命令是否属于shell内置

[root@localhost ~]# type cd
cd is a shell builtin
[root@localhost ~]# type ls
ls is aliased to `ls --color=auto‘

3、whereis  类似which

[root@localhost ~]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
[root@localhost ~]# whereis cd
cd: /usr/share/man/man1/cd.1.gz

4、locate   查找文件

yum install -y mlocate

locate ls

updatedb   刷新库文件, 它每天会自动生成

5、find

(1) -type:按文件类型搜索(-type b,c,d,f,p,l)

find  /root  -type f     列出/root 下的文件,包括隐藏的

find  /root  -type d     搜/root下目录

(2) -name:按文件名

[root@localhost ~]# find /etc/ -name grub*

(3)按时间搜索(atime,ctime,mtime,amin,cmin,tmin)

find /etc   -name *.conf     -mtime +5  5天以前

find /etc   -name *.conf     -mmin   -60  60分钟内

find /etc   -name *.conf     -mmin -60 | xargs  ls -l

find /etc   -name *.conf     -mmin -60 -exec  ls -l {} \;   {} 表示前面命令的结果

****************************

atime    access  访问时间                 

ctime    change  更改文件属性的时间

mtime   modify  更改文件的内容     

用stat file1 查看文件或目录时间属性

vi file 会改3个属性

chmod 改 ctime

****************************

(4)!取反,除目录外的内容

find /etc/*   ! -type d

(5)-o 或者

find  /etc/ -type d -o -mtime -1 


四、软链接和硬链接

************************************************

创建软链接必须绝对路径,否则有时会出现奇葩问题 *

************************************************


[root@localhost ~]# ln -s /etc/selinux/  /tmp/1

[root@localhost ~]# cd /tmp/1

[root@localhost 1]# pwd

/tmp/1

[root@localhost 1]# pwd -P    // 显示真实目录

/etc/selinux

[root@localhost 1]# pwd -L

/tmp/1


硬链接:

不能作用于目录,不能跨分区,硬链接不分主次。

inode相同,文件同步。删除不影响另一链接文件

软链接:

类似于快捷方式。可作用于目录,可跨分区



-mtime 0 表示文件修改时间距离当前为0天的文件,即距离当前时间不到1天(24小时)以内的文件。
-mtime 1 表示文件修改时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时)的文件。
-mtime+1 表示文件修改时间为大于1天的文件,即距离当前时间2天(48小时)之外的文件
-mtime -1 表示文件修改时间为小于1天的文件,即距离当前时间1天(24小时)之内的文件



本文出自 “Linux学习笔记” 博客,请务必保留此出处http://genxin.blog.51cto.com/665191/1622158

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。