鸟哥私房菜读书笔记

文件操作

  1. 读取一个文件的 11-20 行

    • nl test.ext | head -n 20 | tail -n 10

    • nl 命令会列出文件行号

  2. 管道 | 和 xargs

    • 管道 | 表示把管道前的结果(stdout)作为输入交给管道后的命令取处理,实际就是利用管道进行两个进程间单向通信

      例如:echo "--help" | grep "help" 中,"--help" 是作为grep的输入
      
    • xargs, xargs将其接受的字符串做成后续命令的一个命令参数来运行

      例如:echo '--help' | xargs cat 等价于 cat --help 
      
  3. 文件隐藏属性,文件除了rwx还有隐藏属性

    • chattr [+-=] [Asacdistu] 文件名

      例如:文件 +i 这个文件就无法修改删除了,详见 man chattr
      
    • lsattr 文件名 查看文件隐藏属性

  4. 文件特殊权限 SUID SGID SBIT

    • SUID(s):执行该文件的用户短暂获得该文件所有者权限,只能用在二进制可执行文件中

      比如 /usr/bin/passwd,执行这个文件或短暂获得root权限得以修改密码文件 /etc/shadow/passwd,

      [root@ flash]# ls -l /usr/bin/passwd 
      -rwsr-xr-x 1 root root 59640 Mar 23  2019 /usr/bin/passwd
      
    • SGID(s):用于二进制文件执行文件时,获得该文件的群组支持用于目录时,用户进入该目录 建立的文件 群组是该目录的群组 而不是建立文件用户的群组

    • SBIT(t):只能用于目录,该目录下建立的文件和目录只有 创建者和root 可以删除该文件

      chmod u+s,g+s,o+t text  或者 chmod 7777 text
      
  5. 搜索文件

    • find 直接读取磁盘信息查找文件,效率低

    • type/which 指令名 根据[PATH] 搜索指令文件位置

    • whereis 文件名 从特定目录中查找文件 whereis -l 查看搜索目录

    • locate 关键字 由已建立的数据库中搜索结果(/var/lib/mlocate),文件数据库每天更新一次(和系统配置有关),手动更新 updatedb(耗时)

