2018-05-13 课后笔记

 

第三课预习内容
2.17 隐藏权限lsatt chattr
2.18 特殊权限set_uid
2.19 特殊权限set_gid
2.20 特殊权限stick_bit
2.21 软链接文件
2.22 硬连接文件
2.23/2.24/2.25 find命令
2.26 文件名后缀
2.27linux和windows互传文件

 2.17 隐藏权限lsattr chattr

2.17.1 chattr (change attribute)此命令是增加权限。格式为:chattr 【+-=】【Asaci】【文件或者目录】。+代表增加。-代表减少,=代表设定。选项的含义如下:

A:增加属性后,表示文件或目录的atime不能修改。

s:增加该属性后,会将数据同步写入磁盘中。

a:增加该属性后,表示只能追加不能删除,非root用户不能设定该属性。

c:增加该属性后,表示自动压缩该文件,读取时会自动解压。

i:增加该属性后,表示文件不能删除,重命名,设定链接,写入以及新增数据。

举例 i 的用法

[root@localhost ~]# ls -la
总用量 44
dr-xr-x---.  4 root root 4096 5月  12 23:17 .
dr-xr-xr-x. 17 root root 4096 5月   8 03:27 ..
drwxr-xr-x.  2 root root   24 5月  12 23:20 12
drwxr-xr-x.  2 root root    6 5月  12 23:17 34
-rw-------.  1 root root 1244 5月   8 03:28 anaconda-ks.cfg
-rw-------.  1 root root 5248 5月  13 22:00 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
-rw-------.  1 root root   67 5月  11 16:40 .lesshst
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc

[root@localhost ~]# chattr +i 12         【我们可以给12目录加个i权限】
[root@localhost ~]# touch 12/22        【然后在12目录下创建一个新的目录】
touch: 无法创建"12/22": 权限不够         【提示是权限不够的】

[root@localhost ~]# chattr -i 12          【-i代表减去这个权限】
[root@localhost ~]# touch 12/22        【我们在12目录下创建一个22目录】
[root@localhost ~]# ls -l 12/
总用量 8
-rw-r--r--. 1 root root 4 5月  12 23:21 11
-rw-r--r--. 1 root root 4 5月  12 23:21 12
-rw-r--r--. 1 root root 0 5月  16 04:30 22 【提示是可以成功的】

[root@localhost ~]# chattr +i 12
[root@localhost ~]# rm -f 12/22
rm: 无法删除"12/22": 权限不够         【同样在12目录下增加i权限后,是无法删除22目录的】

在上面例子中,给12目录增加i权限后,即使是root用户,也无法在目录12下创建或者删除目录或文件的。

举例a 的用法

[root@localhost ~]# chattr +a 12         【给目录12增加a权限】
[root@localhost ~]# rm -f 12/22           【删除这个目录】   
rm: 无法删除"12/22": 不允许的操作          【提示是不允许的】 
[root@localhost ~]# mkdir -p 12/2.txt     【在目录12下创建一个文件】
[root@localhost ~]# ls -l 12/
总用量 8
-rw-r--r--. 1 root root 4 5月  12 23:21 11
-rw-r--r--. 1 root root 4 5月  12 23:21 12
-rw-r--r--. 1 root root 0 5月  16 04:32 22
drwxr-xr-x. 2 root root 6 5月  16 04:40 2.txt    【显示示可以成功】

同理,其他含义的用法也是一样的

2.17.2  lsattr (list attribute),该命令用于读取文件或者目录的特殊权限,格式为lsattr [-aR]【文件或者目录名】

-a:类似于ls -a 的选项,就是连同隐藏文件一并列出。

-R:连同子目录的数据一同列出。

举例

[root@localhost ~]# lsattr 12     【查看12目录下的隐藏权限】      
---------------- 12/12
---------------- 12/11
----i----------- 12/22                【我们在这里看到12/22目录的隐藏权限i】
---------------- 12/2.txt
[root@localhost ~]# lsattr -aR 12   【我们看到加上-aR选项后,连同子目录都显示出来】           
-----a---------- 12/.                          【我们在上个例子中给目录12加上a权限】
---------------- 12/..
---------------- 12/12
---------------- 12/11
----i----------- 12/22
---------------- 12/2.txt

