特殊权限高级命令  


特殊权限

前面我们学习过linux的基本权限,但如果只有基本权限,可能无法满足各式各样的要求
例如:建立一个公共目录 任何人都可以在目录里建立自己的文件,但只能删除自己的文件,此时基本权限就无能为力了.
如果你想要完成这种需求就必须要借助linux的特殊权限;特殊权限可以更精密的定义文件的权限;
之前我们看到的umask是0022,其中第一个0就是描述的特殊权限.


这类特殊权限共有三种;
suid  sgid  sticky在了解特殊权限的功能前,先来复习一下基本权限的获取流程

开始--->[ root ]--->[  赋予所有权限  ]---\
 [ user ]--->[ 委派user位权限 ]----\
 [ group]--->[ 委派group位权限]----/ 结束
 [ other]--->[ 委派other位权限]---/

那现在来看下第一个特殊权限 SUID
 限定:只能设置在二进制可执行程序上,对目录无效和文本无效
 功能:不管谁来执行程序,linux都以程序的拥有者身份进入权限获取流程中从而决定存取权限
 特征:在uesr位的x显示为S或s,s代表包含了x权限,S代表未包含x权限

试验演示:
 试验一: 用户修改密码借助root身份
 # ll /usr/bin/passwd
     -rwsr-xr-x 1 root root  /usr/bin/passwd
 # ll /etc/shadow
     -r-------- 1 root root  /etc/shadow
 # chmod u-s /usr/bin/passwd
 # ll /usr/bin/passwd
     -rwxr-xr-x 1 root root  /usr/bin/passwd
 # su - seker
 $ passwd
     Changing password for user seker.
     Changing password for seker
     (current) UNIX password:
     passwd: Authentication token manipulation error
 $
 试验二:用户无法读取/etc/shadow,借用root身份使用cat命令则可
 # su - seker
 $ cat /etc/shadow
     cat: /etc/shadow: 权限不够
 $ exit
     logout
 # chmod u+s /bin/cat
 # su - seker
 $ cat /etc/shadow
     root:$1$EV/a2BnK$pRN0qjwqLf8zvpK8w1MFT.:14360:0:99999:7:::


了解了SUID,我们再来看看SGID
 限定:SGID既可以作用于二进制文件又可以作用于目录,但两者的意义却截然不同
 功能:
 先说在二进制文件上,与前面讲的SUID类似:不管是谁来执行,都以文件的所属组身份来决定权限
 学员自己测试
 再说作用于目录上:默认情况下用户建立文件时,文件的所属组是用户的主组,如果在设置了SGID目录下建立文件,则文件的所属组是继承目录的属组,并且新建立的目录也继承g+s权限
 特征:在group位的x显示为S或s,s代表包含了x权限,S代表未包含x权
 试验一:
 # mkdir /home/public
 # chmod g+s !$
 # su - seker
 $ cd ../public
 $ touch sgid_yes
 $ ll sgid_yes
     -rw-rw-r-- 1 seker root sgid_yes