磁盘与文件系统管理

  1. 磁盘格式化:

    文件系统格式化时,可以指定 block (最小存储块)的大小,如果 block 太大,而存储的文件都是小文件,会造成存储空间的浪费,如果 block 太小,在 inode 中就会记录太多 block 号,
    文件读写性能会降低,且因为 inode 中存储 block 的位置大小是有限的,最大可存储文件大小也是固定的。如果磁盘存储大文件居多,则 block 大一些,否则小一些。不过现在磁盘都很大,直接 4k 就行了。

    文件内存存储在各个 block 中,但是并不是一定在连续的 block 号中,如果存储的跨度过大,读写效率会变低,所以一个文件系统大小不要设置过大。

  2. 常见指令:

    • blkid:列出系统已经格式化的装置

    • dumpe2fs /dev/sdb :查询 superblock 和 block group 信息,可以查看文件系统的信息,比如 block、inode 总个数,剩余个数等等,linux 中每个文件都会占用一个 inode ,superblock 中 group0 中有若干 block
      记录了文件的 inode table 信息,一个 inode 大小一般是 25byte,所以这个磁盘的 inode 个数为 inode table * 4096 / 256

    • df [ahikHTm] 查看文件系统磁盘使用情况

    • du [ahskm] 查看目录使用情况

    • diff -r dir1 dir2 :检查目录差异

    • dd : convert and copy a file,该命令会挨个扇区读取

    • cpio :copy files to and from archives

    • iconv :convert text from one character encoding to another

    • dos2unix/unix2dos:转换 linux 和 windows 的换行符

    • netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

  3. 创建文件或目录:

    • EXT文件系统:

      • 当创建一个目录时,首先系统分配一个 inode 和至少一个 block,inode 记录这个目录的权限、属性,以及那个 block 号,block 中具体记录的是目录中 的 子文件以及子目录的 inode 和 文件名,所以一般目录大小为一个 block。
        但是并不是每个目录大小都是 1个 block,如果目录中文件太多了,则会超过 一个 block(一般是4096)

        例如:
        [root@ usr]# ls -dl /usr/bin
        drwxr-xr-x 2 root root 36864 Apr 28 15:26 /usr/bin
        
      • 创建一个文件类似于创建一个目录,

        1. 确定用户对于当前目录的权限
        
        2. 根据 inode bitmap 找到未使用的 inode
        
        3. 根据 block bitmap 找到未使用的 block,将数据写入 block ,并更新 inode 中的 block 指向这些 block
        
        4. 更新 superblock 的内容以及 inode bitmap 和 block bitmap
        
        只不过文件的 block 中记录的是数据,并且要注意 inode 中记录的 block 可能存在多级指向问题,也就是说 inode 中指向的 block 中记录的不是文件内容,而是 block 号,那些 block
        号中才记录着文件内存,inode 中最多有 12 个一级指向。
        
        可以看出,文件名并不记录在 inode 中,而是记录在目录的 block 中。
        
    • XFS文件系统:和 EXT 思路类似,只是 inode 和 block 是动态产生,不是格式化的时候就生成,xfs_info 查看 superblock

  4. 硬盘挂载

    • 查看分区状态,lsblk,blkid,parted(可以查看分区的格式)

    • 磁盘分区:根据 parted 查看格式,如果是 GPT 用 gdisk,如果是 MBR 用 fdisk,

    • 格式化(建立文件系统):mkfs.ext4 或者 mkfs -t ext4

    • 文件系统检验:xfs_repair fsck.ext4

    • 挂载与卸载:mount / unmount

    • 磁盘文件参数修订:mknod(修改硬件装置(b、c、p)的 major 和 minor), xfs_admin/tune2fs 修改 UUID 和 Lable Name

    • 设置开机挂载:修改配置文件 /etc/fstab,修改完成输入 mount -a 验证配置格式正确,否则可能会开机失败

    • 大文件也可以用loop的方式建立文件系统并重新挂载,建立 swap(内存不够时将内存数据存放在磁盘的swap中,格式化:mkswap 启动:swapon

    • 可以使用 parted 一条命令完成磁盘的分区

文件压缩、打包、备份

  1. 文件扩展名在 linux 中没什么作用,但是不同压缩指令的压缩技术不同,一般还是用后缀来区分
    • linux 常见的压缩文件扩展名如下:

      .Z          compress
      .zip        zip
      .gz         gzip
      .bz2        bzip2
      .xz         xz
      .tar        tar(打包程序,不压缩)
      
    • 对应压缩指令如下

      • gzip [-cdtv] 文件名 , gunzip 或 gzip -d 解压 (zcat/zmore/zless) 可以查看压缩文件内容,(egrep)可以过滤压缩文件内容

      • bzip2 [-cdkzv] 文件名, (bzcat/bzmore/bzless/bzgrep)

      • xz [-dtlkc] 文件名, (xzcat/xzmore/xzless/xzgrep)

    • 打包:tar

      压缩方式选项:z(gzip) j(bzip2) J(xz)

      其他选项:v(可视化),f(文件名),c(建立打包文件),x(解包),p(保留文件原有属性,常用于备份),--exclude=filname(不包含)

      例如:打包、压缩(gzip压缩):tar -zcvf filename.tar.gz srcfiles (-f 选项 务必写在最后,后面解析为文件名)

        查询:tar -ztvf filename.tar.gz
      
        解包、解压缩:tar -zxvf filename.tar.gz -C dstdir
      
    `

Bash

  1. linux 下默认使用的 shell

  2. 功能:

    • 记录历史命令在~/.bash_history

    • tab 联想、补全

    • alias 命令别名

    • job control、foreground、background

    • shell 脚本

    • 通配符,例如 ls 可以使用正则来匹配文件名

    • type 指令名可以查看指令的类型

  3. 变量

    • 变量赋值用等号连接,切两边不能有空格

    • 变量值如果有空格,可以用 单引号(特殊字符解析为文本,不处理),双引号(特殊字符保留原本特性,比如 $ 会解析变量),或者转义字符 \

    • 指令嵌套可以用 `指令` 或者 $(指令),例如:

      gdb -p \`ps -a | grep procname\`
      gdb -p $(ps -a | grep procname)
      
    • 变量增加内容 PATH=${PATH}:/var/lib 或者 PATH="$PATH":/var/lib, 取消变量使用为 unset 关键字

    • export 关键字可以将变量导出为环境变量

    • 常见变量

      • 列出环境变量 env

      • 列出所有变量 set

      • $ 也是一个变量,代表该 shell 的进程 ID

      • PS1, 命令提示符

      • ? 存储上一次指令的执行返回值

      Bash特殊符号.png
- 常见指令:
  
    - bash 指令可以在当前 shell 创建一个新的子shell,父shell休眠,exit退出

    - read varName: 读取键盘输入赋值给 varName, declare/typeset [-aixr] 变量名,指定类型(bash 中默认判定变量为字符串)

    - ulimit: get and set user limits

    - stty -a :查看目前环境的快捷键列表

- 数组变量:直接使用 arr[1]=a, 也可以先用 declare -a arrName , 先指定数组类型再使用
  
    ```
    注意:shell 下的数组下标从 1 开始, 不是从 0 开始
    ```
    
- 变量的删除和取代:

    - 删除:`#` 删除最短的那个 `${PATH#/*local/bin:}`, `##` 删除最长的那个 `${PATH##/*:}`, `%` 从后往前删除内容 `${PATH%:*bin}`

    - 替换:`/ ${PATH/sbin/SBIN}`, 用 SBIN 替换 sbin , `//`全部替换 `${PATH//sbin/SBIN}`
        ![Bash变量删除替换.png](https://upload-images.jianshu.io/upload_images/13593096-010925ea8c29610a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        ![Bash变量特殊赋值方式.png](https://upload-images.jianshu.io/upload_images/13593096-9c8a7f3e9998cfa3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

        ```
        [root@ ~]# echo $PATH
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

        [root@ ~]# echo ${PATH#/*local/bin:}
        /usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

        [root@ ~]# echo ${PATH##/*:}
        /snap/bin

        [root@ ~]# echo ${PATH%:*bin}
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

        [root@ ~]# echo ${PATH/sbin/SBIN}
        /usr/local/SBIN:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

        [root@ ~]# echo ${PATH//sbin/SBIN}
        /usr/local/SBIN:/usr/local/bin:/usr/SBIN:/usr/bin:/SBIN:/bin:/usr/games:/usr/local/games:/snap/bin
        ```
  1. Bash shell 操作环境

    • 路径和指令的搜索顺序

      • 以相对或绝对路径执行指令

      • 由 alias 找到该指令执行

      • 由 bash 的内建(builtin)指令执行

      • 根据 PATH 的路径找到第一个执行的位置

    • 环境配置文件

      login-shell

        - 读取 /etc/profile (全局环境配置文件), 才会读取
      
        - 读取个人配置文件,顺序如下,读取到一个就停止 ~/.bash_profile -> ~/.bash_login -> ~/.profile,一般这些文件中也会读取 ~/.bashrc
      

      nonlogin-shell : 直接读取 ~/.bashrc

  2. 数据流重定向

    • command [1>(stdout) 2>(stderr) 2>&1(stderr 重定向给 stdout)] outputFile , 将 command 执行结果输出到 outputFile 中(会覆盖原有内容), >> 表示不覆盖文件中的原有内容, 追加到文件中

    • stdin : command [< <<] inputFile , cat > file1 会将标准输入存入文件file1中,cat > file1 < file2 , 则会将file2的内容作为标准输入存入文件file1

    • 命令执行判断依据:; 分隔命令 , cmd1 && cmd2, cmd1 || cmd2 , 都依赖于 $?(上一个指令的返回值)

      例: ls dir && echo "exist" || echo "not exist"
      
  3. 管道命令(pipe)

    • | , 相当于把前一个命令的 stdout 作为 stdin 传给后一个命令,注意后一个命令一定要能够接受 stdin

    • cut 对一行中的数据进行分解

    • grep, 取出包含存在信息的那一行

    • sort,排序 例:ls -al | sort

    • uniq, 只显示一个

    • wc [-lwm] , 统计有多少行、字(英文单字)、字符

    • tee : read from standard input and write to standard output and files

    • 字符转换:

      tr - translate or delete characters , 
      
      col — filter reverse line feeds from input , 
      
      join - join lines of two files on a common field , 
      
      paste - merge lines of files , 
      
      expand - convert tabs to spaces
      
      split - split a file into pieces, With no FILE, or when FILE is -, read standard input. 合并:cat file* >> totalFile 
      
      xargs - build and execute command lines from standard input, 例:cut -d ':' -f 1 /etc/passwd | head -n 3 | xargs -n 1 id
      

正则表达式和文件格式化处理

  1. 正则表达式


    linux中的正则表达式.png
    • grep 命令支持正则表达式

    • sed - stream editor for filtering and transforming text

      例如:nl file | sed '2,5d' 用 sed 将 2-5 行的显示删除 
      
      [root@ temp]# ifconfig lxcbr_mgt
      lxcbr_mgt: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
      inet 192.168.152.128  netmask 255.255.255.0  broadcast 192.168.152.255
      inet6 fe80::3c83:bbff:fe2d:634b  prefixlen 64  scopeid 0x20<link>
      ether 3e:83:bb:2d:63:4b  txqueuelen 1000  (Ethernet)
      RX packets 23989  bytes 3564278 (3.5 MB)
      RX errors 0  dropped 0  overruns 0  frame 0
      TX packets 32230  bytes 33123567 (33.1 MB)
      TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      
      [root@ temp]# ifconfig lxcbr_mgt | grep "inet " | sed 's\^.*inet\\g' | sed 's\ *netmask.*$\\g'
      192.168.152.128
      
    • 正则表达式的延伸,用 egrep 或者 grep -E


      linux中的正则表达式延申.png
  2. 文件的格式化与相关处理

    • printf 也是一个指令

    • awk: 相比较于 sed 作用于一整行数据的处理,awk 倾向于将整行数据分成数个字段来处理, 一般形式为 awk '条件1{动作1} 条件2{动作2} ...' filename, 也可以接收标准输出


      awk内建变量.png
      [root@ amd64_installer]# last -n 5 
      root     pts/0        192.168.152.1    Thu Aug 19 13:44   still logged in
      root     pts/2        192.168.152.1    Thu Aug 19 13:44   still logged in
      root     pts/3        192.168.152.1    Thu Aug 19 10:17 - 13:28  (03:10)
      root     pts/0        192.168.152.1    Thu Aug 19 08:51 - 13:28  (04:36)
      reboot   system boot  4.15.0-112-gener Thu Aug 19 08:50   still running
      
      wtmp begins Sun Jun 28 17:16:02 2020
      
      # 打印
      [root@ amd64_installer]# last -n 5 | awk '{print $1 "\t lines: " NR}'
      root     lines: 1
      root     lines: 2
      root     lines: 3
      root     lines: 4
      reboot   lines: 5
               lines: 6
      wtmp     lines: 7
      
      # 带逻辑判断 但是第一行未生效
      [root@ amd64_installer]# cat /etc/passwd | awk '{FS=":"} $3 < 10 {print $1 "\t" $3}'
      root:x:0:0:root:/root:/bin/bash
      daemon  1
      bin     2
      sys     3
      sync    4
      games   5
      man     6
      lp      7
      mail    8
      news    9
      
      # BEGIN 从第一行就开始生效
      [root@ amd64_installer]# cat /etc/passwd | awk 'BEGIN{FS=":"} $3 < 10 {print $1 "\t" $3}'
      root    0
      daemon  1
      bin     2
      sys     3
      sync    4
      games   5
      man     6
      lp      7
      mail    8
      news    9
      [root@ amd64_installer]# 
      
      # 带条件动作
      [root@ temp]# cat awk_learn.txt 
      Name    1st     2nd     3th
      hgz     100     200     300
      zzz     99      999     9999
      hgzzz   88      888     8888
      [root@ temp]# cat awk_learn.txt | awk 'NR==1{printf "%-10s %-10s %-10s %-10s %-10s", $1, $2, $3, $4, "Total\n"} \
      > NR>=2{total=$2+$3+$4; printf "%-10s %-10d %-10d %-10d %-10.2f\n", $1, $2, $3, $4, total}'
      Name       1st        2nd        3th        Total
      hgz        100        200        300        600.00    
      zzz        99         999        9999       11097.00  
      hgzzz      88         888        8888       9864.00  
      [root@ temp]# 
      
    • 文件对比工具 diff - compare files line by line(还可以比较目录差异),cmp - compare two files byte by byte

      利用 diff 指令可以生产 patch 文件,结合 patch - apply a diff file to an original 指令可以恢复文件

    • pr - convert text files for printing,文件打印控制相关

shell 脚本

  1. 注意事项

    • 文件最好以 .sh 结尾,并且要有 rx 权限

    • 第一行为 #!/bin/bash 宣告这个脚本使用哪种 shell

  2. script执行方式的差异(source, sh/bash script, ./script,. script)

    • 直接使用绝对路径或者相路径执行(./script), 或者 sh/bash script 执行脚本, 创建一个子 shell 执行这个脚本

    • source 或者 . , 在当前 shell 环境中执行

  3. test - check file types and compare values 指令可以用来做文件的校验(是否存在、属性、内容), 数据比较(整数数值、字符串内容)等测试用途, 在脚本中比较常用

  4. [] 也可以用来判断, 用法类似 test , 但是 [] 中的每个组件都要用空格隔开, 一般配置条件语句去使用

  5. shell 脚本中的参数, $0 为脚本文件名, $1 $2 .... 依次为调用脚本时传入的参数, $#为用户入参个数, $@"$1" "$2" "$3" .... , $*"$1$2$3$4"

  6. 条件判断, 一般一个 [] 写一个条件判断, 用 || 或者 && 连接多个判断表达式

    if [ 条件判断 ]; then
        条件成立执行
    elif [ 条件判断 ]; then
        条件成立执行
    else
        条件不成立执行
    fi
    
    case $variable in 
        "case1")
            满足case1执行
            ;;
        "case2)
            满足case2执行
            ;;
        *)
            其他情况执行
        ;;
    
    esac
    
  7. function 功能

    • 语法
    function funcname()
    {
        脚本代码
    }
    
    
    • 注意事项:

      • 函数在使用之前必须定义

      • 01 2* $# 的含义和 shell 脚本中的含义一致,和参数相关

  8. 循环

    • while 循环, condition 成立时执行

      while [ condition ]
      do
          循环体
      done
      
    • until 循环, 执行到 condition 成立, 其实就是不成立时执行

      until [ condition ]
      do
          循环体
      done
      
    • for 循环, var 依次等于 con1 con2 con3 去执行循环体

      for var in con1 con2 con3...
      do
          循环体
      done
      
      for var in $(seq 1 100)
      do
          循环体
      done
      
      for var in {1..100} # bash 内建机制
      do
          循环体
      done
      
      for ((初始值; 限制值; 执行步阶))
      do
          程序段
      done
      
  9. shell 脚本调试, sh 命令

    • sh -n 只检查语法, 不执行脚本

    • sh -x 将执行过程打印出来

    • sh -v 先将 shell 脚本输出

