Shell Shell
Home
Shell
Linux
Network
Git
Blog (opens new window)
GitHub (opens new window)
Home
Shell
Linux
Network
Git
Blog (opens new window)
GitHub (opens new window)
  • Linux 核心技能与应用

  • Linux 命令行与 Shell 脚本编程大全

  • Linux 实战技能 100 讲

    • 目录
    • 第一章:基础篇
    • 第二章:系统操作篇
      • 08 | 万能的帮助命令:man、help、info
        • man 帮助
        • help 帮助
        • info 帮助
        • 使用网路资源(搜索引擎和官网文档)
        • tldr 帮助
      • 09 | 初识 pwd 和 ls 命令
      • 10 | 详解 ls 命令
      • 11 | 详解 cd 命令
      • 12 | 创建和删除目录
        • 创建目录 mkdir
        • 删除空目录 rmdir
        • 删除文件/目录 rm
      • 13 | 复制和移动目录
        • 复制文件/目录 cp
        • 移动/重命名文件/目录 mv
        • 通配符
      • 14 | 如何在 Linux 下进行文本查看
        • 文本内容显示到终端 cat
        • 文本内容显示到终端 bat
        • 查看文件头部内容 head
        • 查看文件尾部内容 tail
        • 统计文件内容信息 wc
      • 15 | 打包压缩和解压缩
      • 16 | Vim 的四种模式
        • 四种模式
        • vim 配置文件 vimrc
      • 17 | Vim 的正常模式
      • 18 | Vim 的命令模式
      • 19 | Vim 的可视模式
        • 进入可视模式
        • 可视化模式支持使用的命令
      • 20 | 用户和用户组管理及密码管理
        • 新建用户 useradd
        • 修改用户密码 passwd
        • 删除用户 userdel
        • 修改用户属性 usermod
        • 修改用户属性 chage
        • 新建用户组 groupadd
        • 删除用户组 groupdel
      • 21 | su 和 sudo 命令的区别和使用方法
      • 22 | 用户和用户组的配置文件介绍
      • 23 | 文件与目录权限的表示方法
      • 24 | 文件权限的修改方法和数字表示方法
        • 修改文件/目录权限 chmod
        • 更改文件属主/组 chown
        • 单独更改属组(不常用) chgrp
      • 25 | 权限管理以及文件的特殊权限
    • 第三章:系统管理篇
    • 第四章:Shell 篇
    • 第五章:文本操作篇
    • 第六章:服务管理篇
  • CentOS7 环境配置
  • CentOS7 更换国内软件源
  • CentOS7 安装 pigz
  • 编写 bash 脚本
  • linux
  • Linux 实战技能 100 讲
JaimeZeng
2021-09-02

第二章:系统操作篇

# 08 | 万能的帮助命令:man、help、info

# man 帮助

  • man 是 manual 的缩写
  • man 帮助用法演示:man ls
  • 查找所有匹配的手册:man -a passwd
  • man 也是一条命令,分为 9 章
    1. Executable programs or shell commands 用户可从 shell 运行的命令
    2. System calls (functions provided by the kernel) 必须由内核完成的功能
    3. Library calls (functions within program libraries) 大多数 libc 函数,例如 qsort(3))
    4. Special files (usually found in /dev) /dev) 目录中的文件
    5. File formats and conventions, e.g. /etc/passwd /etc/passwd 等人类可读的文件的格式说明
    6. Games 游戏
    7. Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 文件系统标准描述,网络协议,ASCII 和其他字符集,还有你眼前这份文档以及其他东西
    8. System administration commands (usually only for root) 类似 mount(8) 等命令,大部分只能由 root 执行
    9. Kernel routines 这是废弃的章节. 原来曾想把一些关于核心的文件放在这里, 但是实际上只有极少数可以写成文件放在这里,而且它们也很快过时了. 核心开发者可以找到其他更好的资源.
  • 使用 man 命令获取 man 的帮助:man 7 man

# help 帮助

  • shell(命令解释器)自带的命令称为内部命令,其它的是外部命令.type ls
  • 内部命令使用 help 帮助 help cd
  • 外部命令使用 help 帮助 ls --help

# info 帮助

  • info 帮助比 help 更详细,作为 help 的补充.info ls

# 使用网路资源(搜索引擎和官网文档)

  • Manpages of manpages-zh in Debian unstable — Debian Manpages (opens new window)
  • Linux 命令大全(手册) – 真正好用的 Linux 命令在线查询网站 (linuxcool.com) (opens new window)
  • Linux 命令大全 | 菜鸟教程 (runoob.com) (opens new window)

# tldr 帮助

  • tldr (opens new window) (Too Long Didn’t Read):Linux 手册页的简化替代品
  • 安装
    • Debian/Ubuntu apt install tldr
    • CentOS/Fedora yum/dnf install tldr
    • ArchLinux: pacman -S tldr
    • nodejs npm install -g tldr
    • python pip3 install tldr
    • 在线 tldr | simplified, community driven man pages (ostera.io) (opens new window)
    • 手动 curl -o /usr/bin/tldr https://raw.githubusercontent.com/raylee/tldr/master/tldr && chmod +x /usr/bin/tldr
  • 使用
    • 更新缓存 tldr --update
    • 查看手册 tldr <commandname>
root@10-9-82-227:~# tldr man
man

Format and display manual pages.
More information: https://www.man7.org/linux/man-pages/man1/man.1.html.

 - Display the man page for a command:
   man command

 - Display the man page for a command from section 7:
   man 7 command

 - Display the path searched for manpages:
   man --path

 - Display the location of a manpage rather than the manpage itself:
   man -w command

 - Display the man page using a specific locale:
   man command --locale=locale

 - Search for manpages containing a search string:
   man -k "search_string"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 09 | 初识 pwd 和 ls 命令

  • 显示当前目录路径 pwd
  • 更改当前操作目录 cd
  • 查看当前目录下的文件 ls
  • 结束当前执行的命令 Ctrl + C
root@10-9-82-227:~# tldr pwd
pwd

Print name of current/working directory.
More information: https://www.gnu.org/software/coreutils/pwd.

 - Print the current directory:
   pwd

 - Print the current directory, and resolve all symlinks (i.e. show the "physical" path):
   pwd --physical

 - Print the current logical directory:
   pwd --logical

root@10-9-82-227:~# pwd
/root
root@10-9-82-227:~# ls
bot_login.py  install.sh  send_message.py  utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 10 | 详解 ls 命令

  • 终端清屏:clear 或者 Ctrl + L
  • 查看当前目录下的文件 ls
    • ls -l 长格式显示文件(文件类型、权限、用户、组、文件大小、修改时间、文件名等)
    • ls -a 显示隐藏文件
    • ls -r 逆序显示
    • ls -t 按照时间顺序显示
    • ls -R 递归显示
root@10-9-82-227:~# tldr ls
ls