确切的说:UID GID共有四种,一种就是前面我们学习的,也是常见的用户的UID和GID,它们的真实有名字叫做真实UID和真实GID
  而另外两种叫做EUID EGID,就是有效UID和有效GID.有效的这组是为进程访问文件存取而存在的.
  我们的命令大部分都会产生进程;系统就可靠euid egid来判断能否存取文件
  在没有set之前 euid=uid egid=gid;而设置了后,则各自独立;
  set uid 会改变euid;
  set gid 会改变egid;
 
 [seker@stu254 ~]$ id zorro
 uid=501(zorro) gid=501(zorro) groups=501(zorro)
 [seker@stu254 ~]$
 [seker@stu254 ~]$ id
 uid=500(seker) gid=500(seker) groups=500(seker)
 [seker@stu254 ~]$ ll /bin/cat
 ---S-----x 1 zorro zorro 23100 2006-11-28 /bin/cat
 [seker@stu254 ~]$ ll /opt/file
 ----r----- 1 root seker 7 06-15 19:19 /opt/file
 [seker@stu254 ~]$ cat /opt/file
 sdfsdf
 [seker@stu254 ~]$
 
 cat文件被设置了SET UID,则seker用户执行时有效这组是这样: euid=501(zorro) egid=500(seker)
 针对/opt/file的权限进入权限匹配流程
  是否是root  --> 否
  是否是user  --> 否
  是否是group --> 是 于是拿到 r-- 的权限 所以能查看/opt/file的内容
 那现在把/opt/file改动一下
 [root@stu254 opt]# chown :zorro /opt/file
 [seker@stu254 ~]$ ll /opt/file
 ----r----- 1 root zorro 7 06-15 19:19 /opt/file
 [seker@stu254 ~]$
 [root@stu254 opt]# cat /opt/file
 cat: /opt/file: 权限不够
 [root@stu254 opt]#
  是否是root  --> 否
  是否是user  --> 否
  是否是group --> 否
  于是拿到others的 --- 的权限 所以不能查看/opt/file的内容

 [seker@stu254 ~]$
 [seker@stu254 ~]$ id seker
 uid=500(seker) gid=500(seker) groups=500(seker)
 [seker@stu254 ~]$ ll /bin/cat
 ------s--x 1 zorro zorro 23100 2006-11-28 /bin/cat
 [seker@stu254 ~]$ ll /opt/file
 ----r----- 1 root zorro 7 06-15 19:19 /opt/file
 [seker@stu254 ~]$ cat /opt/file
 sdfsdf
 [seker@stu254 ~]$

 cat 被设置了 set gid,则seker用户执行时有效这组是这样:euid=500(seker) egid=501(zorro)
  是否是root  --> 否
  是否是user  --> 否
  是否是group --> 是 于是拿到 r-- 的权限 所以能查看/opt/file的内容


 
sticky 冒险位(黏贴位)
 限定:只作用于目录
 功能:任何人都可以在一个目录下建立文件,却只有root和建立者本人才可以删除文件
 特征:在other位的x显示为T或t,t代表包含了x权限,T代表未包含x权限
 
 # ll /tmp -d
      drwxrwxrwt 5 root root /tmp
 # su - seker
 $ cd /tmp
      -rw------- 1 zorro zorro zorro-file
 $ rm -rf zorro-file
      rm: 无法删除 “zorro-file”: 不允许的操作


设定方法:
 字符模式
 chmod u+s file
 chmod g+s dir/file
 chmod o+t dir
 数字模式:
 chmod 4755 file
 chmod 2755 dir/file
 chmod 1777 dir

 
文字处理高级命令
 输入输出重定向
 标准输入 设备:键盘 文件 标记:0
 标准输出 设备:屏幕 终端 标记:1
 错误输出 设备:屏幕 终端 标记:2
 
输入输出流程:

    
 APP 输入<-- 键盘
    
  |
 APP 处理
  |
     / 1 标准输出 \
 APP 输出-->    ---> 屏幕
     \ 2 错误输出 /

如果我想把一个程序的输出错误存放到单独的一个文件中,那么我们在这个流程中该如何介入呢?
其实很简单,只是用标记符来控制输入的源和输出的目标.
 试验:
   ls > out.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则覆盖
   ls >> out.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则追加
   ls 2> err.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则覆盖
   ls 2>> err.file 将标准输出定向到文件 如果文件不存在则创建,如果文件存在则追加
   ls > out.file 2> err.file 将标准输出与标准错误分别定向到文件
   ls &> all.file  将标准错误和标准输出合并定向到文件
   ls >/dev/null 2>&1 讲标准错误和标准输出合并定向到系统黑洞

   cat < infile  将文件内容读出做cat命令的输入
   # cat << EOF  here document
   > 123
   > abc
   > EOF
   123
   abc
   #
   用here document避免交互输入
   # passwd << EOF
   > linuxcom
   > linuxcom
   > EOF
   Changing password for user root.
   passwd: all authentication tokens updated successfully.
   #
 