linux账号管理和ACL权限设定

  1. Linux 账号和群组

    • UID 和 GID 标识一个用户,系统用的也是 UID 和 GID

      [root@ shell_script]# id
      uid=0(root) gid=0(root) groups=0(root)
      [root@ shell_script]# id root
      uid=0(root) gid=0(root) groups=0(root)
      [root@ shell_script]# 
      
    • /ect/passwd 存储用户名等信息 man 5 passwd

      [root@ shell_script]# cat /etc/passwd
      root:x:0:0:root:/root:/bin/bash
      daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
      
      [root@ shell_script]# man 5 passwd 
      PASSWD(5)                                                   File Formats and Conversions                                                   PASSWD(5)
      
      NAME
          passwd - the password file
      
      DESCRIPTION
         /etc/passwd contains one line for each user account, with seven fields delimited by colons (“:”). These fields are:
      
      ·   login name
      
      ·   optional encrypted password
      
      ·   numerical user ID
      
      ·   numerical group ID
      
      ·   user name or comment field
      
      ·   user home directory
      
      ·   optional user command interpreter
      
      
    • /ect/shadow 存储密码信息 man 5 shadow

      [root@ shell_script]# cat /etc/shadow
      root:$6$BZ7lregP$g.ph7e12WqK/iZjLtlc4xR.O16iDSYi.wWgm3xLML06Il7VUYkWwIjfoQfr4RX3TiF1Vcdd9vzwTYJ8ZambNt.:18441:0:99999:7:::
      
      各字段含义如下
      
      login name
         It must be a valid account name, which exist on the system.
      
      encrypted password
         Refer to crypt(3) for details on how this string is interpreted.
      
         If the password field contains some string that is not a valid result of crypt(3), for instance ! or *, the user will not be able to use
         a unix password to log in (but the user may log in the system by other means).
      
         This field may be empty, in which case no passwords are required to authenticate as the specified login name. However, some applications
         which read the /etc/shadow file may decide not to permit any access at all if the password field is empty.
      
         A password field which starts with an exclamation mark means that the password is locked. The remaining characters on the line represent
         the password field before the password was locked.
      
      date of last password change
         The date of the last password change, expressed as the number of days since Jan 1, 1970.
      
         The value 0 has a special meaning, which is that the user should change her password the next time she will log in the system.
      
         An empty field means that password aging features are disabled.
      
      minimum password age
         The minimum password age is the number of days the user will have to wait before she will be allowed to change her password again.
      
         An empty field and value 0 mean that there are no minimum password age.
      
      maximum password age
         The maximum password age is the number of days after which the user will have to change her password.
      
         After this number of days is elapsed, the password may still be valid. The user should be asked to change her password the next time she
         will log in.
      
         An empty field means that there are no maximum password age, no password warning period, and no password inactivity period (see below).
      
         If the maximum password age is lower than the minimum password age, the user cannot change her password.
      
      password warning period
         The number of days before a password is going to expire (see the maximum password age above) during which the user should be warned.
      
         An empty field and value 0 mean that there are no password warning period.
      
      password inactivity period
        The number of days after a password has expired (see the maximum password age above) during which the password should still be accepted
         (and the user should update her password during the next login).
      
         After expiration of the password and this expiration period is elapsed, no login is possible using the current user's password. The user
         should contact her administrator.
      
         An empty field means that there are no enforcement of an inactivity period.
      
      account expiration date
         The date of expiration of the account, expressed as the number of days since Jan 1, 1970.
      
         Note that an account expiration differs from a password expiration. In case of an account expiration, the user shall not be allowed to
         login. In case of a password expiration, the user is not allowed to login using her password.
      
         An empty field means that the account will never expire.
      
         The value 0 should not be used as it is interpreted as either an account with no expiration, or as an expiration on Jan 1, 1970.
      
      reserved field
         This field is reserved for future use.
      
      
    • /etc/group 存储用户组信息

      • 用户可以加入多个群组,在使用已存在的文件时,用户拥有全部加入群组的权限。但是在创建文件时,新文件的群组是属于有效群组的,使用命令 groups 输出的第一个群组既是有效群组,使用指令 newgrp 切换有效群组,这个指令会新建一个子 shell 进行后续操作。
      [root@localhost /]# man 5 group
      GROUP(5)                                    Linux Programmer's Manual                                    GROUP(5)
      
      NAME
          group - user group file
      
      DESCRIPTION
         The  /etc/group  file  is a text file that defines the groups on the system.  There is one entry per line,
         with the following format:
      
            group_name:password:GID:user_list
      
      The fields are as follows:
      
      group_name  the name of the group.
      
      password    the (encrypted) group password.  If this field is empty, no password is needed.
      
      GID         the numeric group ID.
      
      user_list   a list of the usernames that are members of this group, separated by commas.
      
    • /etc/gshadow

       [root@localhost etc]# man 5 gshadow
       GSHADOW(5)                                 File Formats and Conversions                                GSHADOW(5)
       
       NAME
           gshadow - shadowed group file
       
       DESCRIPTION
      /etc/gshadow contains the shadowed information for group accounts.
      
      This file must not be readable by regular users if password security is to be maintained.
      
      Each line of this file contains the following colon-separated fields:
      
      group name
          It must be a valid group name, which exist on the system.
      
      encrypted password
          Refer to crypt(3) for details on how this string is interpreted.
      
          If the password field contains some string that is not a valid result of crypt(3), for instance ! or
          *, users will not be able to use a unix password to access the group (but group members do not need
          the password).
       
          The password is used when an user who is not a member of the group wants to gain the permissions of
          this group (see newgrp(1)).
       
          This field may be empty, in which case only the group members can gain the group permissions.
       
          A password field which starts with a exclamation mark means that the password is locked. The remaining
          characters on the line represent the password field before the password was locked.
       
          This password supersedes any password specified in /etc/group.
      
      administrators
          It must be a comma-separated list of user names.
      
          Administrators can change the password or the members of the group.
       
          Administrators also have the same permissions as the members (see below).
      
      members
          It must be a comma-separated list of user names.
      
          Members can access the group without being prompted for a password.
       
          You should use the same list of users as in /etc/group.
      
  2. 账号管理

    • 指令

      • useradd [-u uid] [-g gid] [-d homepath] [-s shell]

        - 新建用户会默认新建一个同名群组
        
        -  useradd -D 显示用户添加的默认值,这些值来源于 /etc/default/useradd, 另外还会参考 /etc/login.defs
        
        - 用户的 home 目录复制来源于 /etc/skel
        
      • passwd [--stdin] [账号名], 新创建的用户是没有设定密码的,这个指令可以修改密码,以及密码相关属性,例如密码生存周期

      • chage 也可以修改密码相关的属性

      • usermod 修改用户相关属性

      • userdel 删除用户

      • id 查询 uid gid

      • finger 查询用户信息

      • chfn 修改 finger 中显示的内容

      • chsh 修改用户使用的 shell

      • groupadd, 新建群组

      • groupmod, 修改群组相关属性

      • groupdel, 删除群组,需要群组内没有用户存在

      • gpasswd, 设置群组密码和管理员,或者有群组管理员用来管理群组成员

  3. ACL(Access Control List)

    • ACL可以针对单一使用者,单一目录、文件来进行 rwx 的管理, 需要文件系统去支持该特性

    • 指令

      • setfacl - set file access control lists

        # 设定指定用户test1对于文件的权限
        [root@localhost ~]# setfacl -m u:test1:r test
        
        # 设定指定群组group1对于文件的权限
        [root@localhost ~]# setfacl -m g:group1:r test
        
        # 设定文件的 mask, 设定的权限要和 mask 做与操作,相当于最大权限
        [root@localhost ~]# setfacl -m m:r test
        
        # 设定指定群组group1对于文件的权限, 使用 d 选项表示设置默认属性,在目录下的文件会继承ACL默认属性
        [root@localhost ~]# setfacl -m d:g:group1:r dir
        
      • getfacl - get file access control lists

        [root@localhost ~]# ll
        total 0
        -rw-r--r--+ 1 root root 0 Aug 26 16:24 test
        [root@localhost ~]# getfacl test
        # file: test
        # owner: root
        # group: root
        user::rw-
        user:test1:r--
        group::r--
        mask::r--
        other::r--
        
  4. 切换用户

    • su - run a command with substitute user and group ID,

      • 选项 - 或者 -l 表示使用 login-shell 的方式切换用户,bash 的一些相关配置会重新加载, 如果不用这些选项,切换以后用户环境不会变化

      • 选项 -c 可以只执行一次指令 su - -c "cat /etc/passwd"

      • root 可以随意切换用户而不用输入密码

    • sudo, sudoedit — execute a command as another user

      • sudo [-b 后台执行] [-u username] 指令

      • 用户能不能使用 sudo 指令要看 /etc/sudoers 文件中的配置,一般使用专用指令 visudo 去修改这个文件

        # User privilege specification
        # 含义为  用户名    登录主机来源(可切换的身份:可切换的群组) 可执行的指令
        root    ALL=(ALL:ALL) ALL  
        
        # Members of the admin group may gain root privileges
        %admin ALL=(ALL) ALL  
        
        # 在该文件中还可以使用别名来简化文件书写
        
        # 用户别名
        User_Alias USERS = user1, user2, user3
        
        #主机别名
        Host_Alias HOSTS = smtp, smtp2
        
        # 指令别名 ! 表示该指令不可用,还可以在指令中用正则表达式
        Cmnd_Alias CMDS = !/usr/bin/passwd, !/usr/bin/passwd root, /usr/bin/passwd [A-Za-z]*
        
        # 使用别名指定权限
        USERS    HOSTS=(root:root) CMDS  
        
  5. 特殊的shell和PAM模块

    • 特殊的 shell, /usr/sbin/nologin, 拥有该类shell 的用户无法登录,但是可以使用系统资源

    • PAM(Pluggable Authentication Modules) - for Linux, Linux-PAM is a system of libraries that handle the authentication tasks of applications (services) on the system.

      修改密码的流程:
      1. 用户调用 /usr/bin/passwd
      2. passwd 调用 PAM
      3. PAM 模块到 /etc/pam.d/ 找到和 passwd 同名的配置文件
      4. 根据 /etc/pam.d/passwd 配置,引用 PAM 模块逐步分析验证
      5. PAM 调用结果传回给 passwd
      6. passwd 继续处理
      
    • 具体 PAM 配置文件的格式 可以参考 man 5 pam.d

  6. 用户查询、检查相关指令

    • w - Show who is logged on and what they are doing.

    • who - show who is logged on

    • last, lastb - show a listing of last logged in users

    • lastlog - reports the most recent login of all users or of a given user

    • write user [tty] — send a message to another user

    • pwck - verify integrity of password files

    • pwconv, pwunconv, grpconv, grpunconv - convert to and from shadow passwords and groups

    • chpasswd - update passwords in batch mode