List directory contents.
More information: https://www.gnu.org/software/coreutils/ls.

 - List files one per line:
   ls -1

 - List all files, including hidden files:
   ls -a

 - List all files, with trailing / added to directory names:
   ls -F

 - Long format list (permissions, ownership, size, and modification date) of all files:
   ls -la

 - Long format list with size displayed using human-readable units (KiB, MiB, GiB):
   ls -lh

 - Long format list sorted by size (descending):
   ls -lS

 - Long format list of all files, sorted by modification date (oldest first):
   ls -ltr

 - Only list directories:
   ls -d */

root@10-9-82-227:~# ls
bot_login.py  install.sh  send_message.py  utils.py
root@10-9-82-227:~# ls -la
total 140
drwx------  4 root root  4096 Mar  3 08:01 .
drwxr-xr-x 18 root root  4096 Mar  3 07:30 ..
-rw-------  1 root root   795 Mar  3 07:59 .bash_history
-rw-r--r--  1 root root   571 Apr 10  2021 .bashrc
-rw-r--r--  1 root root   729 Mar  3 07:47 bot_login.py
-rw-r--r--  1 root root 26705 Feb 10 22:39 install.sh
drwxr-xr-x  3 root root  4096 Aug 16  2021 .local
drwxr-xr-x 12 root root  4096 Mar  3 08:00 .oh-my-zsh
-rw-r--r--  1 root root   161 Jul  9  2019 .profile
-rw-r--r--  1 root root  1590 Mar  3 07:47 send_message.py
-rw-r--r--  1 root root    10 Mar  3 08:00 .shell.pre-oh-my-zsh
-rw-r--r--  1 root root  1690 Mar  3 07:46 utils.py
-rw-r--r--  1 root root   215 Mar  3 07:47 .wget-hsts
-rw-------  1 root root   177 Mar  3 08:00 .Xauthority
-rw-r--r--  1 root root 50433 Mar  3 08:00 .zcompdump-10-9-82-227-5.8
-rw-------  1 root root   154 Mar  3 08:01 .zsh_history
-rw-r--r--  1 root root  3866 Mar  3 08:00 .zshrc
root@10-9-82-227:~# ls -lar
total 140
-rw-r--r--  1 root root  3866 Mar  3 08:00 .zshrc
-rw-------  1 root root   154 Mar  3 08:01 .zsh_history
-rw-r--r--  1 root root 50433 Mar  3 08:00 .zcompdump-10-9-82-227-5.8
-rw-------  1 root root   177 Mar  3 08:00 .Xauthority
-rw-r--r--  1 root root   215 Mar  3 07:47 .wget-hsts
-rw-r--r--  1 root root  1690 Mar  3 07:46 utils.py
-rw-r--r--  1 root root    10 Mar  3 08:00 .shell.pre-oh-my-zsh
-rw-r--r--  1 root root  1590 Mar  3 07:47 send_message.py
-rw-r--r--  1 root root   161 Jul  9  2019 .profile
drwxr-xr-x 12 root root  4096 Mar  3 08:00 .oh-my-zsh
drwxr-xr-x  3 root root  4096 Aug 16  2021 .local
-rw-r--r--  1 root root 26705 Feb 10 22:39 install.sh
-rw-r--r--  1 root root   729 Mar  3 07:47 bot_login.py
-rw-r--r--  1 root root   571 Apr 10  2021 .bashrc
-rw-------  1 root root   795 Mar  3 07:59 .bash_history
drwxr-xr-x 18 root root  4096 Mar  3 07:30 ..
drwx------  4 root root  4096 Mar  3 08:01 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

# 11 | 详解 cd 命令

  • 更改当前操作目录 cd
    • cd /path/to/... 绝对路径
    • cd ./path/to/... 相对路径
    • cd ../path/to/... 相对路径
  • 终端补全:Tab
➜ tldr cd
cd

Change the current working directory.
More information: https://manned.org/cd.

 - Go to the given directory:
   cd path/to/directory

 - Go to the home directory of the current user:
   cd

 - Go up to the parent of the current directory:
   cd ..

 - Go to the previously chosen directory:
   cd -
➜ ~ cd /usr/bin/
➜ /usr/bin cd
➜ ~ pwd
/home/jaime
➜ ~ cd -
➜ /usr/bin cd ..
➜ /usr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 12 | 创建和删除目录

# 创建目录 mkdir

  • 创建目录 mkdir directory
  • 创建多个目录 mkdir directory_1 directory_2 ...
  • 创建多级目录 mkdir -p path/to/directory
➜ ~ tldr mkdir
mkdir

Creates a directory.
More information: https://www.gnu.org/software/coreutils/mkdir.

 - Create a directory in current directory or given path:
   mkdir directory

 - Create multiple directories in the current directory:
   mkdir directory_1 directory_2 ...

 - Create directories recursively (useful for creating nested dirs):
   mkdir -p path/to/directory
➜ ~ mkdir a b c d
➜ ~ mkdir -p a/b/c/d/e/f
➜ ~ tree -i -f -d
.
./a
./a/b
./a/b/c
./a/b/c/d
./a/b/c/d/e
./a/b/c/d/e/f
./b
./c
./d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 删除空目录 rmdir

  • 删除空目录 rmdir path/to/directory
  • 删除多级空目录 rmdir -p path/to/directory
➜ ~ tldr rmdir
rmdir

Removes a directory.
More information: https://www.gnu.org/software/coreutils/rmdir.

 - Remove directory, provided it is empty. Use rm -r to remove non-empty directories:
   rmdir path/to/directory

 - Remove the target and its parent directories (useful for nested dirs):
   rmdir -p path/to/directory