/dev/null /dev/zero 介绍
 /dev/null 是系统的黑洞
 /dev/zero 是系统的零发生器
 dd if=/dev/zero of=./big_file bs=10 count=1M
 
 
 wc 计算文件的行数,单词数,字节数
 # wc /etc/passwd
   40   59 1800 /etc/passwd
 # wc -l /etc/passwd
   40 /etc/passwd
 # wc -w /etc/passwd
   59 /etc/passwd
 # wc -c /etc/passwd
   1800 /etc/passwd
 #
 
 cut 按列提取文件
 -d 指明列分隔符 -f 选择输出的区域 -c 指定字符位置
 # cut -d: -f 1,7 /etc/passwd |head -n 2
     root:/bin/bash
     bin:/sbin/nologin
 # cut -c 1-3,6-9 /etc/passwd |head -n 2

 tr 字符的删除替换
  -d 删除
  # tr -d :  < /etc/passwd |head -n 2
  替换
  # tr [a-z] [A-Z]  < /etc/passwd |head -n 2

 sort 排序输出
  默认按首字符从头至尾的顺序排序
  -r 逆序
  -n 按数字排序
  -t 指明分隔符 与 -k 连用
  -k 按指定的域排序
  sort -t: -gk 3 /etc/passwd
 # sort /etc/passwd -t: -gk 3
 
 diff 对比两文件的差异
 d 删除了(delete) -a 新增了(append) -c 改变了(change)
 cp /etc/passwd .
 删几行 改几行 加几行
 diff /etc/passwd passwd
 做解释
 
 | 管道妙用
 将上一个命令的标准输出,传递给下一个命令做标准输入
 cat /etc/passwd | head -n 3 | cut -d: -f 1,3,7 |sort -rt: -k 3 |tr [a-z] [A-Z] | wc
 
 xargs
 前面我们学习了管道,管道只是让后面的命令从前一个命令获取输入
 那我们要建立一个/etc/passwd第一域(用户名)的目录的话,只利用管道就无法实现了
 # cut -d: -f 1 /etc/passwd |head -n 5 | mkdir
     mkdir: 缺少操作数
     请尝试执行“mkdir --help”来获取更多信息。
 # cut -d: -f 1 /etc/passwd |head -n 5 | xargs mkdir
 # ls
     adm  bin  daemon  lp  root
 #

AWK/SED简单使用
 awk -F: '{print}' file
 sed -n 's/old/new/p' file

grep家族
 grep
 fgrep
 pgrep
 egrep

 正则介绍

   ^ 行首
   $ 行尾
   . 除了换行符以外的任意单个字符
   * 前导字符的零个或多个
   .* 所有字符
   [] 字符组内的任一字符
   [^] 对字符组内的每个字符取反(不匹配字符组内的每个字符)
   ^[^] 非字符组内的字符开头的行
   [a-z] 小写字母
   [A-Z] 大写字母
   [a-Z] 小写和大写字母
   [0-9] 数字
   \< 单词头 单词一般以空格或特殊字符做分隔,连续的字符串被当做单词
   \> 单词尾

 扩展正则 加 -r 参数 或转义
   sed -n '/roo\?/p' /etc/passwd 
   sed -rn '/roo?/p' /etc/passwd
   ? 前导字符零个或一个
   + 前导字符一个或多个
   abc|def abc或def
   a(bc|de)f abcf 或 adef
   x\{m\}   x出现m次
   x\{m,\}  x出现m次至多次(至少m次)
   x\{m,n\} x出现m次至n次

 