进阶文件管理系统

  1. Quota

    • 用于文件系统资源分配问题, 可以限制包括群组、用户、目录的最大磁盘配额。 例如在邮箱系统中, 限制每个用户的邮箱存储大小

    • 使用限制

      • 在 EXT 文件系统中仅能针对整个 filesystem,不能针对目录,所以要先搞清楚文件系统的支持情况。

      • Linux 核心必须支持 Quota

      • 对 root 来说不起作用,仅对一般用户有效

      • 若启用 SELinux,不是所有的目录都可以使用 quota,SELinux 会加强某些细节的文件权限

    • Quota 的限制项目

      • 分别针对群组、用户或者目录

      • 容量限制(block)或者文件数量(inode)限制

      • 柔性规劝和硬性规定(soft/hard): 超过 hard 限制就会锁住该用户的磁盘使用权,超过 soft 低于 hard 一般给出告警,并给予一个宽限时间(grace time)

      • 宽限时间(grace time): 超过这个时间 soft 变为 hard,锁住磁盘


        quota.png
  2. 磁盘阵列(Redundant Arrays of Independent Disks,RAID)

    • RAID_0(等量模式,stripe 性能最佳): 将数据分割成不同条带(Stripe)分散写入到所有的硬盘中同时进行读写。多块硬盘的并行操作使同一时间内磁盘读写的速度提升N倍,但是任一磁盘损坏可能导致所有数据无法读取。

    • RAID_1(映像模式,mirror 完整备份): 原理是把一个磁盘的数据镜像到另一个磁盘上,也就是说数据在写入一块磁盘的同时,会在另一块闲置的磁盘上生成镜像文件,在不影响性能情况下最大限度的保证系统的可靠性和可修复性上,只要系统中任何一对镜像盘中至少有一块磁盘可以使用,甚至可以在一半数量的硬盘出现问题时系统都可以正常运行,当一块硬盘失效时,系统会忽略该硬盘,转而使用剩余的镜像盘读写数据,具备很好的磁盘冗余能力。虽然写入效率不佳,但是读出时由于有备份的存在,依然会有所优化,主要是备份功能。

    • RAID0+1/RAID1+0: 结合 RAID_0 和 RAID_1 的优点:数据除分布在多个盘上外,每个盘都有其物理镜像盘,提供全冗余能力,允许一个以下磁盘故障,而不影响数据可用性,并具有快速读/写能力,至少需要四个磁盘。

    • spare disk(预备磁盘): 当磁盘阵列中有磁盘损坏时,spare disk会加入磁盘阵列,将损坏的磁盘移除,并立即重建数据系统。

    • 可以分为硬件磁盘阵列和软件磁盘阵列

      • 硬件磁盘阵列:通过磁盘阵列卡达成数组的目的,卡上由专门的芯片处理RAID任务,性能好,但是价格高,且操作系统需要相关驱动程序来支持磁盘阵列卡。

      • 软件磁盘阵列:通过软件仿真数组任务,对于 cpu 和 I/O 总线消耗更高。例如:centos 的 mdadm,只要由两个以上的分区槽就可以使用软件磁盘阵列,指令见 man mdadm.

        RAID.png

  3. LVM(Logical Volume Manager 逻辑卷管理)

    • 本质上是一个虚拟设备驱动,是在内核中块设备和物理设备之间添加的一个新的抽象层次。它可以将几块磁盘(物理卷,PhysicalVolume)组合起来形成一个存储池或者卷组(VolumeGroup)。LVM可以每次从卷组中划分出不同大小的逻辑卷(LogicalVolume)创建新的逻辑设备。底层的原始的磁盘不再由内核直接控制,而由LVM层来控制。对于上层应用来说卷组替代了磁盘块成为数据存储的基本单元。LVM管理着所有物理卷的物理盘区,维持着逻辑盘区和物理盘区之间的映射。LVM逻辑设备向上层应用提供了和物理磁盘相同的功能,如文件系统的创建和数据的访问等。但LVM逻辑设备不受物理约束的限制,逻辑卷不必是连续的空间,它可以跨越许多物理卷,并且可以在任何时候任意的调整大小。相比物理磁盘来说,更易于磁盘空间的管理。


      LVM.png
    • LVM 注重的是文件系统的弹性调整容量。

    • 指令:pvcreate -> vgcreate -> lvcreate


      LVM指令.png
    • 容量变更: 主要是将 lv 的容量变更,只要 vg 容量足够就可以扩容,使用指令为 lvresize, 然后使用 xfs_growfs 对文件系统扩容,目前 EXT 支持缩小,xfs不支持。

