书架上的这块大部头书在5.1的时候终于有时间来学习了,本文将长期更新,记录UNIX/Linux系统管理技术手册学习中的疑问与解答。该书在豆瓣评分颇高,UNIX/Linux系统管理技术手册.勘误表在本文最后,如有疑问欢迎留言讨论。
笔记中实际操作的系统来自于ubuntu,运行在一款古董级的hp笔记本上,在Macbook Pro中运行了一台centos7虚拟机,电脑卡的时候关闭该虚拟机使用hp笔记本中的虚拟机。

国外网站上的TL;DR (or tl;dr)表示: "Too Long; Didn't Read"
AFAIK:as far as I know
IMO:In my opinion

判断软件是否已经安装

简单的做法是使用shell的which命令找出相关的二进制程序是否已经在搜索路径中。

root@52coder:~# which gcc
/usr/bin/gcc

如果which没有找到要找的命令,使用whereis,该命令搜索更大范围的系统目录,与shell的搜索路径无关。

root@52coder:~# whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz

locate的数据库通常由upgradedb命令定期更新,这条命令由cron来运行。因此,执行一次locate的结果不是总能反映出文件系统最新的变化。locate的搜索不止针对命令或软件包,而是能找到任何类型的文件。例如查找文件signal.h

root@52coder:~# locate signal.h
/root/PR/cl_ev/source/include/cl_ev_signal.h
/root/PR/lualibs/source/ldap/openldap-2.4.23/include/ac/signal.h
/root/PR/lualibs/thirdpart/luaposix/luaposix-34.0.4/doc/modules/posix.signal.html
/root/PR/thirdpart/ldap/source/openldap-2.4.23/include/ac/signal.h
/usr/include/signal.h
/usr/include/asm-generic/signal.h
/usr/include/boost/signal.hpp
/usr/include/boost/process/detail/posix/signal.hpp
/usr/include/boost/signals2/preprocessed_signal.hpp
/usr/include/boost/signals2/signal.hpp
/usr/include/boost/signals2/variadic_signal.hpp
/usr/include/linux/signal.h
/usr/include/x86_64-linux-gnu/asm/signal.h
/usr/include/x86_64-linux-gnu/sys/signal.h
/usr/src/linux-headers-5.0.0-13/arch/alpha/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/alpha/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/arc/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/arm/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/arm/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/arm64/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/h8300/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/h8300/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/hexagon/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/ia64/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/ia64/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/m68k/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/m68k/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/mips/include/asm/compat-signal.h
/usr/src/linux-headers-5.0.0-13/arch/mips/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/mips/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/nios2/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/parisc/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/parisc/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/powerpc/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/powerpc/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/s390/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/s390/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/sh/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/sparc/include/asm/compat_signal.h
/usr/src/linux-headers-5.0.0-13/arch/sparc/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/sparc/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/x86/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/x86/include/asm/fpu/signal.h
/usr/src/linux-headers-5.0.0-13/arch/x86/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/xtensa/include/asm/signal.h
/usr/src/linux-headers-5.0.0-13/arch/xtensa/include/uapi/asm/signal.h
/usr/src/linux-headers-5.0.0-13/include/asm-generic/audit_signal.h
/usr/src/linux-headers-5.0.0-13/include/asm-generic/signal.h
/usr/src/linux-headers-5.0.0-13/include/linux/signal.h
/usr/src/linux-headers-5.0.0-13/include/linux/sched/signal.h
/usr/src/linux-headers-5.0.0-13/include/trace/events/signal.h
/usr/src/linux-headers-5.0.0-13/include/uapi/asm-generic/signal.h
/usr/src/linux-headers-5.0.0-13/include/uapi/linux/signal.h

ubuntu中查询是否已安装软件:

root@52coder:~# dpkg -l | grep gcc
ii  gcc                                        4:8.3.0-1ubuntu3                     amd64        GNU C compiler
ii  gcc-8                                      8.3.0-6ubuntu1                       amd64        GNU C compiler
ii  gcc-8-base:amd64                           8.3.0-6ubuntu1                       amd64        GCC, the GNU Compiler Collection (base package)
ii  gcc-9-base:amd64                           9-20190402-1ubuntu1                  amd64        GCC, the GNU Compiler Collection (base package)
ii  libgcc-8-dev:amd64                         8.3.0-6ubuntu1                       amd64        GCC support library (development files)
ii  libgcc1:amd64                              1:9-20190402-1ubuntu1                amd64        GCC support library

redhat中查询是否已安装软件:

rom -q | grep gcc

安装软件

