Linux 基础学习训练教材 - CentOS 8.x

课程问题参考解答

单纯提供一个相对的解答,并不是标准答案!

最近更新时间: 2020/06/11

单纯就是个解答的参考,写完之后再来这边查查看答案跟你想的一样不一样!?

第 05 堂课

  • 5.1.2 课程上的那个表格填空结果
    操作动作/dir1/dir1/file1/dir2重点
    读取 file1 内容 xr- 目录:因为你已经知道文件的文件名是 file1,所以不需要使用 ls 去查找文件名,因此目录只要具备 x 即可。若不知道文件名,才需要有目录的 r。
    文件:需要知道内容才能读取,所以需要 r 的权限
    修改 file1 内容 xrw- 目录:目录的权限如同上面。
    文件:由于我们需要『修改』该文件内容,因此,file1 最好具备 r 的权限,否则会看不到之前的数据喔!
    运行 file1 内容 xrx (x)- 目录:你需要进入该目录才能够去运行在该目录内的文件啊!所以需要 x 即可。
    文件:若要运行该文件至少需要有 x 权限。但是,如果该文件是脚本档 (shell script),那得要看到内容后才有办法运行。 因此答案可能是 x 也可能是 rx。
    删除 file1 文件 wx-- 目录:因为你需要删除该文件名,因此至少得要能够进入以及更动文件名的权限,所以至少需要 wx 才行。不需要 r 的原因如第一点所示。
    文件:如同前一小节最后一个练习,要删除文件名与该文件无关,与该文件所在的目录有关喔!
    将 file1 拷贝到 /dir2 xrwx 来源目录与文件:就跟第一点一样,要能够读到该文件的权限
    目标目录:创建文件跟删除文件意义差不多,要能进入且修改文件名,所以需要 wx 。
    将 file1 移动到 /dir2 wx-(r)wx 同一文件系统中:假设这两个目录在同一个文件系统上面,那么移动文件名就跟文件本身无关,因此只需要两者目录均有更改文件名的权限 (wx) 即可。
    不同文件系统中:但是如果这两个目录是在不同的文件系统当中,那么文件就是『被拷贝后删除』,此时文件要加上 r 的权限才行!
  • 例题 5.1.2-1:
    1. 直接使用 root 的身份进行底下的任务即可:
      [student@station10-101 ~]$ su -
      密码:
      [root@station10-101 ~]# cd /dev/shm
      [root@station10-101 shm]# mkdir unit05
      [root@station10-101 shm]# chmod 775 unit05
      [root@station10-101 shm]# cd unit05
      [root@station10-101 unit05]# mkdir dir1
      [root@station10-101 unit05]# chmod 754 dir1
      [root@station10-101 unit05]# cp /etc/hosts dir1/file1
      [root@station10-101 unit05]# chmod 644 dir1/file1
      [root@station10-101 unit05]# mkdir dir2
      [root@station10-101 unit05]# chmod 751 dir2
      [root@station10-101 unit05]# cp /etc/hosts dir2/file2
      [root@station10-101 unit05]# chmod 644 dir2/file2
      [root@station10-101 unit05]# mkdir dir3
      [root@station10-101 unit05]# chmod 755 dir3
      [root@station10-101 unit05]# cp /etc/hosts dir3/file3
      [root@station10-101 unit05]# chmod 666 dir3/file3
      [root@station10-101 unit05]# mkdir dir4
      [root@station10-101 unit05]# chmod 777 dir4
      [root@station10-101 unit05]# cp /etc/hosts dir4/file4
      [root@station10-101 unit05]# chmod 600 dir4/file4
      [root@station10-101 unit05]# ll -d . dir* dir*/*
      drwxrwxr-x. 6 root root 120 Mar 21 21:53 .
      drwxr-xr--. 2 root root  60 Mar 21 21:52 dir1
      -rw-r--r--. 1 root root 158 Mar 21 21:52 dir1/file1
      drwxr-x--x. 2 root root  60 Mar 21 21:53 dir2
      -rw-r--r--. 1 root root 158 Mar 21 21:53 dir2/file2
      drwxr-xr-x. 2 root root  60 Mar 21 21:53 dir3
      -rw-rw-rw-. 1 root root 158 Mar 21 21:53 dir3/file3
      drwxrwxrwx. 2 root root  60 Mar 21 21:54 dir4
      -rw-------. 1 root root 158 Mar 21 21:54 dir4/file4
      
    2. 使用 student 的身份进行底下的动作,并说明原因为何
      a. 的部份
      [student@station10-101 ~]$ cd /dev/shm/unit05/
      [student@station10-101 unit05]$ ll dir1
      ls: 无法访问 'dir1/file1': 拒绝不符权限的操作
      总计 0
      -????????? ? ? ? ?             ? file1
      # 因为有 dir1 的 r 权限,因此可以看到『文件名列表』没问题。
      # 但是,因为没有 x 的权限,因此无法针对 file1 的详细属性进行查阅
      # 所以,就像上面这样,权限与属性的部份会没权限!文件名是可以看到的!
      
      [student@station10-101 unit05]$ ll dir2
      ls: cannot open directory 'dir2': 拒绝不符权限的操作
      # 不用讲,因为没有 dir2 的 r 权限,所以看不到任何东西!
      
      [student@station10-101 unit05]$ ll dir3
      总计 4
      -rw-rw-rw-. 1 root root 158  3月 21 21:53 file3
      [student@station10-101 unit05]$ ll dir4
      总计 4
      -rw-------. 1 root root 158  3月 21 21:54 file4
      # 因为有 dir3, dir4 的 r 与 x 权限,所以可以看到『文件名列表』与权限!
      
      b. 的部份 [student@station10-101 unit05]$ ll dir1/file1 ls: 无法访问 'dir1/file1': 拒绝不符权限的操作 # 查看文件名数据与目录的权限有关。因为 student 仅有 dir1 的 r 没有 x 的权限, # 所以不能将 file1 拿出来看,因此只能看到文件名,无法知道文件属性。 [student@station10-101 unit05]$ ll dir2/file2 -rw-r--r--. 1 root root 158 3月 21 21:53 dir2/file2 # 与上面不同,student 具有 dir2 的 x 权限,而且已经知道 file2 的文件名, # 所以不需要 dir2 的 r 也能得到 file2 的文件名数据,也能直接看到 file2 的权限属性! [student@station10-101 unit05]$ ll dir3/file3 -rw-rw-rw-. 1 root root 158 3月 21 21:53 dir3/file3 [student@station10-101 unit05]$ ll dir4/file4 -rw-------. 1 root root 158 3月 21 21:54 dir4/file4 # 因为具有 dir3, dir4 的 rx 权限,所以可以直接查看到 file3, file4 的文件名与权限
      至于 c 的部份,请自行使用 vim 去编辑文件,然后进行强制保存 (:w!) ,会得到底下的结果:
      • vim dir1/file1:(1)不能读取 file1 内容,是因为没有 dir1 的 x 权限; (2)不能强制保存,原因与第 1 点相同。
      • vim dir2/file2:(1)可以看到文件内容,因为有 dir2 的 x 权限,也有 file2 的 r 权限; (2)不能强制保存,因为没有 file2 的 w 权限, 同时也没有 dir2 的 w 权限之故。
      • vim dir3/file3:(1)可以看到文件内容,因为有 dir3 的 x 权限,也有 file3 的 r 权限; (2)可以写入,因为有 file3 的 w 权限。
      • vim dir4/file4:(1)不能读取 file4 内容,因为没有 file4 的 r 权限; (2)可以强迫写入,是因为 student 具有 dir4 的 wx 权限, 因此,该文件『可以被删除后重新创建一个相同文件名的文件』。所以,当你强制写入之后,回去 ls -l dir4/file4 , 就可以很明显的发现,dir4/file4 文件拥有者变成 student 了!这个地方最重要!
  • 例题 5.2.2-1:
    1. 找出 PID 为 1 的,因为需要找寻系统的数据,所以,只能用 pstree -p 或 ps aux 去找寻
      a. 部份
      [student@station10-101 ~]$ pstree -p | grep '(1)'
      systemd(1)-+-ModemManager(973)-+-{ModemManager}(991)
      # 所以得到答案为 systemd 啰!
      
      [student@station10-101 ~]$ ps aux | grep ' 1 '
      root         1  0.0  0.5 244840 10084 ?        Ss    3月16   0:04 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
      student  16374  0.0  0.0  12108  1068 pts/0    S+   15:58   0:00 grep --color=auto  1
      
      不论是 pstree 还是 ps aux ,要找 PID 为 1,可以通过找到 (1) 及 1 单独存在 (1 的两边有个空白),就可以找到了。所以答案就是 systemd 这个指令。 至于 b 要找到 crond 的 PID,同样可以通过这个方式来找到:
      [student@station10-101 ~]$ pstree -p | grep crond
                 |-crond(1523)
      [student@station10-101 ~]$ ps aux | grep crond
      root      1523  0.0  0.1  36088  3220 ?        Ss    3月16   0:00 /usr/sbin/crond -n
      student  16406  0.0  0.0  12108   972 pts/0    S+   16:01   0:00 grep --color=auto crond
      
      都可以找到 PID 为 1523。另外要注意,每个系统的 PID 都不一样,所以,在你的系统上面看到的数值不同,也是合理的!
    2. 先来使用 student 登录系统,并且重复切换帐号:
      [student@station10-101 ~]$ su -
      密码:
      [root@station10-101 ~]# su - student
      [student@station10-101 ~]$ su -
      Password:
      [root@station10-101 ~]# ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      4 S     0 16424 14132  0  80   0 - 47473 -      pts/0    00:00:00 su <==这里变成 root
      4 S     0 16430 16424  0  80   0 -  6639 -      pts/0    00:00:00 bash
      4 S     0 16460 16430  0  80   0 - 47467 -      pts/0    00:00:00 su <==这里变成 student
      4 S     0 16487 16461  0  80   0 - 47470 -      pts/0    00:00:00 su <==这里又变成 root
      4 S     0 16491 16487  0  80   0 -  6639 -      pts/0    00:00:00 bash
      0 R     0 16515 16491  0  80   0 - 11191 -      pts/0    00:00:00 ps
      
      你可以很轻松的查阅到,下一行的 PPID 就是前一行的 PID (几乎啦!),其实整笔输出应该还少了一个项目, 那就是 bash 这个进程!毕竟每运行一次 su 后,系统就会给予一个新的权限的 bash,但是上面只输出 2 个而已啊! 此外,也只有 root 权限才能往前追踪~也因为已经使用了 3 次 su 啊,所以当然要输入 3 次 exit 后, 才能回到原本的 bash 喔!
    3. 需要找到 ps 的应用,才能够『只输出我需要的部份』这样的结果。如果使用 man ps 去查找,那可以找到很前面的 example 的部份, 它有这些东西的介绍,相当有趣:
      [root@station10-101 ~]# man ps
      ......
      EXAMPLES
             To see every process on the system using standard syntax:
                ps -e
                ps -ef
                ps -eF
                ps -ely
      
             To see every process on the system using BSD syntax:
                ps ax
                ps axu
      
             To print a process tree:
                ps -ejH
                ps axjf
      
             To get info about threads:
                ps -eLf
                ps axms
      
             To get security info:
                ps -eo euser,ruser,suser,fuser,f,comm,label
                ps axZ
                ps -eM
      
             To see every process running as root (real & effective ID) in user format:
                ps -U root -u root u
      
             To see every process with a user-defined format:
                ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
                ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
                ps -Ao pid,tt,user,fname,tmout,f,wchan
      ......
      
      题目说,我们只需要 pid, pri, ni 与指令 (可用 comm 或 args),因此,可以这样处理:
      [root@station10-101 ~]# ps -eo pid,pri,ni,args
        PID PRI  NI COMMAND
          1  19   0 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
          2  19   0 [kthreadd]
          3  39 -20 [rcu_gp]
          4  39 -20 [rcu_par_gp]
          6  39 -20 [kworker/0:0H-kblockd]
      ......
      16922  19   0 ps -eo pid,pri,ni,args
      26698  19   0 [kworker/7:3-cgroup_destroy]
      
      至于排序部份,当你 man ps ,并且搜索 --sort 之后,可以找到这一段:
      [root@station10-101 ~]# man ps
             --sort spec
                    Specify sorting order.  Sorting syntax is [+|-]key[,[+|-]key[,...]].  Choose a multi-letter key from
                    the STANDARD FORMAT SPECIFIERS section.  The "+" is optional since default direction is increasing
                    numerical or lexicographic order.  Identical to k.  For example: ps jax --sort=uid,-ppid,+pid
      
      因此,要使用指令名称来排序,基本上可以这样做:
      [student@station10-101 ~]$ ps -eo pid,pri,ni,comm --sort=comm
        PID PRI  NI COMMAND
       2041  19   0 (sd-pam)
       2425  19   0 (sd-pam)
      14117  19   0 (sd-pam)
        973  19   0 ModemManager
       1104  19   0 NetworkManager
      ......
       1002  19   0 avahi-daemon
       2432  19   0 bash
      14132  19   0 bash
      15381  19   0 bash
      15316  19   0 boltd
       2304  19   0 colord
      ......
      
    4. 当 man top 之后,出现的语法如下:
      [student@station10-101 ~]$ man top
      TOP(1)                        User Commands                        TOP(1)
      
      NAME
             top - display Linux processes
      
      SYNOPSIS
             top -hv|-bcEHiOSs1 -d secs -n max -u|U user -p pid -o fld -w [cols]
      
             The traditional switches `-' and whitespace are optional.
      ......
      
      因此,只要『 top -d 2 』即可每两秒更新一次画面了。另外,如果需要查找 top 里面数据的排序,可以使用 man top 之后, 搜索 sorting 这个关键字,就能得到底下的画面:
      [student@station10-101 ~]$ man top
      ......
             SORTING of task window
      
                For compatibility, this top supports most of the former top sort keys.  Since this is primarily  a  service
                to former top users, these commands do not appear on any help screen.
                      command   sorted-field                  supported
                      A         start time (non-display)      No
                      M         %MEM                          Yes
                      N         PID                           Yes
                      P         %CPU                          Yes
                      T         TIME+                         Yes
      ......
      
      所以,你只要输入『 top -d 2 』之后,在活动的窗口中,直接输入大写字符的『 M 』以及『 P 』以及『 T 』, 就可以知道排序的差异了!缺省是使用 P 这个 CPU 使用率排序的!
  • 例题 5.2.3-1:
    1. 使用 ps 的方式来搞定 PRI 与 NI 等信息:
      a. 使用 man ps 找到 exapmle 之后,知道如下的动作可以查找 ps 的输出:
      [roott@station10-101 ~]# ps -eo pid,pri,ni,comm
        PID PRI  NI COMMAND
          1  19   0 systemd
          2  19   0 kthreadd
      ......
      
      b. 搭配 grep 来抓关键字:
      [root@station10-101 ~]# ps -eo pid,pri,ni,comm | grep crond
       1523  19   0 crond
      
      c. 将上面的 1523 PID 的 ni 值改掉:
      [root@station10-101 ~]# renice -n -15 -p 1523
      1523 (process ID) old priority 0, new priority -15
      
      [root@station10-101 ~]# ps -eo pid,pri,ni,comm | grep crond
       1523  34 -15 crond
      
      最终的 NI 从 0 变成了 -15 了,至于 PRI 则从原本的 19 变成了 34 了!(19 - (-15) = 19+15 = 34)
    2. 再来则是切换身份成为 student,但是在成为 student 之前,使用 nice 这个指令,将 su 的优先序降低看看:
      [root@station10-101 ~]# man nice
      NICE(1)                                 User Commands                                NICE(1)
      
      NAME
             nice - run a program with modified scheduling priority
      
      SYNOPSIS
             nice [OPTION] [COMMAND [ARG]...]
      
      DESCRIPTION
             Run  COMMAND  with  an  adjusted niceness, which affects process scheduling.  With no
             COMMAND, print the current niceness.  Niceness values range from -20 (most  favorable
             to the process) to 19 (least favorable to the process).
      
             Mandatory arguments to long options are mandatory for short options too.
      
             -n, --adjustment=N
                    add integer N to the niceness (default 10)
      
      知道指令下达的方式之后,就可以开始处理了:
      [root@station10-101 ~]# nice -n 10 su - student
      [student@station10-101 ~]$ ps -l
      F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
      0 S  1000 20131 20124  0  80   0 -  6859 -      pts/0    00:00:00 bash
      4 S  1000 20200 20199  0  90  10 -  6108 -      pts/0    00:00:00 bash
      0 R  1000 20226 20200  0  90  10 - 11191 -      pts/0    00:00:00 ps
      
      可以很清楚的看到, nice 值是会向后继承的,因此,在上面的 20200 这个 PID 之后衍生出来的进程, 他的 Nice 都会缺省是 10 喔!这个部份也得注意!
    3. 如之前的实际练习当中的方式,man top 时,可以发现到 M, P, T 等按钮可以处理排序,而 -p PID 则可以只查阅某支进程。 刚刚的 PID 是 20200 这一个,现在,我们用 top 只查找这个进程:
      [student@station10-101 ~]$ top -d 2 -p 20200
      top - 22:11:30 up 6 days,  7:24,  3 users,  load average: 0.00, 0.00, 0.00
      Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
      %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.1 hi,  0.0 si,  0.0 st
      MiB Mem :   1828.4 total,    152.5 free,   1139.6 used,    536.3 buff/cache
      MiB Swap:   2048.0 total,   1999.7 free,     48.2 used.    507.9 avail Mem
      
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      20200 student   30  10   24432   5116   3268 S   0.0   0.3   0:00.02 bash
      
      现在,当你 man top 然后搜索 renice 时,就会看到有个 r 的指令可以应用~此时,请在 top 的窗口内, 按下『 r 』,然后将 ni 值改为 0 与 15 看看。你会发现 0 不能改, 15 则可以更改成功!
      [student@station10-101 ~]$ top -d 2 -p 20200
      PID to renice [default pid = 20200] 20200
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      20200 student   30  10   24432   5116   3268 S   0.0   0.3   0:00.02 bash
      
      Renice PID 20200 to value 0
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      20200 student   30  10   24432   5116   3268 S   0.0   0.3   0:00.02 bash
      
       Failed renice of PID 20200 to 0: Permission denied
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      20200 student   30  10   24432   5116   3268 S   0.0   0.3   0:00.02 bash
      # 上面依序输入 r, 20200, 0 的结果会是这样~如果依序输入 r, 20200, 15 的结果,会变这样:
      
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      20200 student   35  15   24432   5116   3268 S   0.0   0.3   0:00.02 bash
      
  • 例题 5.2.4-1:
    1. 快速的将工作丢到背景去:
      [student@station10-101 ~]$ find /
      /
      /boot
      /boot/initramfs-4.18.0-147.el8.x86_64kdump.img
      ......
      /proc/37/task/37/oom_score
      /proc/37/task/37/oom_adj
      /proc/37/task/37/oom_score_adj^Z  <==按下 [ctrl]+z 喔!
      [1]+  Stopped                 find /
      
      如上所示,系统会告诉你,有一个编号 [1] 号的工作在系统内暂停 (stopped),指令内容为 find / 这样!相当清楚!
    2. 不过,要仔细查看背景工作,就用 jobs 吧!
      [student@station10-101 ~]$ jobs
      [1]+  Stopped                 find /
      [student@station10-101 ~]$ jobs -l
      [1]+ 20380 Stopped                 find /
      
      jobs -l 可以多出一个数字,那个数字就是 PID 啰!这样观察比较清楚!
    3. 让该工作在背景底下运行的话,就用『 bg %1 』即可!不过,画面很乱喔! 此时你按下『 [ctrl]+z 或 [ctrl]+c 』该程序都不会理你!这是因为那是背景工作~前景指令管不到! 如果想要短时间处理该进程,就得要很快速的『 fg %1 』然后再『 [ctrl]+z 』才行!
      [student@station10-101 ~]$ bg %1
      ......
      
    4. 运行『 find / & 』之后,该指令就会被丢到系统背景去运行,因此按下『 [crtl]+z 』就没有任何作用了!
    5. 连续指令的运行可以使用分号 (;) 来链接运行,但是因为前一个指令使用了 & 的结尾符号,在 & 之后不能再加其他指令。 因此只好使用两次指令运行!但记得速度要快!
      [student@station10-101 ~]$ find / &> /tmp/findroot.txt &
      [1] 20850
      [student@station10-101 ~]$ jobs -l
      [1]+ 20850 退出 1                find / &> /tmp/findroot.txt
      
      确实是可以看到 jobs 喔!不过因为运行过一次以后,Linux 会将搜索到的数据缓存在内存,因此再次使用 find 时, 速度会快的跟在飞一样!所以,如果第二个指令运行不够快速,你就看不到运行过程了。另外,使用数据流重导向,可以让输出消息导向到文件, 所以屏幕就不会被文件名搞乱了!
    6. sleep 是个挺有趣的指令,它会让系统暂停,然后倒数一段时间,等到时间经过之后,才会继续往下工作。 因此,直接输入 sleep 60s 时,你的 bash 会暂停 60 秒,这时只要按下 [ctrl]+z ,这个倒数就会被暂停~ 前景操作会还给你。
      [student@station10-101 ~]$ sleep 60s
      ^Z  <==其实这里是按下 [ctrl]+z 喔!
      [1]+  已停止               sleep 60s
      [student@station10-101 ~]$ jobs -l
      [1]+ 20886 停止                  sleep 60s
      
      所以,确认结果后, sleep 60s 现在正在『暂停』中没错!没有持续倒数喔!
    7. 直接输入『 bg %1 』即可!
      [student@station10-101 ~]$ bg %1
      [1]+ sleep 60s &
      [1]+ 20886 已完成               sleep 60s
      
      静待一段时间后,bash 画面会丢出 sleep 已经完成的消息给妳看喔!
    8. 直接输入 vim 之后,在 vim 的画面中按下『 [ctrl]+z 』看看:
      [student@station10-101 ~]$ vim
         <==在 vim 当中按下 [ctrl]+z 的结果!
      [1]+  已停止               vim
      [student@station10-101 ~]$ jobs -l
      [1]+ 20959 停止                  vim
      
      确定 vim 在背景中暂停喔!
    9. 跟 find 还有 sleep 一样,输入 bg %1 看看即可!
      [student@station10-101 ~]$ bg %1
      [1]+ vim &
      
      [1]+  已停止               vim
      [student@station10-101 ~]$ jobs -l
      [1]+ 20959 停止 (tty 输出)     vim
      
      vim 依旧是暂停的状态,这是因为 vim 一定要与人交互 (输入) 才能运行,不能自己乱动。因此,vim 在背景底下无法变成 run 的模式! 所以在背景下, vim 将永远是暂停的噜!
    10. 输入 fg %1 然后按下『 :q! 』即可!
  • 例题 5.3.1-1:
    1. 先用 student 帐号改自己的密码!
      [student@station10-101 ~]$ passwd
      更改用户 student 的密码。
      Current password: <==这里先输入原本的密码,就是 mystdgo
      新 密码:         <==这里输入 123456
      不良的密码:密码短于 8 个字符
      passwd: 验证记号处理错误
      
      因为密码短之外,又太烂,所以无法通过系统的密码强度验证,就直接给妳丢出来,因此密码没有更改喔!
    2. openssl 是个很常用于密码相关的指令,其中 rand 功能就是用来猜测密码的
      [student@station10-101 ~]$ openssl rand -base64 8
      nGGpkK4Kjkk=
      [student@station10-101 ~]$ openssl rand -base64 8
      4Rmi5usNDJg=
      
      相同的指令,但是每次都会产生不同的密码!我们将第二个密码拿出来,并且不要输入 = 号,亦即以『 4Rmi5usNDJg 』 作为我们的新密码看看。
    3. 开始修改密码:
      [student@station10-101 ~]$ passwd
      更改用户 student 的密码。
      Current password:   <==这里先输入原本的密码,就是 mystdgo
      新 密码:           <==输入新密码 4Rmi5usNDJg
      再次输入新的 密码: <==再输入一次!
      passwd:所有核对代符都已成功更新。
      
    4. 查看密码档最后一次被更新的时间:
      [student@station10-101 ~]$ date
      日  3月 22 23:19:16 CST 2020
      [student@station10-101 ~]$ ll /etc/shadow
      ----------. 1 root root 1996  3月 22 23:17 /etc/shadow
      
      文件确实在几分钟前被更新过!所以,用户确实可以更改自己的密码喔!
    5. 注意 /etc/shadow 是密码档,很可爱的是, student 可以更改 /etc/shadow 喔!很不可思议吧! 但是 /etc/shadow 却是 000 的分数,事实上,传统的 rwx 权限中, student 根本不可能更改这个文件啊!
    6. 请使用 root 的权限,将 student 密码改回来!
      [student@station10-101 ~]$ su -
      密码:
      [root@station10-101 ~]# echo mystdgo | passwd --stdin student
      Changing password for user student.
      passwd: all authentication tokens updated successfully.
      [root@station10-101 ~]# exit
      logout
      
  • 例题 5.3.1-2:
    1. 先运行 passwd 看看:
      [student@station10-101 ~]$ passwd
      更改用户 student 的密码。
      Current password:
      
    2. 在上述的输入密码的阶段,按下『 [ctrl]+z 』然后按下『 [enter] 』之后,在输入 jobs 看看。
      Current password:
      
      [1]+  已停止               passwd
      [student@station10-101 ~]$ jobs -l
      [1]+ 21350 停止                  passwd
      
    3. 开始运行 pstree 看看
      [student@station10-101 ~]$ pstree -pu | grep -A1 -B1 passwd
         |-gnome-terminal-(15372)-+-bash(15381)-+-grep(21446)
         |                        |             |-passwd(21423,root)
         |                        |             `-pstree(21445)
      
      所以 passwd 运行期间,确实就是 root 的身份啊!
    4. 按下『 fg %1 』然后『 [ctrl]+c 』结束即可。
  • 例题 5.3.1-3:
    1. 使用 student 的身份下达指令
      [student@station10-101 ~]$ locate passwd
      locate: 无法运行 stat()「/var/lib/mlocate/mlocate.db」: 没有此一文件或目录
      
      [student@station10-101 ~]$ su -
      密码:
      [root@station10-101 ~]# updatedb
      [root@station10-101 ~]# exit
      logout
      
      [student@station10-101 ~]$ locate passwd
      /etc/passwd
      /etc/passwd-
      /etc/pam.d/passwd
      ......
      
      这个指令可以通过数据库环境,直接找到文件名列表!速度很快喔!
    2. 观察一下刚刚创建的数据库在哪里:
      [student@station10-101 ~]$ ll -d /var/lib/mlocate/
      drwxr-x---. 2 root slocate 24  3月 23 08:57 /var/lib/mlocate/
      
    3. 承上,看来权限是 750,而 student 属于 others 的权限,因此用户并没有权限可以进入该目录,也就无法取用该目录下的任何数据。 因此,读者们应该要思考,那为何 student 可以运行 locate 这个指令呢?
    4. 查找一下 locate 这个指令在何方:
      [student@station10-101 ~]$ which locate
      /usr/bin/locate
      [student@station10-101 ~]$ type -a locate
      locate 是 /usr/bin/locate
      
      基本上有两个指令可以查找『指令的绝对路径文件名』,那就是 which 以及 type -a 这两个!
    5. 接下来,查找一下 locate 的权限:
      [student@station10-101 ~]$ ll /usr/bin/locate
      -rwx--s--x. 1 root slocate 48552  5月 11  2019 /usr/bin/locate
      
      搭配前几点来看, locate 具有 SGID,亦即 student 运行 locate 时,其所属的『有效群组』将会暂时切换成为 locate 这个指令的群组, 亦即 student 的有效群组将会转成 slocate 这个群组名称。然后, /var/lib/mlocate 是属于 slocate 群组的, 而群组具有 r-x 的群线,因此,就在 student 操作 locate 时,就能够取得 r-x 的权限来读取 /var/lib/mlocate 的数据了!
  • 例题 5.3.1-4:
    1. 看文件权限,直接用 ll 最快
      [root@station10-101 ~]# ll -d /run/log/journal/
      drwxr-sr-x. 3 root systemd-journal 60  3月 16 14:46 /run/log/journal/
      
      属于 systemd-journal 这个群组之外,权限设置为 SGID 喔!
    2. 创建个文件看看
      [root@station10-101 ~]# touch /tmp/fromroot
      [root@station10-101 ~]# ll /tmp/fromroot
      -rw-r--r--. 1 root root 0 Mar 23 13:31 /tmp/fromroot
      
      既然是 root 处理的,当然帐号与群组都是属于 root 的!大致上就是这样!
    3. 但是在 SGID 的目录里面新建的数据会变这样:
      [root@station10-101 ~]# touch /run/log/journal/fromroot
      [root@station10-101 ~]# ll /run/log/journal/fromroot
      -rw-r--r--. 1 root systemd-journal 0 Mar 23 13:33 /run/log/journal/fromroot
      
      群组会变这样!这就是 SGID 的能力!
  • 例题 5.3.1-5:
    1. 观察 /tmp 权限先:
      [student@station10-101 ~]$ ll -d /tmp
      drwxrwxrwt. 12 root root 4096  3月 23 13:31 /tmp
      
      可以看到其他人 (others) 在 x 权限的位置上,变成了 t 这个旗标了!
    2. 用 root 身份进入 /tmp 看看
      [student@station10-101 ~]$ su -
      密码:
      [root@station10-101 ~]# cd /tmp
      
    3. 拷贝数据并修改权限
      [root@station10-101 tmp]# cp /etc/hosts myhosts
      [root@station10-101 tmp]# chmod 777 myhosts
      [root@station10-101 tmp]# ll myhosts
      -rwxrwxrwx. 1 root root 158 Mar 23 13:51 myhosts
      
      所以变成任何人都可以对这个文件进行变更的情况喔!
    4. 用 student 来到 /tmp 看看
      [student@station10-101 ~]$ cd /tmp
      [student@station10-101 tmp]$ ll myhosts
      -rwxrwxrwx. 1 root root 158  3月 23 13:51 myhosts
      
    5. 用 student 去编辑 myhosts
      [student@station10-101 tmp]$ vim myhosts
      
      编辑完可以按下 :w 保存没问题!
    6. 尝试删除
      [student@station10-101 tmp]$ rm myhosts
      rm: 无法移除 'myhosts': 此项操作并不被允许
      
      因为虽然 /tmp 权限有包含 777 在内,但是因为有 SBIT 的功能,因此 myhost 仅有 root 及文件拥有者 (也恰巧是 root) 才能删除。 所以 student 是没有权限可以删除的喔!
  • 例题 5.3.2-1:
    1. 一般用户运行指令会产生与 root 相同的权限,那就是 SUID 的功能!因此可以这么做:
      [student@station10-101 tmp]$ su -
      密码:
      [root@station10-101 ~]# cd /usr/local/bin
      [root@station10-101 bin]# cp /usr/bin/cat mycat2
      [root@station10-101 bin]# chmod u+s mycat2
      [root@station10-101 bin]# ll mycat2
      -rwsr-xr-x. 1 root root 51856 Mar 23 14:22 mycat2
      
      [root@station10-101 bin]# exit
      logout
      [student@station10-101 tmp]$ cat /etc/shadow
      cat: /etc/shadow: 拒绝不符权限的操作
      
      [student@station10-101 tmp]$ mycat2 /etc/shadow
      root:$6$1sR2WQzGzLtoqnKA$gJXeZEYh5tOwZyhhzQgWPbhXvtCQhibDdlUbEo8nQEdaf5CZJ1b44OZpVPE1b7ZVTuynIT303BxNKi.XcEinW.::0:99999:7:::
      ......
      
      所以,一般帐号运行两个相同功能的指令, cat 与 mycat2 时,其产生的效果就有显著的不一样啊!
    2. 再来测试 SGID 目录的功能
      [student@station10-101 ~]$ su -
      密码:
      [root@station10-101 ~]# cd /srv
      [root@station10-101 srv]# ll -d project1/
      drwxrwx---. 2 root progroup 6 Mar 16 00:38 project1
      
      [root@station10-101 srv]# chmod g+s project1/
      [root@station10-101 srv]# ll -d project1/
      drwxrws---. 2 root progroup 6 Mar 16 00:38 project1/
      
      [root@station10-101 srv]# su - prouser1
      [prouser1@station10-101 ~]$ cd /srv/project1/
      [prouser1@station10-101 project1]$ touch fromme
      [prouser1@station10-101 project1]$ ll fromme
      -rw-rw-r--. 1 prouser1 progroup 0 Mar 23 14:27 fromme
      
      [prouser1@station10-101 project1]$ exit
      logout
      
      这样,任何『新建』的数据,其群组会与该目录的群组相同!
  • 例题 5.4 课后练习
    1. 至少要让其他人可以进入 prouser1 的家目录,所以其他人需要具备 x 权限在 prouser1 的家目录上。接下来就能简单的设计好 student 的权限了。
      [root@station10-101 ~]# cd ~prouser1
      [root@station10-101 prouser1]# mkdir for_student
      [root@station10-101 prouser1]# chown prouser1:student for_student/
      [root@station10-101 prouser1]# chmod 770 for_student/
      [root@station10-101 prouser1]# chmod o+x ~prouser1
      [root@station10-101 prouser1]# ll -d ~prouser1 ~prouser1/for_student/
      drwx-----x. 4 prouser1 prouser1 118 Mar 23 14:48 /home/prouser1
      drwxrwx---. 2 prouser1 student    6 Mar 23 14:48 /home/prouser1/for_student/
      
      [student@station10-101 ~]$ ll /home/prouser1
      ls: cannot open directory '/home/prouser1': 拒绝不符权限的操作  <==只有 x 权限,不能浏览也是正常的
      
      [student@station10-101 ~]$ cd /home/prouser1/for_student  <==没有 r ,所以不能用 [tab] 补齐!
      [student@station10-101 for_student]$ pwd
      /home/prouser1/for_student
      [student@station10-101 for_student]$ touch abcd
      [student@station10-101 for_student]$ ll
      总计 0
      -rw-rw-r--. 1 student student 0  3月 23 14:52 abcd
      
    2. 接下来也是在 prouser1 的家目录,因为同样也要让其他人进来本目录,所以上一题设置的 x 就很重要了!
      [root@station10-101 ~]# cd ~prouser1
      [root@station10-101 prouser1]# mkdir for_read
      [root@station10-101 prouser1]# chown prouser1 for_read
      [root@station10-101 prouser1]# chmod 755 for_read/
      [root@station10-101 prouser1]# ll -d ~prouser1 ~prouser1/for_read/
      drwx-----x. 5 prouser1 prouser1 134 Mar 23 14:55 /home/prouser1
      drwxr-xr-x. 2 prouser1 root       6 Mar 23 14:55 /home/prouser1/for_read/
      
    3. 很简单!就用 nice 即可
      [student@station10-101 ~]$ nice -n 15 sh -c "sleep 60s; head -n 1 /etc/passwd"
      
    4. 优化 ni 值就是 -20 了!所以这样做即可:
      [root@station10-101 ~]# ps -eo pid,pri,ni,comm | grep gdm
       1520  19   0 gdm
       2032  19   0 gdm-session-wor
       2048  19   0 gdm-x-session
      14712  19   0 gdm-session-wor
      14743  19   0 gdm-x-session
      [root@station10-101 ~]# renice -n -20 -p 1520
      1520 (process ID) old priority 0, new priority -20
      [root@station10-101 ~]# ps -eo pid,pri,ni,comm | grep gdm
       1520  39 -20 gdm
       2032  19   0 gdm-session-wor
       2048  19   0 gdm-x-session
      14712  19   0 gdm-session-wor
      14743  19   0 gdm-x-session
      
修改历史:
  • 2020/02/25:尝试直接在课程中加入解答,让大家有个参考依据。不过,没事不要来看啊!
  • 2020/03/10:加入了第三章的后课练习部份。
2020/03/02 以来统计人数
计数器
其他链接
环境工程模式篇
鸟园讨论区
鸟哥旧站

今日 人数统计
昨日 人数统计
本月 人数统计
上月 人数统计