查找文件
 which 搜索命令的位置 搜索的源是内存中的命令别名和$PATH
 # which lslll
     /usr/bin/which: no lslll in ($PATH)
 # which ls
   alias ls='ls --color=tty'
   /bin/ls
 #
 locate 搜索所有文件 搜索的源始updatadb库 库定期更新 所以不能搜到最新的资料
 locate passwd | head -n 3

 find 搜索真是文件系统,搜索方式多样
 find .
  -type 类型 f d l p c b
  -name 名称 可以通配
  -size 大小 +1M 大于1M,-1M 小于1M,1M 等于1M
  -user 文件拥有者
  -group文件属组
  -perm 权限 有+ -时0是通配;
   +代表(或)三组权限匹配其中之一;比如 r-x 满足r-- --x r-x 三个都成立   
   -代表(与)三组权限同时匹配; 比如 r-xr-xr-x 满足r-----xr-x 也算成立
  -o    或
  -not  非
  -ls   详细信息
  -exec CMD {} \; -ok CMD {} \;
  -mtime +3 从当天向历史天数推算的第三天前(三天前 不包含第三天)
  -atime -3 从当前向历史天数推算的前三天至当天这个段范围
  -ctime 3  从当天向历史天数推算的第三天
  与管道连用 | xargs

压缩和解压
 .gz
 解压1:gunzip FileName.gz
 解压2:gzip -d FileName.gz
 压缩:gzip FileName
 .bz2
 解压1:bzip2 -d FileName.bz2
 解压2:bunzip2 FileName.bz2
 压缩: bzip2 -z FileName

 创建各种不同类型的压缩文件
 tar cvf etc_init.d.tar /etc/init.d/
 tar xvf etc_init.d.tar /etc/init.d/

 查看压缩文件中的内容
 tar tvf  etc_init.d.tar

 tar 不过是一个打包工具;
 若需要进行对打包文件进行压缩 还需要其他工具gzip gunzip bzip2 bunzip2
 这些工具已经被tar所集成
 tar cvzf etc_init.d.tar.gz /etc/init.d/
 tar cvjf etc_init.d.tar.bz2 /etc/init.d/
 
 zip etc-backup.tar.bz2.zip etc-backup.tar.bz2
 unzip etc-backup.tar.bz2.zip
 gz gunzip etc-backup.gz


备份还原dump restore
 tar也可以备份,若对小量数据备份没有问题,但数据量每日的地增量不多,原始数据又很大的话
 用tar备份就很不适合了.因为相同的数据每天都要重复备份,既占空间又耗费时间和资源
 用dump则可以做差异备份
 
        差异:只做上一次备份后的变更数据
 备份级别 0-9,0是完全备份,1,2,3...做上一次备份后的变更数据
 -u 更新 /etc/dumpdatas 数据库
 -f 备份文件
   试验:
 完全备份/boot分区到/tmp/boot.dump文件
 # dump -0uf /tmp/boot.dump /boot
 备份自上一次备份(0级)后的所有变更数据
 # dump -1uf /tmp/boot.dump /boot
 备份自上一次备份(1级)后的所有变更数据
 # dump -2uf /tmp/boot.dump /boot
 备份自上一次备份(0级)后的所有变更数据,也自0级备份后的所有变更
 # dump -1uf /tmp/boot.dump /boot
 
查看备份文件中的内容
 restore -tf /tmp/boot.dump

恢复 完全恢复 指定文件恢复
 完全恢复
 # restore -rf /tmp/boot.dump
 交互式部分恢复
 # restore -if /tmp/boot.dump
 restore > ls initrd-2.6.18-128.el5.img
 initrd-2.6.18-128.el5.img
 restore > add initrd-2.6.18-128.el5.img
 restore > ls initrd-2.6.18-128.el5.img
 *initrd-2.6.18-128.el5.img
 restore >
 restore > extract
   You have not read any volumes yet.
   Unless you know which volume your file(s) are on you should start
   with the last volume and work towards the first.
   Specify next volume # (none if no more volumes): 1
   Mount tape volume 1
   Enter ``none'' if there are no more tapes
   otherwise enter tape name (default: /tmp/boot.dump)
   resync restore, skipped 3 blocks
   set owner/mode for '.'? [yn] y
 restore >
 add后文件会被标记为*
 因为备份时可能会分片,我们备份出来的只是一个文件,所以写1就好了.如果是多个片的话则逐一指明.