使用源码安装软件时一般是使用./configure make make install三条命令
./configure --help查看所有的配置选项,另一个有用的配置选项是--prefix=directory,这个选项可以把软件安装到/usr/local之外的其它目录。

root@52coder:~/valgrind-3.15.0# ./configure --help
`configure' configures Valgrind 3.15.0 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc.  You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --libexecdir=DIR        program executables [EPREFIX/libexec]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  --libdir=DIR            object code libraries [EPREFIX/lib]
  --includedir=DIR        C header files [PREFIX/include]
  --oldincludedir=DIR     C header files for non-gcc [/usr/include]
  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  --infodir=DIR           info documentation [DATAROOTDIR/info]
  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
  --mandir=DIR            man documentation [DATAROOTDIR/man]
  --docdir=DIR            documentation root [DATAROOTDIR/doc/valgrind]
  --htmldir=DIR           html documentation [DOCDIR]
  --dvidir=DIR            dvi documentation [DOCDIR]
  --pdfdir=DIR            pdf documentation [DOCDIR]
  --psdir=DIR             ps documentation [DOCDIR]

Program names:
  --program-prefix=PREFIX            prepend PREFIX to installed program names
  --program-suffix=SUFFIX            append SUFFIX to installed program names
  --program-transform-name=PROGRAM   run sed PROGRAM on installed program names

System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]

Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-silent-rules   less verbose build output (undo: "make V=1")
  --disable-silent-rules  verbose build output (undo: "make V=0")
  --enable-maintainer-mode
                          enable make rules and dependencies not useful (and
                          sometimes confusing) to the casual installer
  --enable-dependency-tracking
                          do not reject slow dependency extractors
  --disable-dependency-tracking
                          speeds up one-time build
  --enable-only64bit      do a 64-bit only build
  --enable-only32bit      do a 32-bit only build
  --enable-inner          enables self-hosting
  --enable-ubsan          enables the undefined behaviour sanitiser
  --enable-lto          enables building with link time optimisation
  --enable-tls            platform supports TLS

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-tmpdir=PATH      Specify path for temporary files
  --with-mpicc=           Specify name of MPI2-ised C compiler

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  LIBS        libraries to pass to the linker, e.g. -l<library>
  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  CPP         C preprocessor
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags
  LTO_RANLIB  Library indexer command for link time optimisation
  AR          Archiver command
  LTO_AR      Archiver command for link time optimisation
  CCAS        assembler compiler command (defaults to CC)
  CCASFLAGS   assembler compiler flags (defaults to CFLAGS)

Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to <valgrind-users@lists.sourceforge.net>.

常用快捷键

在终端中回到命令开头,输入回到命令行尾。
增量搜索历史命令,输入相应命令进行匹配

标准错误重定向

在搜索文件的时候使用find / -name core,通常会出现很多"permission denied"这样的错误消息,从而把真正的结果给淹没在混乱的输出中。要消除所有出错的消息,可以使用如下命令:

find / -name core 2> /dev/null

sort命令

sort -t: -k3,3 -n /etc/group
对/etc/group文件的内容按照由冒号分割的第三个域即组ID进行排序,-n选项指定按照整数排序。
对sort命令的详细解释及例子参考Linux sort command

grep命令

grep命令有许多选项,这包括:打印匹配的行数-c,匹配时忽略大小写-i,打印不匹配的行-v,打印匹配的文件的名字-l.

shell中的数组

今天翻这本书的一个重大收获,是在学习2.2.7数组和算术运算章节,有如下shell脚本:
arr.sh

#!/bin/bash
example=(aa 'bb cc' dd)
example[3]=ee

echo "example[@]=${example[@]}"
echo "example array contains ${#example[@]} elements"

for elt in "${example[@]}";do
        echo "Element=$elt"
done

使用sh arr.sh和 chmod +x arr.sh ./arr.sh执行结果不一致。
在stackoverflow上有一篇详细的解释

修改命令行前缀

修改前:

[root@CentOS7-x64-10 ~]# echo $PS1
[\u@\h \W]\$
\u – Username
\h – Hostname
\w – Full path of the current working directory

在root/.bashrc文件中新增一行导入环境变量,去掉系统名称。重启后者source .bashrc文件即可生效。

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

export PS1='[\u \W]\$'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

勘误表

p27 2.1.4常见的过滤命令上面的例子

root@52coder:~# echo "There are `wc -l /etc/passwd` lines in the passwd file."
There are 45 /etc/passwd lines in the passwd file.
root@52coder:~# echo "There are `cat /etc/passwd | wc -l` lines in the passwd file."
There are 45 lines in the passwd file.