Linux 基础学习训练教材 - RockyLinux 9.x

课程/课后例题参考解答

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

最近更新时间: 2023/05/01

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

第 07 堂课 (2023/02/26)

  • 例题 7.1.1-1:
    1. 在 /etc/passwd 这个文件内,数据是以 : 分隔,第一个字段是帐号名称,第七个字段则是 shell 程序名称。 我们可以通过 cut 来进行数据字段的切割,先使用 man 查找 cut 功能后,再开始处理文件:
      [root@station10-101 ~]# man cut
      DESCRIPTION
             Print selected parts of lines from each FILE to standard output.
      
             With no FILE, or when FILE is -, read standard input.
      
             Mandatory arguments to long options are mandatory for short options too.
      
             -b, --bytes=LIST
                    select only these bytes
      
             -c, --characters=LIST
                    select only these characters
      
             -d, --delimiter=DELIM
                    use DELIM instead of TAB for field delimiter
      
             -f, --fields=LIST
                    select only these fields;  also print any line that contains no delimiter character,
                    unless the -s option is specified
      
      [root@station10-101 ~]# cut -d ':' -f 1,7 /etc/passwd
      root:/bin/bash
      bin:/sbin/nologin
      daemon:/sbin/nologin
      .......
      theuser3:/bin/bash
      theuser1:/bin/bash
      
    2. 这时,搭配 grep 即可抓到正确的数据了:
      [root@station10-101 ~]# cut -d ':' -f 1,7 /etc/passwd | grep daemon
      daemon:/sbin/nologin
      
      是 /sbin/nologin 这个 shell 喔!
  • 例题 7.1.1-2:
    1. 安装 C shell,若登录的是图形界面,请自行在右上角的网络工具中,激活你的网络环境即可!底下使用文本界面来说明启动网络的方式:
      [student@station10-101 ~]$ su -
      Password:
      [root@station10-101 ~]# yum install tcsh
      Last metadata expiration check: 1:22:34 ago on Sun 26 Feb 2023 01:24:07 PM CST.
      Dependencies resolved.
      =======================================================================================
       Package               Architecture     Version               Repository          Size
      =======================================================================================
      Installing:
       tcsh                  x86_64           6.22.03-6.el9         appstream          456 k
      
      Transaction Summary
      =======================================================================================
      Install  1 Package
      
      Total download size: 456 k
      Installed size: 1.2 M
      Is this ok [y/N]: y
      Downloading Packages:
      tcsh-6.22.03-6.el9.x86_64.rpm                               3.3 MB/s | 456 kB     00:00
      ---------------------------------------------------------------------------------------
      Total                                                       245 kB/s | 456 kB     00:01
      Rocky Linux 9 - AppStream                                   1.7 MB/s | 1.7 kB     00:00
      Importing GPG key 0x350D275D:
       Userid     : "Rocky Enterprise Software Foundation - Release key 2022 <releng@rockylinux.org>"
       Fingerprint: 21CB 256A E16F C54C 6E65 2949 702D 426D 350D 275D
       From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9
      Is this ok [y/N]: y
      Key imported successfully
      Running transaction check
      Transaction check succeeded.
      Running transaction test
      Transaction test succeeded.
      Running transaction
        Preparing        :                                                       1/1
        Installing       : tcsh-6.22.03-6.el9.x86_64                             1/1
        Running scriptlet: tcsh-6.22.03-6.el9.x86_64                             1/1
        Verifying        : tcsh-6.22.03-6.el9.x86_64                             1/1
      
      Installed:
        tcsh-6.22.03-6.el9.x86_64
      
      Complete!
      
      [root@station10-101 ~]# cat /etc/shells
      /bin/sh
      /bin/bash
      /usr/bin/sh
      /usr/bin/bash
      /bin/csh
      /bin/tcsh
      /usr/bin/csh
      /usr/bin/tcsh
      # 果然,有加入 csh 及 tcsh 的 shell 指令了!
      
      [root@station10-101 ~]# exit
      logout
      
    2. 操作 bash 环境来确认一下 shell program
      [student@station10-101 ~]$ echo $BASH
      /bin/bash
      [student@station10-101 ~]$ echo $shell
      
      [student@station10-101 ~]$ echo $0
      -bash
      
      可以看得出来, bash 环境下,会具有 $BASH 以及 $0 的两个变量,都是指令名称!
    3. 开始切换到 csh 看看:
      [student@station10-101 ~]$ /bin/csh
      # 其实,这时已经变成 tcsh 这个强化版的 csh 了!只是提示字符看不出来!所以要用底下的方式检查一下!
      
      [student@station10-101 ~]$ echo $BASH
      BASH: Undefined variable.
      [student@station10-101 ~]$ echo $shell
      /usr/bin/tcsh
      [student@station10-101 ~]$ echo $0
      /bin/csh
      [student@station10-101 ~]$ exit
      exit
      [student@station10-101 ~]$ echo $0
      -bash
      
      可以看到呈现的效果不太一样~这就是 C shell 啰!由这个练习我们也会知道, echo $0 真是重要啊!
    4. 如果运行一个 /sbin/nologin 时,会很奇怪!
      [student@station10-101 ~]$ /sbin/nologin
      This account is currently not available.
      
      也就是说, /sbin/nologin 其实是一个怪怪的 shell 喔!
  • 例题 7.1.1-3:
    1. 使用之前玩过的 usermod 来修改 shell
      [student@station10-101 ~]$ su -
      Password:
      # 先来看看 student 在 /etc/passwd 当中的设置值
      [root@station10-101 ~]# grep student /etc/passwd  <==旧的方法
      [root@station10-101 ~]# getent passwd student     <==这样比较简单!
      student:x:1000:1000:student:/home/student:/bin/bash
      
      [root@station10-101 ~]# usermod -s /sbin/nologin student
      [root@station10-101 ~]# getent passwd student
      student:x:1000:1000:student:/home/student:/sbin/nologin
      
      可以看到用户 student 的 shell 变成 nologin 了!
    2. 开始使用 su - student 测试:
      [root@station10-101 ~]# su - student
      This account is currently not available.
      # 直接运行 /sbin/nologin ,然后无法切换成为 student 喔!
      
    3. 改回原本的 bash 吧!
      [root@station10-101 ~]# usermod -s /bin/bash student
      [root@station10-101 ~]# getent passwd student
      student:x:1000:1000:student:/home/student:/bin/bash
      
  • 例题 7.1.1-4:
    1. 测试不同类型的帐号:
      1. 查看 UID 的模样
        [root@station10-101 ~]# id bin
        uid=1(bin) gid=1(bin) groups=1(bin)
        [root@station10-101 ~]# id student
        uid=1000(student) gid=1000(student) groups=1000(student)
        
        可以看到两者的 UID 号码差异很大!一般来说,小于 1000 以下的 uid,通常是保留给系统使用的,因此, bin 这个帐号, 应该就是所谓的『系统帐号』,这种帐号是不能登录才对的!
      2. 因为是 root ,所以可以顺利的切换成为 student 而无须输入密码!这是一般状态!
      3. 如果是系统帐号的 bin 时:
        [root@station10-101 ~]# su - bin
        This account is currently not available.
        [root@station10-101 ~]# getent passwd bin
        bin:x:1:1:bin:/bin:/sbin/nologin
        
        不能切换成为 bin 这个用户,因为他的 shell 是 /sbin/nologin!
    2. 创建不能取得交互 shell (就是 /sbin/nologin) 的帐号
      [root@station10-101 ~]# useradd -s /sbin/nologin puser1
      [root@station10-101 ~]# echo MyPuser1 | passwd --stdin puser1
      更改用户 puser1 的密码。
      passwd:所有核对代符都已成功更新。
      [root@station10-101 ~]# getent passwd bin
      puser1:x:1007:1009::/home/puser1:/sbin/nologin
      
      [root@station10-101 ~]# su - puser1
      This account is currently not available.
      
      这样就具有无法取得交互 shell 的帐号!要注意,这种帐号虽然无法取得 shell,但是依旧可以使用 ftp / email / web service 等功能! 所以还是得注意其密码状态喔!
  • 例题 7.1.2-1:
    # A. 设置变量
    [student@station10-101 ~]$ myname=peter pan    <==这是错误的,不能直接有空白
    bash: pan: command not found...
    [student@station10-101 ~]$ myname="peter pan"  <==加上双引号/单引号就可以!
    
    # B. 调用变量内容
    [student@station10-101 ~]$ echo $myname
    peter pan
    [student@station10-101 ~]$ echo ${myname}
    peter pan
    # 两种方法都可以调用顺利!
    
    # C. 测试数字开头的变量
    [student@station10-101 ~]$ 2myname="peter pan"
    bash: 2myname=peter pan: command not found...
    # 不行喔!变量名称不能以数字开头!
    
    # D. 这个在测试单引号与双引号的用途!
    [student@station10-101 ~]$ varsymbo="$myname"
    [student@station10-101 ~]$ echo $varsymbo
    peter pan
    [student@station10-101 ~]$ varsymbo='$myname'
    [student@station10-101 ~]$ echo $varsymbo
    $myname
    # 双引号会保留 $var 变量的内容,而单引号会将 $ 视为纯文本来处理~
    
    # E. 使用双引号处理变量内容的叠代
    [student@station10-101 ~]$ hero="I am $myname"
    [student@station10-101 ~]$ echo ${hero}
    I am peter pan
    # 这个例子就得要使用双引号才合理了!
    
    # F. 内核版本调用方式
    [student@station10-101 ~]$ uname -r
    5.14.0-162.12.1.el9_1.0.2.x86_64
    
    # G. 变量内藏着指令的协助
    [student@station10-101 ~]$ kver="my kernel version is uname -r"
    [student@station10-101 ~]$ echo $kver
    my kernel version is uname -r
    
    [student@station10-101 ~]$ kver="my kernel version is $(uname -r)"
    [student@station10-101 ~]$ echo $kver
    my kernel version is 5.14.0-162.12.1.el9_1.0.2.x86_64
    # 可以将 $() 想成四则运算的括号,要先做的意思~
    
  • 例题 7.1.2-2:
    1. 找出 find 的相关参数,这个很重要喔:
      [student@station10-101 ~]$ man find
      ......
             -perm mode
                    File's permission bits are exactly mode (octal or symbolic).
                    # 权限要刚刚好,例如 -perm 755,就是找出权限一定是 755
      
             -perm -mode
                    All of the permission bits mode are set for the file.
                    # 权限必须包含全部,例如 -perm -644 时,连 755 也会被抓出来!
      
             -perm /mode
                    Any of the permission bits mode are set for the file.
                    # 权限可以是任何一个,例如 -perm /755 代表 rwxr-xr-x 当中任一个出现都行!
      
    2. 题目中的 -perm /6000 当中, 6000 指的是『 --s--s--- 』这样的权限,所以,任何一个 s 出现 (SUID/SGID) 都可以被抓出来!
      [student@station10-101 ~]$ find /usr/bin /usr/sbin -perm /6000 | sort
      /usr/bin/at
      /usr/bin/chage
      /usr/bin/chfn
      .....
      /usr/sbin/pam_timestamp_check
      /usr/sbin/unix_chkpwd
      
    3. 总是需要确认一下,因此使用底下的指令来列出权限数据:
      [student@station10-101 ~]$ ls -l $(find /usr/bin /usr/sbin -perm /6000)
      -rwsr-xr-x. 1 root root     57976 Oct 26 10:53 /usr/bin/at
      -rwsr-xr-x. 1 root root     74848 Nov  1 07:34 /usr/bin/chage
      -rws--x--x. 1 root root     32032 Nov 17 05:55 /usr/bin/chfn
      ....
      -rwsr-xr-x. 1 root root     16088 Oct 31 22:41 /usr/sbin/pam_timestamp_check
      -rwsr-xr-x. 1 root root     24512 Oct 31 22:41 /usr/sbin/unix_chkpwd
      
      你可以看到每个指令都有 s 出现在权限的旗标里面!
  • 例题 7.1.2-3:
    # A. 找出适当的文件名:
    [student@station10-101 ~]$ ll $(find /usr/sbin /usr/bin -perm 4755)
    -rwsr-xr-x. 1 root root 57976 Oct 26 10:53 /usr/bin/at
    -rwsr-xr-x. 1 root root 74848 Nov  1 07:34 /usr/bin/chage
    ....
    -rwsr-xr-x. 1 root root 16088 Oct 31 22:41 /usr/sbin/pam_timestamp_check
    -rwsr-xr-x. 1 root root 24512 Oct 31 22:41 /usr/sbin/unix_chkpwd
    # 确实每一个都是 4755 的权限数据!
    
    # B.C. 创建目录后,将文件拷贝过去
    [student@station10-101 ~]$ mkdir ~/unit07
    [student@station10-101 ~]$ cp -a $(find /usr/sbin /usr/bin -perm 4755) ~/unit07
    [student@station10-101 ~]$ ll ~/unit07
    total 676
    -rwxr-xr-x. 1 student student 57976 Oct 26 10:53 at
    -rwxr-xr-x. 1 student student 74848 Nov  1 07:34 chage
    ....
    -rwxr-xr-x. 1 student student 24512 Oct 31 22:41 unix_chkpwd
    -rwxr-xr-x. 1 student student 15536 Nov 15 18:09 vmware-user-suid-wrapper
    
  • 例题 7.1.3-1:
    1. 使用系统管理员的身份运行变量的设置,其中特别注意,PATH 不可以没有 /bin !否则系统可能会出问题!
      # a. 印出 PATH 变量内容
      [root@station10-101 ~]# echo ${PATH}
      /root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
      # 使用冒号分隔,每个间隔都是指令搜索的路径,先找到就先运行!
      
      # b. 将原有的设置先备份下来之意:
      [root@station10-101 ~]# oldpath=${PATH}
      [root@station10-101 ~]# echo ${oldpath}
      /root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
      
      # c. 这题在假设 PATH 设置错误的情况,要注意,至少含有 /bin 才行!
      [root@station10-101 ~]# PATH=/bin
      [root@station10-101 ~]# echo ${PATH}
      /bin
      
      # d. 开始运行管理员的专用指令,亦即放置到 /sbin 底下的指令:
      [root@station10-101 ~]# useradd --help
      bash: useradd: command not found...
      Similar command is: 'useradd'
      [root@station10-101 ~]# usermod --help
      bash: usermod: command not found...
      Install package 'shadow-utils' to provide command 'usermod'? [N/y] n
      # 反正,就是让你无法顺利运行位于 /sbin 底下的指令!所以, PATH 真是好重要!
      
      # e. 开始使用绝对路径来运行,不要使用 PATH 的功能,就写绝对路径吧!
      [root@station10-101 ~]# /sbin/usermod --help
      Usage: usermod [options] LOGIN
      
      Options:
        -b, --badnames                allow bad names
        -c, --comment COMMENT         new value of the GECOS field
      ......
      
      # f. 测试完毕之后,请立即将 PATH 改回来,不然会出问题喔!
      [root@station10-101 ~]# PATH=${oldpath}
      [root@station10-101 ~]# echo $PATH
      /root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
      [root@station10-101 ~]# useradd --help
      
      这个题目在告诉我们,PATH 就是所有指令的来源!它相当重要啊!而如果你要使用某个指令, 若不想被 PATH 所制约,那么使用绝对路径,大概是你唯一可以做的事情!很重要!很重要!
    2. 通过 student 帐号进行其他任务:
      # a. 创建 student 的个人化专用目录
      [student@station10-101 ~]$ mkdir cmd
      [student@station10-101 ~]$ cp /bin/cat cmd/scat
      [student@station10-101 ~]$ ll cmd
      total 40
      -rwxr-xr-x. 1 student student 37464 Feb 26 16:01 scat
      # 这个动作其实在模仿自己创建一只指令在 ~/cmd/scat 的意思而已!我们使用 cat 为例!
      
      # b. 测试运行指令是正常的,就用绝对路径/相对路径文件名吧!
      [student@station10-101 ~]$ ./cmd/scat /etc/hosts
      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      # 确定指令可以正常输出喔!
      
      # c. 测试 scat 是否可以通过缺省 PATH 的搜索找到?
      [student@station10-101 ~]$ scat /etc/hosts
      bash: scat: command not found...
      Similar commands are::
      'zcat'
      'cat'
      
      # d. 重点来了!累加 PATH 的功能在这里!很重要!
      [student@station10-101 ~]$ PATH="${PATH}:~/cmd"
      [student@station10-101 ~]$ echo $PATH
      /home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:~/cmd
      [student@station10-101 ~]$ scat /etc/hosts
      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      
      你就知道 PATH 真是很重要了!
  • 例题 7.1.3-2:
    1. 找寻一下 PS1 这个变量与命令提示字符的相关性:
      [student@station10-101 ~]$ echo $PS1
      [\u@\h \W]\$
      
      你可以发现,命令提示字符『 [student@station10-101 ~]$ 』与 PS1 『 [\u@\h \W]\$ 』格式几乎一模一样!没错, 因为提示字符就是由 PS1 所设置的!
    2. 现在请通过 man bash 找到 PS1 这个变量的设置方式:
      [student@station10-101 ~]$ man bash
      ......
             \a     an ASCII bell character (07)
             \d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
             \D{format}
                    the  format  is passed to strftime(3) and the result is inserted into the prompt string; 
                    an empty format results in a locale-specific time representation.  The braces are required
             \e     an ASCII escape character (033)
             \h     the hostname up to the first `.'
             \H     the hostname
             \j     the number of jobs currently managed by the shell
             \l     the basename of the shell's terminal device name
             \n     newline
             \r     carriage return
             \s     the name of the shell, the basename of $0 (the portion following the final slash)
             \t     the current time in 24-hour HH:MM:SS format
             \T     the current time in 12-hour HH:MM:SS format
             \@     the current time in 12-hour am/pm format
             \A     the current time in 24-hour HH:MM format
             \u     the username of the current user
             \v     the version of bash (e.g., 2.00)
             \V     the release of bash, version + patch level (e.g., 2.00.0)
             \w     the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)
             \W     the basename of the current working directory, with $HOME abbreviated with a tilde
             \!     the history number of this command
             \#     the command number of this command
             \$     if the effective UID is 0, a #, otherwise a $
             \nnn   the character corresponding to the octal number nnn
             \\     a backslash
             \[     begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
             \]     end a sequence of non-printing characters
      ......
      
    3. 加入一些额外的设置,就是指令数『 \# 』的效果:
      [student@station10-101 ~]$ PS1="[\u@\h \# \W]\$"
      [student@station10-101 13 ~]$
      
  • 例题 7.1.4-1:
    1. 在父进程设置变量,并检测是区域还是全域变量:
      # a. 列出变量来搜索特定内容
      [student@station10-101 17 ~]$ set | grep mypp
      [student@station10-101 18 ~]$ env | grep mypp
      [student@station10-101 19 ~]$ export | grep mypp
      # set 可以列出全部变量 (区域、全域), env / export 只能列出全域变量
      
      # b. 设置变量
      [student@station10-101 20 ~]$ mypp="from_ppid"
      [student@station10-101 21 ~]$ echo ${mypp}
      from_ppid
      
      # c. 检测是区域还是全域变量?
      [student@station10-101 22 ~]$ set | grep mypp
      mypp=from_ppid
      [student@station10-101 23 ~]$ env | grep mypp
      [student@station10-101 24 ~]$ export | grep mypp
      
      所以这个变量 mypp 目前是『区域变量』的意思!
    2. 在子进程讨论刚刚父进程创建的变量:
      # a.b. 进入子进程,并查看 mypp 是否存在?
      [student@station10-101 25 ~]$ bash
      [student@station10-101 ~]$ set | grep mypp
      [student@station10-101 ~]$ env | grep mypp
      [student@station10-101 ~]$ export | grep mypp
      # 其实,只有全域变量的值才会继承给子进程!所以刚刚的 mypp 这个区域变量,不会传给子进程
      
      # c.d. 创建子进程内的变量,并且回到父进程
      [student@station10-101 ~]$ mypp2="from_cpid"
      [student@station10-101 ~]$ echo ${mypp2}
      from_cpid
      [student@station10-101 ~]$ exit
      exit
      [student@station10-101 26 ~]$
      
      区域变量没有办法传给子进程的!
    3. 在父进程找子进程创建的信息,以及将区域变量转为全域变量:
      # a. 一定找不到子进程所设置的变量!因为是不同的进程啊!
      [student@station10-101 27 ~]$ echo $mypp2
      
      # b. 将区域变量改成全域变量!很简单,使用 export 即可!
      [student@station10-101 28 ~]$ export mypp
      [student@station10-101 29 ~]$ export | grep mypp
      declare -x mypp="from_ppid"
      [student@station10-101 30 ~]$ set | grep mypp
      mypp=from_ppid
      [student@station10-101 31 ~]$ env | grep mypp
      mypp=from_ppid
      
      基本上, set 列出所有变量,包括区域与全域,env 与 export 只列出全域变量!
    4. 在子进程确认有全域变量的存在!
      [student@station10-101 32 ~]$ bash
      [student@station10-101 ~]$ env | grep mypp
      mypp=from_ppid
      [student@station10-101 ~]$ echo ${mypp}
      from_ppid
      [student@station10-101 ~]$ exit
      exit
      [student@station10-101 33 ~]$
      
      许多程序都有所谓的区域/全域变量的设置概念,当你某个变量要让整体程序库全部都使用时,就得要在父进程设置成为全域变量, 然后子进程才有办法继承的意思!
  • 例题 7.1.5-1:
    # A.B. 将 vim 指令丢到背景,并查看 job number 与 pid
    [student@station10-101 43 ~]$ vim checking.txt
    abc
    [ctrl]+z
    [1]+  Stopped                 vim checking.txt
    [student@station10-101 44 ~]$ jobs -l
    [1]+  6826 Stopped                 vim checking.txt
    
    # C. 尝试『正常删除』该进程
    [student@station10-101 45 ~]$ kill 6826
    [student@station10-101 46 ~]$ jobs -l
    [1]+  6826 Stopped                 vim checking.txt
    # 正常关闭一只进程的方式是无法删除 vim 进程的呢!
    [student@station10-101 47 ~]$ kill %1
    [1]+  Stopped                 vim checking.txt
    [student@station10-101 48 ~]$ jobs -l
    [1]+  6826 Stopped (tty output)    vim checking.txt
    # 使用 kill 可以删除 PID (不加 %),或加上 %n 去删除 n 号工作!
    # 然后,正常情况下,使用 -15 这个正常信号,是无法结束背景中的 vim 的!
    
    # D. 尝试使用强迫删除
    [student@station10-101 49 ~]$ kill -9 %1
    [1]+  Killed                  vim checking.txt
    [student@station10-101 50 ~]$ jobs -l
    
    # E. 再次编辑看看:
    [student@station10-101 52 ~]$ vim checking.txt
    E325: ATTENTION
    Found a swap file by the name ".checking.txt.swp"   <==这里!!!
              owned by: student   dated: Sun Feb 26 20:48:54 2023
             file name: ~student/checking.txt
              modified: YES
             user name: student   host name: station2-253.gocloud.vm
            process ID: 6826
    While opening file "checking.txt"
          CANNOT BE FOUND
    (1) Another program may be editing the same file.  If this is the case,
        be careful not to end up with two different instances of the same
        file when making changes.  Quit, or continue with caution.
    (2) An edit session for this file crashed.
        If this is the case, use ":recover" or "vim -r checking.txt"
        to recover the changes (see ":help recovery").
        If you did this already, delete the swap file ".checking.txt.swp"
        to avoid this message.
    
    Swap file ".checking.txt.swp" already exists!
    [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort: R
    
    abc
    # 然后不保存强制离开! :q! 
    
    [student@station10-101 71 ~]$ ll .checking.txt.swp
    -rw-------. 1 student student 4096 Feb 26 20:48 .checking.txt.swp
    [student@station10-101 72 ~]$ rm .checking.txt.swp
    
    事实上, vim 会创建很多编辑过程中的暂存盘,如果曾经编辑文件到一半当掉,可以通过 r 来复原,否则就按 d 结束掉该文件即可!
  • 例题 7.1.6-1:
    1. 先来检查 login shell 的 ~/.bash_profile 内容
      [student@station10-101 74 ~]$ cat ~/.bash_profile
      if [ -f ~/.bashrc ]; then
              . ~/.bashrc
      fi
      
      事实上,只有读取家目录的 .bashrc 而已!在来看 .bashrc 的内容:
      [student@station10-101 74 ~]$ cat ~/.bashrc
      # 这里指的是,若存在 /etc/bashrc ,就去读入 /etc/bashrc 设置值!
      if [ -f /etc/bashrc ]; then
              . /etc/bashrc
      fi
      
      # 底下指的是,如果 PATH 里面不含有家目录的路径,就加入该路径 (可避免重复设置)
      if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
      then
          PATH="$HOME/.local/bin:$HOME/bin:$PATH"
      fi
      export PATH
      
      # 底下指的是,可以分门别类的将各自的设置加入 ~/.bashrc.d/ 目录内,方便用户设计!
      if [ -d ~/.bashrc.d ]; then
              for rc in ~/.bashrc.d/*; do
                      if [ -f "$rc" ]; then
                              . "$rc"
                      fi
              done
      fi
      unset rc
      
      额外去调用 /etc/bashrc 这个文件,这个文件如果你用 vim 去看,里面还有这一段:
          for i in /etc/profile.d/*.sh; do
              if [ -r "$i" ]; then
                  if [ "$PS1" ]; then
                      . "$i"
                  else
                      . "$i" >/dev/null
                  fi
              fi
          done
      
      又读入不少设置档,大多在 /etc/profile.d 目录内的设置档!这就是每个用户登录时,都会去处理的整体流程!
  • 例题 7.1.6-2:
    [student@station10-101 78 ~]$ vim ~/.bashrc
    HISTSIZE=10000
    alias cp="cp -i"
    alias rm="rm -i"
    alias mv="mv -i"
    ! [[ "$PATH" =~ "~/cmd" ]] && PATH="${PATH}:~/cmd"
    kver=$( uname -r )
    export LANG=zh_TW.utf8
    PS1="[\u@\h \A \# \W]$ "
    h_start=$( wc -l ~/.bash_history | awk '{print $1}' )
    
    [student@station10-101 78 ~]$ source ~/.bashrc
    [student@station10-101 10:03 84 ~]$ echo $h_start
    555
    [student@station10-101 10:04 85 ~]$ echo $HISTSIZE
    10000
    [student@station10-101 10:04 86 ~]$ echo $PATH
    /home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:~/cmd
    [student@station10-101 10:04 87 ~]$ echo $kver
    5.14.0-162.12.1.el9_1.0.2.x86_64
    
  • 例题 7.1.6-3:
    1. 开始设计 ~/.bash_logout 内容
      [student@station10-101 10:04 88 ~]$ vim .bash_logout
      date +%Y/%m/%d" "%H:%M >> ~/history.log
      h_end=$( history | wc -l )
      h_now=$(( $h_end - $h_start +1 ))
      history $h_now >> ~/history.log
      
      [root@station10-101 ~]# su - student
      [student@station10-101 10:10 1 ~]$ cat .bash_logout
      [student@station10-101 10:11 2 ~]$ echo $kver
      [student@station10-101 10:11 3 ~]$ exit
      [root@station10-101 ~]# cat ~student/history.log
      2023/02/26 21:14
        649  cat .bash_logout
        650  echo $kver
        651  exit
      
      每次登录 student 的 login shell 环境下,这个 history.log 的文件都会长大喔!
  • 例题 7.4 课后练习
    
    # a. 合法的 shell 都在 /etc/shells 当中,所以可以这样做:
    [root@station10-101 ~]# vim /etc/shells
    ......
    /usr/bin/csh
    /usr/bin/tcsh
    /bin/false
    /bin/true
    
    [root@station10-101 ~]# chsh -l
    ......
    /bin/false
    /bin/true
    
    # b. 使用 usermod -s [tab][tab] 出现什么?可能的意义是什么?
    [root@station10-101 ~]# usermod -s [tab][tab]
    /bin/bash      /bin/csh       /bin/false     /bin/sh        /bin/tcsh
    /bin/true      /usr/bin/bash  /usr/bin/csh   /usr/bin/sh    /usr/bin/tcsh
    # 看起来就是自动补齐合法的 shells,亦即是取得 /etc/shells 的内容!
    
    # c. 将 /bin/false, /bin/true 移除合法 shell 的列表中。
    [root@station10-101 ~]# vim /etc/shells
    # 将刚刚加入的那两个 shells 移除!
    
    # d. 就是找到最后面出现 /bin/bash 的那个帐号即可
    [root@station10-101 ~]# grep '/bin/bash' /etc/passwd | cut -d ',' -f1,7
    root:x:0:0:root:/root:/bin/bash
    student:x:1000:1000:student:/home/student:/bin/bash
    prouser1:x:1001:1002::/home/prouser1:/bin/bash
    prouser2:x:1002:1003::/home/prouser2:/bin/bash
    prouser3:x:1003:1004::/home/prouser3:/bin/bash
    theuser2:x:1005:1007::/home/theuser2:/bin/bash
    theuser3:x:1006:1008::/home/theuser3:/bin/bash
    theuser1:x:1004:1006::/home/theuser1:/bin/bash
    
    # e. 全域变量可用 env 或 export 查阅
    [root@station10-101 ~]# export
    declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/0/bus"
    declare -x DEBUGINFOD_URLS="https://debuginfod.centos.org/ "
    declare -x HISTCONTROL="ignoredups"
    declare -x HISTSIZE="1000"
    ......
    
    # f.g. 调用出 PS1 变量,然后修改 
    [root@station10-101 ~]# echo $PS1
    [\u@\h \W]\$
    [root@station10-101 ~]# PS1='C:\\win> '
    C:\win>
    C:\win> PS1='[\u@\h \W]\$ '
    [root@station10-101 ~]#
    
    # h.i. 延伸 PATH 的内容
    [root@station10-101 ~]# echo ${PATH}
    /root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    [root@station10-101 ~]# PATH=${PATH}:/opt/bin
    [root@station10-101 ~]# echo ${PATH}
    /root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/bin
    
    # j. 以子指令设置好变量内容
    [root@station10-101 ~]# kver=$( uname -r )
    [root@station10-101 ~]# echo $kver
    5.14.0-162.12.1.el9_1.0.2.x86_64
    
    # k. 设置 kver 为全域变量
    [root@station10-101 ~]# export | grep kver
    [root@station10-101 ~]# export kver
    [root@station10-101 ~]# export | grep kver
    declare -x kver="5.14.0-162.12.1.el9_1.0.2.x86_64"
    
    # l. 查找 signal
    [root@station10-101 ~]# man 7 signal
    ......
       Standard signals
    ......
           Signal      Standard   Action   Comment
           ──────────────────────────────────
    ......
           SIGHUP       P1990      Term    Hangup detected on controlling terminal
                                           or death of controlling process
    ......
       Signal numbering for standard signals
    ......
           Signal        x86/ARM     Alpha/   MIPS   PARISC   Notes
                       most others   SPARC
           ────────────────────────────
           SIGHUP           1           1       1       1
           SIGINT           2           2       2       2
           SIGQUIT          3           3       3       3
           SIGILL           4           4       4       4
           SIGTRAP          5           5       5       5
           SIGABRT          6           6       6       6
           SIGIOT           6           6       6       6
           SIGBUS           7          10      10      10
           SIGEMT           -           7       7      -
           SIGFPE           8           8       8       8
           SIGKILL          9           9       9       9
           SIGUSR1         10          30      16      16
           SIGSEGV         11          11      11      11
           SIGUSR2         12          31      17      17
           SIGPIPE         13          13      13      13
           SIGALRM         14          14      14      14
           SIGTERM         15          15      15      15
           SIGSTKFLT       16          -       -        7
    ......
    # 这功能有点类似重新加载 (reload) 的感觉!
    
    # m. 手动变更 PID 的 hangup (reload) 功能
    [root@station10-101 ~]# ps aux | grep rsyslog
    root     748  0.0  0.3 160248  7240 ?        Ssl  23:11   0:00 /usr/sbin/rsyslogd -n
    root    1918  0.0  0.1 221796  2292 pts/0    S+   23:43   0:00 grep --color=auto rsyslog
    
    [root@station10-101 ~]# date
    Sun Feb 26 11:47:16 PM CST 2023
    
    [root@station10-101 ~]# kill -1 748
    [root@station10-101 ~]# ps aux | grep rsyslog
    root     748  0.0  0.3 160248  7240 ?        Ssl  23:11   0:00 /usr/sbin/rsyslogd -n
    root    1925  0.0  0.1 221796  2252 pts/0    S+   23:44   0:00 grep --color=auto rsyslog
    # PID 号码没改变,所以依旧是原本那只 PID!只是可能状态被改变而已!
    
    # n. 查看注册表信息
    [root@station10-101 ~]# tail -n 100 /var/log/messages | grep rsyslog
    Feb 26 23:46:13 station2-253 rsyslogd[748]: [origin software="rsyslogd" swVersion="8.2102.0-105.el9" 
      x-pid="748" x-info="https://www.rsyslog.com"] rsyslogd was HUPed
    
    # o. 查找 signal
    [root@station10-101 ~]# man 7 signal
    ......
       Standard signals
    ......
           Signal      Standard   Action   Comment
           ──────────────────────────────────
    ......
           SIGINT       P1990      Term    Interrupt from keyboard
    ......
           SIGTSTP      P1990      Stop    Stop typed at terminal
    ......
           Signal        x86/ARM     Alpha/   MIPS   PARISC   Notes
                       most others   SPARC
           ─────────────────────────────
           SIGINT           2           2       2       2
           SIGTSTP         20          18      24      25
    # 各有说明的意义!
    
    # p.q. 直接开另一个窗口并激活 vim 软件,假设完成后,查阅该软件之后处理
    [root@station10-101 ~]# ps aux | grep vim
    root        2031  0.0  0.5 229400  9168 tty3     S+   23:51   0:00 vim
    root        2038  0.0  0.1 221664  2232 pts/0    S+   23:51   0:00 grep --color=auto vim
    [root@station10-101 ~]# kill -20 2031
    [root@station10-101 ~]# ps aux | grep vim
    root        2031  0.0  0.5 229400  9168 tty3     T    23:51   0:00 vim
    root        2099  0.0  0.1 221664  2272 pts/0    S+   23:58   0:00 grep --color=auto vim
    
    # r. 使用 bash 计算整数!
    [root@station10-101 ~]# echo $(( 60*60*24 ))
    86400
    
修改历史:
  • 2023/02/17:RHEL 改版到 EL9 了,只好再度改版!否则教学上面挺困扰!
2023/02/17 以来统计人数
计数器
其他链接
环境工程模式篇
鸟园讨论区
鸟哥旧站

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