➜ ~ rmdir -p a/b/c/d/e/f
➜ ~ tree -i -f -d
.
./b
./c
./d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 删除文件/目录 rm

  • 删除文件 rm path/to/file path/to/another/file
  • 递归删除目录(删除目录及其子目录) rm -r path/to/directory
  • 递归删除目录,不会出现警告信息,忽略不存在的文件 rm -rf path/to/directory
  • 交互模式删除(每次删除前询问用户是否操作) rm -i file(s)
  • 删除目录并显示详细执行过程 rm -v path/to/directory/*
➜ ~ tldr rm
rm

Remove files or directories.
More information: https://www.gnu.org/software/coreutils/rm.

 - Remove files from arbitrary locations:
   rm path/to/file path/to/another/file

 - Recursively remove a directory and all its subdirectories:
   rm -r path/to/directory

 - Forcibly remove a directory, without prompting for confirmation or showing error messages:
   rm -rf path/to/directory

 - Interactively remove multiple files, with a prompt before every removal:
   rm -i file(s)

 - Remove files in verbose mode, printing a message for each removed file:
   rm -v path/to/directory/*
➜ ~ rm -rf a/b/c/d/e/f
➜ ~ tree -i -f -d
.
./a
./a/b
./a/b/c
./a/b/c/d
./a/b/c/d/e
./b
./c
./d

8 directories
➜ ~ rm -rfv a
removed directory 'a/b/c/d/e'
removed directory 'a/b/c/d'
removed directory 'a/b/c'
removed directory 'a/b'
removed directory 'a'
➜ ~ tree -i -f -d
.
./b
./c
./d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 13 | 复制和移动目录

# 复制文件/目录 cp

  • 复制文件到其它位置并重命名 cp path/to/source_file.ext path/to/target_file.ext
  • 复制文件到其它位置并保持文件名不变 cp path/to/source_file.ext path/to/target_parent_directory
  • 若目标文件已存在,则会直接覆盖原文件 cp -f path/to/source_file.ext path/to/target_file.ext
  • 递归复制目录 cp -R path/to/source_directory path/to/target_directory
  • 递归复制目录并显示详细执行过程 cp -vR path/to/source_directory path/to/target_directory
  • 交互模式复制(每次复制前询问用户是否操作)cp -i *.txt path/to/target_directory
  • 对源文件建立硬连接,而非复制文件 cp -L link path/to/target_directory
➜ ~ tldr cp
cp

Copy files and directories.
More information: https://www.gnu.org/software/coreutils/cp.

 - Copy a file to another location:
   cp path/to/source_file.ext path/to/target_file.ext

 - Copy a file into another directory, keeping the filename:
   cp path/to/source_file.ext path/to/target_parent_directory

 - Recursively copy a directory's contents to another location (if the destination exists, the directory is copied inside it):
   cp -R path/to/source_directory path/to/target_directory

 - Copy a directory recursively, in verbose mode (shows files as they are copied):
   cp -vR path/to/source_directory path/to/target_directory

 - Copy text files to another location, in interactive mode (prompts user before overwriting):
   cp -i *.txt path/to/target_directory

 - Follow symbolic links before copying:
   cp -L link path/to/target_directory
➜ ~ cp -vR a/b b
'a/b' -> 'b/b'
'a/b/c' -> 'b/b/c'
'a/b/c/d' -> 'b/b/c/d'
'a/b/c/d/e' -> 'b/b/c/d/e'
'a/b/c/d/e/f' -> 'b/b/c/d/e/f'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 移动/重命名文件/目录 mv

  • 移动文件并重命名 mv source target
  • 移动多个文件并保持文件名不变 mv source1 source2 source3 target_directory
  • 若目标文件已存在,则会直接覆盖原文件 mv -f source target
  • 交互模式移动(若存在同名文件,则向用户询问是否覆盖) mv -i source target
  • 显示详细执行过程 mv -v source target
➜ ~ tldr mv
mv

Move or rename files and directories.
More information: https://www.gnu.org/software/coreutils/mv.

 - Move a file to an arbitrary location:
   mv source target

 - Move files into another directory, keeping the filenames:
   mv source1 source2 source3 target_directory

 - Do not prompt for confirmation before overwriting existing files:
   mv -f source target

 - Prompt for confirmation before overwriting existing files, regardless of file permissions:
   mv -i source target

 - Do not overwrite existing files at the target:
   mv -n source target

 - Move files in verbose mode, showing files after they are moved:
   mv -v source target
➜ ~ touch a/b/c/d/e/f/source_file
➜ ~ tree -i -f
.
./a
./a/b
./a/b/c
./a/b/c/d
./a/b/c/d/e
./a/b/c/d/e/f
./a/b/c/d/e/f/source_file
./b
./c
./d

9 directories, 1 file
➜ ~ mv a/b/c/d/e/f/source_file b/
➜ ~ ls b/
source_file
➜ ~ touch a/b/c/d/e/f/source_file
➜ ~ mv a/b/c/d/e/f/source_file b/target_file -v
renamed 'a/b/c/d/e/f/source_file' -> 'b/target_file'
➜ ~ ls b/
source_file  target_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# 通配符

  • 定义: Shell 内建的符号
  • 用途:操作多个相似(有简单规律)的文件
  • 常用通配符
    • * 匹配任何字符串
    • ? 匹配 1 个字符串
    • [xyz] 匹配 xyz 中任意一个字符
    • [a-z] 匹配一个范围
    • [!xyz] 或 [^xyz] 不匹配

# 14 | 如何在 Linux 下进行文本查看

# 文本内容显示到终端 cat

  • 显示文件内容 cat file
  • 显示文件内容带行号 cat -n file
➜ ~ tldr cat
cat

Print and concatenate files.
More information: https://www.gnu.org/software/coreutils/cat.

 - Print the contents of a file to the standard output:
   cat file

 - Concatenate several files into the target file:
   cat file1 file2 > target_file

 - Append several files into the target file:
   cat file1 file2 >> target_file

 - Number all output lines:
   cat -n file

 - Display non-printable and whitespace characters (with M- prefix if non-ASCII):
   cat -v -t -e file
➜ ~ cat multiplication_table
1 x 1 = 1
2 x 1 = 2  2 x 2 = 4
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
➜ ~ bat -n multiplication_table
   1 1 x 1 = 1
   2 2 x 1 = 2  2 x 2 = 4
   3 3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
   4 4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
   5 5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
   6 6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 文本内容显示到终端 bat

  • 特点 相比 cat ,bat 提供语法高亮显示和 Git 集成
  • 安装 dnf/apt install bat or pacman -S bat
  • 显示文件内容 bat file
  • 显示文件内容带行号 bat -n file
  • 语法高亮显示文件 bat --language json file.json
➜ ~ tldr bat
bat

Print and concatenate files.
A cat clone with syntax highlighting and Git integration.
More information: https://github.com/sharkdp/bat.

 - Print the contents of a file to the standard output:
   bat file

 - Concatenate several files into the target file:
   bat file1 file2 > target_file

 - Append several files into the target file:
   bat file1 file2 >> target_file

 - Number all output lines:
   bat -n file

 - Syntax highlight a JSON file:
   bat --language json file.json

 - Display all supported languages:
   bat --list-languages
➜ ~ bat multiplication_table
───────┬──────────────────────────────────────────────────────────────────────
       │ File: multiplication_table
       │ Size: 236 B
───────┼──────────────────────────────────────────────────────────────────────
   1   │ 1 x 1 = 1
   2   │ 2 x 1 = 2  2 x 2 = 4
   3   │ 3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
   4   │ 4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
   5   │ 5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
   6   │ 6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
───────┴──────────────────────────────────────────────────────────────────────
➜ ~ bat -n multiplication_table
   1 1 x 1 = 1
   2 2 x 1 = 2  2 x 2 = 4
   3 3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
   4 4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
   5 5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
   6 6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 查看文件头部内容 head

  • 动态更新文件内容,显示信息同步更新(查看日志文件) head -f
  • 显示文件前几行 head --lines <number> file 或者 head --lines=<number> file 或者 head -<number> file
➜ ~ tldr head
head

Output the first part of files.
More information: https://www.gnu.org/software/coreutils/head.

 - Output the first few lines of a file:
   head --lines count path/to/file

 - Output the first few bytes of a file:
   head --bytes count path/to/file

 - Output everything but the last few lines of a file:
   head --lines -count path/to/file

 - Output everything but the last few bytes of a file:
   head --bytes -count path/to/file
➜ ~ head --lines 3 multiplication_table
1 x 1 = 1
2 x 1 = 2  2 x 2 = 4
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
➜ ~ head --lines=3 multiplication_table
1 x 1 = 1
2 x 1 = 2  2 x 2 = 4
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
➜ ~ head -3 multiplication_table
1 x 1 = 1
2 x 1 = 2  2 x 2 = 4
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 查看文件尾部内容 tail

  • 动态更新文件内容,显示信息同步更新(查看日志文件) tail -f
  • 显示文件尾几行 tail --lines <number> file 或者 tail --lines=<number> file 或者 tail -<number> file
➜ ~ tldr tail
tail

Display the last part of a file.
See also: head.
More information: https://www.gnu.org/software/coreutils/tail.

 - Show last 'count' lines in file:
   tail --lines count path/to/file

 - Print a file from a specific line number:
   tail --lines +count path/to/file

 - Print a specific count of bytes from the end of a given file:
   tail --bytes count path/to/file

 - Print the last lines of a given file and keep reading file until Ctrl + C:
   tail --follow path/to/file

 - Keep reading file until Ctrl + C, even if the file is inaccessible:
   tail --retry --follow path/to/file

 - Show last 'num' lines in 'file' and refresh every 'n' seconds:
   tail --lines count --sleep-interval seconds --follow path/to/file
➜ ~ tail --lines 3 multiplication_table
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
➜ ~ tail --lines=3 multiplication_table
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
➜ ~ tail -3 multiplication_table
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# 统计文件内容信息 wc

  • 统计文件行数 wc -l file
  • 统计文件字数 wc -w file
➜ ~ tldr wc
wc

Count lines, words, and bytes.
More information: https://www.gnu.org/software/coreutils/wc.

 - Count all lines in a file:
   wc --lines path/to/file

 - Count all words in a file:
   wc --words path/to/file

 - Count all bytes in a file:
   wc --bytes path/to/file

 - Count all characters in a file (taking multi-byte characters into account):
   wc --chars path/to/file

 - Count all lines, words and bytes from stdin:
   find . | wc

 - Count the length of the longest line in number of characters:
   wc --max-line-length path/to/file
➜ ~ wc -l multiplication_table
6 multiplication_table
➜ ~ wc -w multiplication_table
105 multiplication_table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 15 | 打包压缩和解压缩

  • 打包 tar
    • 最早的 Linux 备份介质是磁带,使用的命令是 tar(tape archive)
    • 可以将多个文件合并为一个文件(未压缩),打包后的文件扩展名为 .tar
    • 可以打包后的磁带文件进行压缩储存,压缩的命令是 gzip 和 bzip2
    • 经常使用的扩展名是 .tar.gz(.tgz) .tar.bz2(.tbz2) 使用扩展名缩写方便网络中传输
    • 参数
      • c 打包 [c]reate
      • x 解包 E[x]tract
      • g gzip 格式 g[z]ipped
      • j bzip2 格式 比传统的 gzip 或者 ZIP 的压缩效率更高,但是压缩速度较慢
      • v 详细执行过程 [v]erbosely
      • f 包文件名 [f]ile
      • t 查看包内文件目录 Lis[t]
    • 命令
      • 创建一个打包文件 tar cf target.tar file1 file2 file3
      • 创建一个打包文件并使用 gzip 压缩 tar czf target.tar.gz file1 file2 file3
      • 创建一个打包文件,使用 gzip 压缩并显示详细执行过程 tar czvf target.tar.gz file1 file2 file3
      • 查看包内文件信息 tar tf source.tar
      • 查看包内文件详细信息 tar tvf source.tar
      • 解包 tar xzf source.tar.gz
      • 解包显示详细过程 tar xzvf source.tar.gz
➜ ~ tldr tar
tar

Archiving utility.
Often combined with a compression method, such as gzip or bzip2.
More information: https://www.gnu.org/software/tar.

 - [c]reate an archive and write it to a [f]ile:
   tar cf target.tar file1 file2 file3

 - [c]reate a g[z]ipped archive and write it to a [f]ile:
   tar czf target.tar.gz file1 file2 file3

 - [c]reate a g[z]ipped archive from a directory using relative paths:
   tar czf target.tar.gz --directory=path/to/directory .

 - E[x]tract a (compressed) archive [f]ile into the current directory [v]erbosely:
   tar xvf source.tar[.gz|.bz2|.xz]

 - E[x]tract a (compressed) archive [f]ile into the target directory:
   tar xf source.tar[.gz|.bz2|.xz] --directory=directory

 - [c]reate a compressed archive and write it to a [f]ile, using [a]rchive suffix to determine the compression program:
   tar caf target.tar.xz file1 file2 file3

 - Lis[t] the contents of a tar [f]ile [v]erbosely:
   tar tvf source.tar

 - E[x]tract files matching a pattern from an archive [f]ile:
   tar xf source.tar --wildcards "*.html"
➜ ~ tar -cvf /home/jaime/dotfiles.tar demo/
demo/
demo/zshrc
demo/bash_history
demo/profile
demo/bashrc
demo/zsh_history
demo/functions
demo/zimrc
➜ ~ tar -czvf /home/jaime/dotfiles.tgz demo/
demo/
demo/zshrc
demo/bash_history
demo/profile
demo/bashrc
demo/zsh_history
demo/functions
demo/zimrc
➜ ~ ls -la dot*
.rw-r--r--  30k jaime  4 Mar 19:55 dotfiles.tar
.rw-r--r-- 6.8k jaime  4 Mar 19:55 dotfiles.tgz
➜ ~ tar -tvf /home/jaime/dotfiles.tgz
drwxr-xr-x jaime/jaime       0 2021-03-04 19:46 demo/
-rw------- jaime/jaime    3987 2021-03-04 19:45 demo/zshrc
-rw------- jaime/jaime    2604 2021-03-04 19:44 demo/bash_history
-rw-r--r-- jaime/jaime     807 2021-03-04 19:46 demo/profile
-rw-r--r-- jaime/jaime    3526 2021-03-04 19:43 demo/bashrc
-rw------- jaime/jaime    1241 2021-03-04 19:44 demo/zsh_history
-rw-r--r-- jaime/jaime    5812 2021-03-04 19:45 demo/functions
-rw------- jaime/jaime     622 2021-03-04 19:45 demo/zimrc
➜ ~ tar -tf /home/jaime/dotfiles.tgz
demo/
demo/zshrc
demo/bash_history
demo/profile
demo/bashrc
demo/zsh_history
demo/functions
demo/zimrc

➜ ~ tree -i -f -d
.
./demo

1 directory
➜ ~ rm -rf demo
➜ ~ tar -xzvf dotfiles.tgz
demo/
demo/zshrc
demo/bash_history
demo/profile
demo/bashrc
demo/zsh_history
demo/functions
demo/zimrc
➜ ~ tree -i -f -d
.
./demo

1 directory
➜ ~ tree -i -f demo
demo
demo/bash_history
demo/bashrc
demo/functions
demo/profile
demo/zimrc
demo/zsh_history
demo/zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

# 16 | Vim 的四种模式

# 四种模式

  • 正常模式(Normal-mode)
  • 插入模式(Insert-mode)
  • 命令模式(Command-mode)
  • 可视模式(Visual-mode)

技巧提示

当你不知道自己到底处于什么模式下, 可以按两下 返回正常/普通模式下.不过不能在 Ex 模式里这么做(用 ":visual" 返回普通模式).如果在你按下 Esc 后发现屏幕闪烁或者听到响声,这证明你已经回到普通模式.然而,在插入模式里按下 Ctrl + o 后再按下 Esc 也会听到喇叭的响声,此时你仍然处于插入模式,再按一下 Esc 即可.

# vim 配置文件 vimrc

  • Vimrc 是 Vim 的配置文件.Vim 编辑器相关的所有功能开关都可以通过.vimrc 文件进行设置.
    • rc = run command
    • 系统级 vimrc 和用户级 vimrc.系统 vimrc 配置文件存放在 Vim 的安装目录,默认路径为 /usr/share/vim/.vimrc.可以使用命令 echo $VIM 来确定 Vim 的安装目录.用户 vimrc 文件,存放在用户主目录下 ~/.vimrc.可以使用命令 echo $HOME 确定用户主目录. 注意:用户配置文件优先于系统配置文件,Vim 启动时会优先读取当前用户根目录下的 .vimrc 文件.所以与个人用户相关的个性化配置一般都放在 ~/.vimrc 中.
    • 每一行作为一个命令执行

# 17 | Vim 的正常模式

vivim-graphical

  • i 光标当前位置插入 I 到行首插入
  • a 附加(光标位置后插入) A 行尾附加
  • o 后分段(光标下一行插入)O 前分段(光标上一行插入)
  • h 向左 j 向下 k 向上 l 向右
  • w/W 移动到下一个单词的开头,e/E 移动到下一个单词的结尾,b/B 移动到上一个单词的开头,其中小写的表示以非字母为分割单位(比如冒号,引号,逗号,空格都死分割单位),大写表示以空白符为分割的单位
  • yy 复制当前行内容 3yy 复制光标线下 3 行的内容 y$ 复制光标到行结束内容 yG 复制光标到尾行的内容 y1G 复制光标到首行的内容
  • dd 剪切当前行内容 3dd 剪切光标线下 3 行的内容 d$ 复制光标到行结束内容 dG 剪切光标到尾行的内容 d1G 剪切光标到首行的内容
  • p 后粘贴 P 前粘贴
  • u 撤销
  • Ctrl + r 重做
  • x 删除字符
  • r 替换光标当前字符 3r [a-z] 替换光标前 3 个字符
  • :set nu 显示行号
  • num + G 跳转到第 num 行(100G:光标移动到第 100 行)
  • g 文本首行(vim 中是 gg) G 文本末行
  • ^ 行首 $ 行尾

提示

按键盘左上角的 Esc 键,就会从其他任意模式退回到普通模式.

在普通模式中,用的编辑器命令,比如移动光标,删除文本等等.这也是 Vim 启动后的默认模式.这正好和许多新用户期待的操作方式相反(大多数编辑器默认模式为插入模式).

Vim 强大的编辑能力来自于其普通模式命令.普通模式命令往往需要一个操作符结尾.例如普通模式命令 "dd" 删除当前行,但是第一个 "d" 的后面可以跟另外的移动命令来代替第二个 "d",比如用移动到下一行的 "j" 键就可以删除当前行和下一行.另外还可以指定命令重复次数,"2dd"(重复 "dd" 两次),和 "dj" 的效果是一样的.用户学习了各种各样的文本间移动/跳转的命令和其他的普通模式的编辑命令,并且能够灵活组合使用的话,能够比那些没有模式的编辑器更加高效的进行文本编辑.

在普通模式中,有很多方法可以进入插入模式.比较普通的方式是按 a(append/追加)键或者 i(insert/插入)键.

按键盘上的 i, I, a, A, o, O 键,就会从普通模式切换为插入模式.处于插入模式时,在左下角会显示--INSERT--.

  • i : 在光标所在字符前面进入插入模式
  • a : 在光标所在字符后面进入插入模式
  • shift + I : 在光标所在行的开头进入插入模式
  • shift + A : 在光标所在行的末尾进入插入模式
  • o : 在光标所在行的下面插入新的一行,光标移动到新插入的这一行,并进入插入模式
  • shift + O : 在光标所在行的上面插入新的一行,光标移动到新插入的这一行,并进入插入模式. 在这个模式中,大多数按键都会向文本缓冲区中插入文本.大多数新用户希望文本编辑器编辑过程中一直保持这个模式.

在插入模式中,可以按 Esc 键回到普通模式.

# 18 | Vim 的命令模式

在命令行模式中可以输入会被解释成并执行的文本.例如执行命令(:键),搜索(/ 和 ? 键)或者过滤命令(! 键).在命令执行之后,Vim 返回到命令行模式之前的模式,通常是普通模式.

  • :set hlsearch 搜索时,高亮显示匹配结果
  • :set nohlsearch 搜索时,不要高亮显示匹配结果
  • :set nu 显示行号
  • :set nonu 不显示行号
  • :w 保存
  • :q 退出
  • :wq 保存退出
  • :q! 不保存退出
  • :wi 强制写入
  • :w [filename] 另存为 [filename] 文件
  • :new 新建文件
  • ZZ 保存退出
  • ZQ 不保存退出
  • /word 向下查找
  • ?word 向上查找
  • :s/x/y/g 替换行内所有 x 为 y
  • :%s/x/y/gc 替换文本内所有 x 为 y,替换前询问
  • :n1,n2s/x/y/g 替换 n1 行到 n2 行内所有 x 为 y

# 19 | Vim 的可视模式

# 进入可视模式

  • v 字符可视化模式,此模式下目标文本的选择是以字符为单位的,也就是说,该模式下要一个字符一个字符的选中要操作的文本
  • V 行可视化模式,此模式化目标文本的选择是以行为单位的,也就是说,该模式化可以一行一行的选中要操作的文本
  • Ctrl + v 块可视化模式,该模式下可以选中文本中的一个矩形区域作为目标文本,以按下 Ctrl + v 位置作为矩形的一角,光标移动的终点位置作为它的对角

# 可视化模式支持使用的命令

  • d 删除选中的部分文本
  • D 删除选中部分所在的行,和 d 不同之处在于,即使选中文本中有些字符所在的行没有都选中,删除时也会一并删除
  • y 将选中部分复制到剪贴板中
  • p 将剪贴板中的内容粘贴到光标之后
  • P 将剪贴板中的内容粘贴到光标之前
  • u 将选中部分中的大写字符全部改为小写字符
  • U 将选中部分中的小写字符全部改为大写字符
  • > 将选中部分右移(缩进)一个 tab 键规定的长度(CentOS 6.x 中,一个 tab 键默认相当于 8 个空白字符的长度)
  • < 将选中部分左移一个 tab 键规定的长度(CentOS 6.x 中,一个 tab 键默认相当于 8 个空白字符的长度)

# 20 | 用户和用户组管理及密码管理

# 新建用户 useradd

  • 新建用户并创建家目录 useradd -m username
  • 新建用户并添加到用户组 useradd -G group1,group2,... username
  • 系统中用户信息
    • 新用户家目录 /home/username
    • 密码文件 /etc/passwd
    • 影子文件 /etc/shadow
  • 显示用户信息(UID,组 ID,所属用户组)id username
➜ ~ tldr useradd
useradd

Create a new user.
See also: users, userdel, usermod.
More information: https://manned.org/useradd.

 - Create a new user:
   sudo useradd username

 - Create a new user with the specified user id:
   sudo useradd --uid id username

 - Create a new user with the specified shell:
   sudo useradd --shell path/to/shell username

 - Create a new user belonging to additional groups (mind the lack of whitespace):
   sudo useradd --groups group1,group2,... username

 - Create a new user with the default home directory:
   sudo useradd --create-home username

 - Create a new user with the home directory filled by template directory files:
   sudo useradd --skel path/to/template_directory --create-home username

 - Create a new system user without the home directory:
   sudo useradd --system username
➜ ~ sudo useradd jiemi
[sudo] password for jaime:
➜ ~ ls /home
jaime  jiemi
➜ ~ tail -3 /etc/passwd
jaime:x:1000:1000:,,,:/home/jaime:/usr/bin/zsh
messagebus:x:104:111::/nonexistent:/usr/sbin/nologin
jiemi:x:1001:1001::/home/jiemi:/bin/sh
➜ ~ tail -3 /etc/shadow
tail: cannot open '/etc/shadow' for reading: Permission denied
➜ ~ sudo tail -3 /etc/shadow
jaime:$y$j9T$YRDNSSHoLz2cyhiqBRuuC.$EaRGxSSZaS0wKtKUDGx7F56tfguncVIPhL9/qk7slV2:19054:0:99999:7:::
messagebus:*:19054:0:99999:7:::
jiemi:!:19056:0:99999:7:::
➜ ~ id jiemi
uid=1001(jiemi) gid=1001(jiemi) groups=1001(jiemi)
➜ ~ id abc
id: ‘abc’: no such user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

# 修改用户密码 passwd

  • 更改当前用户密码 passwd
  • 更改其他用户密码 passed username
➜ ~ tldr passwd
passwd

Passwd is a tool used to change a user's password.
More information: https://manned.org/passwd.

 - Change the password of the current user interactively:
   passwd

 - Change the password of a specific user:
   passwd username

 - Get the current status of the user:
   passwd -S

 - Make the password of the account blank (it will set the named account passwordless):
   passwd -d
➜ ~ passwd jiemi
passwd: You may not view or modify password information for jiemi.
➜ ~ sudo passwd jiemi
New password:
Retype new password:
passwd: password updated successfully
➜ ~ pass
zsh: command not found: pass
➜ ~ passwd
Changing password for jaime.
Current password:
New password:
passwd: password updated successfully
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 删除用户 userdel

  • 删除用户保留其家目录 userdel username
  • 删除用户及其家目录 userdel -r username
➜ ~ tldr userdel
userdel

Remove a user account or remove a user from a group.
See also: users, useradd, usermod.
More information: https://manned.org/userdel.

 - Remove a user:
   sudo userdel username

 - Remove a user in other root directory:
   sudo userdel --root path/to/other/root username

 - Remove a user along with the home directory and mail spool:
   sudo userdel --remove username
➜ ~ sudo useradd -m zhangsan
➜ ~ ls /home
jaime  jiemi  zhangsan
➜ ~ sudo userdel jiemi
➜ ~ sudo userdel -r zhangsan
userdel: zhangsan mail spool (/var/mail/zhangsan) not found
➜ ~ ls /home
jaime  jiemi
➜ ~ tail -4 /etc/passwd
systemd-network:x:102:103:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
systemd-resolve:x:103:104:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin
jaime:x:1000:1000:,,,:/home/jaime:/usr/bin/zsh
messagebus:x:104:111::/nonexistent:/usr/sbin/nologin
➜ ~ sudo tail -4 /etc/shadow
systemd-network:*:18970:0:99999:7:::
systemd-resolve:*:18970:0:99999:7:::
jaime:$y$j9T$YRDNSSHoLz2cyhiqBRuuC.$EaRGxSSZaS0wKtKUDGx7F56tfguncVIPhL9/qk7slV2:19054:0:99999:7:::
messagebus:*:19054:0:99999:7:::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 修改用户属性 usermod

  • 更改用户家目录位置 usermod -d <path> username
  • 更改用户名 usermod -l new_username username
➜ ~ tldr usermod
usermod

Modifies a user account.
See also: users, useradd, userdel.
More information: https://manned.org/usermod.

 - Change a username:
   sudo usermod --login new_username username

 - Change a user id:
   sudo usermod --uid id username

 - Change a user shell:
   sudo usermod --shell path/to/shell username

 - Add a user to supplementary groups (mind the lack of whitespace):
   sudo usermod --append --groups group1,group2,... username

 - Change a user home directory:
   sudo usermod --move-home --home path/to/new_home username
➜ ~ man usermod
➜ ~ sudo useradd -m zhangsan
➜ ~ ls /home
jaime  zhangsan
➜ ~ sudo usermod -l luoxiang zhangsan
➜ ~ ls /home
jaime  zhangsan
➜ ~ id zhangsan
id: ‘zhangsan’: no such user
➜ ~ id luoxiang
uid=1001(luoxiang) gid=1001(zhangsan) groups=1001(zhangsan)
➜ ~ sudo mkdir -p /home/luoxiang/
➜ ~ sudo usermod -d /home/luoxiang luoxiang
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 修改用户属性 chage

  • 更改用户密码过期时间
➜ ~ tldr chage
chage

Change user account and password expiry information.
More information: https://manned.org/chage.

 - List password information for the user:
   chage --list username

 - Enable password expiration in 10 days:
   sudo chage --maxdays 10 username

 - Disable password expiration:
   sudo chage --maxdays -1 username

 - Set account expiration date:
   sudo chage --expiredate YYYY-MM-DD username

 - Force user to change password on next log in:
   sudo chage --lastday 0 username
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 新建用户组 groupadd

  • 新建用户组 groupadd group_name
  • 用户加入组
    • 新建用户时指定用户组 useradd -g group_name username
    • 新建用户时指定多个用户组 useradd -G group1,group2,... username
    • 将已有用户添加进组 usermod -g group_name username
➜ ~ tldr groupadd
groupadd

Add user groups to the system.
See also: groups, groupdel, groupmod.
More information: https://manned.org/groupadd.

 - Create a new group:
   sudo groupadd group_name

 - Create a new system group:
   sudo groupadd --system group_name

 - Create a new group with the specific groupid:
   sudo groupadd --gid id group_name
➜ ~ sudo groupadd criminal
➜ ~ sudo useradd zhangsan
➜ ~ id zhangsan
uid=1001(zhangsan) gid=1001(zhangsan) groups=1001(zhangsan)
➜ ~ sudo usermod -g criminal zhangsan
➜ ~ id zhangsan
uid=1001(zhangsan) gid=1002(criminal) groups=1002(criminal)
➜ ~
➜ ~ sudo useradd -g criminal lisi
➜ ~ id lisi
uid=1003(lisi) gid=1002(criminal) groups=1002(criminal)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 删除用户组 groupdel

  • 删除用户组 groupdel group_name
➜ ~ tldr groupdel
groupdel

Delete existing user groups from the system.
See also: groups, groupadd, groupmod.
More information: https://manned.org/groupdel.

 - Delete an existing group:
   sudo groupdel group_name
➜ ~ sudo groupdel criminal
➜ ~ sudo useradd -g criminal lisi
useradd: group 'criminal' does not exist
1
2
3
4
5
6
7
8
9
10
11
12

# 21 | su 和 sudo 命令的区别和使用方法

  • 切换用户 su
    • 使用 login shell 方式切换用户 su - username
    • 切换用户但是不切换工作目录 su username
  • 以其他用户身份执行命令 sudo
    • 设置需要使用 sudo 的用户(组)visudo
    • 格式 username localhost/all=(command) all/nopasswd:all 例如 zhangsan ALL=(shutdown -c) ALL
➜ ~ tldr su
su

Switch shell to another user.
More information: https://manned.org/su.

 - Switch to superuser (requires the root password):
   su

 - Switch to a given user (requires the user's password):
   su username

 - Switch to a given user and simulate a full login shell:
   su - username

 - Execute a command as another user:
   su - username -c "command"

➜ ~ tldr sudo
sudo

Executes a single command as the superuser or another user.
More information: https://www.sudo.ws/sudo.html.

 - Run a command as the superuser:
   sudo less /var/log/syslog

 - Edit a file as the superuser with your default editor:
   sudo --edit /etc/fstab

 - Run a command as another user and/or group:
   sudo --user=user --group=group id -a

 - Repeat the last command prefixed with sudo (only in bash, zsh, etc.):
   sudo !!

 - Launch the default shell with superuser privileges and run login-specific files (.profile, .bash_profile, etc.):
   sudo --login

 - Launch the default shell with superuser privileges without changing the environment:
   sudo --shell

 - Launch the default shell as the specified user, loading the user's environment and reading login-specific files (.profile, .bash_profile, etc.):
   sudo --login --user=user

 - List the allowed (and forbidden) commands for the invoking user:
   sudo --list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 22 | 用户和用户组的配置文件介绍

  • 用户配置文件 /etc/passwd
    • 格式 jaime:x:1000:1000:,,,:/home/jaime:/usr/bin/zsh 共 7 个字段
      1. jaime 用户名
      2. x 登录是否需要密码验证
      3. 1000 UID
      4. 1000 GID
      5. ,,, 注释
      6. /home/jaime 用户家目录
      7. /usr/bin/zsh 登录后的命令解释器 /sbin/nologin 不允许该用户登录
  • 用户配置文件 /etc/shadow
    • 前两个字段为 用户名 和 加密后的密码
    • 两个用户的密码相同,在 /etc/shadow 配置文件中也会不一样,目的防止其它用户看到后使用其密码登录
  • 用户组配置文件 /etc/group
    • 格式 jaime:x:1000:
      1. jaime 用户组名称
      2. x 是否需要密码验证
      3. 1000 用户组 id

# 23 | 文件与目录权限的表示方法

-rw-r-xr-- 1 username groupname mtime filename

  • 文件类型
    • 普通文件(-)
    • 目录(d)
    • 字符设备(c)
    • 块设备(b)
    • 套接口/字文件(s)
    • 符号链接(l)
    • 命名管道(p)
  • 文件权限
    • 字符权限 r 读/ w 写/ x 执行 例如:rw- 文件属主的权限 r-x 文件属组的权限 r-- 其他用户的权限
    • 数字权限 r=4/ w=2/ x=1
    • 创建新文件有默认权限,根据 umask 值计算,属主和属组根据当前进程的用户来设定
  • 所属用户和组
  • 目录权限
    • x 进入目录
    • rx 显示目录内文件名
    • wx 修改目录内文件名
# 创建 lisi 用户组
➜ ~ sudo groupadd lisi
[sudo] password for jaime:
# 创建 lisi 用户并添加进 lisi 组,然后设置用户密码
➜ ~ sudo useradd -g lisi lisi
➜ ~ sudo passwd lisi
New password:
Retype new password:
passwd: password updated successfully
# 查看两个用户的信息
➜ ~ id
uid=1000(jaime) gid=1000(jaime) groups=1000(jaime),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev)
➜ ~ id lisi
uid=1001(lisi) gid=1001(lisi) groups=1001(lisi)
# 创建测试文件
➜ ~ cd demo && echo 123456> test_file
# 查看当前文件权限 test_file 644(jaime 用户读写权限,jaime 组读权限,其他用户读权限)
➜ ~/demo ls -l test_file
.rw-r--r-- 7 jaime  5 Mar 21:35 test_file
# 切换 lisi 用户
➜ ~/demo su lisi
Password:
# 测试可以读文件
$ cat test_file
123456
# 测试不能写文件
$ echo 456789>test_file
sh: 2: cannot create test_file: Permission denied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 24 | 文件权限的修改方法和数字表示方法

文件权限

创建新文件有默认权限,系统默认所有创建的文件都不可以执行,执行用数字 1 表示,所以文件最大的是 666,目录最大的权限是 777。根据 umask 值计算,系统新创建的目录的权限由最大权限 777 减去 umask 控制权限 022 得到的就是 755,所以说新创建的目录的权限为 755,新创建文件的权限由文件最大权限 666 减去 umask 控制权限 022,得到 644 权限,所以说新创建的文件的权限为 644,当文件权限遇到奇数时,在奇数为 +1

# 修改文件/目录权限 chmod

  • 用户
    • 文件属主 [u]ser
    • 文件属组 [g]roup
    • 其他用户 [o]thers
    • 所有用户 [a]ll
  • 权限
    • 读 [r]ead / 4
    • 写 [w]rite / 2
    • 执行 e[x]ecutable / 1
  • 操作
    • 添加权限 +
    • 撤销权限 -
    • 设置权限 = 例如,移除其它用户的所有权限 chmod o= file
  • 字符权限 chmod u+x /tmp/testfile
  • 数字权限 chmod 755 /tmp/testfile
  • 对目前目录下的所有文件与子目录进行相同的权限变更(即以递回的方式逐个变更) chmod -R a+rX directory
➜ ~ tldr chmod
chmod

Change the access permissions of a file or directory.
More information: https://www.gnu.org/software/coreutils/chmod.

 - Give the [u]ser who owns a file the right to e[x]ecute it:
   chmod u+x file

 - Give the [u]ser rights to [r]ead and [w]rite to a file/directory:
   chmod u+rw file_or_directory

 - Remove e[x]ecutable rights from the [g]roup:
   chmod g-x file

 - Give [a]ll users rights to [r]ead and e[x]ecute:
   chmod a+rx file

 - Give [o]thers (not in the file owner's group) the same rights as the [g]roup:
   chmod o=g file

 - Remove all rights from [o]thers:
   chmod o= file

 - Change permissions recursively giving [g]roup and [o]thers the ability to [w]rite:
   chmod -R g+w,o+w directory

 - Recursively give [a]ll users [r]ead permissions to files and e[X]ecute permissions to sub-directories within a directory:
   chmod -R a+rX directory

# 更改文件权限使 lisi 用户具有读写权限
➜ ~/demo chmod o+w test_file
mode of 'test_file' changed from 0644 (rw-r--r--) to 0646 (rw-r--rw-)
➜ ~/demo su lisi
Password:
$ echo 456789 > test_file
$ head test_file
456789
$
# 撤销用户权限后 lisi 用户又无法写文件
➜ ~/demo chmod o=r test_file
mode of 'test_file' changed from 0646 (rw-r--rw-) to 0644 (rw-r--r--)
➜ ~/demo su lisi
Password:
$ echo 456 > test_file
sh: 1: cannot create test_file: Permission denied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# 更改文件属主/组 chown

  • 更改文件/目录属主 chown user path/to/file_or_directory
  • 更改文件/目录属组 chown :group path/to/file_or_directory
  • 对目前目录下的所有文件与子目录进行文件属主变更 chown -R user path/to/directory
➜ ~ tldr chown
chown

Change user and group ownership of files and directories.
More information: https://www.gnu.org/software/coreutils/chown.

 - Change the owner user of a file/directory:
   chown user path/to/file_or_directory

 - Change the owner user and group of a file/directory:
   chown user:group path/to/file_or_directory

 - Recursively change the owner of a directory and its contents:
   chown -R user path/to/directory

 - Change the owner of a symbolic link:
   chown -h user path/to/symlink

 - Change the owner of a file/directory to match a reference file:
   chown --reference=path/to/reference_file path/to/file_or_directory

# lisi 用户无法编辑测试文件
➜ ~/demo su lisi
Password:
$ echo 456 > test_file
sh: 1: cannot create test_file: Permission denied
$ ls -la test_file
-rw-r--r-- 1 jaime jaime 7 Mar  5 21:45 test_file
$
# 更改测试文件属主和属组为 lisi
➜ ~/demo sudo chown lisi:lisi test_file
➜ ~/demo ls -la test_file
.rw-r--r-- 7 lisi  5 Mar 21:45 test_file
➜ ~/demo su lisi
Password:
$ ls -la test_file
-rw-r--r-- 1 lisi lisi 7 Mar  5 21:45 test_file
# 使用 lisi 用户身份可以写文件
$ echo lisi > test_file
$ head test_file
lisi
$
# 使用 jaime 身份无法编辑文件,只能读取文件
➜ ~/demo echo jaime >> test_file
zsh: permission denied: test_file
➜ ~/demo cat test_file
lisi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 单独更改属组(不常用) chgrp

  • 更改文件/目录属组 chgrp group path/to/file_or_directory
  • 对目前目录下的所有文件与子目录进行文件属组变更 chgrp -R group path/to/directory
➜ ~ tldr chgrp
chgrp

Change group ownership of files and directories.
More information: https://www.gnu.org/software/coreutils/chgrp.

 - Change the owner group of a file/directory:
   chgrp group path/to/file_or_directory

 - Recursively change the owner group of a directory and its contents:
   chgrp -R group path/to/directory

 - Change the owner group of a symbolic link:
   chgrp -h group path/to/symlink

 - Change the owner group of a file/directory to match a reference file:
   chgrp --reference=path/to/reference_file path/to/file_or_directory

# 使用 jaime 身份无法编辑文件,只能读取文件
➜ ~/demo echo jaime >> test_file
zsh: permission denied: test_file
➜ ~/demo cat test_file
lisi
➜ ~/demo su lisi
Password:
$ ls -la test_file
-rw-r--r-- 1 lisi lisi 7 Mar  5 21:45 test_file
# 更改测试文件属组为 jaime,然后给组添加写权限
$ chgrp jaime test_file
$ chmod g+w test_file
$ ls -l test_file
-rw-rw-r-- 1 lisi jaime 5 Mar  5 21:55 test_file
# 使用 jaime 身份就可以写文件了
➜ ~/demo echo jaime >> test_file
➜ ~/demo head test_file
lisi
jaime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 25 | 权限管理以及文件的特殊权限

  • SUID 用于二进制可执行文件,执行命令时取得文件属主权限,让执行命令的人具有和该命令拥有者相同的权限。如 /usr/bin/passwd。当用户执行该执行文件时,会拥有该执行文件所有者的权限。如果给一个非二进制文件文件附加 suid 权限,则会显示大写 S,属于无效。chmod u+s file chmod 4755 file

    # 可执行文件 hello
    ➜ ~/demo ls -la hello
    .rw-r--r-- 254 jaime  5 Mar 22:18 hello
    ➜ ~/demo cat hello
    #!/usr/bin/env bash
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo "hello jaime~"
    ➜ ~/demo chmod +x hello
    mode of 'hello' changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x)
    ➜ ~/demo ./hello
    hello jaime~
    
    ➜ ~/demo chmod 4755 hello
    mode of 'hello' changed from 0755 (rwxr-xr-x) to 4755 (rwsr-xr-x)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
  • SGID 用于文件或目录,在该目录下创建新的文件和目录,权限自动更改为该目录的属组。chmod g+s directory chmod 2765 directory

  • SBIT 用于目录,该目录下新建的文件和目录,仅 root 和自己可以删除,如 /tmp,防止目录下自己的文件被其他用户删除。chmod o+t directory chmod 1765 directory

    # /tmp 目录有 SBIT 权限
    ➜ ~/demo ls -ld /tmp
    drwxrwxrwt - root  5 Mar 22:18 /tmp
    ➜ ~/demo su lisi
    Password:
    $ ls -la /tmp/zsh-fzf-tab-jaime
    drwxr-xr-x  2 jaime jaime 4096 Mar  5 22:24 zsh-fzf-tab-jaime
    
    # lisi 用户无法删除 jaime 用户的文件
    $ rm -rf /tmp/zsh-fzf-tab-jaime
    rm: cannot remove '/tmp/zsh-fzf-tab-jaime': Operation not permitted
    
    # 给 demo 目录 1777 权限,lisi 用户有了 demo 目录写权限
    ➜ ~ ls -ld demo
    drwxr-xr-x - jaime  5 Mar 22:19 demo
    ➜ ~ chmod 1777 demo
    mode of 'demo' changed from 0755 (rwxr-xr-x) to 1777 (rwxrwxrwt)
    ➜ ~ su lisi
    Password:
    # 但是 lisi 用户无法删除 demo 目录下的文件
    $ rm -f demo/hello
    rm: cannot remove 'demo/hello': Operation not permitted
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
第一章:基础篇
第三章:系统管理篇

← 第一章:基础篇 第三章:系统管理篇→

Theme by Vdoing | Copyright © 2020-2022 JaimeZeng | ❤️ | CC BY-NC-SA 4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式