书架上的这块大部头书在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

常用快捷键

基础
Ctrl + a :移到命令行首
Ctrl + e :移到命令行尾
Ctrl + f :按字符前移(右向)
Ctrl + b :按字符后移(左向)
Alt + f :按单词前移(右向)
Alt + b :按单词后移(左向)
Ctrl + xx:在命令行首和光标之间移动
Ctrl + u :从光标处删除至命令行首
Ctrl + k :从光标处删除至命令行尾
Ctrl + w :从光标处删除至字首
Alt + d :从光标处删除至字尾
Ctrl + d :删除光标处的字符
Ctrl + h :删除光标前的字符
Ctrl + y :粘贴至光标后
Alt + c :从光标处更改为首字母大写的单词
Alt + u :从光标处更改为全部大写的单词
Alt + l :从光标处更改为全部小写的单词
Ctrl + t :交换光标处和之前的字符
Alt + t :交换光标处和之前的单词
Alt + Backspace:与 Ctrl + w 相同

重新执行命令
Ctrl + r:逆向搜索命令历史
Ctrl + g:从历史搜索模式退出
Ctrl + p:历史中的上一条命令
Ctrl + n:历史中的下一条命令
Alt + .:使用上一条命令的最后一个参数

控制命令
Ctrl + l:清屏
Ctrl + o:执行当前命令,并选择上一条命令
Ctrl + s:阻止屏幕输出
Ctrl + q:允许屏幕输出
Ctrl + c:终止命令
Ctrl + z:挂起命令

Bang (!) 命令
!!:执行上一条命令
!blah:执行最近的以 blah 开头的命令,如 !ls
!blah:p:仅打印输出,而不执行
!$:上一条命令的最后一个参数,与 Alt + . 相同
!$:p:打印输出 !$ 的内容
!*:上一条命令的所有参数
!*:p:打印输出 !* 的内容
^blah:删除上一条命令中的 blah
^blah^foo:将上一条命令中的 blah 替换为 foo
^blah^foo^:将上一条命令中所有的 blah 都替换为 foo

vi编辑器中快捷键
iterm2

(注意下面的”前”都是指”左”或”上”,”后”是”右”或”下; ⌃ 表示 ctrl )

⌃ Control
注意下面的前都是指左或上,后是右或下

⌃ + r 查找历史执行命令
⌃ + p 前一条指令
⌃ + n 后一条指令
⌃ + c 终止已经运行的命令或者取消已经输入的命令
⌃ + o/⌃ + j/⌃ + m 执行当前行输入的命令,跟 enter 类似
⌃ + l 清屏,clear 命令

⌃ + a 移动光标到行首
⌃ + e 移动光标到行尾

⌃ + t 交换光标前俩字符的位置
⌃ + h 往后删除一字符
⌃ + d 往前删除一字符
⌃ + b 往后移动一个字符
⌃ + f 往前移动一个字符

⌃ + w 剪切前一个单词(空格间隔的字符串单元)
⌃ + u 剪切到行首
⌃ + k 剪切到行尾
⌃ + y 粘贴剪切

其它命令
sudo chmod 755 -R node 修改目录权限
sudo lsof -nP -iTCP -sTCP:LISTEN 查看本地服务
ps -ef | grep websocket 查看websocket进程
ps aux | grep mysql 查看mysql进程
sudo kill 443 杀掉进程

通配符?

通配符星号比较常用,作用如下:

The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name.

通配符问作用如下:

The character ? will match exactly one character.
So ?ouse will match files like house and mouse, but not grouse. 

apropos keyword
match commands with keyword in their man pages

判断软件是否已经安装

简单的做法是使用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.
例子:
science.txt中查找science并忽略大小写

grep -i  science science.txt

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

cd命令

cd -回到之前的目录
cd 回到用户目录,如果是root用户cd后将切换到/root目录

ubuntu下格式化U盘

ubuntu(linux)下格式化U盘成exfat格式 windows Linux Mac通用
sudo fdisk -l
sudo mkfs.exfat -n LABEL /dev/sdx
sudo fsck.exfat /dev/sdXn
执行命令报错的话根据提示安装相应的软件。

查找进程杀进程

ps -efww|grep -w 'helloworld'|grep -v grep|cut -c 9-15|xargs kill -9
说明:管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。

“ps -ef”是查看所有进程的命令。这时检索出的进程将作为下一条命令“grep“的输入,注意要结束其它程序时,请将上面命令中的helloworld替换成其它程序名,grep的-w选项强制 PATTERN 仅完全匹配字词。
ps的-w选项man手册解释:
w Wide output. Use this option twice for unlimited width.
“grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。
“cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。
“xargs kill -9”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -9”命令的参数,并执行该命令。
“kill -9”会强行杀掉指定进程,这样就成功清除了同名进程。

ssh相关知识

SSH原理与运用(一):远程登录

head的例子

Head command in Linux with examples
其中-q参数作用主要在操作多文件时,对比执行结果如下:

[root ~]#head INSTALL /etc/passwd
==> INSTALL <==
See README

==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root redis-3.0-annotated]#
[root ~]#head -q INSTALL /etc/passwd
See README
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

kill命令

It is sometimes necessary to kill a process (for example, when an executing program is in an infinite loop)
To kill a job running in the foreground, type ^C (control c). For example, run

% sleep 100
^C

To kill a suspended or background process, type

% kill %jobnumber

For example, run

% sleep 100 &
% jobs

If it is job number 4, type

% kill %4

history命令

The C shell keeps an ordered list of all the commands that you have entered. Each command is given a number according to the order it was entered.

% history (show command history list)

If you are using the C shell, you can use the exclamation character (!) to recall commands easily.

% !! (recall last command)

% !-3 (recall third most recent command)

% !5 (recall 5th command in list)

% !grep (recall last command starting with grep)

You can increase the size of the history buffer by typing

% set history=100

信号

在POSIX兼容的平台上,SIGSEGV是当一个进程执行了一个无效的内存引用,或发生段错误时发送给它的信号。SIGSEGV的符号常量在头文件signal.h中定义。因为在不同平台上,信号数字可能变化,因此符号信号名被使用。通常,它是信号11。
SIG是信号名的通用前缀。SEGV是segmentation violation(段违例)的缩写。
在一个程序接收到SIGSEGV时的默认动作是异常终止。这个动作也许会结束进程,但是可能生成一个核心文件以帮助调试,或者执行一些其他特定于某些平台的动作。例如,使用了grsecurity补丁的Linux系统可能记录SIGSEGV信号以监视可能的使用缓存溢出的攻击尝试。
SIGSEGV可以被捕获。也就是说,应用程序可以请求它们想要的动作,以替代默认发生的动作。这样的动作可以是忽略它、调用一个函数,或恢复默认的动作。在一些情形下,忽略SIGSEGV导致未定义行为。
SIGSEGV通常由操作系统生成,但是有适当权限的用户可以在需要时使用kill系统调用或kill命令(一个用户级程序,或者一个shell内建命令)来向一个进程发送信号。

数字签名

数字签名

勘误表

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.