定时任务

  1. at

    • at and batch read commands from standard input or a specified file which are to be executed at a later time.

    • 处理只执行一次的任务,依赖于 atd 服务,如果任务有 stdout 或者 stderr 会发邮件

      systemctrl (enable | restart | status) atd/crond

      [root@ /]# at now + 1 minute
      at> echo "hello" > /dev/pts/2 # at执行和终端环境无关    
      at> ^C[root@dwqiu etc]# 
       [root@ /]# at now + 1 minute
      at> echo "hello at"  
      at> <EOT>
      job 2 at 2021-09-03 16:09
       [root@ /]# 
      
    • 用户权限问题:查找 /etc/at.allow /etc/at.deny 中查看用户是否可以使用该功能,原则是如果 /etc/at.allow 存在,则只有其中的用户可以使用, 如果 /etc/at.deny 存在,则其中用户不能使用,都不存在只有 root 能使用。

    • atq: 查看

    • atrm: 删除

    • batch 也是调用 at,不过会选择在系统不忙碌的时候去执行任务,而不是指定时间

  2. crontab

    • 处理循环执行的任务,依赖于 crond 服务,如果任务有 stdout 或者 stderr 会发邮件

    • /etc/cron.allow /etc/cron.deny 和 at 类似

    • /var/spool/cron 中记录设置的任务,且以用户名分文件存储,crontab -e 产生的任务存储在这里,一般个人化的任务放在这个文件里面

    • /etc/crontab 中存储着系统的定时任务,如果要增加系统性的任务,直接编辑该文件

    • /etc/cron.d/* 中的任务也会定时执行,如果是自己开发软件,则可以将任务放在该目录下

    • 用法: crontab [-u username] [-l(list) | -e(edit) | -r(remove)]


      cron -e.png
      [root@ mail]# crontab -e        
      1 # Edit this file to introduce tasks to be run by cron.
      2 # 
      3 # Each task to run has to be defined through a single line
      4 # indicating with different fields when the task will be run
      5 # and what command to run for the task
      6 # 
      7 # To define the time you can provide concrete values for
      8 # minute (m), hour (h), day of month (dom), month (mon),
      9 # and day of week (dow) or use '*' in these fields (for 'any').# 
      10 # Notice that tasks will be started based on the cron's system
      11 # daemon's notion of time and timezones.
      12 # 
      13 # Output of the crontab jobs (including errors) is sent through
      14 # email to the user the crontab file belongs to (unless redirected).
      15 # 
      16 # For example, you can run a backup of all your user accounts
      17 # at 5 a.m every week with:
      18 # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
      19 # 
      20 # For more information see the manual pages of crontab(5) and cron(8)
      21 # 
      22 # m h  dom mon dow   command
      23 # 添加这一行 每分钟输出一次 hello
      24 */1 * * * * echo "hello" > /dev/pts/1 
      
      
  3. anacron

    • 和 crontab 和 at 不同,anacron 是一个程序而不是一个服务,crontab 和 at 是定时执行任务,一旦这个时间过了而没有执行任务,是不会再执行的(比如正好要执行的时候断电了,再重启则不会执行任务),而 anacron 则会去执行这个任务。

    • /etc/anacrontab 配置了要执行的任务(可以设置执行周期(hour)和延时(分钟)等),anacron 的原理是会将任务执行的时间戳存储,再次激活时检查上次任务执行的时间和系统时间差值决定是否执行任务,而激活的操作在 /etc/cron.hourly 里面,每小时执行一次。

    • 流程:

      • crond 服务每分钟主动读取 /etc/cron.d/0hourly
      • 执行 /etc/cron.hourly
      • 执行 /etc/cron.hourly/0anacron, 每小时执行 anacron
      • 根据 /etc/anacrontab 执行其中的配置

