linux —— 简介 & 基础(1)
https://linuxtools-rst.readthedocs.io/zh_CN/latest/index.html#
Linux工具快速教程
前言
Linux下有很多命令行工具供我们使用,每个工具总是提供了大量参数供我们选择; 实际工作中,我们用到的工具,最常用的总是那么几个参数组合; 为此,我写了这本书相对实用的书;
这本书专注于Linux工具的最常用用法,以便读者能以最快时间掌握,并在工作中应用;
说明
全书分为三个部分:
- 第一部分为基础篇,介绍我们工作中常用的工具的高频用法;
- 第二部分为进阶篇,介绍的工具更多的适合程序员使用,分为程序构建、程序调试及程序优化;
- 第三部分是工具参考篇,主要介绍实用工具的用法和实例;相比第一二部分,这里针对每个工具的介绍更全面;
同时,这个教程也可当作Linux命令手册使用,使用左边栏的目录和搜索栏可以很方便的查阅;
建议
- 最好安装一个Linux系统(对于新手Ubuntu容易入门),将教程中的命令敲到bash中看看效果
- 如果有兴趣,可以在了解之后立即查看相关更完备的内容 (比如查阅官方文档)
PS C:\****> docker pull ubuntu:latest latest: Pulling from library/ubuntu 5bed26d33875: Pull complete f11b29a9c730: Pull complete 930bda195c84: Pull complete 78bf9a5ad49e: Pull complete Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest PS C:\****> docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 4e5021d210f6 2 weeks ago 64.2MB PS C:\****> docker run -ti 4e5021d210f6 /bin root@e554660a7d52:/bin#
写作工具
本书使用的reStructuredText标记语言,相对Markdown来说,在写书方面更有优势:
- 使用sphnix能够自动生成目录和索引文件,方便查询和检索;
- 有大量漂亮的HTML书籍主题模版,可为书籍轻松换肤(类似Wordpress的网站模版);
- 对于参考手册类书籍的编写在语法上更为便利(python官方帮助文档的使用者);
目录
- Linux基础
- Linux工具进阶
- 工具参考篇
- 1. gdb 调试利器
- 2. ldd 查看程序依赖库
- 3. lsof 一切皆文件
- 4. ps 进程查看器
- 5. pstack 跟踪进程栈
- 6. strace 跟踪进程中的系统调用
- 7. ipcs 查询进程间通信状态
- 8. top linux下的任务管理器
- 9. free 查询可用内存
- 10. vmstat 监视内存使用情况
- 11. iostat 监视I/O子系统
- 12. sar 找出系统瓶颈的利器
- 13. readelf elf文件格式分析
- 14. objdump 二进制文件分析
- 15. nm 目标文件格式分析
- 16. size 查看程序内存映像大小
- 17. wget 文件下载
- 18. scp 跨机远程拷贝
- 19. crontab 定时任务
Linux基础
这一部分主要介绍Linux常用命令工具,比如文件管理、文本处理;为了让读者用最少的时间掌握到常用的知识,对于每个工具的举例,尽量做到小而精;
本书并非一本讲解Linux各种命令的完整用法的书,并且假设读者已经熟悉Linux命令行下的基本操作。如果读者对Linux操作系统尚不了解,可以参考 《鸟哥的私房菜 基础学习篇》[1]一书。
1. 学会使用命令帮助
1.1. 概述
在linux终端,面对命令不知道怎么用,或不记得命令的拼写及参数时,我们需要求助于系统的帮助文档; linux系统内置的帮助文档很详细,通常能解决我们的问题,我们需要掌握如何正确的去使用它们;
- 在只记得部分命令关键字的场合,我们可通过man -k来搜索;
- 需要知道某个命令的简要说明,可以使用whatis;而更详细的介绍,则可用info命令;
- 查看命令在哪个位置,我们需要使用which;
- 而对于命令的具体参数及使用方法,我们需要用到强大的man;
下面介绍这些命令;
1. 学会使用命令帮助
1.1. 概述
在linux终端,面对命令不知道怎么用,或不记得命令的拼写及参数时,我们需要求助于系统的帮助文档; linux系统内置的帮助文档很详细,通常能解决我们的问题,我们需要掌握如何正确的去使用它们;
- 在只记得部分命令关键字的场合,我们可通过man -k来搜索;
- 需要知道某个命令的简要说明,可以使用whatis;而更详细的介绍,则可用info命令;
- 查看命令在哪个位置,我们需要使用which;
- 而对于命令的具体参数及使用方法,我们需要用到强大的man;
下面介绍这些命令;
#dockerfile # base image FROM ubuntu:latest LABEL maintainer="Pan" # WORKDIR /app EXPOSE 3000 EXPOSE 35729 COPY sources.list /etc/apt RUN apt-get update # && apt-get install -y locales \ # && apt-get install debconf \ # && echo "Asia/Shanghai" > /etc/timezone \ # && dpkg-reconfigure -f noninteractive tzdata \ # && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ # && sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \ # && echo 'LANG="en_US.UTF-8"'>/etc/default/locale \ # && dpkg-reconfigure --frontend=noninteractive locales \ # && update-locale LANG=en_US.UTF-8 \ # && apt-get install man # CMD ["/bin"]
# sources.list # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # 预发布软件源,不建议启用 # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# docker command PS D:\PanPan\Learning\docker-ubuntu> docker build -t ubuntu:v1.0.0.3 . Sending build context to Docker daemon 6.656kB Step 1/7 : FROM ubuntu:latest ---> 4e5021d210f6 Step 2/7 : LABEL maintainer="Pan" ---> Using cache ---> 9104887d1144 Step 3/7 : WORKDIR /app ---> Using cache ---> 5b7cf068506b Step 4/7 : EXPOSE 3000 ---> Using cache ---> b9b0437b6696 Step 5/7 : EXPOSE 35729 ---> Using cache ---> c5e9d6178d4e Step 6/7 : COPY sources.list /etc/apt ---> Using cache ---> b450c018e0ea Step 7/7 : RUN apt-get update ---> Using cache ---> cb85e9b0ac9b Successfully built cb85e9b0ac9b Successfully tagged ubuntu:v1.0.0.3 SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. PS D:\PanPan\Learning\docker-ubuntu> docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu v1.0.0.2 86788b12c035 2 minutes ago 107MB ubuntu v1.0.0.3 cb85e9b0ac9b 2 minutes ago 107MB PS D:\PanPan\Learning\docker-ubuntu> docker run -ti cb85e9b0ac9b root@940eeb002636:/app# ls root@940eeb002636:/app# ls root@940eeb002636:/app# cd .. root@940eeb002636:/# ls app bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@940eeb002636:/# man -k bash: man: command not found root@940eeb002636:/# cd app root@940eeb002636:/app# cd .. root@940eeb002636:/# apt-get install man Reading package lists... Done Building dependency tree Reading state information... Done Note, selecting 'man-db' instead of 'man' The following additional packages will be installed: bsdmainutils gcc-10-base groff-base libbsd0 libc-bin libc6 libcrypt1 libgcc-s1 libgdbm6 libidn2-0 libpipeline1 libtinfo6 libuchardet0 Suggested packages: cpp wamerican | wordlist whois vacation groff manpages glibc-doc locales gdbm-l10n apparmor less www-browser The following NEW packages will be installed: bsdmainutils gcc-10-base groff-base libbsd0 libcrypt1 libgcc-s1 libgdbm6 libpipeline1 libtinfo6 libuchardet0 man-db The following packages will be upgraded: libc-bin libc6 libidn2-0 3 upgraded, 11 newly installed, 0 to remove and 81 not upgraded. Need to get 5930 kB of archives. After this operation, 10.5 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 gcc-10-base amd64 10-20200324-1ubuntu1 [18.5 kB] Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libgcc-s1 amd64 10-20200324-1ubuntu1 [41.6 kB] Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libcrypt1 amd64 1:4.4.10-10ubuntu4 [78.2 kB] Get:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libc6 amd64 2.31-0ubuntu7 [2711 kB] Get:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libc-bin amd64 2.31-0ubuntu7 [637 kB] Get:6 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libbsd0 amd64 0.10.0-1 [45.4 kB] Get:7 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libtinfo6 amd64 6.2-0ubuntu2 [87.0 kB] Get:8 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 bsdmainutils amd64 11.1.2ubuntu3 [181 kB] Get:9 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libuchardet0 amd64 0.0.6-3build1 [65.2 kB] Get:10 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 groff-base amd64 1.22.4-4build1 [847 kB] Get:11 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libgdbm6 amd64 1.18.1-5 [27.4 kB] Get:12 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libpipeline1 amd64 1.5.2-2build1 [27.7 kB] Get:13 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 man-db amd64 2.9.1-1 [1112 kB] Get:14 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/main amd64 libidn2-0 amd64 2.2.0-2 [50.4 kB] Fetched 5930 kB in 1s (6872 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package gcc-10-base:amd64. (Reading database ... 4046 files and directories currently installed.) Preparing to unpack .../gcc-10-base_10-20200324-1ubuntu1_amd64.deb ... Unpacking gcc-10-base:amd64 (10-20200324-1ubuntu1) ... Setting up gcc-10-base:amd64 (10-20200324-1ubuntu1) ... Selecting previously unselected package libgcc-s1:amd64. (Reading database ... 4052 files and directories currently installed.) Preparing to unpack .../libgcc-s1_10-20200324-1ubuntu1_amd64.deb ... Unpacking libgcc-s1:amd64 (10-20200324-1ubuntu1) ... Replacing files in old package libgcc1:amd64 (1:8.3.0-26ubuntu1~18.04) ... Setting up libgcc-s1:amd64 (10-20200324-1ubuntu1) ... (Reading database ... 4054 files and directories currently installed.) Preparing to unpack .../libc6_2.31-0ubuntu7_amd64.deb ... debconf: unable to initialize frontend: Dialog debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.) debconf: falling back to frontend: Teletype Checking for services that may need to be restarted... Checking init scripts... Checking for services that may need to be restarted... Checking init scripts... Nothing to restart. Unpacking libc6:amd64 (2.31-0ubuntu7) over (2.27-3ubuntu1) ... Selecting previously unselected package libcrypt1:amd64. Preparing to unpack .../libcrypt1_1%3a4.4.10-10ubuntu4_amd64.deb ... Unpacking libcrypt1:amd64 (1:4.4.10-10ubuntu4) ... Setting up libcrypt1:amd64 (1:4.4.10-10ubuntu4) ... Setting up libc6:amd64 (2.31-0ubuntu7) ... debconf: unable to initialize frontend: Dialog debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.) debconf: falling back to frontend: Teletype Checking for services that may need to be restarted... Checking init scripts... Nothing to restart. (Reading database ... 4055 files and directories currently installed.) Preparing to unpack .../libc-bin_2.31-0ubuntu7_amd64.deb ... Unpacking libc-bin (2.31-0ubuntu7) over (2.27-3ubuntu1) ... Setting up libc-bin (2.31-0ubuntu7) ... Updating /etc/nsswitch.conf to current default. Selecting previously unselected package libbsd0:amd64. (Reading database ... 4055 files and directories currently installed.) Preparing to unpack .../0-libbsd0_0.10.0-1_amd64.deb ... Unpacking libbsd0:amd64 (0.10.0-1) ... Selecting previously unselected package libtinfo6:amd64. Preparing to unpack .../1-libtinfo6_6.2-0ubuntu2_amd64.deb ... Unpacking libtinfo6:amd64 (6.2-0ubuntu2) ... Selecting previously unselected package bsdmainutils. Preparing to unpack .../2-bsdmainutils_11.1.2ubuntu3_amd64.deb ... Unpacking bsdmainutils (11.1.2ubuntu3) ... Selecting previously unselected package libuchardet0:amd64. Preparing to unpack .../3-libuchardet0_0.0.6-3build1_amd64.deb ... Unpacking libuchardet0:amd64 (0.0.6-3build1) ... Selecting previously unselected package groff-base. Preparing to unpack .../4-groff-base_1.22.4-4build1_amd64.deb ... Unpacking groff-base (1.22.4-4build1) ... Selecting previously unselected package libgdbm6:amd64. Preparing to unpack .../5-libgdbm6_1.18.1-5_amd64.deb ... Unpacking libgdbm6:amd64 (1.18.1-5) ... Selecting previously unselected package libpipeline1:amd64. Preparing to unpack .../6-libpipeline1_1.5.2-2build1_amd64.deb ... Unpacking libpipeline1:amd64 (1.5.2-2build1) ... Selecting previously unselected package man-db. Preparing to unpack .../7-man-db_2.9.1-1_amd64.deb ... Unpacking man-db (2.9.1-1) ... Preparing to unpack .../8-libidn2-0_2.2.0-2_amd64.deb ... Unpacking libidn2-0:amd64 (2.2.0-2) over (2.0.4-1.1ubuntu0.2) ... Setting up libidn2-0:amd64 (2.2.0-2) ... Setting up libtinfo6:amd64 (6.2-0ubuntu2) ... Setting up libpipeline1:amd64 (1.5.2-2build1) ... Setting up libbsd0:amd64 (0.10.0-1) ... Setting up libuchardet0:amd64 (0.0.6-3build1) ... Setting up libgdbm6:amd64 (1.18.1-5) ... Setting up bsdmainutils (11.1.2ubuntu3) ... update-alternatives: using /usr/bin/bsd-write to provide /usr/bin/write (write) in auto mode update-alternatives: warning: skip creation of /usr/share/man/man1/write.1.gz because associated file /usr/share/man/man1/bsd-write.1.gz (of link group write) doesn't existupdate-alternatives: using /usr/bin/bsd-from to provide /usr/bin/from (from) in auto mode update-alternatives: warning: skip creation of /usr/share/man/man1/from.1.gz because associated file /usr/share/man/man1/bsd-from.1.gz (of link group from) doesn't exist Setting up groff-base (1.22.4-4build1) ... Setting up man-db (2.9.1-1) ... debconf: unable to initialize frontend: Dialog debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.) debconf: falling back to frontend: Teletype Building database of manual pages ... Processing triggers for libc-bin (2.31-0ubuntu7) ... root@940eeb002636:/# man -k apropos what?
# dockerfile 添加 && apt-get install -y man \ && apt-get install manpages \ && apt-get install manpages-dev \ && apt-get install manpages-posix \ && apt-get install manpages-posix-dev \ && apt-get install -y vim
docker上安装Ubuntu环境man命令出现No manual entry for xxx的问题
在docker上安装Ubuntu后,发现没有安装man,执行 apt-get update && apt-get install man ,安装man,完成后,运行man man 会发现并没有出现预想中的情况,而是出现如下信息:
No manual entry for man
See 'man 7 undocumented' for help when manual pages are not available.
这是什么情况?
其实原因很简单,为了使镜像文件体积尽可能的小,所以Ubuntu版本在安装文件时,过滤掉了其中包含的文档内容,这部分配置是在/etc/dpkg/dpkg.cfg.d/excludes文件中声明的(执行man dpkg会有相关配置信息的说明,当然你要先把No manual entry for dpkg的问题解决)。打开该文件cat /etc/dpkg/dpkg.cfg.d/excludes,有如下内容:
# Drop all man pages
path-exclude=/usr/share/man/*
# Drop all documentation ...
path-exclude=/usr/share/doc/*
# ... except copyright files ...
path-include=/usr/share/doc/*/copyright
# ... and Debian changelogs
path-include=/usr/share/doc/*/changelog.Debian.*
所以,只要删除该文件或注释掉path-exclude的部分,之后安装的应用,就可以使用man文档了。但是,之前的应用依然没有办法使用man文档,比如ls、sed等命令,所以如果你需要的话,可以重新安装所有的应用。
使用dpkg -l查看所有安装的程序,用grep ^ii 过滤出所有安装成功的条目,用cut -d' ' -f3命令(因为状态标识ii和程序名之间有两个空格,所以要用f3)筛选出程序名,最后使用apt-get重新安装就好了,命令如下:
dpkg -l | grep ^ii | cut -d' ' -f3 | xargs apt-get install -y --reinstall
所以,整体的命令是这个样子的:
rm /etc/dpkg/dpkg.cfg.d/excludes&&apt-get update&&dpkg -l | grep ^ii | cut -d' ' -f3 | xargs apt-get install -y --reinstall&&apt-get install man&&rm -r /var/lib/apt/lists/*
当然,如果你已经安装了man那就不用apt-get install man这部分命令了,可以根据自己的需要执行相关命令。
————————————————
版权声明:本文为CSDN博主「lsp_123456」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lsp_123456/article/details/85932172
本书为开源图书,版权归作者所有;欢迎下载及编辑(个人用途),但未经作者同意必须保留此段声明,且不可用于商业用途,否则保留追究法律责任的权利。
- 作者:大CC
- 博客:http://blog.me115.com
- Github地址:https://github.com/me115/linuxtools_rst