12/2.txt:
---------------- 12/2.txt/.
-----a---------- 12/2.txt/..
 

2.18 特殊权限set_uid

set uid 该权限是针对二进制可执行文件,使文件在执行阶段具有文件所有者的权限。

举例:

[root@localhost ~]# which passwd
/usr/bin/passwd
[root@localhost ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd   【这里passwd文件显示的是rws                                                                                                                   而不是普通的rwx】

如果想给一个目录设置一个set uid权限,是这样操作的

[root@localhost ~]# ls -l /usr/bin/ls
-rwxr-xr-x. 1 root root 117616 11月 20 2015 /usr/bin/ls  【ls的权限是rwxr-xr-x】

[root@localhost ~]# su - hongwei                                   【我们切换到一个hongwei用户】
上一次登录:三 5月 16 05:06:02 CST 2018pts/0 上
[hongwei@localhost ~]$ date
2018年 05月 16日 星期三 05:19:46 CST
[hongwei@localhost ~]$ ls /root                                    【ls /root/ 一下】
ls: 无法打开目录/root: 权限不够                                         【提示权限是不够的】
[hongwei@localhost ~]$ su - root
密码:
上一次登录:三 5月 16 05:07:20 CST 2018pts/0 上
[root@localhost ~]# chmod u+s /usr/bin/ls                【我们设置set uid权限是chmod u+s】
[root@localhost ~]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117616 11月 20 2015 /usr/bin/ 

[root@localhost ~]# su - hongwei                               【我们再次切换到宏伟用户】
上一次登录:三 5月 16 05:19:32 CST 2018pts/0 上
[hongwei@localhost ~]$ ls /root                                  【在ls /root/一下】
12  34  anaconda-ks.cfg                                   【在这里可以看到 就是因为加了uid的权限】 
[hongwei@localhost ~]$ su - root
密码:
上一次登录:三 5月 16 05:21:44 CST 2018pts/0 上
[root@localhost ~]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117616 11月 20 2015 /usr/bin/ls
[hongwei@localhost ~]$ ls /root/
12  34  anaconda-ks.cfg 

同样我们想取消这个uid权限 就用:chmod u-s /usr/bin/ls  就可以了 

举例 我们用u=rws 操作一下:

[root@localhost ~]# chmod u=rws /usr/bin/ls            
[root@localhost ~]# ls -l /usr/bin/ls
-rwSr-xr-x. 1 root root 117616 11月 20 2015 /usr/bin/ls      这里的S变成了大写 是因为缺少个x 
[root@localhost ~]# chmod u+x /usr/bin/ls                       要想变成s 直接加上x就可以了
[root@localhost ~]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117616 11月 20 2015 /usr/bin/ls       这里的rws变成了s了  

新的知识点:su - hongei 表示的是切换用户     不管set_uid权限上是大写S还是小写s 都没有关系都代表存在set_uid权限
 


2.19 特殊权限set_gid

set gid 该权限可以作用在文件上(二进制可执行文件),也可以作用在目录上。作用在文件上时,功能和set uid 一样,他会使文件在执行阶段具有文件按所属组的权限。目录设置这个权限后,任何用户在此目录下创建的文件都具有和该目录所属组相同的组

举例:

[root@localhost ~]# useradd user1
[root@localhost ~]# ls -ld /tmp/hw
drwxr-sr-x. 3 root root 14 5月  11 16:12 /tmp/hw
[root@localhost ~]# chown :user1 /tmp/hw
[root@localhost ~]# ls -ld /tmp/hw
drwxr-sr-x. 3 root user1 14 5月  11 16:12 /tmp/hw
[root@localhost ~]# touch /tmp/hw/22
[root@localhost ~]# ls -ld /tmp/hw
drwxr-sr-x. 3 root user1 23 5月  16 17:12 /tmp/hw
[root@localhost ~]# ls -ld /tmp/hw/22
-rw-r--r--. 1 root user1 0 5月  16 17:12 /tmp/hw/22
[root@localhost ~]# ls -l /tmp/hw
总用量 0
drwxr-xr-x. 2 root root  6 5月  11 16:12 2
-rw-r--r--. 1 root user1 0 5月  16 17:12 22
[root@localhost ~]# ls -l /tmp/hw/22
-rw-r--r--. 1 root user1 0 5月  16 17:12 /tmp/hw/22
[root@localhost ~]# ls -l /tmp/hw
总用量 0
drwxr-xr-x. 2 root root  6 5月  11 16:12 2
-rw-r--r--. 1 root user1 0 5月  16 17:12 22
drwxr-sr-x. 2 root user1 6 5月  16 17:23 23
[root@localhost ~]# touch /tmp/hw/23/1234
-rw-r--r--. 1 root user1 0 5月  16 17:23 1234
[root@localhost ~]# ls -ld /tmp/hw
drwxr-sr-x. 4 root user1 32 5月  16 17:23 /tmp/hw
[root@localhost ~]# ls -ld /tmp/hw/23
drwxr-sr-x. 2 root user1 17 5月  16 17:23 /tmp/hw/23
[root@localhost ~]# ls -l /tmp/hw/23
总用量 0
-rw-r--r--. 1 root user1 0 5月  16 17:23 1234
[root@localhost ~]# ls -la /tmp/hw/23

总用量 0
drwxr-sr-x. 2 root user1 17 5月  16 17:23 .
drwxr-sr-x. 4 root user1 32 5月  16 17:23 ..
-rw-r--r--. 1 root user1  0 5月  16 17:23 1234
上面例子中,set uid 和set gid用法基本相同但是唯一 一点不同的是 创建一个目录 修改目录的所属组,在目录下创建一个新的文件,那么set gid下连同目录的子目录所属组也是uer1

2.20 特殊权限stick_bit

stick_bit 可以理解为防删除位。文件是否可以被某用户删除,主要取决于该文件所在的目录是否对该用户具有写权限,如果没有写权限,则这个目录下的所有文件都不能删除,同时也不能添加新的文件。如果希望用户能够添加文件但不能删除该目录下其他用户的文件,则可以对父目录增加该权限。设置该权限后,就算用户对目录具有写权限,也不能删除其他用户的文件

[root@localhost ~]# ls -ld /tmp
drwxrwxrwt. 12 root root 4096 5月  16 16:41 /tmp   tmp下就是带t的权限,任何人是不能删除的

 

2.21 软链接文件 

软链接是建立在一个独立的文件上的,当读取这个链接文件时,它会把读取的行为转发到该文件所链接的文件上。软链接就是相当于windows下的快捷方式。例如,现在有一个文件a,我们做一个软链接文件b,b指向了a,当读取b时,b就会把读取的动作转发到a上,这样就读取了文件a,当我们删除文件a时,链接文件b不会被删除,但如果再次读取b时,会提示无法打开文件,然而,当我们删除b时,a是不会有任何影响的。

创建软链接的命令 ln 格式为ln【-s】【来源文件】【目的文件】

[root@localhost ~]# ls -l /bin
lrwxrwxrwx. 1 root root 7 5月   8 03:13 /bin -> usr/bin

我们可以看到权限位前面的l就是一个软链接文件,而usr/bin是它真正的目录。

如果我们不用-s选项就是一个硬连接文件

举例:

[root@localhost ~]# ln -s /tmp/yum.log /root/yum.log
[root@localhost ~]# ls -l yum.log
lrwxrwxrwx. 1 root root 12 5月  16 18:08 yum.log -> /tmp/yum.log
[root@localhost ~]# ls -ld /root
dr-xr-x---. 4 root root 4096 5月  16 18:08 /root
[root@localhost ~]# ls -l /root
总用量 4
drwxr-xr-x. 3 root root   45 5月  16 04:40 12
drwxr-xr-x. 2 root root    6 5月  12 23:17 34
-rw-------. 1 root root 1244 5月   8 03:28 anaconda-ks.cfg
lrwxrwxrwx. 1 root root   12 5月  16 18:08 yum.log -> /tmp/yum.log

我们创建一个yum.log的软链接,软链接到root下,在ls -l /root 下显示yum.log真正的目录是在/tmp/yum.log 

软链接总结:

  1. 当我们建立软链接的时候,目的文件不可以存在,否则建立不成功。
  2. 所有软链接本身就只能是一个文件!这个文件可以指向目录也可以指向文件。
  3. 当我们建立软链接的时候,一般情况下都是绝对路径。如果建立一个相对路径的软链接,会有弊端!当我们把所有的文件拷贝到另一台机器上,会发现源文件不存在

硬连接文件 

当系统要读取一个文件时,会先读取inode信息,然后在根据inode中的信息到块区域将数据取出来。而硬连接是直接建立一个inode连接到文件放置的块区域,即进行硬连接时该文件内容没有任何变化,只是增加一个指向这个文件的inode,并不会额外占用磁盘空间。

硬链接有二个限制:1.不能跨文件系统,因为不同的文件系统会有不用的inode table

                             2.不能连接目录

硬链接不支持目录,仅支持文件,不能跨分区,可删除,因为还有其他文件使用了inode

举例:

[root@localhost ~]# ln  /tmp/1.txt /root/1.txt
ln: "/tmp/1.txt": 不允许将硬链接指向目录             这个例子就是不能硬链接目录

[root@localhost ~]# ln /tmp/hw/23/1234  /root/1234
[root@localhost ~]# ls -l /root
总用量 4
drwxr-xr-x. 3 root root    45 5月  16 04:40 12
-rw-r--r--. 2 root user1    0 5月  16 17:23 1234
drwxr-xr-x. 2 root root     6 5月  12 23:17 34
-rw-------. 1 root root  1244 5月   8 03:28 anaconda-ks.cfg
lrwxrwxrwx. 1 root root    12 5月  16 18:08 yum.log -> /tmp/yum.log
[root@localhost ~]# ls -ld /root/1234
-rw-r--r--. 2 root user1 0 5月  16 17:23 /root/1234
[root@localhost ~]# ls -la /root/1234
-rw-r--r--. 2 root user1 0 5月  16 17:23 /root/1234
[root@localhost ~]# rm -f /root/1234
[root@localhost ~]# rm -f /root/1234
[root@localhost ~]# ls -la /root/1234
ls: 无法访问/root/1234: 没有那个文件或目录               可以删除硬链接的文件

  1. 硬链接与软链接区别:

    硬链接:

  2. 当系统要读取一个文件时,就会先去读 inode table, 一个文件对应一个 inode, 然后再根据 inode 中的信息到块区域去将数据取出来。硬链接是直接使用了和源文件相同的 inode, 硬链接文件直接链接到文件放置的块区域。 也就是说,进行硬链接时实际上该文件内容没有任何变化, 只是制定了相同的 inode。
  3. 创建硬链接会增加额外的记录项以引用文件,对应于同一文件系统上一个物理文件每个文件引用相同的 inode 号 创建时链接数递增
  4. 删除文件时:rm 命令递减计数的链接,文件要存在,至少有一个链接数当链接数为零时,该文件被删除
  5. 不能跨越驱动器或分区
    语法 : ln filename [linkname ]
  6. 软链接:

  7. 软链接是建立一个独立的文件, 而这个文件的作用是当读取这个链接文件时,他会把读取的行为转发到该文件所 链接 的文件上。
  8. 一个符号链接指向另一个文件。
  9. 可以对目录或文件进行软链接。
  10. 可以跨分区
  11. 指向的是另一个文件的路径;其大小为指向的路径字符串的长度。
  12. 不增加或减少目标文件inode 的引用计数。
    语法: ln -s filename [linkname]

新的知识点 df -h 查看用户进程 

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   28G  904M   27G    4% /
devtmpfs                 483M     0  483M    0% /dev
tmpfs                    493M     0  493M    0% /dev/shm
tmpfs                    493M  6.8M  487M    2% /run
tmpfs                    493M     0  493M    0% /sys/fs/cgroup
/dev/sda1                197M  110M   88M   56% /boot
tmpfs                     99M     0   99M    0% /run/user/0
 

 2.23/2.24/2.25 find命令

查找的命令有很多,

比如 whereis 次命令是通过预先生成的一个文件列表库查找与给出的文件相关的文件。格式为  whereis 【-bms】【文件名】

-b 只查找二进制文件 

-m 只查找帮助的文件(在man目录下)

-s 只查找源代码文件

[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
whereis  只是模糊查找,只要带ls 的文件就会例如,有局限性,很少用。

locate  locate类似于whereis,也是通过查找预先生成的文件列表库来告诉用户要查找的文件在哪里,后面直接跟用户名。如果linux没有这个命令安装命令如下

yum install -y mlocate

locate所所搜的文件列表,不管是目录名还是文件名,只要包含我们要搜索的关键词,都会列出来,不适用于精准搜索。

find

1.find这个搜索工具是用的最多的一个。格式为【路径】【参数】

-atime +n/-n 表示访问或执行时间大于或者小于n天的文件

-ctime +n/-n 表示写入、更改indoe属性的时间大于或小于n天的文件

-mtime +n/-n  表示写入时间大于或小于n天的文件

atime (access time)访问时间 读取执行时更改。

ctime(change time)属性权限更改时间

mtime(modify time)内容更改时间 写入文件时更改。

举例 用法

[root@localhost ~]# find /tmp/hw -atime -1
/tmp/hw
/tmp/hw/22
/tmp/hw/23
/tmp/hw/23/1234
[root@localhost ~]# find /tmp/hw -ctime -1
/tmp/hw
/tmp/hw/22
/tmp/hw/23
/tmp/hw/23/1234
[root@localhost ~]# find /root -mtime -1
/root
/root/.bash_history
/root/12
/root/12/22
/root/12/2.txt
/root/yum.log
[root@localhost ~]# 

2. type:按文件类型查找。文件类型包括:b块设备。d目录。f一般文件。c字符文件。p管道文件。l链接文件。

查找 /tmp 目录下的普通文件

find /tmp -type f

[root@localhost ~]# find /tmp -type s
[root@localhost ~]# find /tmp -type d
/tmp
/tmp/.X11-unix
/tmp/.XIM-unix
/tmp/.Test-unix
/tmp/.font-unix
/tmp/.ICE-unix
/tmp/hongweilinux
/tmp/hongweilinux/2
/tmp/hw
/tmp/hw/2
/tmp/hw/23
/tmp/tset
/tmp/tset/abc
/tmp/test_mv
/tmp/test_mv/dirb
/tmp/test_mv/dirb/dira
/tmp/test_mv/dirc
/tmp/1.txt

3.根据文件名查找文件,通配符需要用引号引起来。find /目录 -name "test*"   *代表是模糊搜索 就是所有test的文件 

[root@localhost ~]# find /tmp -name "test*"
/tmp/tset/test.1
/tmp/tset/test.2
/tmp/test_mv

4.-newer file:file为一个文件,列出比file新的文件

列出tmp目录下,创建修改file1之后的文件。

find /tmp -newer file1

5.根据用户和用户组

根据用户和用户组的权限查找

-uid n:n为用户的uid

在tmp目录下找出所有uid为500的文件。

find /tmp -uid 500

-username name :name为用户名

 找出tmp目录下所有用户wang的文件。

find /tmp -username wang

-gid n:n为用户的gid

同上

-nouser :寻找文件的所有者不存在的文件

-nogroup: 寻找文件所属组不存在的文件

找到不正常的文件

find / -nouser

6.根据文件权限和大小

-size [+-] xx:查找比xx要大(+)或小(-)的文件。(c代表byte,k代表1024bytes)

查找tmp目录下比file文件大的文件。 

find /tmp -size +file

在 /tmp文件夹下, 查找一个文件大小小于10K的文件。

find /tmp -size -10K

[root@localhost ~]# find /tmp -size -10k
/tmp
/tmp/yum.log
/tmp/.X11-unix
/tmp/.XIM-unix
/tmp/.Test-unix
/tmp/.font-unix
/tmp/.ICE-unix
/tmp/ks-script-43y7KI
/tmp/hongweilinux
/tmp/hongweilinux/2
/tmp/hw
/tmp/hw/2
/tmp/hw/22
/tmp/hw/23
/tmp/hw/23/1234
/tmp/tset
/tmp/tset/abc
/tmp/tset/test.1
/tmp/tset/test.2
/tmp/test_mv
/tmp/test_mv/dirb
/tmp/test_mv/dirb/dira
/tmp/test_mv/dirc
/tmp/dirb
/tmp/2.test
/tmp/3.test
/tmp/4.test
/tmp/1.txt
/tmp/qwer
[root@localhost ~]# 
 

7.-perm mode:查找文件权限刚好等于mode的文件,

文件权限为-rwsrwsret的文件 

find   / -perm  7777  -type  f

 

-perm -mode:查找文件权限最少包括mode的文件,

 查找rwxr--r--的文件,结果resr-xr-x的文件会被列出来。搜索的是0744 结果是4755

find   /   -perm   -0744

 

-perm +mode:查找文件权限包含任mode的文件

包含s或t的都会被列出来。 

find -perm  +7000

 

8.根据inode号

-inum xxx:根据inode号查找inode号相同的文件。查找硬链接文件

查找出来1.txt的硬链接文件。

find  /root/  -inum 33610734

9.根据时间来找文件

[root@localhost ~]# find /tmp -type f -mmin  +120
/tmp/yum.log
/tmp/ks-script-43y7KI
/tmp/hw/22
/tmp/tset/test.1
/tmp/tset/test.2
/tmp/dirb
/tmp/2.test
/tmp/3.test
/tmp/4.test
/tmp/qwer
[root@localhost ~]# 
 

find命令的特殊操作

1. -exec

-exec command : command为其他命令,exec后面可接其他命令来处理查询的结果。

在/tmp目录下查找文件类型为一般文件,文件名包含t的,文件大小要求大于1k切小于3k的文件,并使用ls -l 来查看其权限 

find   /tmp   -type   f   -name   t*  -size  +1k   -size  -3k -exec ls -l {}\;

[root@localhost ~]# find /var/log/ -type f -mtime +10 -exec cp {} {}.tmp \;

[root@localhost ~]# find /var/log/ -name *.tmp -exec rm {} \;

在/var/log/目录下10天之前的文件,拷贝为文件名.tmp 

在/var/log/目录下,查找文件后缀名为.tmp的文件。然后删除

这个命令的意思:前面的条件搜到的文件。交给-exec 后面的命令处理。{}代表前面搜索的结果。;有特殊意义,所以需要转义。

 2.26 文件名后缀

在linux系统中,文件名的后缀没有具体意义,但是为了区分,在定义一个文件名时加一个后缀,这样用户看到这个文件时就会知道是什么文件,

例如。1.sh 他是一个shell脚本

         2.tar.gz 代表是一个压缩包

         3.my.cnf 代表他是一个配置文件

         4.test.zip 代表是一个压缩文件

2.27linux和windows互传文件 

如何实现linux与windows互传文件  需要安装一个命令

yum install -y lrzsz

想要把linux文件传到windows上 使用命令sz 

sz 1.txt 就会弹出一个保存文件的对话框

想要把windows文件传到linux上 使用rz 

rz 1.txt 同样的道理 

 

 

 

转载于:https://my.oschina.net/u/3851487/blog/1813152

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值