进程管理和SELinux

  1. 进程

    • 进程:在 linux 中,触发任何一个时间,系统都会将它定义成一个进程,并且赋予一个PID,同时按照触发这个进程的用户给与进程相应的权限


      proc文件.png
    • 一些常驻的进程称为服务(daemon), 一般名字都是以 d 结尾, 例如 crond atd ssd 等

    • 多人多任务环境,可以在终端中使用 Alt+[F1-F7] 切换 tty

  2. job control

    • 在我们登陆 bash shell 以后,在一个终端下同行进行多个任务的行为管理,每个任务其实都是当前 bash 的子进程。

    • job 分为 foreground job 和 background job, 在使用指令时加上 & 可以使得任务进入 background 执行,但是这类任务一般都是自动执行的,不需要交互。

      • 指令 & 将指令在后台执行, stdout stderr 仍然会输出,可以将其重定向到文件中

      • ctrl + z:将正在执行的指令改为 background 并暂停

      • jobs 显示当前执行的 job, + 表示最近添加的 job, - 则是倒数第二个,其它的不会有符号

        [root@ hgz]# jobs
        [1]+  Stopped                 vi test.c
        [2]-  Running                 find / -name "*.html" > /hgz/find.log 2>&1 &
        
      • fg + %jobnum, 可以将job切换到前台操作, jobnum 也可以是 + - 没有指定则是 + 那个job

      • bg 可以把 background 中暂停(ctrl + z)的 job 运行起来

      • kill -signal %jobnum,杀死job,一定要加 %,否则就是杀死进程

      • nohup - run a command immune to hangups, with output to a non-tty,可以脱离当前 tty 执行

  3. 进程管理

    • ps 指令查看进程信息

      • ps -l:查看自己的 bash 相关进程

        F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
        4 S     0  2344  2093  0  80   0 -  5433 wait   pts/0    00:00:01 bash
        
        F:权限 4 表示 root 权限
        S: status, R 运行, S 休眠(可以被signal), D 不可唤醒的休眠(等待I/O))), T 停止, Z 僵尸态
        UID/PID/PPID  
        C: cpu 利用率
        PRI/NI: 执行优先级 
        ADDR/SZ/WCHAN:ADDR表示进程在内存那个位置 - 表示 running, SZ 表示用了多少内存,WCHAN 表示是否在运行
        TTY:执行终端
        TIME:实际执行花费的 cpu 时间
        CMD:执行指令
        
      • ps aux:列出内存中所有进程

        USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
        daemon    1465  0.0  0.1  28332  1980 ?        Ss   09:58   0:00 /usr/sbin/atd -f
        root      1480  0.0  0.2  30028  2864 ?        Ss   09:58   0:00 /usr/sbin/cron -f
        
        USER:进程用户
        PID:进程号
        %CPU:cpu使用率
        %MEM:内存使用率
        VSZ:虚拟内存使用量(Kbytes)
        RSS:占用固定内存量(Kbytes)
        TTY:终端  
        STAT:状态
        START:启动时间
        TIME:在cpu中实际运行时间
        COMMAND:指令
        
      • pstree:display a tree of processes

      • top:观察进程动态变化数据

      • kill - send a signal to a process

      • killall - kill processes by name

    • 进程优先级

      • 也就是之前 ps 指令中的 PRI、NI,用户只可以指定 NI 值来修改优先级, PRI 由内核动态调整,关系为:PRI(new) = PRI(old) + NI,NI 值子进程继承于父进程

      • nice [-n (-20-19)] cmd:以指定的 NI 值执行 cmd

      • `renice [number] PID - alter priority of running processes

    • 系统资源

      • free - Display amount of free and used memory in the system

      • uname - print system information

      • uptime - Tell how long the system has been running.

      • netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

      • dmesg - print or control the kernel ring buffer

      • vmstat - Report virtual memory statistics

  4. 特殊文件与进程

    • SUID/SGID:在执行拥有这些权限的文件时,在程序执行期间可以获得文件所有者的权限,也就是说启动的进程权限不是调用者权限,而是文件所有者权限,例如:passwd 指令

      [root@ bin]# ll /usr/bin/passwd
      -rwsr-xr-x 1 root root 59640 Mar 23  2019 /usr/bin/passwd*
      
      用户在执行 passwd 时获得 root 指令,得以修改 /etc/shaow 文件
      
    • /proc/* 中存储了运行中的进程相关的信息和数据

    • fuser -uv file - 查看文件被那些进程使用

    • lsof -u usernmae +d dir:列出打开的全部文件

    • pidof program_nmae:列出 program_nmae 的PID

  5. SELinux:security enhanced linux

    • 传统文件权限和账号关系是DAC discretionary access control,依据文件的 rwx 属性来判断用户权限,但是对于 root 不生效。SELinux 使用MAC mandatory access control,权限设置的主体不再是用户,而是进程,可以针对进程设置访问权限,并且有一些预设的 policy,其中存在默认的多个 rule。

    • 运行模式

      • 安全控制主体为进程

      • 安全控制目标为文件系统的权限

      • 安全控制依据为 policy,依照某些服务的特性来预指定 policy,policy中还有详细的规则,例如限制 httpd 进程只能访问 /Apache/WWW 目录,当然,这些都可以修改

      • security context,除了 policy,还要对比主体和目标的security context是否一致,类似于 rwx

      • 可执行文件运行时会创建一个进程,进程会根据可执行文件的类型(type)获得一个域(domain),在 policy 中已经为各个domain制定了规则,其中的规则就包括这个domain的进程可以访问哪些类型(type)的文件

      • sestatus | getenforce:获取 SELinux 运行状态,包括 enforcing(强制),permissive(宽容,只提示),disabled(禁用)

      • setenforce [0|1]: (临时修改)不能设置禁用,设置禁用需要修改配置文件 /etc/selinux/config,并重启

      • getsebool/getsebool [-a] [rulename]:查看规则是否启用

      • sesearch [-s domain]:查看进程domain能够访问的文件type

      • chcon [-R] [-t type] file:修改文件类型

      • restorecon [-Rv] file:恢复预设类型

系统服务

  1. 早期的 System V 的 init 管理行为

    • 所有的系统服务启动脚本都放置在 /etc/init.d/ 底下

    • 脚本的执行分为[0-6],各个执行等级的脚本通过 /etc/rc.d/rc[0-6]/SXXscript 连接到 /etc/init.d/script,S 为 start 的含义,XX为数字,表示启动顺序。

    • chkconfig - updates and queries runlevel information for system services

  2. systemd 使用的 unit 分类

    • 从 centos 7.x 以后,改用 systemd 这个启动服务管理机制

    • 多个服务同时启动

    • 服务器依赖性的自我检查,例如服务A依赖服务B,那么在启动A时会自动启动服务B

    • 依照服务的功能分类,将服务定义一个服务单位unit,并分类(service socket target path snapshot timer)


      systemd扩展名.png
    • 将多个服务归类为一个群组(target)

    • 兼容 init 管理行为

    • 文件,优先级依次升高

      • /usr/lib/systemd/system,每个服务最主要的启动脚本设定

      • /run/systemd/system 系统执行过程中产生的服务脚本

      • /etc/systemd/system 管理员依据主机系统的需求所建立的执行脚本

    • 注意:不要用 kill 去杀死一个正在被 systemd 监管的程序,否则 systemd 会无法继续监管该程序,用 systemctl stop servername

    • systemd + systemctl 指令,systemctl [OPTIONS...] COMMAND [NAME...]

      • systemctl 查看所有服务

      • systemctl list-units --type=service --all 带选项筛选查看

      • systemctl show 服务 查看服务的属性

      • systemctl stop 服务 关闭服务

      • systemctl mask 服务 This will link these unit files to /dev/null,这个服务无法再启动,可以用 unmask 取消

      • systemctl mask XXX.target 对于 target 类型的 unit,使用 isolate 命令

      • systemctl poweroff | reboot | suspend(暂停) | hibernate(休眠) | rescue(救援模式) | (emergency)

      • systemctl list-dependencies --reverse 服务 列出服务间的依赖关系

      • systemctl daemon-reload Reload the systemd manager configuration,新增服务增加了配置文件需要使用这个指令

    • /etc/services - Internet network services list,存储服务和端口号的默认对应关系

    • 将所有服务状态写入文件 systemctl list-units | awk '{print $1}' | xargs -n 1 systemctl status >> /var/server_status,可以将这个命令设置成定期执行任务

    • 配置文件,以 sshd 为例

      [root@ system]# cat sshd.service 
      [Unit] # unit 本身说明和依赖关系说明
      Description=OpenBSD Secure Shell server # 说明
      After=network.target auditd.service # 该unit在哪些服务之后启动(非强制)
      ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
      
      [Service]
      EnvironmentFile=-/etc/default/ssh # 启动脚本的环境变量配置文件
      ExecStartPre=/usr/sbin/sshd -t # 启动前额外执行的脚本
      ExecStart=/usr/sbin/sshd -D $SSHD_OPTS # 启动脚本
      ExecReload=/usr/sbin/sshd -t # 和 systemctl relaod 相关
      ExecReload=/bin/kill -HUP $MAINPID
      KillMode=process # 终止模式(process 终止当前程序 control-group 终止产生的所有程序)
      Restart=on-failure
      RestartPreventExitStatus=255
      Type=notify
      RuntimeDirectory=sshd
      RuntimeDirectoryMode=0755
      
      [Install]
      WantedBy=multi-user.target # 附挂在哪一个 target 底下
      Alias=sshd.service # 别名
      [root@ system]# 
      
    • 利用 timer 这种 unit 也可以达到定时执行任务的功能

      • 前提:一定要启动 timer.target

      • 配置一个 unit,配置一个 .timer,在 .timer 中配置时间相关的配置以及要执行的 unit 详情见 man systemd.timer,配置文件在 /etc/systemd/system/*.timer

        timer.png

日志文件

  1. 常用日志文件

    • /var/log/boot.log 开机启动信息日志

    • /var/log/cron cron 服务相关日志

    • /var/log/dmesg 开机的硬件检测过程日志

    • /var/log/lastlog 所有账号最近登录时间

    • /var/log/messages 系统发生的错误信息都记录在这个文件里

    • /var/log/secure 涉及到输入密码的程序的登入信息都记录在这个文件中

    • /var/log/wtmp /var/log/faillog 记录 正确/错误 登入的账户信息

  2. 日志文件记录方式

    • 软件自主记录日志文件

    • 使用 rsyslog.service 统一管理日志文件

    • systemd 管理系统的各种服务,也会将服务的启动等操作日志通过 systemd-journald.service 以二进制的形式记录在内存中,再发送给 rsyslog.service进一步记录在文件中

  3. rsyslog.service

    • 配置文件在 /etc/rsyslog.conf 中,配置服务的名称,服务产生日志的等级和记录日志的文件位置

      #rules
      #add by hgz learning linux
      *.info          /var/log/admin.log
      auth.=error     root  # 将 auth 模块的错误日志发送邮件给 root 
      
      syslog服务类型.png

      syslog日志等级.png
    • 配置本机作为日志服务器,接收网络日志

      # provides UDP syslog reception 需要开启 UDP 接收日志就开启底下两行
      #module(load="imudp")
      #input(type="imudp" port="514")
      
      # provides TCP syslog reception 需要开启 TCP 接收日志就开启底下两行
      #module(load="imtcp")
      #input(type="imtcp" port="514")
      
    • 将日志记录到日志服务器中的配置

      *.*     @@192.168.1.100  # TCP
      *.*     @192.168.1.100   # UDP
      
  4. 日志轮换功能 --- logrotate

    • 将旧的日志文件重命名或删除,创建一个新的日志文件来重新开始记录日志

    • 利用 /etc/cron.daily/logrotate,每天去执行日志轮换的脚本

    • 配置文件在 /etc/logrotate.con /etc/logrotate.d,可以把/etc/logrotate.con 中的配置理解为全局配置 /etc/logrotate.d 中的则是每个日志文件的具体配置

      [root@ cron.daily]# more /etc/logrotate.conf 
      # see "man logrotate" for details
      # rotate log files weekly
      weekly # 全局类型的
      
      # use the syslog group by default, since this is the owning group
      # of /var/log/syslog.
      su root syslog
      
      # keep 4 weeks worth of backlogs
      rotate 4
      
      # create new (empty) log files after rotating old ones
      create
      
      # uncomment this if you want your log files compressed
      #compress
      
      # packages drop log rotation information into this directory
      include /etc/logrotate.d
      
      # no packages own wtmp, or btmp -- we'll rotate them here
      /var/log/wtmp { # 针对每个日志文件的定制配置
          missingok
          monthly
          create 0664 root utmp
          rotate 1
      }
      
      /var/log/btmp {
          missingok
          monthly
          create 0660 root utmp
          rotate 1
          size=10M # 超过10M就轮换,不需要等到monthly
          compress # 压缩
          sharedscripts # 脚本开始
          prerotate # 轮换执行之前执行这里的shell指令
              /usr/bin/chattr -a /var/log/btmp
          endscript # 脚本结束
          sharedscripts 
          postrotate # 轮换执行之前执行这里的shell指令
              # rsyslog reload
              /bin/kill -HUP `cat /var/run/syslog.pid 2> /dev/null` 2> /dev/null || true 
              /usr/bin/chattr +a /var/log/btmp
          endscript
      }
      
      # system-specific logs may be configured here
      
    • 除了定期执行,也可以使用指令来轮换日志文件 ogrotate [-dv] [-f|--force] [-s|--state file] config_file

  5. systemd-journald.service 简介

    • rsyslogd需要在服务启动之后才能够开始记录日志,而在设备启动前的日志则是由 systemd 启动 systemd-journald.service 来记录,记录在 ram 中,掉电丢失

    • 日志记录在文件 /run/log 中,实际就是运行内存中

    • 配置文件 /etc/systemd/journald.conf

    • 指令

      • journalctl - Query the systemd journal

      • logger - enter messages into the system log

      [root@ log]# logger -p user.info "I am hgz"
      [root@ log]# journalctl SYSLOG_FACILITY=1 -n 3
      -- Logs begin at Sun 2020-06-28 17:16:17 CST, end at Sat 2021-09-11 16:21:27 CST. --
      Sep 11 16:21:27 simulator_pro root[2972]: I am hgz
      

开机流程,模块管理和Loader

  1. Linux 的开机流程
    • 载入 BIOS 的硬件信息与进行自我测试,并依据设置取得第一个可开机的设备;
    • 读取并执行第一个开机设备内 MBR 的 boot Loader (亦即是 grub2, spfdisk 等程序) ;
    • 依据 boot loader 的设置载入 Kernel ,Kernel 会开始侦测硬件与载入驱动程序;
    • 在硬件驱动成功后,Kernel 会主动调用 systemd 程序,并以 default.target 流程开机;
      • systemd 执行 sysinit.target 初始化系统及 basic.target 准备操作系统;
      • systemd 启动 multi-user.target 下的本机与服务器服务;
      • systemd 执行 multi-user.target 下的 /etc/rc.d/rc.local 文件;
      • systemd 执行 multi-user.target 下的 getty.target 及登陆服务;
      • systemd 执行 graphical 需要的服务
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容