9.1 软件安装
在 Linux 中安装软件分为以下三种方式:
-
源码安装
-
rpm包安装
-
yum/dnf安装
9.1.1 源码安装
软件准备
我们接下通过一个源码程序来演示如何安装这种软件。
[root@jock ~]# ll total 60 -rw-r--r--. 1 root root 59172 Feb 22 15:43 tree-2.0.4.tgz # 创建一个目录 [root@jock ~]# mkdir /test # 将上传的文件移动到此目录中 [root@jock ~]# mv tree-2.0.4.tgz /test # 切换到创建的目录 [root@jock ~]# cd /test [root@jock test]# ll total 60 -rw-r--r--. 1 root root 59172 Feb 22 15:43 tree-2.0.4.tgz # 解压上传的文件 [root@jock test]# tar -xzvf tree-2.0.4.tgz tree-2.0.4/CHANGES tree-2.0.4/INSTALL ..... # 验证解压 [root@jock test]# ll total 64 drwxr-xr-x. 3 root root 4096 Feb 22 15:45 tree-2.0.4 -rw-r--r--. 1 root root 59172 Feb 22 15:43 tree-2.0.4.tgz # 进入到解压目录中 [root@jock test]# cd tree-2.0.4/ [root@jock tree-2.0.4]# ll total 204 -rw-r--r--. 1 4437 users 18101 Sep 6 2022 CHANGES -rw-r--r--. 1 4437 users 15968 Jun 16 2022 color.c drwxr-xr-x. 2 root root 71 Feb 22 15:45 doc -rw-r--r--. 1 4437 users 5241 Jun 16 2022 file.c -rw-r--r--. 1 4437 users 4531 Aug 27 2022 filter.c -rw-r--r--. 1 4437 users 3118 Jun 16 2022 hash.c -rw-r--r--. 1 4437 users 6283 Jun 16 2022 html.c -rw-r--r--. 1 4437 users 4610 Jun 16 2022 info.c -rw-r--r--. 1 4437 users 597 Jan 4 2018 INSTALL -rw-r--r--. 1 4437 users 5877 Aug 30 2022 json.c -rw-r--r--. 1 4437 users 18009 Aug 13 2004 LICENSE -rw-r--r--. 1 4437 users 7009 Aug 27 2022 list.c -rw-r--r--. 1 4437 users 2918 Sep 6 2022 Makefile -rw-r--r--. 1 4437 users 8372 Aug 27 2022 README -rw-r--r--. 1 4437 users 5346 Apr 14 2021 strverscmp.c -rw-r--r--. 1 4437 users 853 Dec 21 2021 TODO -rw-r--r--. 1 4437 users 38706 Sep 5 2022 tree.c -rw-r--r--. 1 4437 users 8646 Jun 16 2022 tree.h -rw-r--r--. 1 4437 users 3166 Jun 16 2022 unix.c -rw-r--r--. 1 4437 users 5207 Aug 27 2022 xml.c
由于系统中自带了 tree 命令,我们通过 dnf 命令来把它删除掉。
[root@jock tree-2.0.4]# dnf remove tree -y # remove 表示删除文件 -y 表示删除时不提示确认 Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. Dependencies resolved. ============================================================================================================================================================ Package Architecture Version Repository Size ============================================================================================================================================================ Removing: tree x86_64 1.8.0-10.el9 @anaconda 113 k Transaction Summary ============================================================================================================================================================ Remove 1 Package Freed space: 113 k Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Erasing : tree-1.8.0-10.el9.x86_64 1/1 Running scriptlet: tree-1.8.0-10.el9.x86_64 1/1 Verifying : tree-1.8.0-10.el9.x86_64 1/1 Installed products updated. Removed: tree-1.8.0-10.el9.x86_64 Complete! # 验证删除是否成功 [root@jock tree-2.0.4]# tree -bash: /usr/bin/tree: No such file or directory
安装 tree-2.0.4 软件:
# 由于程序是使用 c 语言编写的,我们在进行源码安装时,需要先进行编译,而编译 c 语言时需要有它的环境,因此我们需要先安装 c 语言的编译环境。通过执行如下的命令来安装这个环境。 [root@jock tree-2.0.4]# dnf install -y make gcc* # dnf install 这个命令表示要安装软件 # dnf remove 表示删除软件 # -y 表示安装时不提示确认,直接安装 # make 表示要安装的软件 # gcc* 表示要安装以 gcc 开头的所有软件
注意:执行上面的命令时,出现了如下的错误:
[root@jock tree-2.0.4]# dnf install -y make gcc* Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. Error: There are no enabled repositories in "/etc/yum.repos.d", "/etc/yum/repos.d", "/etc/distro.repos.d".
这个错误是告诉我们,没有仓库地址。因此,我们就需要去配置一个仓库。需要在 /etc/yum.repos.d 目录下去新一个后缀为 .repo 的文件。
配置仓库**
# vim /etc/yum.repos.d/base.repo 文件的内容如下: [base] name=base baseurl=/mnt/BaseOS gpgcheck=0 [app] name=app baseurl=/mnt/AppStream gpgcheck=0
base.repo 文件内容说明如下:
[base] 名称,在整个文件中要唯一 name=base 仓库名称,一般设置为上面的名称,因为唯一 baseurl=/mnt/BaseOS # 这个仓库对应的地址 gpgcheck=0 # 是否需要校验,0 表示不校验,如果为 1 则表示要校验,需要指定文件的校验地址
[app] name=app baseurl=/mnt/AppStream gpgcheck=0
然后再把光盘挂载到 /mnt 目录下:
[root@jock yum.repos.d]# mount /dev/sr0 /mnt mount: /mnt: WARNING: source write-protected, mounted read-only. [root@jock yum.repos.d]# ls /mnt AppStream BaseOS EFI EULA extra_files.json GPL images isolinux media.repo RPM-GPG-KEY-redhat-beta RPM-GPG-KEY-redhat-release
也可以修改 /etc/fstab 文件,在这个文件中进行自动挂载。
[root@jock yum.repos.d]# vim /etc/fstab /dev/mapper/rhel_bogon-root / xfs defaults 0 0 UUID=98e55bfe-116c-411f-956c-d3e925b515c5 /boot xfs defaults 0 0 /dev/mapper/rhel_bogon-swap none swap defaults 0 0 /dev/nvme0n2p2 /opt/mydisk2 xfs defaults 0 0 /dev/sr0 /mnt iso9660 defaults 0 0
安装编译环境
执行完上面的仓库配置后,我们再次来安装 c 语言的运行环境:
[root@jock yum.repos.d]# dnf install -y make gcc* Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. base 106 MB/s | 1.7 MB 00:00 app 77 MB/s | 6.3 MB 00:00 Dependencies resolved. ============================================================================================================================================================ Package Architecture Version Repository Size ============================================================================================================================================================ Installing: gcc x86_64 11.3.1-4.3.el9 app 32 M gcc-c++ x86_64 11.3.1-4.3.el9 app 13 M gcc-gfortran x86_64 11.3.1-4.3.el9 app 13 M gcc-offload-nvptx x86_64 11.3.1-4.3.el9 app 15 M gcc-plugin-annobin x86_64 11.3.1-4.3.el9 app 66 k gcc-toolset-12 x86_64 12.0-6.el9 app 11 k gcc-toolset-12-annobin-annocheck x86_64 11.08-2.el9 app 181 k gcc-toolset-12-annobin-docs noarch 11.08-2.el9 app 95 k gcc-toolset-12-annobin-plugin-gcc x86_64 11.08-2.el9 app 894 k gcc-toolset-12-binutils x86_64 2.38-16.el9 app 5.5 M gcc-toolset-12-binutils-devel x86_64 2.38-16.el9 app 12 M gcc-toolset-12-binutils-gold x86_64 2.38-16.el9 app 781 k gcc-toolset-12-build x86_64 12.0-6.el9 app 9.1 k gcc-toolset-12-dwz x86_64 0.14-4.el9 app 130 k gcc-toolset-12-gcc x86_64 12.2.1-7.4.el9 app 43 M gcc-toolset-12-gcc-c++ x86_64 12.2.1-7.4.el9 app 13 M gcc-toolset-12-gcc-gfortran x86_64 12.2.1-7.4.el9 app 14 M gcc-toolset-12-gcc-plugin-annobin x86_64 12.2.1-7.4.el9 app 43 k gcc-toolset-12-gcc-plugin-devel x86_64 12.2.1-7.4.el9 app 1.6 M gcc-toolset-12-gdb x86_64 11.2-4.el9 app 4.3 M gcc-toolset-12-libasan-devel x86_64 12.2.1-7.4.el9 app 495 k gcc-toolset-12-libatomic-devel x86_64 12.2.1-7.4.el9 app 33 k gcc-toolset-12-libgccjit x86_64 12.2.1-7.4.el9 app 9.8 M gcc-toolset-12-libgccjit-devel x86_64 12.2.1-7.4.el9 app 39 k gcc-toolset-12-libgccjit-docs x86_64 12.2.1-7.4.el9 app 609 k gcc-toolset-12-libitm-devel x86_64 12.2.1-7.4.el9 app 69 k gcc-toolset-12-liblsan-devel x86_64 12.2.1-7.4.el9 app 259 k gcc-toolset-12-libquadmath-devel x86_64 12.2.1-7.4.el9 app 187 k gcc-toolset-12-libstdc++-devel x86_64 12.2.1-7.4.el9 app 3.3 M gcc-toolset-12-libstdc++-docs x86_64 12.2.1-7.4.el9 app 13 M gcc-toolset-12-libtsan-devel x86_64 12.2.1-7.4.el9 app 455 k gcc-toolset-12-libubsan-devel x86_64 12.2.1-7.4.el9 app 240 k gcc-toolset-12-offload-nvptx x86_64 12.2.1-7.4.el9 app 16 M gcc-toolset-12-runtime x86_64 12.0-6.el9 app 62 k make x86_64 1:4.3-7.el9 base 542 k Installing dependencies: annobin x86_64 11.05-1.el9 app 978 k boost-regex x86_64 1.75.0-8.el9 app 279 k dwz x86_64 0.14-3.el9 app 130 k efi-srpm-macros noarch 6-2.el9_0 app 24 k environment-modules x86_64 5.0.1-2.el9 base 496 k fonts-srpm-macros noarch 1:2.0.5-7.el9.1 app 29 k ghc-srpm-macros noarch 1.5.0-6.el9 app 9.0 k glibc-devel x86_64 2.34-60.el9 app 54 k glibc-headers x86_64 2.34-60.el9 app 556 k gmp-c++ x86_64 1:6.2.0-10.el9 app 21 k gmp-devel x86_64 1:6.2.0-10.el9 app 177 k go-srpm-macros noarch 3.2.0-1.el9 app 29 k kernel-headers x86_64 5.14.0-284.11.1.el9_2 app 5.0 M kernel-srpm-macros noarch 1.0-12.el9 app 17 k libasan8 x86_64 12.2.1-7.4.el9 app 431 k libbabeltrace x86_64 1.5.8-10.el9 app 194 k libgfortran x86_64 11.3.1-4.3.el9 base 821 k libgomp-offload-nvptx x86_64 11.3.1-4.3.el9 app 59 k libipt x86_64 2.0.4-5.el9 app 58 k libitm x86_64 11.3.1-4.3.el9 app 97 k liblsan x86_64 11.3.1-4.3.el9 app 215 k libmpc-devel x86_64 1.2.1-4.el9 app 13 k libquadmath x86_64 11.3.1-4.3.el9 base 216 k libquadmath-devel x86_64 11.3.1-4.3.el9 app 56 k libstdc++-devel x86_64 11.3.1-4.3.el9 app 2.4 M libtsan2 x86_64 12.2.1-7.4.el9 app 399 k libubsan x86_64 11.3.1-4.3.el9 app 207 k libxcrypt-devel x86_64 4.4.18-3.el9 app 32 k lua-srpm-macros noarch 1-6.el9 app 10 k mpfr-devel x86_64 4.1.0-7.el9 app 23 k ocaml-srpm-macros noarch 6-6.el9 app 9.1 k openblas-srpm-macros noarch 2-11.el9 app 8.6 k perl-srpm-macros noarch 1-41.el9 app 9.4 k pyproject-srpm-macros noarch 1.6.2-1.el9 app 15 k python-srpm-macros noarch 3.9-52.el9 app 24 k qt5-srpm-macros noarch 5.15.3-1.el9 app 9.9 k redhat-rpm-config noarch 199-1.el9 app 76 k rust-srpm-macros noarch 17-4.el9 app 11 k scl-utils x86_64 1:2.0.3-4.el9 app 41 k scl-utils-build x86_64 1:2.0.3-4.el9 app 17 k source-highlight x86_64 3.1.9-11.el9 app 685 k zlib-devel x86_64 1.2.11-39.el9 app 47 k Transaction Summary ============================================================================================================================================================ Install 77 Packages Total size: 227 M Installed size: 784 M Downloading Packages: Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : libquadmath-11.3.1-4.3.el9.x86_64 1/77 Installing : libgfortran-11.3.1-4.3.el9.x86_64 2/77 Installing : libgomp-offload-nvptx-11.3.1-4.3.el9.x86_64 3/77 Installing : make-1:4.3-7.el9.x86_64 4/77 Installing : zlib-devel-1.2.11-39.el9.x86_64 5/77 Installing : rust-srpm-macros-17-4.el9.noarch 6/77 Installing : qt5-srpm-macros-5.15.3-1.el9.noarch 7/77 Installing : pyproject-srpm-macros-1.6.2-1.el9.noarch 8/77 Installing : perl-srpm-macros-1-41.el9.noarch 9/77 Installing : openblas-srpm-macros-2-11.el9.noarch 10/77 Installing : ocaml-srpm-macros-6-6.el9.noarch 11/77 Installing : lua-srpm-macros-1-6.el9.noarch 12/77 Installing : libubsan-11.3.1-4.3.el9.x86_64 13/77 Installing : libtsan2-12.2.1-7.4.el9.x86_64 14/77 Running scriptlet: libtsan2-12.2.1-7.4.el9.x86_64 14/77 Installing : libstdc++-devel-11.3.1-4.3.el9.x86_64 15/77 Installing : liblsan-11.3.1-4.3.el9.x86_64 16/77 Installing : libitm-11.3.1-4.3.el9.x86_64 17/77 Installing : libipt-2.0.4-5.el9.x86_64 18/77 Installing : libbabeltrace-1.5.8-10.el9.x86_64 19/77 Installing : libasan8-12.2.1-7.4.el9.x86_64 20/77 Running scriptlet: libasan8-12.2.1-7.4.el9.x86_64 20/77 Installing : kernel-srpm-macros-1.0-12.el9.noarch 21/77 Installing : kernel-headers-5.14.0-284.11.1.el9_2.x86_64 22/77 Installing : gmp-c++-1:6.2.0-10.el9.x86_64 23/77 Installing : gmp-devel-1:6.2.0-10.el9.x86_64 24/77 Installing : mpfr-devel-4.1.0-7.el9.x86_64 25/77 Installing : libmpc-devel-1.2.1-4.el9.x86_64 26/77 Installing : glibc-headers-2.34-60.el9.x86_64 27/77 Installing : libxcrypt-devel-4.4.18-3.el9.x86_64 28/77 Installing : glibc-devel-2.34-60.el9.x86_64 29/77 Installing : gcc-11.3.1-4.3.el9.x86_64 30/77 Installing : annobin-11.05-1.el9.x86_64 31/77 Installing : gcc-plugin-annobin-11.3.1-4.3.el9.x86_64 32/77 Installing : libquadmath-devel-11.3.1-4.3.el9.x86_64 33/77 Installing : ghc-srpm-macros-1.5.0-6.el9.noarch 34/77 Installing : efi-srpm-macros-6-2.el9_0.noarch 35/77 Installing : dwz-0.14-3.el9.x86_64 36/77 Installing : fonts-srpm-macros-1:2.0.5-7.el9.1.noarch 37/77 Installing : go-srpm-macros-3.2.0-1.el9.noarch 38/77 Installing : python-srpm-macros-3.9-52.el9.noarch 39/77 Installing : redhat-rpm-config-199-1.el9.noarch 40/77 Running scriptlet: redhat-rpm-config-199-1.el9.noarch 40/77 Installing : scl-utils-build-1:2.0.3-4.el9.x86_64 41/77 Installing : boost-regex-1.75.0-8.el9.x86_64 42/77 Installing : source-highlight-3.1.9-11.el9.x86_64 43/77 Installing : environment-modules-5.0.1-2.el9.x86_64 44/77 Running scriptlet: environment-modules-5.0.1-2.el9.x86_64 44/77 Installing : scl-utils-1:2.0.3-4.el9.x86_64 45/77 Installing : gcc-toolset-12-runtime-12.0-6.el9.x86_64 46/77 Running scriptlet: gcc-toolset-12-runtime-12.0-6.el9.x86_64 46/77 Installing : gcc-toolset-12-binutils-2.38-16.el9.x86_64 47/77 Running scriptlet: gcc-toolset-12-binutils-2.38-16.el9.x86_64 47/77 Installing : gcc-toolset-12-binutils-gold-2.38-16.el9.x86_64 48/77 Running scriptlet: gcc-toolset-12-binutils-gold-2.38-16.el9.x86_64 48/77 Installing : gcc-toolset-12-gcc-12.2.1-7.4.el9.x86_64 49/77 Installing : gcc-toolset-12-annobin-docs-11.08-2.el9.noarch 50/77 Installing : gcc-toolset-12-annobin-plugin-gcc-11.08-2.el9.x86_64 51/77 Running scriptlet: gcc-toolset-12-annobin-plugin-gcc-11.08-2.el9.x86_64 51/77 Installing : gcc-toolset-12-libgccjit-12.2.1-7.4.el9.x86_64 52/77 Running scriptlet: gcc-toolset-12-libgccjit-12.2.1-7.4.el9.x86_64 52/77 Installing : gcc-toolset-12-libquadmath-devel-12.2.1-7.4.el9.x86_64 53/77 Installing : gcc-toolset-12-gcc-gfortran-12.2.1-7.4.el9.x86_64 54/77 Running scriptlet: gcc-toolset-12-gcc-gfortran-12.2.1-7.4.el9.x86_64 54/77 Installing : gcc-toolset-12-dwz-0.14-4.el9.x86_64 55/77 Installing : gcc-toolset-12-gdb-11.2-4.el9.x86_64 56/77 Installing : gcc-toolset-12-libgccjit-docs-12.2.1-7.4.el9.x86_64 57/77 Running scriptlet: gcc-toolset-12-libgccjit-docs-12.2.1-7.4.el9.x86_64 57/77 Installing : gcc-toolset-12-libstdc++-devel-12.2.1-7.4.el9.x86_64 58/77 Installing : gcc-toolset-12-gcc-c++-12.2.1-7.4.el9.x86_64 59/77 Installing : gcc-toolset-12-12.0-6.el9.x86_64 60/77 Installing : gcc-toolset-12-libgccjit-devel-12.2.1-7.4.el9.x86_64 61/77 Installing : gcc-toolset-12-annobin-annocheck-11.08-2.el9.x86_64 62/77 Installing : gcc-toolset-12-gcc-plugin-annobin-12.2.1-7.4.el9.x86_64 63/77 Running scriptlet: gcc-toolset-12-gcc-plugin-annobin-12.2.1-7.4.el9.x86_64 63/77 Installing : gcc-toolset-12-gcc-plugin-devel-12.2.1-7.4.el9.x86_64 64/77 Installing : gcc-toolset-12-libitm-devel-12.2.1-7.4.el9.x86_64 65/77 Installing : gcc-toolset-12-binutils-devel-2.38-16.el9.x86_64 66/77 Installing : gcc-toolset-12-build-12.0-6.el9.x86_64 67/77 Installing : gcc-toolset-12-libasan-devel-12.2.1-7.4.el9.x86_64 68/77 Installing : gcc-toolset-12-libatomic-devel-12.2.1-7.4.el9.x86_64 69/77 Installing : gcc-toolset-12-liblsan-devel-12.2.1-7.4.el9.x86_64 70/77 Installing : gcc-toolset-12-libstdc++-docs-12.2.1-7.4.el9.x86_64 71/77 Installing : gcc-toolset-12-libtsan-devel-12.2.1-7.4.el9.x86_64 72/77 Installing : gcc-toolset-12-libubsan-devel-12.2.1-7.4.el9.x86_64 73/77 Installing : gcc-toolset-12-offload-nvptx-12.2.1-7.4.el9.x86_64 74/77 Installing : gcc-gfortran-11.3.1-4.3.el9.x86_64 75/77 Installing : gcc-c++-11.3.1-4.3.el9.x86_64 76/77 Installing : gcc-offload-nvptx-11.3.1-4.3.el9.x86_64 77/77 Running scriptlet: gcc-offload-nvptx-11.3.1-4.3.el9.x86_64 77/77 Verifying : environment-modules-5.0.1-2.el9.x86_64 1/77 Verifying : libgfortran-11.3.1-4.3.el9.x86_64 2/77 Verifying : libquadmath-11.3.1-4.3.el9.x86_64 3/77 Verifying : make-1:4.3-7.el9.x86_64 4/77 Verifying : annobin-11.05-1.el9.x86_64 5/77 Verifying : boost-regex-1.75.0-8.el9.x86_64 6/77 Verifying : dwz-0.14-3.el9.x86_64 7/77 Verifying : efi-srpm-macros-6-2.el9_0.noarch 8/77 Verifying : fonts-srpm-macros-1:2.0.5-7.el9.1.noarch 9/77 Verifying : gcc-11.3.1-4.3.el9.x86_64 10/77 Verifying : gcc-c++-11.3.1-4.3.el9.x86_64 11/77 Verifying : gcc-gfortran-11.3.1-4.3.el9.x86_64 12/77 Verifying : gcc-offload-nvptx-11.3.1-4.3.el9.x86_64 13/77 Verifying : gcc-plugin-annobin-11.3.1-4.3.el9.x86_64 14/77 Verifying : gcc-toolset-12-12.0-6.el9.x86_64 15/77 Verifying : gcc-toolset-12-annobin-annocheck-11.08-2.el9.x86_64 16/77 Verifying : gcc-toolset-12-annobin-docs-11.08-2.el9.noarch 17/77 Verifying : gcc-toolset-12-annobin-plugin-gcc-11.08-2.el9.x86_64 18/77 Verifying : gcc-toolset-12-binutils-2.38-16.el9.x86_64 19/77 Verifying : gcc-toolset-12-binutils-devel-2.38-16.el9.x86_64 20/77 Verifying : gcc-toolset-12-binutils-gold-2.38-16.el9.x86_64 21/77 Verifying : gcc-toolset-12-build-12.0-6.el9.x86_64 22/77 Verifying : gcc-toolset-12-dwz-0.14-4.el9.x86_64 23/77 Verifying : gcc-toolset-12-gcc-12.2.1-7.4.el9.x86_64 24/77 Verifying : gcc-toolset-12-gcc-c++-12.2.1-7.4.el9.x86_64 25/77 Verifying : gcc-toolset-12-gcc-gfortran-12.2.1-7.4.el9.x86_64 26/77 Verifying : gcc-toolset-12-gcc-plugin-annobin-12.2.1-7.4.el9.x86_64 27/77 Verifying : gcc-toolset-12-gcc-plugin-devel-12.2.1-7.4.el9.x86_64 28/77 Verifying : gcc-toolset-12-gdb-11.2-4.el9.x86_64 29/77 Verifying : gcc-toolset-12-libasan-devel-12.2.1-7.4.el9.x86_64 30/77 Verifying : gcc-toolset-12-libatomic-devel-12.2.1-7.4.el9.x86_64 31/77 Verifying : gcc-toolset-12-libgccjit-12.2.1-7.4.el9.x86_64 32/77 Verifying : gcc-toolset-12-libgccjit-devel-12.2.1-7.4.el9.x86_64 33/77 Verifying : gcc-toolset-12-libgccjit-docs-12.2.1-7.4.el9.x86_64 34/77 Verifying : gcc-toolset-12-libitm-devel-12.2.1-7.4.el9.x86_64 35/77 Verifying : gcc-toolset-12-liblsan-devel-12.2.1-7.4.el9.x86_64 36/77 Verifying : gcc-toolset-12-libquadmath-devel-12.2.1-7.4.el9.x86_64 37/77 Verifying : gcc-toolset-12-libstdc++-devel-12.2.1-7.4.el9.x86_64 38/77 Verifying : gcc-toolset-12-libstdc++-docs-12.2.1-7.4.el9.x86_64 39/77 Verifying : gcc-toolset-12-libtsan-devel-12.2.1-7.4.el9.x86_64 40/77 Verifying : gcc-toolset-12-libubsan-devel-12.2.1-7.4.el9.x86_64 41/77 Verifying : gcc-toolset-12-offload-nvptx-12.2.1-7.4.el9.x86_64 42/77 Verifying : gcc-toolset-12-runtime-12.0-6.el9.x86_64 43/77 Verifying : ghc-srpm-macros-1.5.0-6.el9.noarch 44/77 Verifying : glibc-devel-2.34-60.el9.x86_64 45/77 Verifying : glibc-headers-2.34-60.el9.x86_64 46/77 Verifying : gmp-c++-1:6.2.0-10.el9.x86_64 47/77 Verifying : gmp-devel-1:6.2.0-10.el9.x86_64 48/77 Verifying : go-srpm-macros-3.2.0-1.el9.noarch 49/77 Verifying : kernel-headers-5.14.0-284.11.1.el9_2.x86_64 50/77 Verifying : kernel-srpm-macros-1.0-12.el9.noarch 51/77 Verifying : libasan8-12.2.1-7.4.el9.x86_64 52/77 Verifying : libbabeltrace-1.5.8-10.el9.x86_64 53/77 Verifying : libgomp-offload-nvptx-11.3.1-4.3.el9.x86_64 54/77 Verifying : libipt-2.0.4-5.el9.x86_64 55/77 Verifying : libitm-11.3.1-4.3.el9.x86_64 56/77 Verifying : liblsan-11.3.1-4.3.el9.x86_64 57/77 Verifying : libmpc-devel-1.2.1-4.el9.x86_64 58/77 Verifying : libquadmath-devel-11.3.1-4.3.el9.x86_64 59/77 Verifying : libstdc++-devel-11.3.1-4.3.el9.x86_64 60/77 Verifying : libtsan2-12.2.1-7.4.el9.x86_64 61/77 Verifying : libubsan-11.3.1-4.3.el9.x86_64 62/77 Verifying : libxcrypt-devel-4.4.18-3.el9.x86_64 63/77 Verifying : lua-srpm-macros-1-6.el9.noarch 64/77 Verifying : mpfr-devel-4.1.0-7.el9.x86_64 65/77 Verifying : ocaml-srpm-macros-6-6.el9.noarch 66/77 Verifying : openblas-srpm-macros-2-11.el9.noarch 67/77 Verifying : perl-srpm-macros-1-41.el9.noarch 68/77 Verifying : pyproject-srpm-macros-1.6.2-1.el9.noarch 69/77 Verifying : python-srpm-macros-3.9-52.el9.noarch 70/77 Verifying : qt5-srpm-macros-5.15.3-1.el9.noarch 71/77 Verifying : redhat-rpm-config-199-1.el9.noarch 72/77 Verifying : rust-srpm-macros-17-4.el9.noarch 73/77 Verifying : scl-utils-1:2.0.3-4.el9.x86_64 74/77 Verifying : scl-utils-build-1:2.0.3-4.el9.x86_64 75/77 Verifying : source-highlight-3.1.9-11.el9.x86_64 76/77 Verifying : zlib-devel-1.2.11-39.el9.x86_64 77/77 Installed products updated. Installed: annobin-11.05-1.el9.x86_64 boost-regex-1.75.0-8.el9.x86_64 dwz-0.14-3.el9.x86_64 efi-srpm-macros-6-2.el9_0.noarch environment-modules-5.0.1-2.el9.x86_64 fonts-srpm-macros-1:2.0.5-7.el9.1.noarch gcc-11.3.1-4.3.el9.x86_64 gcc-c++-11.3.1-4.3.el9.x86_64 gcc-gfortran-11.3.1-4.3.el9.x86_64 gcc-offload-nvptx-11.3.1-4.3.el9.x86_64 gcc-plugin-annobin-11.3.1-4.3.el9.x86_64 gcc-toolset-12-12.0-6.el9.x86_64 gcc-toolset-12-annobin-annocheck-11.08-2.el9.x86_64 gcc-toolset-12-annobin-docs-11.08-2.el9.noarch gcc-toolset-12-annobin-plugin-gcc-11.08-2.el9.x86_64 gcc-toolset-12-binutils-2.38-16.el9.x86_64 gcc-toolset-12-binutils-devel-2.38-16.el9.x86_64 gcc-toolset-12-binutils-gold-2.38-16.el9.x86_64 gcc-toolset-12-build-12.0-6.el9.x86_64 gcc-toolset-12-dwz-0.14-4.el9.x86_64 gcc-toolset-12-gcc-12.2.1-7.4.el9.x86_64 gcc-toolset-12-gcc-c++-12.2.1-7.4.el9.x86_64 gcc-toolset-12-gcc-gfortran-12.2.1-7.4.el9.x86_64 gcc-toolset-12-gcc-plugin-annobin-12.2.1-7.4.el9.x86_64 gcc-toolset-12-gcc-plugin-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-gdb-11.2-4.el9.x86_64 gcc-toolset-12-libasan-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libatomic-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libgccjit-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libgccjit-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libgccjit-docs-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libitm-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-liblsan-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libquadmath-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libstdc++-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libstdc++-docs-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libtsan-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-libubsan-devel-12.2.1-7.4.el9.x86_64 gcc-toolset-12-offload-nvptx-12.2.1-7.4.el9.x86_64 gcc-toolset-12-runtime-12.0-6.el9.x86_64 ghc-srpm-macros-1.5.0-6.el9.noarch glibc-devel-2.34-60.el9.x86_64 glibc-headers-2.34-60.el9.x86_64 gmp-c++-1:6.2.0-10.el9.x86_64 gmp-devel-1:6.2.0-10.el9.x86_64 go-srpm-macros-3.2.0-1.el9.noarch kernel-headers-5.14.0-284.11.1.el9_2.x86_64 kernel-srpm-macros-1.0-12.el9.noarch libasan8-12.2.1-7.4.el9.x86_64 libbabeltrace-1.5.8-10.el9.x86_64 libgfortran-11.3.1-4.3.el9.x86_64 libgomp-offload-nvptx-11.3.1-4.3.el9.x86_64 libipt-2.0.4-5.el9.x86_64 libitm-11.3.1-4.3.el9.x86_64 liblsan-11.3.1-4.3.el9.x86_64 libmpc-devel-1.2.1-4.el9.x86_64 libquadmath-11.3.1-4.3.el9.x86_64 libquadmath-devel-11.3.1-4.3.el9.x86_64 libstdc++-devel-11.3.1-4.3.el9.x86_64 libtsan2-12.2.1-7.4.el9.x86_64 libubsan-11.3.1-4.3.el9.x86_64 libxcrypt-devel-4.4.18-3.el9.x86_64 lua-srpm-macros-1-6.el9.noarch make-1:4.3-7.el9.x86_64 mpfr-devel-4.1.0-7.el9.x86_64 ocaml-srpm-macros-6-6.el9.noarch openblas-srpm-macros-2-11.el9.noarch perl-srpm-macros-1-41.el9.noarch pyproject-srpm-macros-1.6.2-1.el9.noarch python-srpm-macros-3.9-52.el9.noarch qt5-srpm-macros-5.15.3-1.el9.noarch redhat-rpm-config-199-1.el9.noarch rust-srpm-macros-17-4.el9.noarch scl-utils-1:2.0.3-4.el9.x86_64 scl-utils-build-1:2.0.3-4.el9.x86_64 source-highlight-3.1.9-11.el9.x86_64 zlib-devel-1.2.11-39.el9.x86_64 Complete!
看到以上的信息表示 c 语言的编译环境就已经安装好了。
源码安装软件
安装 tree-2.0.4 软件:
[root@jock yum.repos.d]# cd /test/tree-2.0.4/ [root@jock tree-2.0.4]# ll total 204 -rw-r--r--. 1 4437 users 18101 Sep 6 2022 CHANGES -rw-r--r--. 1 4437 users 15968 Jun 16 2022 color.c drwxr-xr-x. 2 root root 71 Feb 22 15:45 doc -rw-r--r--. 1 4437 users 5241 Jun 16 2022 file.c -rw-r--r--. 1 4437 users 4531 Aug 27 2022 filter.c -rw-r--r--. 1 4437 users 3118 Jun 16 2022 hash.c -rw-r--r--. 1 4437 users 6283 Jun 16 2022 html.c -rw-r--r--. 1 4437 users 4610 Jun 16 2022 info.c -rw-r--r--. 1 4437 users 597 Jan 4 2018 INSTALL -rw-r--r--. 1 4437 users 5877 Aug 30 2022 json.c -rw-r--r--. 1 4437 users 18009 Aug 13 2004 LICENSE -rw-r--r--. 1 4437 users 7009 Aug 27 2022 list.c -rw-r--r--. 1 4437 users 2918 Sep 6 2022 Makefile -rw-r--r--. 1 4437 users 8372 Aug 27 2022 README -rw-r--r--. 1 4437 users 5346 Apr 14 2021 strverscmp.c -rw-r--r--. 1 4437 users 853 Dec 21 2021 TODO -rw-r--r--. 1 4437 users 38706 Sep 5 2022 tree.c -rw-r--r--. 1 4437 users 8646 Jun 16 2022 tree.h -rw-r--r--. 1 4437 users 3166 Jun 16 2022 unix.c -rw-r--r--. 1 4437 users 5207 Aug 27 2022 xml.c # 在解压目录中执行 make 命令来进行编译 [root@jock tree-2.0.4]# make gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o tree.o tree.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o list.o list.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o hash.o hash.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o color.o color.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o file.o file.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o filter.o filter.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o info.o info.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o unix.o unix.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o xml.o xml.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o json.o json.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o html.o html.c gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o strverscmp.o strverscmp.c gcc -o tree tree.o list.o hash.o color.o file.o filter.o info.o unix.o xml.o json.o html.o strverscmp.o # 在解压目录中执行 make install 命令来进行安装 [root@jock tree-2.0.4]# make install install -d /usr/local/bin install -d /usr/local/man/man1 install tree /usr/local/bin/tree; \ install -m 644 doc/tree.1 /usr/local/man/man1/tree.1 # 清空 hash 表 [root@jock tree-2.0.4]# hash -r # 验证安装 [root@jock ~]# cd /test [root@jock test]# tree . ├── tree-2.0.4 │ ├── CHANGES │ ├── color.c │ ├── color.o │ ├── doc │ │ ├── global_info │ │ ├── tree.1 │ │ ├── tree.fr.1 │ │ └── xml.dtd │ ├── file.c │ ├── file.o │ ├── filter.c │ ├── filter.o │ ├── hash.c │ ├── hash.o │ ├── html.c │ ├── html.o │ ├── info.c │ ├── info.o │ ├── INSTALL │ ├── json.c
从上面的结果可以发现:源码安装 tree 命令是成功的。
源码安装总结
-
需要有 c 语言编译环境
1. 配置本地仓库,需要在 /etc/yum.repos.d 目录下新建一个创建文件,如: base.repo,并在这个文件中配置 BaseOS 和 AppStream 路径 2. 挂载光盘 3. 安装环境,执行 dnf install -y make gcc*
-
源码安装
1. 在解压目录下执行 make 命令来进行编译 2. 在解压目录下执行 make install 命令来进行安装
9.1.2 rpm包安装
RPM是红帽包管理(Redhat Package Manager)的缩写,由Red Hat公司提出的一种软件包管理标准,是Linux各发行版中应用最广泛的软件包格式之一。
RPM软件包的典型命名格式:软件名-版本号-发行号.操作系统版本.硬件平台的类型.rpm
,比如 zsh-5.0.2-14.el7.x86_64.rpm
zsh | -5 | 0 | 2 | 14 | el7 | x86 | 64 |
---|---|---|---|---|---|---|---|
软件名 | 主版本号 | 次版本号 | 修订号 | 软件发布次数 | 发行商RHEL7 | CPU架构平台 | 支持系统位数 |
命令执行格式:
rpm -i install # 安装 -e erase # 删除 -u update # 更新 -q query # 查询 --nodeps # 不验证软件包依赖 -v, --verbose # 提供更多的详细信息输出
下面我们以安装 qq 软件为例来演示 rpm 包软件的操作:
[root@jock test]# cd ~ # 下载 qq 软件安装包 [root@jock ~]# curl https://dldir1.qq.com/qqfile/qq/QQNT/ad5b5393/linuxqq_3.1.2-13107_x86_64.rpm -o ./linuxqq_3.1.2-13107_x86_64.rpm # 查看 [root@jock ~]# ll total 122204 -rw-r--r--. 1 root root 125136548 Feb 22 16:22 linuxqq_3.1.2-13107_x86_64.rpm # 安装 [root@jock ~]# rpm -ivh linuxqq_3.1.2-13107_x86_64.rpm error: Failed dependencies: libXScrnSaver is needed by linuxqq-3.1.2_13107-1.x86_64 # 安装时报错了,说安装 linuxqq-3.1.2_13107-1.x86_64 软件需要依赖 libXScrnSaver # 我们从本地仓库中去搜索所依赖的 libXScrnSaver 软件所在位置 [root@jock ~]# find /mnt -name libXScrnSaver [root@jock ~]# find /mnt/AppStream/Packages/ -name "libXScrnSaver*" /mnt/AppStream/Packages/libXScrnSaver-1.2.3-10.el9.i686.rpm /mnt/AppStream/Packages/libXScrnSaver-1.2.3-10.el9.x86_64.rpm /mnt/AppStream/Packages/libXScrnSaver-devel-1.2.3-10.el9.i686.rpm /mnt/AppStream/Packages/libXScrnSaver-devel-1.2.3-10.el9.x86_64.rpm # 安装所需要的依赖 [root@jock ~]# rpm -ivh /mnt/AppStream/Packages/libXScrnSaver-1.2.3-10.el9.x86_64.rpm warning: /mnt/AppStream/Packages/libXScrnSaver-1.2.3-10.el9.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:libXScrnSaver-1.2.3-10.el9 ################################# [100%] # 当依赖的软件安装好后,我们再来安装 qq [root@jock ~]# rpm -ivh linuxqq_3.1.2-13107_x86_64.rpm Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:linuxqq-3.1.2_13107-1 ################################# [100%] # 此时,QQ 就已经安装成功了 # 搜索qq安装的位置 [root@jock ~]# rpm -ql linuxqq /opt/QQ/LICENSE.electron.txt /opt/QQ/LICENSES.chromium.html /opt/QQ/chrome-sandbox /opt/QQ/chrome_100_percent.pak /opt/QQ/chrome_200_percent.pak /opt/QQ/chrome_crashpad_handler /opt/QQ/icudtl.dat /opt/QQ/libEGL.so /opt/QQ/libGLESv2.so /opt/QQ/libffmpeg.so ....... # 卸载QQ [root@jock ~]# rpm -evh linuxqq Preparing... ################################# [100%] Cleaning up / removing... 1:linuxqq-3.1.2_13107-1 ################################# [100%]
下面再次演示安装一个在屏幕上显示字母的软件:
wget https://rpmfind.net/linux/fedora/linux/releases/38/Everything/x86_64/os/Packages/c/cmatrix-2.0-5.fc38.x86_64.rpm # 安装cmatrix软件 [root@jock ~]# rpm -ivh cmatrix-2.0-5.fc38.x86_64.rpm warning: cmatrix-2.0-5.fc38.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID eb10b464: NOKEY Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:cmatrix-2.0-5.fc38 ################################# [100%] # 使用软件 [root@jock ~]# cmatrix [root@jock ~]# cmatrix -C red [root@jock ~]# cmatrix -C blue # 删除 [root@jock ~]# rpm -evh cmatrix Preparing... ################################# [100%] Cleaning up / removing... 1:cmatrix-2.0-5.fc38 ################################# [100%]
通过上面两个软件的安装,我发现一个问题?
有些 rpm 包安装时比较方便,一下子就安装好了;而另一个软件安装时会有依赖,我们必须先把依赖的软件安装好后,才能安装我们真正要使用的软件。这就很麻烦。
9.1.3 yum/dnf安装
YUM (Yellow dog Updater,Modified) ,RHEL8中默认使用的软件批量管理工具由原版本的yum换成了速度更快的dnf(DNF = Dandified YUM),原有的yum命令仅为dnf的软链接,当然依旧可以使用。
[root@jock ~]# ll `which yum` lrwxrwxrwx. 1 root root 5 Mar 15 2023 /usr/bin/yum -> dnf-3
这个命令可以实现下载、安装、卸载、更新。而且它还可以自动去搜索被安装的软件所需要的依赖,并一起下载安装。
下面我们以安装 httpd 服务来演示如何通过 yum/dnf 来安装所需要的软件。
# 1. 确保本地仓库或网络仓库可用 # 2. 安装 httpd 服务软件 [root@jock ~]# dnf install httpd Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. Last metadata expiration check: 0:40:19 ago on Fri 23 Feb 2024 09:24:03 AM CST. Dependencies resolved. ============================================================================================================================================================ Package Architecture Version Repository Size ============================================================================================================================================================ Installing: httpd x86_64 2.4.53-11.el9_2.4 app 54 k Installing dependencies: apr x86_64 1.7.0-11.el9 app 127 k apr-util x86_64 1.6.1-20.el9 app 98 k apr-util-bdb x86_64 1.6.1-20.el9 app 15 k httpd-core x86_64 2.4.53-11.el9_2.4 app 1.5 M httpd-filesystem noarch 2.4.53-11.el9_2.4 app 17 k httpd-tools x86_64 2.4.53-11.el9_2.4 app 88 k redhat-logos-httpd noarch 90.4-1.el9 app 18 k Installing weak dependencies: apr-util-openssl x86_64 1.6.1-20.el9 app 17 k mod_http2 x86_64 1.15.19-4.el9_2.4 app 153 k mod_lua x86_64 2.4.53-11.el9_2.4 app 63 k Transaction Summary ============================================================================================================================================================ Install 11 Packages Total size: 2.1 M Installed size: 5.9 M Is this ok [y/N]: # 在此处输入 y 来安装 Is this ok [y/N]: y Downloading Packages: Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : apr-1.7.0-11.el9.x86_64 1/11 Installing : apr-util-bdb-1.6.1-20.el9.x86_64 2/11 Installing : apr-util-openssl-1.6.1-20.el9.x86_64 3/11 Installing : apr-util-1.6.1-20.el9.x86_64 4/11 Installing : httpd-tools-2.4.53-11.el9_2.4.x86_64 5/11 Installing : redhat-logos-httpd-90.4-1.el9.noarch 6/11 Running scriptlet: httpd-filesystem-2.4.53-11.el9_2.4.noarch 7/11 Installing : httpd-filesystem-2.4.53-11.el9_2.4.noarch 7/11 Installing : httpd-core-2.4.53-11.el9_2.4.x86_64 8/11 Installing : mod_http2-1.15.19-4.el9_2.4.x86_64 9/11 Installing : mod_lua-2.4.53-11.el9_2.4.x86_64 10/11 Installing : httpd-2.4.53-11.el9_2.4.x86_64 11/11 Running scriptlet: httpd-2.4.53-11.el9_2.4.x86_64 11/11 Verifying : apr-1.7.0-11.el9.x86_64 1/11 Verifying : apr-util-1.6.1-20.el9.x86_64 2/11 Verifying : apr-util-bdb-1.6.1-20.el9.x86_64 3/11 Verifying : apr-util-openssl-1.6.1-20.el9.x86_64 4/11 Verifying : httpd-2.4.53-11.el9_2.4.x86_64 5/11 Verifying : httpd-core-2.4.53-11.el9_2.4.x86_64 6/11 Verifying : httpd-filesystem-2.4.53-11.el9_2.4.noarch 7/11 Verifying : httpd-tools-2.4.53-11.el9_2.4.x86_64 8/11 Verifying : mod_http2-1.15.19-4.el9_2.4.x86_64 9/11 Verifying : mod_lua-2.4.53-11.el9_2.4.x86_64 10/11 Verifying : redhat-logos-httpd-90.4-1.el9.noarch 11/11 Installed products updated. Installed: apr-1.7.0-11.el9.x86_64 apr-util-1.6.1-20.el9.x86_64 apr-util-bdb-1.6.1-20.el9.x86_64 apr-util-openssl-1.6.1-20.el9.x86_64 httpd-2.4.53-11.el9_2.4.x86_64 httpd-core-2.4.53-11.el9_2.4.x86_64 httpd-filesystem-2.4.53-11.el9_2.4.noarch httpd-tools-2.4.53-11.el9_2.4.x86_64 mod_http2-1.15.19-4.el9_2.4.x86_64 mod_lua-2.4.53-11.el9_2.4.x86_64 redhat-logos-httpd-90.4-1.el9.noarch Complete! # 安装好后,我们来启动 httpd 服务 [root@jock ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
服务启动好后,我们打开浏览器,在地址栏中输入 http://192.168.72.120 发现不能显示页面,原因是我们的防火墙是处于运行中。我们可以执行如下的命令来查看防火墙的状态:
[root@jock ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; preset: enabled) Active: active (running) since Fri 2024-02-23 09:00:07 CST; 1h 7min ago Docs: man:firewalld(1) Main PID: 997 (firewalld) Tasks: 2 (limit: 48630) Memory: 41.8M CPU: 655ms CGroup: /system.slice/firewalld.service └─997 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid Feb 23 09:00:07 jock systemd[1]: Starting firewalld - dynamic firewall daemon... Feb 23 09:00:07 jock systemd[1]: Started firewalld - dynamic firewall daemon.
从这个状态上可以发现,目前防火墙是开启并运行的。我们需要执行如下的命令来关闭防火墙:
[root@jock ~]# systemctl stop firewalld [root@jock ~]# systemctl status firewalld ○ firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; preset: enabled) Active: inactive (dead) since Fri 2024-02-23 10:09:33 CST; 4s ago Duration: 1h 9min 25.928s Docs: man:firewalld(1) Process: 997 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS) Main PID: 997 (code=exited, status=0/SUCCESS) CPU: 716ms Feb 23 09:00:07 jock systemd[1]: Starting firewalld - dynamic firewall daemon... Feb 23 09:00:07 jock systemd[1]: Started firewalld - dynamic firewall daemon. Feb 23 10:09:33 jock systemd[1]: Stopping firewalld - dynamic firewall daemon... Feb 23 10:09:33 jock systemd[1]: firewalld.service: Deactivated successfully. Feb 23 10:09:33 jock systemd[1]: Stopped firewalld - dynamic firewall daemon.
此时,防火墙已经关闭了,我们再次刷新浏览器来查看。
此时,我们发现可以看到一个测试页面了,说明服务是 OK 的。如果大家在做这个实验时还看不到,那么我们就需要执行如下的命令来查看 selinux 的是否关闭。
[root@jock ~]# getenforce Enforcing
然后就可以执行如下的命令来关闭 enfore
[root@jock ~]# setenforce 0 [root@jock ~]# getenforce Permissive
执行完后,我们再次刷新浏览器就应该可以看到界面了。
我们还可以把显示的页面修改为我们自己的页面:
# 进入到页面所在目录 [root@jock ~]# cd /var/www/html/ [root@jock html]# ll total 0 # 创建一个首页页面文件并输入显示内容 [root@jock html]# echo welcome to http > index.html
配置本地源
在 /etc/yum.repos.d 目录下新建一个仓库文件,如 base.repo,然后在这个文件中添加如下内容:
[base] name=base baseurl=/mnt/BaseOS gpgcheck=0 [app] name=app baseurl=/mnt/AppStream gpgcheck=0
然后将光盘挂载到 /mnt 目录下:
[root@jock html]# mount /dev/sr0 /mnt
至此,本地仓库就建立好了。
配置网络源
建立网络源(Redhat9暂时无公开的网络源,暂不可配置,其他发行版本可自行选择对应网源仓库)
-
阿里云网络源,aliyun的镜像站点为阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区
-
中国科技大学镜像站点USTC Open Source Software Mirror
-
网易开源镜像站欢迎访问网易开源镜像站
查看系统内核版本信息:
[root@jock html]# cat /etc/os-release NAME="Red Hat Enterprise Linux" VERSION="9.2 (Plow)" ID="rhel" ID_LIKE="fedora" VERSION_ID="9.2" PLATFORM_ID="platform:el9" PRETTY_NAME="Red Hat Enterprise Linux 9.2 (Plow)" ANSI_COLOR="0;31" LOGO="fedora-logo-icon" CPE_NAME="cpe:/o:redhat:enterprise_linux:9::baseos" HOME_URL="https://www.redhat.com/" DOCUMENTATION_URL="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9" BUG_REPORT_URL="https://bugzilla.redhat.com/" REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 9" REDHAT_BUGZILLA_PRODUCT_VERSION=9.2 REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="9.2" [root@jock html]# cat /etc/redhat-release Red Hat Enterprise Linux release 9.2 (Plow)
下面我们来配置一个网络源(仓库),我们以阿里的网络源为例来进行演示:
首先我们在 /etc/yum.repos.d 目录下新建一个名为 aliyun.repo 仓库文件:
[root@jock yum.repos.d]# touch aliyun.repo [root@jock yum.repos.d]# ll total 8 -rw-r--r--. 1 root root 0 Feb 23 10:23 aliyun.repo -rw-r--r--. 1 root root 98 Feb 22 15:59 base.repo -rw-r--r--. 1 root root 358 Jan 25 15:05 redhat.repo
然后在这个文件中添加如下的内容:
[baseOS] name=baseOS baseurl=https://mirrors.aliyun.com/redhat/rhel/rhel-9-beta/baseos/x86_64/ gpgcheck=0 [appStream] name=appStream baseurl=https://mirrors.aliyun.com/redhat/rhel/rhel-9-beta/appstream/x86_64/ gpgcheck=0
下面配置一个网易网络仓库:
[tunaBaseOS] name=tunaBaseOS baseurl=http://mirrors.163.com/rocky/9.3/BaseOS/x86_64/os/ gpgcheck=0 [tunaAppStream] name=tunaAppStream baseurl=http://mirrors.163.com/rocky/9.3/AppStream/x86_64/os/ gpgcheck=0
配置好后,我们可以来测试一个软件,例如安装 bind 软件:
[root@jock yum.repos.d]# dnf install -y bind Updating Subscription Management repositories. Unable to read consumer identity This system is not registered with an entitlement server. You can use subscription-manager to register. aliyuBaseOS 1.3 MB/s | 1.0 MB 00:00 aliyuAppStream 7.3 MB/s | 5.6 MB 00:00 tunaBaseOS 915 kB/s | 2.2 MB 00:02 tunaAppStream 569 kB/s | 7.4 MB 00:13 Dependencies resolved. ====================================================================================================================== Package Architecture Version Repository Size ====================================================================================================================== Installing: bind x86_64 32:9.16.23-14.el9_3.0.1 tunaAppStream 487 k Upgrading: bind-libs x86_64 32:9.16.23-14.el9_3.0.1 tunaAppStream 1.2 M bind-license noarch 32:9.16.23-14.el9_3.0.1 tunaAppStream 12 k bind-utils x86_64 32:9.16.23-14.el9_3.0.1 tunaAppStream 199 k Installing dependencies: bind-dnssec-doc noarch 32:9.16.23-14.el9_3.0.1 tunaAppStream 44 k python3-bind noarch 32:9.16.23-14.el9_3.0.1 tunaAppStream 60 k python3-ply noarch 3.11-14.el9.0.1 tunaBaseOS 103 k Installing weak dependencies: bind-dnssec-utils x86_64 32:9.16.23-14.el9_3.0.1 tunaAppStream 112 k Transaction Summary ====================================================================================================================== Install 5 Packages Upgrade 3 Packages Total download size: 2.2 M Downloading Packages: (1/8): python3-ply-3.11-14.el9.0.1.noarch.rpm 222 kB/s | 103 kB 00:00 (2/8): python3-bind-9.16.23-14.el9_3.0.1.noarch.rpm 206 kB/s | 60 kB 00:00 (3/8): bind-dnssec-utils-9.16.23-14.el9_3.0.1.x86_64.rpm 128 kB/s | 112 kB 00:00 (4/8): bind-dnssec-doc-9.16.23-14.el9_3.0.1.noarch.rpm 196 kB/s | 44 kB 00:00 (5/8): bind-9.16.23-14.el9_3.0.1.x86_64.rpm 368 kB/s | 487 kB 00:01 (6/8): bind-license-9.16.23-14.el9_3.0.1.noarch.rpm 86 kB/s | 12 kB 00:00 (7/8): bind-utils-9.16.23-14.el9_3.0.1.x86_64.rpm 297 kB/s | 199 kB 00:00 (8/8): bind-libs-9.16.23-14.el9_3.0.1.x86_64.rpm 450 kB/s | 1.2 MB 00:02 ---------------------------------------------------------------------------------------------------------------------- Total 601 kB/s | 2.2 MB 00:03 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Upgrading : bind-license-32:9.16.23-14.el9_3.0.1.noarch 1/11 Upgrading : bind-libs-32:9.16.23-14.el9_3.0.1.x86_64 2/11 Upgrading : bind-utils-32:9.16.23-14.el9_3.0.1.x86_64 3/11 Installing : bind-dnssec-doc-32:9.16.23-14.el9_3.0.1.noarch 4/11 Installing : python3-ply-3.11-14.el9.0.1.noarch 5/11 Installing : python3-bind-32:9.16.23-14.el9_3.0.1.noarch 6/11 Installing : bind-dnssec-utils-32:9.16.23-14.el9_3.0.1.x86_64 7/11 Running scriptlet: bind-32:9.16.23-14.el9_3.0.1.x86_64 8/11 Installing : bind-32:9.16.23-14.el9_3.0.1.x86_64 8/11 Running scriptlet: bind-32:9.16.23-14.el9_3.0.1.x86_64 8/11 Cleanup : bind-utils-32:9.16.23-11.el9.x86_64 9/11 Cleanup : bind-libs-32:9.16.23-11.el9.x86_64 10/11 Cleanup : bind-license-32:9.16.23-11.el9.noarch 11/11 Running scriptlet: bind-license-32:9.16.23-11.el9.noarch 11/11 Verifying : python3-ply-3.11-14.el9.0.1.noarch 1/11 Verifying : bind-dnssec-utils-32:9.16.23-14.el9_3.0.1.x86_64 2/11 Verifying : bind-32:9.16.23-14.el9_3.0.1.x86_64 3/11 Verifying : python3-bind-32:9.16.23-14.el9_3.0.1.noarch 4/11 Verifying : bind-dnssec-doc-32:9.16.23-14.el9_3.0.1.noarch 5/11 Verifying : bind-utils-32:9.16.23-14.el9_3.0.1.x86_64 6/11 Verifying : bind-utils-32:9.16.23-11.el9.x86_64 7/11 Verifying : bind-libs-32:9.16.23-14.el9_3.0.1.x86_64 8/11 Verifying : bind-libs-32:9.16.23-11.el9.x86_64 9/11 Verifying : bind-license-32:9.16.23-14.el9_3.0.1.noarch 10/11 Verifying : bind-license-32:9.16.23-11.el9.noarch 11/11 Installed products updated. Upgraded: bind-libs-32:9.16.23-14.el9_3.0.1.x86_64 bind-license-32:9.16.23-14.el9_3.0.1.noarch bind-utils-32:9.16.23-14.el9_3.0.1.x86_64 Installed: bind-32:9.16.23-14.el9_3.0.1.x86_64 bind-dnssec-doc-32:9.16.23-14.el9_3.0.1.noarch bind-dnssec-utils-32:9.16.23-14.el9_3.0.1.x86_64 python3-bind-32:9.16.23-14.el9_3.0.1.noarch python3-ply-3.11-14.el9.0.1.noarch Complete!
可以发现,我们配置的 aliyun.repo 和 tuna.repo 都是生效的,说明配置是 OK 的。
删除所安装的软件
[root@jock yum.repos.d]# dnf remove bind # 删除 httpd 服务 [root@jock yum.repos.d]# dnf remove -y httpd # 查看端口占用情况 [root@jock yum.repos.d]# netstat -nltup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 5433/sshd: root@pts tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1154/sshd: /usr/sbi tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1150/cupsd tcp6 0 0 :::22 :::* LISTEN 1154/sshd: /usr/sbi tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 ::1:631 :::* LISTEN 1150/cupsd tcp6 0 0 ::1:6010 :::* LISTEN 5433/sshd: root@pts udp 0 0 0.0.0.0:44729 0.0.0.0:* 927/avahi-daemon: r udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd udp 0 0 127.0.0.1:323 0.0.0.0:* 984/chronyd udp 0 0 0.0.0.0:5353 0.0.0.0:* 927/avahi-daemon: r udp6 0 0 :::111 :::* 1/systemd udp6 0 0 ::1:323 :::* 984/chronyd udp6 0 0 :::5353 :::* 927/avahi-daemon: r udp6 0 0 :::38789 :::* 927/avahi-daemon: r [root@jock yum.repos.d]# netstat -nltup | grep 80
yum/dnf命令用法
yum repolist [all] # 查看系统中 yum 的资源配置信息 yum list all # 列出仓库里可安装的软件包 yum list installed [包名] # 列出已经安装的软件 yum info 包名 # 查看指定软件包的详细信息 yum grouplist # 查看仓库中所有包组 yum groupinfo 包组名 #查看指定包组的信息 yum install [-y] 包名 # 安装指定的包 yum update [-y] 包名 # 更新指定的包 yum groupinstall 组名 # 安装指定的组名包 yum remove [-y] 包名 # 删除指定的包名 yum provides 包名 # 查找指定的包名是由哪个提供的 yum search 包名 # 查看包名在哪个软件中 yum clean packages all # 清除下载到本机的指定的软件包或yum的缓存文件
9.2 进程管理
9.2.1 概念
程序: 二进制文件,文件存储在磁盘中,例如/usr/bin/目录下 进程:进程是已启动的可执行程序的运行实例。
-
进程和程序并不是一一对应的关系,相同的程序运行在不同的数据集上就是不同的进程
-
进程还具有并发性和交往性,而程序却是封闭的
线程:是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。
-
一个进程可以拥有多个线程,而一个线程同时只能被一个进程所拥有
-
线程不能单独执行,但是每一个线程都有程序的入口、执行序列以及程序出口,它必须组成进程才能被执行。
9.2.2 进程分类
9.2.3 运行程序
# 安装 httpd 包 [root@jock yum.repos.d]# dnf install -y httpd # 查看 httpd 的服务状态 [root@jock yum.repos.d]# systemctl status httpd ○ httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled) Active: inactive (dead) # 未启用状态 Docs: man:httpd.service(8) Feb 23 10:06:15 jock systemd[1]: Starting The Apache HTTP Server... Feb 23 10:06:15 jock httpd[6337]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na> Feb 23 10:06:15 jock httpd[6337]: Server configured, listening on: port 80 Feb 23 10:06:15 jock systemd[1]: Started The Apache HTTP Server. Feb 23 10:51:37 jock systemd[1]: Stopping The Apache HTTP Server... Feb 23 10:51:38 jock systemd[1]: httpd.service: Deactivated successfully. Feb 23 10:51:38 jock systemd[1]: Stopped The Apache HTTP Server. Feb 23 10:51:38 jock systemd[1]: httpd.service: Consumed 3.056s CPU time. # 启用 httpd 服务 [root@jock yum.repos.d]# systemctl start httpd # 查看服务的进程 [root@jock yum.repos.d]# netstat -lntup | grep httpd tcp6 0 0 :::80 :::* LISTEN 8148/httpd # 或者查看服务的状态 [root@jock yum.repos.d]# systemctl status httpd ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled) Active: active (running) since Fri 2024-02-23 11:27:34 CST; 44s ago # 启用状态 Docs: man:httpd.service(8) Main PID: 8148 (httpd) Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec" Tasks: 213 (limit: 48630) Memory: 37.1M CPU: 94ms CGroup: /system.slice/httpd.service ├─8148 /usr/sbin/httpd -DFOREGROUND ├─8151 /usr/sbin/httpd -DFOREGROUND ├─8152 /usr/sbin/httpd -DFOREGROUND ├─8153 /usr/sbin/httpd -DFOREGROUND └─8154 /usr/sbin/httpd -DFOREGROUND Feb 23 11:27:34 jock systemd[1]: Starting The Apache HTTP Server... Feb 23 11:27:34 jock httpd[8148]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na> Feb 23 11:27:34 jock httpd[8148]: Server configured, listening on: port 80 Feb 23 11:27:34 jock systemd[1]: Started The Apache HTTP Server. # 停止httpd服务 [root@jock yum.repos.d]# systemctl stop httpd # 查看进程 [root@jock yum.repos.d]# netstat -lntup | grep httpd # 查看服务状态 [root@jock yum.repos.d]# systemctl status httpd ○ httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled) Active: inactive (dead) Docs: man:httpd.service(8) Feb 23 10:51:38 jock systemd[1]: httpd.service: Deactivated successfully. Feb 23 10:51:38 jock systemd[1]: Stopped The Apache HTTP Server. Feb 23 10:51:38 jock systemd[1]: httpd.service: Consumed 3.056s CPU time. Feb 23 11:27:34 jock systemd[1]: Starting The Apache HTTP Server... Feb 23 11:27:34 jock httpd[8148]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na> Feb 23 11:27:34 jock httpd[8148]: Server configured, listening on: port 80 Feb 23 11:27:34 jock systemd[1]: Started The Apache HTTP Server. Feb 23 11:29:43 jock systemd[1]: Stopping The Apache HTTP Server... Feb 23 11:29:44 jock systemd[1]: httpd.service: Deactivated successfully. Feb 23 11:29:44 jock systemd[1]: Stopped The Apache HTTP Server. # 重启 httpd 服务 [root@jock yum.repos.d]# systemctl restart httpd # 将 httpd 服务加入到开机启动列表,即系统一运行程序就运行 [root@jock yum.repos.d]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. # 将 httpd 服务从开机列表中删除 [root@jock yum.repos.d]# systemctl disable --now httpd Removed "/etc/systemd/system/multi-user.target.wants/httpd.service".
9.2.4 查看进程
9.2.4.1 ps
ps显示某个时间点的程序运行情况
-a :显示所有用户的进程 -u :显示用户名和启动时间 -x :显示没有控制终端的进程 -e :显示所有进程,包括没有控制终端的进程 -l :长格式显示 -w:宽行显示,可以使用多个 w 进行加宽显示 -f:做一个更完整的输出
使用示例:
# 查看当前用户的进程 [root@jock yum.repos.d]# ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 0 5442 5433 0 80 0 - 56188 do_wai pts/0 00:00:00 bash 4 R 0 8917 5442 0 80 0 - 56370 - pts/0 00:00:00 ps
命令结果说明: F:代表这个程序的标示(Flag),常见的值有:4表示此程序的权限为root;1表示此子程序仅进行复制(fork)而没有实际执行(exec) S:代表这个程序的状态(Stat),主要有如下的状态: - R(Running):该程序处于运行状态 - S(Sleep):该程序处理眨眼状态,但是可以唤醒 - D:该程序处于不可唤醒的状态 - T(Stop):该程序处于停止状态 - Z(Zombie):该程序处于僵尸状态 - + :表示它是一个进程组 UID/PID/PPID:代表用户的uid,进程pid和它的父id C:代码 CPU 的使用率,单位为百分比 PRI/NI:进程的优先级,值越少表示该程序越快被 CPU 执行 ADDR/SZ/WCHAN:它们是与内存相关的,ADDR 是内核,SZ 内存使用多少,WCHAN表示是否在运行中 TTY:表示终端信息 TIME:使用的 CPU 时间 CMD:触发此进程的指令是什么
# 查看所有用户执行的进程的详细信息 [root@jock yum.repos.d]# ps -le F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1 0 0 80 0 - 43879 ep_pol ? 00:00:02 systemd 1 S 0 2 0 0 80 0 - 0 kthrea ? 00:00:00 kthreadd 1 I 0 3 2 0 60 -20 - 0 rescue ? 00:00:00 rcu_gp 1 I 0 4 2 0 60 -20 - 0 rescue ? 00:00:00 rcu_par_gp 1 I 0 5 2 0 60 -20 - 0 rescue ? 00:00:00 slub_flushwq 1 I 0 6 2 0 60 -20 - 0 rescue ? 00:00:00 netns 1 I 0 8 2 0 60 -20 - 0 worker ? 00:00:00 kworker/0:0H-events_highpri 1 I 0 10 2 0 60 -20 - 0 worker ? 00:00:00 kworker/0:1H-events_highpri 1 I 0 11 2 0 60 -20 - 0 rescue ? 00:00:00 mm_percpu_wq 1 I 0 13 2 0 80 0 - 0 rcu_ta ? 00:00:00 rcu_tasks_kthre ..... # 查看当前用户的进程 [root@jock yum.repos.d]# ps -u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 5442 0.0 0.0 224752 6380 pts/0 Ss 09:00 0:00 -bash root 8921 0.0 0.0 225480 3552 pts/0 R+ 11:49 0:00 ps -u # 工作中使用的选项 [root@jock yum.repos.d]# ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 175516 19772 ? Ss 09:00 0:02 /usr/lib/systemd/systemd rhgb --switched-root --sys root 2 0.0 0.0 0 0 ? S 09:00 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 09:00 0:00 [rcu_gp] [root@jock yum.repos.d]# ps -aux | head USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 175516 19772 ? Ss 09:00 0:02 /usr/lib/systemd/systemd rhgb --switched-root --system --deserialize 31 root 2 0.0 0.0 0 0 ? S 09:00 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 09:00 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 09:00 0:00 [rcu_par_gp] root 5 0.0 0.0 0 0 ? I< 09:00 0:00 [slub_flushwq] root 6 0.0 0.0 0 0 ? I< 09:00 0:00 [netns] root 8 0.0 0.0 0 0 ? I< 09:00 0:00 [kworker/0:0H-events_highpri] root 10 0.0 0.0 0 0 ? I< 09:00 0:00 [kworker/0:1H-events_highpri] root 11 0.0 0.0 0 0 ? I< 09:00 0:00 [mm_percpu_wq] 字段说明: USER:进行进程的用户 PID:进程的ID %CPU:CPU的占用率 %MEM:内存占用率 VSZ:占用虚拟内存情况 RSS:占用实际内存情况,它是需要驻留内存 TTY:进程运行的终端 STAT:进程状态, S:可中断的睡眠 I:以线程运行方式运行 s:进程的领导者,即父进程 R:运行 D:不可中断的睡眠 T:停止 START:进程的启动时间 TIME:进程占用的CPU总时间 COMMAND:进程触发的命令 [root@jock yum.repos.d]# ps -ef | grep httpd root 8655 1 0 11:33 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 8656 8655 0 11:33 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 8657 8655 0 11:33 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 8658 8655 0 11:33 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 8659 8655 0 11:33 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND root 8929 5442 0 11:56 pts/0 00:00:00 grep --color=auto httpd # 查看指定进程的PID [root@jock yum.repos.d]# pgrep -l httpd 8655 httpd 8656 httpd 8657 httpd 8658 httpd 8659 httpd [root@jock yum.repos.d]# pidof httpd 8659 8658 8657 8656 8655
9.2.4.2 top
语法格式
这个命令也是查看进程的,它的语法格式如下:
[root@jock yum.repos.d]# man top top [-d 数字] | [-bnp] 常用的选项: -d 后面可以接秒数,就是整个进程界面更新的秒数,默认是5秒 -b 以批次的方式执行 top,还可以有更多的参数可用,通常会搭配数据流重定向来将批量处理的结果输出到指定的文件中 -n 与-b搭配,进行几次top的输出结果 -i 不显示闲置或者僵死的进程信息 -c 显示进程整个的命令路径,而不是只显示命令名称 -s 使top命令在安全模式下运行 -p 指定某些pid来进行查看
使用示例
# 通过 top 命令来查看进程,每隔山3秒钟进行就会刷新一次 [root@jock yum.repos.d]# top -d 3 top - 14:15:00 up 5:15, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 304 total, 1 running, 303 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.1 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.1 hi, 0.0 si, 0.0 st MiB Mem : 7661.0 total, 6096.9 free, 1097.0 used, 750.3 buff/cache MiB Swap: 5120.0 total, 5120.0 free, 0.0 used. 6564.0 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 944 root 20 0 529976 9064 7468 S 0.3 0.1 0:16.17 vmtoolsd 8658 apache 20 0 2258896 15024 5740 S 0.3 0.2 0:02.05 httpd
top的输出可以分为统计信息区和进程信息区两个部分,即前面5行属于统计信息区,而后面的内容属于进程信息区。
统计信息区
第一行为任务队列信息:top - 14:15:00 up 5:15, 1 user, load average: 0.00, 0.00, 0.00 14:15:00 执行此命令的当前系统时间 up 5:15 表示系统已经启动 5 小时 15 分钟 1 user 表示当前的登录用户数 load average: 0.00, 0.00, 0.00 表示系统1分钟,5分钟,15分钟前到现在的平均负载 第二行为任务信息:Tasks: 304 total, 1 running, 303 sleeping, 0 stopped, 0 zombie 304 total 进程总数 1 running 正在运行的进程数 303 sleeping 处于睡眠的进程数 0 stopped 处于停止的进程数 0 zombie 僵尸进程数 第三行是进程的占用信息:%Cpu(s): 0.0 us, 0.1 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.1 hi, 0.0 si, 0.0 st 0.0 us 表示用户进程占用CPU的百分比 0.1 sy 表示系统进程占用CPU的百分比 0.0 ni 用户进程空间内改变过优先级的进程占用CPU的百分比 99.8 id 空闲CPU占用百分比 0.0 wa 代表I/O等待输入/输出的进程占用CPU的百分比 0.1 hi 表示CPU服务于硬件中断所耗费的时间总额 0.0 si 表示CPU服务于软件中断所耗费的时间总额 0.0 st 表示虚拟机被hypervisor偷去的CPU时间 第四行内存使用情况:MiB Mem : 7661.0 total, 6096.9 free, 1097.0 used, 750.3 buff/cache 7661.0 total 内存总共大小 6096.9 free 内存可用大小 1097.0 used 内存已使用大小 750.3 buff/cache 缓存大小 第五行交换空间使用情况:MiB Swap: 5120.0 total, 5120.0 free, 0.0 used. 6564.0 avail Mem
进程信息区
在这个区域中显示的是每个进程的运行状态,这些状态的字段含义与 ps 命令执行后的含义相同。
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 944 root 20 0 529976 9064 7468 S 0.3 0.1 0:16.17 vmtoolsd PID 进行的id USER 进程所有者的用户名 PR 进程优先级 NI 它是nice值,负值表示高优先级,正值表示低优先级 VIRT 进程使用的虚拟内存总量,单位为KB RES 进程使用的物理内存大小,单位为KB SHR 共享内存大小,单位为KB S 进程状态,它有以下几个值 D 表示不可中断的睡眠状态 R 表示正在运行的状态 S 表示可中断的睡眠状态 T 表示跟踪/停止的状态 Z 表示僵尸进程状态 %CPU 上次更新到现在的CPU时间占用百分比 %MEM 进程占用的物理内存的百分比 TIME+ 进程总计使用的CPU时间,单位为1/100秒 COMMAND 正在运行的进程的命令名或命令路径
9.2.5 终止进程
终止进程我们可以使用 kill 命令,这个命令名我们可以使用 kill -l 来查看所以可列出的支持信号:
[root@jock yum.repos.d]# kill -l 1) SIGHUP 重新加载配置 2) SIGINT 键盘中断^C 3) SIGQUIT 键盘退出 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 强制终止 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 终止(正常结束),缺省信号 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 继续 19) SIGSTOP 停止 20) SIGTSTP 暂停^Z 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
在实际使用中,用得多的就是信号9,通常用它来强制终止某个进程。使用的格式为:
kill -9 进程ID 或者 killall # 来终止所有的进程 还可以使用 killall 程序名称 # 来终止指定程序
使用示例:
# 1. 在窗口A中执行vim命令 [root@jock yum.repos.d]# vim # 2. 在窗口B中查看vim进程 [root@jock ~]# ps -ef | grep vim root 9119 5442 0 14:50 pts/0 00:00:00 vim root 9121 9069 0 14:50 pts/1 00:00:00 grep --color=auto vim # 3. 在窗口B中终止vim进程 [root@jock ~]# killall vim # 4. 在窗口A中可发现vim进程已经退出 Vim: Caught deadly signal TERM Vim: Finished. Terminated # 根据PID值来终止进程 # 1. 启动服务 [root@jock ~]# systemctl start httpd # 2. 查看服务的PID值 [root@jock ~]# ps -ef | grep httpd root 8655 1 0 11:33 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 8656 8655 0 11:33 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 8657 8655 0 11:33 ? 00:00:03 /usr/sbin/httpd -DFOREGROUND apache 8658 8655 0 11:33 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND apache 8659 8655 0 11:33 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND root 9129 9069 0 14:54 pts/1 00:00:00 grep --color=auto httpd # 3. 根据PID值为终止进程 [root@jock ~]# kill -9 8655 # 4. 验证是否已经成功终止指定的进程 [root@jock ~]# ps -ef | grep httpd root 9134 9069 0 14:56 pts/1 00:00:00 grep --color=auto http
9.2.6 进程转换
进程转换通常是把前台进程转换为后台进程或者把后台进程转换为前台进程。
# 1. 前台进程转换为后台进程 [root@jock ~]# sleep 3000 adfa asdfa aasdf adfa [root@jock ~]# sleep 3000 & [1] 9138 [root@jock ~]# # 可以发现,通过在命令后带上 & 符号,就可以把前台进程转换为后台进程。 # 除此以外,我们还可以使用 ctrl+z 来将前台进程转换为后台进程 [root@jock yum.repos.d]# sleep 3000 ^Z [2]+ Stopped sleep 3000 [root@jock ~]# ps aux | grep sleep root 9138 0.0 0.0 220952 1020 pts/1 S 15:00 0:00 sleep 3000 root 9178 0.0 0.0 220952 1020 pts/0 S 15:04 0:00 sleep 3000 root 9181 0.0 0.0 220952 1020 pts/0 T 15:04 0:00 sleep 3000 root 9183 0.0 0.0 221664 2284 pts/1 S+ 15:05 0:00 grep --color=auto sleep
如果希望查看后台的进程,我们需要使用 jobs 命令
[root@jock yum.repos.d]# jobs -l [1]- 9178 Running sleep 3000 & [2]+ 9181 Stopped sleep 3000 + 表示最后被放到后台的进程 - 表示最后第二个被放到后台的进程
如果我们希望某个后台停止进程再次运行,我们需要使用 bg 命令,它的使用格式为:bg %编号
[root@jock yum.repos.d]# jobs -l [1]- 9178 Running sleep 3000 & [2]+ 9181 Stopped sleep 3000 # 让 2 号进程重新运行 [root@jock yum.repos.d]# bg %2 [2]+ sleep 3000 & # 再次查看 [root@jock yum.repos.d]# jobs -l [1]- 9178 Running sleep 3000 & [2]+ 9181 Running sleep 3000 &
如果希望将后台进程转换为前台进程则需要使用 fg 命令,它的使用格式为:fg %编号
[root@jock yum.repos.d]# jobs -l [1]- 9178 Running sleep 3000 & [2]+ 9181 Running sleep 3000 & # 将编号为2的后台进程转换为前台进程 [root@jock yum.repos.d]# fg %2 sleep 3000
如果希望终止后台进程,则通过 kill %编号
格式来实现:
[root@jock ~]# jobs -l [1]+ 9138 Running sleep 3000 & [root@jock ~]# kill %1 [root@jock ~]# jobs -l [1]+ 9138 Terminated sleep 3000