管理Linux软件包和进程

RPM管理工具

(1)RPM是红帽包管理(Redhat Package Manager)的缩写。
由Red Hat公司提出的一种软件包管理标准。
是Linux各发行版中应用最广泛的软件包格式之一(还有debian的发行版deb安装包)。
RPM功能通过rpm命令使用不同参数来实现。  
(2)rpm软件包中包含有关于本软件包的相关信息, 便于对软件包信息进行查询。RPM软件包的典型
其格式为:软件名-版本号-发行号.操作系统版本.硬件平台的类型.rpm 

(3)rpm包相关网站
 http://rpmfind.net/
 http://rpm.pbone.net/

(4)rpm安装

参数 说明
-i 安装软件包
-nodeps 不验证软件包依赖
-v 提供更多详细信息输出
-h 软件包安装时列出哈希标记
-U 软件包升级
-e

软件包卸载

从网上下载rpm包并安装,http://rpmfind.net/linux/epel/8/Everything/x86_64/Packages/s/sl-5.
02-1.el8.x86_64.rpm

[root@server ~]# wget http://rpmfind.net/linux/epel/8/Everything/x86_64/Packages/s/sl-5.
--2022-11-11 09:30:18--  http://rpmfind.net/linux/epel/8/Everything/x86_64/Packages/s/sl-5.
Resolving rpmfind.net (rpmfind.net)... 195.220.108.108
Connecting to rpmfind.net (rpmfind.net)|195.220.108.108|:80... connected.

(5)rpm查询功能(rpm –q 常与下面参数组合使用)

参数 说明
-a 查询所有已经安装的软件包
-l 显示已经安装软件包的相关文件列表和信息
-f 查询文件所属软件包
-i 显示已经安装的rpm软件包信息

 查询所有已经安装的包

[root@server ~]# rpm -qa
redhat-backgrounds-84.5-1.el8.noarch
libnftnl-1.1.5-4.el8.x86_64
python3-pycurl-7.43.0.2-4.el8.x86_64
polkit-libs-0.115-12.el8.x86_64
man-db-2.7.6.1-18.el8.x86_64
liberation-fonts-common-2.00.3-7.el8.noarch

查询安装该包后会产生哪些文件

[root@server ~]# rpm -ql tree
/usr/bin/tree
/usr/lib/.build-id
/usr/lib/.build-id/50
/usr/lib/.build-id/50/ae300877a586b9e7b87f19dfe58d303a887fb9
/usr/share/doc/tree
/usr/share/doc/tree/LICENSE
/usr/share/doc/tree/README
/usr/share/man/man1/tree.1.gz
[root@server ~]#

查询该文件由哪个软件包产生

[root@server ~]# rpm -qf /usr/bin/ls
coreutils-8.30-12.el8.x86_64
[root@server ~]#

(6)rpm包签名验证

系统中的数字证书

[root@server ~]# ll /etc/pki/rpm-gpg/
total 16
-rw-r--r--. 1 root root 1923 Oct  7  2021 ISV-Container-signing-key
-rw-r--r--. 1 root root 1669 Oct  7  2021 RPM-GPG-KEY-redhat-beta
-rw-r--r--. 1 root root 5135 Oct  7  2021 RPM-GPG-KEY-redhat-release

导入密钥

[root@server ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[root@server ~]# rpm -qa |grep gpg-pubke
gpg-pubkey-fd431d51-4ae0493b
gpg-pubkey-d4082792-5b32db75
[root@server ~]#

 YUM/DNF管理工具 

(1)YUM/DNF 功能
        在线下载、 安装、 卸载、 升级rpm软件包
        自动查找并解决rpm包之间的依赖关系,一次性安装所有具有依赖关系的rpm包,而无需管理员逐个、 手工地去安装每一个rpm包 

YUM、DNF位置及相关信息

[root@server mnt]# which yum
/usr/bin/yum
[root@server mnt]# which dnf
/usr/bin/dnf
[root@server mnt]# ll /usr/bin/{dnf,yum}
lrwxrwxrwx. 1 root root 5 Sep 13  2021 /usr/bin/dnf -> dnf-3
lrwxrwxrwx. 1 root root 5 Sep 13  2021 /usr/bin/yum -> dnf-3


(2)如何实现yum/DNF安装
        系统会去读取/etc/yum.repos.d/ 目录下的.repo 结尾的文件,从该文件中读取以下内容实现装包:包含各种rpm安装文件的软件仓库(rhel8有两个仓库,rhel7只有一个仓库)
        BaseOS存储库:提供一套核心的底层操作系统功能,为基础软件安装库
        AppStream存储库:包括额外的用户空间应用程序、运行时语言和数据库软件仓库的地址信息 
(3)配置.repo 文件,即yum源
建立本地源(光盘)

首先创建用于挂在光盘的文件

[root@server ~]# mkdir /root/mnt

挂载光盘

[root@server mnt]# mount /dev/sr0  /mnt
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@server mnt]# ll
total 48
dr-xr-xr-x. 4 root root  2048 Oct 13  2021 AppStream
dr-xr-xr-x. 4 root root  2048 Oct 13  2021 BaseOS
dr-xr-xr-x. 3 root root  2048 Oct 13  2021 EFI
-r--r--r--. 1 root root  8154 Oct 13  2021 EULA
-r--r--r--. 1 root root  1455 Oct 13  2021 extra_files.json
-r--r--r--. 1 root root 18092 Oct 13  2021 GPL
dr-xr-xr-x. 3 root root  2048 Oct 13  2021 images
dr-xr-xr-x. 2 root root  2048 Oct 13  2021 isolinux
-r--r--r--. 1 root root   103 Oct 13  2021 media.repo
-r--r--r--. 1 root root  1669 Oct 13  2021 RPM-GPG-KEY-redhat-beta
-r--r--r--. 1 root root  5135 Oct 13  2021 RPM-GPG-KEY-redhat-release
-r--r--r--. 1 root root  1796 Oct 13  2021 TRANS.TBL
[root@server mnt]#

 查看软件包的位置

[root@server mnt]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        364M     0  364M   0% /dev
tmpfs           392M     0  392M   0% /dev/shm
tmpfs           392M  6.3M  385M   2% /run
tmpfs           392M     0  392M   0% /sys/fs/cgroup
/dev/nvme0n1p2   30G  4.8G   26G  16% /
/dev/nvme0n1p1  495M  242M  254M  49% /boot
tmpfs            79M   36K   79M   1% /run/user/0
/dev/sr0         11G   11G     0 100% /mnt
[root@server mnt]#

 书写本地源位置

[root@server mnt]# cd /etc/yum.repos.d/
[root@server yum.repos.d]# vim bendi-csa.repo


[app]
name=appstream
baseurl=file:///mnt/AppStream
gpgcheck=0


[base]
name=baseos
baseurl=file:///mnt/BaseOS
gpgcheck=0


 装包

[root@server yum.repos.d]# yum 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:00:53 ago on Sat 12 Nov 2022 07:09:23 AM EST.
Dependencies resolved.


Complete!
[root@server yum.repos.d]#

查验是否装包成功

[root@server yum.repos.d]# yum install bind-utils -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.

Last metadata expiration check: 0:08:30 ago on Sat 12 Nov 2022 07:09:23 AM EST.
Package bind-utils-32:9.11.26-6.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@server yum.repos.d]#

建立网络源

阿里云网络源,aliyun的镜像站点为centos-vault-8.5.2111安装包下载_开源镜像站-阿里云 (aliyun.com)https://mirrors.aliyun.com/centos-vault/8.5.2111/

首先检查网络是否畅通

[root@server ~]# ping mirrors.aliyun.com
PING mirrors.aliyun.com.w.alikunlun.com (14.17.66.238) 56(84) bytes of data.
64 bytes from 14.17.66.238 (14.17.66.238): icmp_seq=1 ttl=128 time=549 ms
64 bytes from 14.17.66.238 (14.17.66.238): icmp_seq=2 ttl=128 time=459 ms
64 bytes from 14.17.66.238 (14.17.66.238): icmp_seq=3 ttl=128 time=436 ms
64 bytes from 14.17.66.238 (14.17.66.238): icmp_seq=4 ttl=128 time=365 ms
64 bytes from 14.17.66.238 (14.17.66.238): icmp_seq=5 ttl=128 time=288 ms

--- mirrors.aliyun.com.w.alikunlun.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 287.663/419.266/549.170/88.356 ms
[root@server ~]#

检查时间

[root@server ~]# date
Sat Nov 12 20:39:53 CST 2022
[root@server ~]#

编写配置文件

[root@server yum.repos.d]# vim aliyun.repo
[aliyun-app]
name=app
baseurl=https://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/x86_64/os/
gpgcheck=0

[base]
name=base
baseurl=https://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/x86_64/os/
gpgcheck=0

装包

[root@server yum.repos.d]# yum install httpd -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.

app                                                              133 kB/s | 8.4 MB     01:05
base                                                              58 kB/s | 4.6 MB     01:20
Last metadata expiration check: 0:00:01 ago on Sat 12 Nov 2022 09:02:16 PM CST.
Package httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64 is already installed.
Dependencies resolved.
=================================================================================================
 Package              Arch       Version                                    Repository      Size
=================================================================================================
Upgrading:
 httpd                x86_64     2.4.37-43.module_el8.5.0+1022+b541f3b1     aliyun-app     1.4 M
 httpd-filesystem     noarch     2.4.37-43.module_el8.5.0+1022+b541f3b1     aliyun-app      39 k
 httpd-tools          x86_64     2.4.37-43.module_el8.5.0+1022+b541f3b1     aliyun-app     107 k

Transaction Summary
=================================================================================================
Upgrade  3 Packages

Total download size: 1.5 M
Downloading Packages:
(1/3): httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.n  53 kB/s |  39 kB     00:00
(2/3): httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 118 kB/s | 107 kB     00:00
(3/3): httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64.rpm   476 kB/s | 1.4 MB     00:03
-------------------------------------------------------------------------------------------------
Total                                                            521 kB/s | 1.5 MB     00:03
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                         1/1
  Running scriptlet: httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64               1/1
  Upgrading        : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64               1/6
  Running scriptlet: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          2/6
  Upgrading        : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          2/6
  Upgrading        : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     3/6
  Running scriptlet: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     3/6
  Running scriptlet: httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64                    4/6
  Cleanup          : httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64                    4/6
  Running scriptlet: httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64                    4/6
  Cleanup          : httpd-filesystem-2.4.37-41.module+el8.5.0+11772+c8e0c271.noarch         5/6
  Cleanup          : httpd-tools-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64              6/6
  Running scriptlet: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     6/6
  Running scriptlet: httpd-tools-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64              6/6
  Verifying        : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     1/6
  Verifying        : httpd-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64                    2/6
  Verifying        : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          3/6
  Verifying        : httpd-filesystem-2.4.37-41.module+el8.5.0+11772+c8e0c271.noarch         4/6
  Verifying        : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64               5/6
  Verifying        : httpd-tools-2.4.37-41.module+el8.5.0+11772+c8e0c271.x86_64              6/6
Installed products updated.

Upgraded:
  httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
  httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
  httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64

Complete!
[root@server yum.repos.d]#

(4)yum/dnf工具管理软件包

查看系统中yum资源库配置信息

[root@server yum.repos.d]# yum repolist
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.

repo id                                             repo name
aliyun-app                                          app
base                                                base

列出仓库里所有可安装的软件包

[root@server yum.repos.d]# yum list all
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:10:56 ago on Sat 12 Nov 2022 09:02:16 PM CST.
Installed Packages
GConf2.x86_64                                3.2.6-22.el8                              @AppStream
ModemManager.x86_64                          1.10.8-4.el8                              @anaconda
ModemManager-glib.x86_64                     1.10.8-4.el8                              @anaconda
NetworkManager.x86_64                        1:1.32.10-4.el8                           @anaconda
NetworkManager-adsl.x86_64                   1:1.32.10-4.el8                           @anaconda
NetworkManager-bluetooth.x86_64              1:1.32.10-4.el8                           @anaconda
NetworkManager-config-server.noarch          1:1.32.10-4.el8                           @anaconda
NetworkManager-libnm.x86_64                  1:1.32.10-4.el8                           @anaconda
NetworkManager-team.x86_64                   1:1.32.10-4.el8                           @anaconda

列出己经安装的所有的或指定的rpm包

[root@server yum.repos.d]# yum list installed 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.

Installed Packages
httpd.x86_64                  2.4.37-43.module_el8.5.0+1022+b541f3b1                  @aliyun-app

查看指定软件包的详细信息

[root@server yum.repos.d]# yum info 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:12:33 ago on Sat 12 Nov 2022 09:02:16 PM CST.
Installed Packages
Name         : httpd
Version      : 2.4.37
Release      : 43.module_el8.5.0+1022+b541f3b1
Architecture : x86_64
Size         : 4.3 M
Source       : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.src.rpm
Repository   : @System
From repo    : aliyun-app
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

[root@server yum.repos.d]#

查看仓库里而所有包组

[root@server yum.repos.d]# yum grouplist
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:14:40 ago on Sat 12 Nov 2022 09:02:16 PM CST.
Available Environment Groups:
   Server
   Minimal Install
   Workstation
   Virtualization Host
   Custom Operating System
Installed Environment Groups:
   Server with GUI
Installed Groups:
   Container Management
   Headless Management
Available Groups:
   .NET Core Development
   RPM Development Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Network Servers
   Scientific Support
   Security Tools
   Smart Card Support
   System Tools
[root@server yum.repos.d]#

查看指定包组里而的详细信息

[root@server yum.repos.d]# yum groupinfo Server
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:15:44 ago on Sat 12 Nov 2022 09:02:16 PM CST.
Environment Group: Server
 Description: An integrated, easy-to-manage server.
 Mandatory Groups:
   Container Management
   Core
   Hardware Support
   Headless Management
   Server product core
   Standard
 Optional Groups:
   Basic Web Server
   DNS Name Server
   Debugging Tools
   FTP Server
   File and Storage Server
   GNOME
   Guest Agents
   Hardware Monitoring Utilities
   Infiniband Support
   Mail Server
   Network File System Client
   Network Servers
   Performance Tools
   Remote Management for Linux
   Virtualization Hypervisor
   Windows File Server
[root@server yum.repos.d]#

安装指定的软件包

[root@server yum.repos.d]# yum install httpd -y

升级指定的软件包或主机中所有已安装的软件包

[root@server yum.repos.d]# yum update httpd -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.

Last metadata expiration check: 0:17:00 ago on Sat 12 Nov 2022 09:02:16 PM CST.
Dependencies resolved.
Nothing to do.
Complete!
[root@server yum.repos.d]#

查找指定的文件属于哪个包

[root@server yum.repos.d]# yum provides httpd -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.

Last metadata expiration check: 0:17:28 ago on Sat 12 Nov 2022 09:02:16 PM CST.
httpd-2.4.37-41.module_el8.5.0+977+5653bbea.x86_64 : Apache HTTP Server
Repo        : aliyun-app
Matched from:
Provide    : httpd = 2.4.37-41.module_el8.5.0+977+5653bbea

httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 : Apache HTTP Server
Repo        : @System
Matched from:
Provide    : httpd = 2.4.37-43.module_el8.5.0+1022+b541f3b1

httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 : Apache HTTP Server
Repo        : aliyun-app
Matched from:
Provide    : httpd = 2.4.37-43.module_el8.5.0+1022+b541f3b1

[root@server yum.repos.d]#

清除下载到本机的指定的软件包或所有软件包的yum源缓存


[root@server yum.repos.d]# yum clean packages all

 源代码编译

(1)使用源代码安装软件的优点
由于自由软件的最新版本大都以源码的形式最先发布,编译安装可以获得软件的最新版本,及时修
复bug。如果当前安装的程序无法满足需求,用户可以根据需要重新配置、自由修改源代码,加入新的功能
(2)编译需求
确认源代码编译环境:需安装支持 C/C++程序语言的编译器yum install gcc
下载相应的源码软件包
(3)安装

获得源码包

[root@server opt]# curl https://mirrors.tuna.tsinghua.edu.cn/gnu/wget/wget2-latest.tar.gz -o wget-latest.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 3692k  100 3692k    0     0   138k      0  0:00:26  0:00:26 --:--:--  155k
[root@server opt]# ll
total 3704
-rw-------. 1 root root    1382 Oct 17 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Desktop
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Documents
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Downloads
-rw-r--r--. 1 root root       0 Oct 24 19:39 file-10-24
-rw-r--r--. 1 root root    1654 Oct 17 01:04 initial-setup-ks.cfg
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Music
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Pictures
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Public
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Templates
drwxr-xr-x. 2 root root       6 Oct 17 01:28 Videos
-rw-r--r--. 1 root root 3780748 Nov 12 21:35 wget-latest.tar.gz
[root@server opt]#

压缩

[root@server opt]# tar xf wget-latest.tar.gz
[root@server opt]# ll
total 3708
-rw-------.  1 root root    1382 Oct 17 00:54 anaconda-ks.cfg
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Desktop
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Documents
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Downloads
-rw-r--r--.  1 root root       0 Oct 24 19:39 file-10-24
-rw-r--r--.  1 root root    1654 Oct 17 01:04 initial-setup-ks.cfg
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Music
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Pictures
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Public
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Templates
drwxr-xr-x.  2 root root       6 Oct 17 01:28 Videos
drwxr-xr-x. 15 root root    4096 May 27 17:46 wget2-2.0.1
-rw-r--r--.  1 root root 3780748 Nov 12 21:35 wget-latest.tar.gz
[root@server opt]#

装载c编译器

[root@server wget2-2.0.1]# yum install gcc -y

装载c编译器的依赖

[root@server wget2-2.0.1]# yum install openssl openssl-devel  make -y

编译

[root@server wget2-2.0.1]# ./configure

 将源代码文件编译为二进制的可执行程序

[root@server wget2-2.0.1]#make

将编译好的程序文件复制到系统中


[root@server wget2-2.0.1]# make install
[root@server opt]# wget www.baidu.com -O index.html
--2022-11-12 21:58:35--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 14.215.177.38, 14.215.177.39
Connecting to www.baidu.com (www.baidu.com)|14.215.177.38|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

index.html               100%[===============================>]   2.33K  --.-KB/s    in 0.006s

2022-11-12 21:58:35 (359 KB/s) - ‘index.html’ saved [2381/2381]

[root@server opt]#

进程简介 

(1)程序、进程、线程
程序: 二进制文件,文件存储在磁盘中,例如/usr/bin/目录下
进程:进程是已启动的可执行程序的运行实例。
进程和程序并不是一一对应的关系,相同的程序运行在不同的数据集上就是不同的进程
进程还具有并发性和交往性,而程序却是封闭的
线程:是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。
一个进程可以拥有多个线程,而一个线程同时只能被一个进程所拥有
线程不能单独执行,但是每一个线程都有程序的入口、执行序列以及程序出口,它必须组成进程才
能被执行。

 查看进程 

 ps命令查看进程 

ps显示某个时间点的程序运行情况

命令

效果

-a

显示所有用户的进程

-u 显示用户名和启动时间
-x 显示没有控制终端的进程
-e 显示所有进程,包括没有控制终端的进程
-l 长格式显示
-w 宽行显示
-f

做一个更完整的输出

[root@server opt]# ps -a
    PID TTY          TIME CMD
   3225 tty2     00:00:00 Xorg
   3291 tty2     00:00:00 gnome-session-b
   3384 tty2     00:00:07 gnome-shell
   3404 tty2     00:00:00 ibus-daemon
   3409 tty2     00:00:00 ibus-dconf
   3410 tty2     00:00:00 ibus-extension-
   3414 tty2     00:00:00 ibus-x11
[root@server opt]# ps -u
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        3223  0.0  0.1 235084  1252 tty2     Ssl+ 19:30   0:00 /usr/libexec/gdm-x-session --r
root        3225  0.0  0.1 714168  1572 tty2     Sl+  19:30   0:00 /usr/libexec/Xorg vt2 -display
root        3291  0.0  0.2 732568  1968 tty2     Sl+  19:30   0:00 /usr/libexec/gnome-session-bin
root        3384  0.0  5.1 2752588 41480 tty2    Sl+  19:30   0:07 /usr/bin/gnome-shell
root        3404  0.0  0.1 391208  1272 tty2     Sl   19:30   0:00 ibus-daemon --xim --panel disa
root        3409  0.0  0.1 302916  1260 tty2     Sl   19:30   0:00 /usr/libexec/ibus-dconf
[root@server opt]# ps -x
    PID TTY      STAT   TIME COMMAND
      1 ?        Ss     0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
      2 ?        S      0:00 [kthreadd]
      3 ?        I<     0:00 [rcu_gp]
      4 ?        I<     0:00 [rcu_par_gp]
      6 ?        I<     0:00 [kworker/0:0H-events_highpri]
      9 ?        I<     0:00 [mm_percpu_wq]
[root@server opt]# ps -e
    PID TTY          TIME CMD
      1 ?        00:00:03 systemd
      2 ?        00:00:00 kthreadd
      3 ?        00:00:00 rcu_gp
      4 ?        00:00:00 rcu_par_gp
      6 ?        00:00:00 kworker/0:0H-events_highpri
      9 ?        00:00:00 mm_percpu_wq
     10 ?        00:00:00 ksoftirqd/0
     11 ?        00:00:00 rcu_sched
[root@server opt]# ps -l
F S   UID     PID    PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S     0    4199    4197  0  80   0 -  6986 -      pts/1    00:00:00 bash
4 S     0   36836    4199  0  80   0 - 38295 -      pts/1    00:00:00 su
4 S     0   36839   36836  0  80   0 -  6987 -      pts/1    00:00:00 bash
0 R     0   60419   36839  0  80   0 - 11370 -      pts/1    00:00:00 ps
[root@server opt]# ps -w
    PID TTY          TIME CMD
   4199 pts/1    00:00:00 bash
  36836 pts/1    00:00:00 su
  36839 pts/1    00:00:00 bash
  60420 pts/1    00:00:00 ps
[root@server opt]# ps -f
UID          PID    PPID  C STIME TTY          TIME CMD
root        4199    4197  0 19:44 pts/1    00:00:00 -bash
root       36836    4199  0 21:40 pts/1    00:00:00 su - root
root       36839   36836  0 21:40 pts/1    00:00:00 -bash
root       60421   36839  0 22:04 pts/1    00:00:00 ps -f
[root@server opt]#

 top命令查看进程 

语法:top [-d 数字]  |  [-bnp]

命令 效果
-d 后面可以接秒数,就是整个进程界面更新的秒数,默认是5秒
-b 以批次的方式执行top
-n 与-b搭配,进行几次top的输出结果
-i 不显示闲置或者僵死的进程信息
-c 显示进程的整个命令路径,而不是只显示命令名称
-s 使top命令在安全模式下运行
-p 指定某些个PID来进行查看检测
[root@server opt]# top -d 1
top - 22:07:40 up  4:09,  2 users,  load average: 0.00, 0.07, 0.15
Tasks: 293 total,   2 running, 291 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    782.2 total,     74.5 free,    415.7 used,    292.0 buff/cache
MiB Swap:   1024.0 total,    228.1 free,    795.9 used.    239.9 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND

查看指定用户进程

[root@server opt]# top -d 1 -p 10126
top - 22:10:19 up  4:12,  2 users,  load average: 0.01, 0.05, 0.12
Tasks:   0 total,   0 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  1.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    782.2 total,     72.7 free,    416.4 used,    293.1 buff/cache
MiB Swap:   1024.0 total,    228.8 free,    795.2 used.    239.3 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND

在top执行过程中可以使用的按键命令:

交互式命令 说明
h或? 显示帮助信息,给出交互式命令的一些说明
k  终止一个进程,系统将提示用户输入一个需要终止进程的PID
i 忽略闲置进程和僵死进程,这是一个开关式命令
s 改变top输出信息两次刷新之间的时间,系统将提示输入新的时间,单位是秒。
o或O 改变top输出信息中显示项的顺序。按小写的a-z键可以将相应的列向右移动,而按大写的
A-Z键可以将相应的列向左移动。最后按enter确定
f或F 从当前显示列表中添加或者删除项,按f键之后会显示列的列表,按a-z键即可显示或隐藏
对应的列。最后按enter确定
m  切换显示内存信息
t 切换显示进程和cpu状态信息
r 重新设置一个进程的优先级,系统提示用户输入需要改变的进程PID以及需要设置的进程
优先级值。
l  切换显示平均负载和启动时间信息
q  退出top显示
c  切换显示完整命令行和命令名称信息
M  根据驻留内存大小进行排序输出
N  以PID来排序
P 根据CPU使用百分比大小进行排序输出
T  根据时间/累计时间进行排序输出
S 切换到累计模式
W 将当前top设置写入~/.toprc文件中

实验

1、RPM软件管理
1)通过rpm安装vsftpd软件。

[root@server opt]# cd  /mnt/AppStream/Packages/
[root@server Packages]# rpm -ivh vsftpd-3.0.3-34.el8.x86_64.rpm
warning: vsftpd-3.0.3-34.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:vsftpd-3.0.3-34.el8              ################################# [100%]
[root@server Packages]#


2)查询vsftpd软件包的信息。

[root@server Packages]# rpm -qi vsftpd
Name        : vsftpd
Version     : 3.0.3
Release     : 34.el8
Architecture: x86_64
Install Date: Sat 12 Nov 2022 10:24:47 PM CST
Group       : System Environment/Daemons
Size        : 355692
License     : GPLv2 with exceptions
Signature   : RSA/SHA256, Wed 21 Apr 2021 08:42:12 PM CST, Key ID 199e2f91fd431d51
Source RPM  : vsftpd-3.0.3-34.el8.src.rpm
Build Date  : Tue 20 Apr 2021 10:28:24 PM CST
Build Host  : x86-vm-15.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager    : Red Hat, Inc. 
Vendor      : Red Hat, Inc.
URL         : https://security.appspot.com/vsftpd.html
Summary     : Very Secure Ftp Daemon
Description :
vsftpd is a Very Secure FTP daemon. It was written completely from
scratch.
[root@server Packages]#


3)卸载软件包vsftpd。

[root@server Packages]# rpm -evh vsftpd
Preparing...                          ################################# [100%]
Cleaning up / removing...
   1:vsftpd-3.0.3-34.el8              ################################# [100%]
[root@server Packages]#

2、YUM软件管理
1)挂载本地光盘,配置yum源为rhel8.repo。

[root@server Packages]# mount /dev/sr0 /mnt
mount: /mnt: /dev/sr0 already mounted on /run/media/root/RHEL-8-5-0-BaseOS-x86_64.
[root@server yum.repos.d]# vim rhel8.repo
[root@server yum.repos.d]# cat rhel8.repo
[app]
name=appstream
baseurl=file:///mnt/AppStream
gpgcheck=0
enable=1


[base]
name=baseos
baseurl=file:///mnt/BaseOS
gpgcheck=0
enable=1
[root@server yum.repos.d]#


2)通过yum来安装httpd。

[root@server yum.repos.d]# yum install httpd -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.

Repository base is listed more than once in the configuration
Last metadata expiration check: 0:01:17 ago on Sat 12 Nov 2022 10:31:29 PM CST.
Dependencies resolved.
=================================================================================================
 Package               Arch      Version                                     Repository     Size
=================================================================================================
Installing:
 httpd                 x86_64    2.4.37-43.module_el8.5.0+1022+b541f3b1      aliyun-app    1.4 M
Installing dependencies:
 apr                   x86_64    1.6.3-12.el8                                aliyun-app    129 k
 apr-util              x86_64    1.6.1-6.el8                                 aliyun-app    105 k
 centos-logos-httpd    noarch    85.8-2.el8                                  base           75 k
 httpd-filesystem      noarch    2.4.37-43.module_el8.5.0+1022+b541f3b1      aliyun-app     39 k
 httpd-tools           x86_64    2.4.37-43.module_el8.5.0+1022+b541f3b1      aliyun-app    107 k
 mod_http2             x86_64    1.15.7-3.module+el8.4.0+8625+d397f3da       app           154 k
Installing weak dependencies:
 apr-util-bdb          x86_64    1.6.1-6.el8                                 aliyun-app     25 k
 apr-util-openssl      x86_64    1.6.1-6.el8                                 aliyun-app     27 k

Transaction Summary
=================================================================================================
Install  9 Packages

Total size: 2.1 M
Total download size: 1.9 M
Installed size: 5.6 M
Downloading Packages:
(1/8): apr-util-bdb-1.6.1-6.el8.x86_64.rpm                        48 kB/s |  25 kB     00:00
(2/8): apr-util-1.6.1-6.el8.x86_64.rpm                           169 kB/s | 105 kB     00:00
(3/8): apr-util-openssl-1.6.1-6.el8.x86_64.rpm                   152 kB/s |  27 kB     00:00
(4/8): apr-1.6.3-12.el8.x86_64.rpm                               184 kB/s | 129 kB     00:00
(5/8): httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.n 226 kB/s |  39 kB     00:00
(6/8): httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 477 kB/s | 107 kB     00:00
(7/8): centos-logos-httpd-85.8-2.el8.noarch.rpm                  360 kB/s |  75 kB     00:00
(8/8): httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64.rpm   767 kB/s | 1.4 MB     00:01
-------------------------------------------------------------------------------------------------
Total                                                            776 kB/s | 1.9 MB     00:02
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                         1/1
  Installing       : apr-1.6.3-12.el8.x86_64                                                 1/9
  Running scriptlet: apr-1.6.3-12.el8.x86_64                                                 1/9
  Installing       : apr-util-bdb-1.6.1-6.el8.x86_64                                         2/9
  Installing       : apr-util-openssl-1.6.1-6.el8.x86_64                                     3/9
  Installing       : apr-util-1.6.1-6.el8.x86_64                                             4/9
  Running scriptlet: apr-util-1.6.1-6.el8.x86_64                                             4/9
  Installing       : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64               5/9
  Installing       : centos-logos-httpd-85.8-2.el8.noarch                                    6/9
  Running scriptlet: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          7/9
  Installing       : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          7/9
  Installing       : mod_http2-1.15.7-3.module+el8.4.0+8625+d397f3da.x86_64                  8/9
  Installing       : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     9/9
  Running scriptlet: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     9/9
  Verifying        : apr-1.6.3-12.el8.x86_64                                                 1/9
  Verifying        : apr-util-1.6.1-6.el8.x86_64                                             2/9
  Verifying        : apr-util-bdb-1.6.1-6.el8.x86_64                                         3/9
  Verifying        : apr-util-openssl-1.6.1-6.el8.x86_64                                     4/9
  Verifying        : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     5/9
  Verifying        : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          6/9
  Verifying        : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64               7/9
  Verifying        : centos-logos-httpd-85.8-2.el8.noarch                                    8/9
  Verifying        : mod_http2-1.15.7-3.module+el8.4.0+8625+d397f3da.x86_64                  9/9
Installed products updated.

Installed:
  apr-1.6.3-12.el8.x86_64
  apr-util-1.6.1-6.el8.x86_64
  apr-util-bdb-1.6.1-6.el8.x86_64
  apr-util-openssl-1.6.1-6.el8.x86_64
  centos-logos-httpd-85.8-2.el8.noarch
  httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
  httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
  httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
  mod_http2-1.15.7-3.module+el8.4.0+8625+d397f3da.x86_64

Complete!
[root@server yum.repos.d]#


3)使用yum查询ifconfig命令是由那个软件包提供的。

[root@server yum.repos.d]# yum whatprovides ifconfig
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.

Repository base is listed more than once in the configuration
Last metadata expiration check: 0:02:04 ago on Sat 12 Nov 2022 10:31:29 PM CST.
net-tools-2.0-0.52.20160912git.el8.x86_64 : Basic networking tools
Repo        : @System
Matched from:
Filename    : /usr/sbin/ifconfig

net-tools-2.0-0.52.20160912git.el8.x86_64 : Basic networking tools
Repo        : base
Matched from:
Filename    : /usr/sbin/ifconfig

[root@server yum.repos.d]#


4)卸载软件包httpd。

[root@server yum.repos.d]# yum install httpd -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.

Repository base is listed more than once in the configuration
appstream                                                        1.9 MB/s | 3.2 kB     00:00
Package httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@server yum.repos.d]#
[root@server yum.repos.d]# yum remove httpd -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.

Repository base is listed more than once in the configuration
Dependencies resolved.
=================================================================================================
 Package               Arch      Version                                    Repository      Size
=================================================================================================
Removing:
 httpd                 x86_64    2.4.37-43.module_el8.5.0+1022+b541f3b1     @aliyun-app    4.3 M
Removing unused dependencies:
 apr                   x86_64    1.6.3-12.el8                               @app           272 k
 apr-util              x86_64    1.6.1-6.el8                                @app           230 k
 apr-util-bdb          x86_64    1.6.1-6.el8                                @app            12 k
 apr-util-openssl      x86_64    1.6.1-6.el8                                @app            20 k
 httpd-filesystem      noarch    2.4.37-43.module_el8.5.0+1022+b541f3b1     @aliyun-app    400
 httpd-tools           x86_64    2.4.37-43.module_el8.5.0+1022+b541f3b1     @aliyun-app    193 k
 mod_http2             x86_64    1.15.7-3.module+el8.4.0+8625+d397f3da      @app           394 k
 redhat-logos-httpd    noarch    84.5-1.el8                                 @base          8.2 k

Transaction Summary
=================================================================================================
Remove  9 Packages

Freed space: 5.4 M
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                         1/1
  Running scriptlet: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     1/1
  Running scriptlet: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     1/9
  Erasing          : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     1/9
  Running scriptlet: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     1/9
  Erasing          : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          2/9
  Running scriptlet: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          2/9
  Erasing          : redhat-logos-httpd-84.5-1.el8.noarch                                    3/9
  Erasing          : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64               4/9
  Erasing          : apr-util-1.6.1-6.el8.x86_64                                             5/9
  Running scriptlet: apr-util-1.6.1-6.el8.x86_64                                             5/9
  Erasing          : apr-1.6.3-12.el8.x86_64                                                 6/9
  Running scriptlet: apr-1.6.3-12.el8.x86_64                                                 6/9
  Erasing          : apr-util-bdb-1.6.1-6.el8.x86_64                                         7/9
  Erasing          : apr-util-openssl-1.6.1-6.el8.x86_64                                     8/9
  Erasing          : mod_http2-1.15.7-3.module+el8.4.0+8625+d397f3da.x86_64                  9/9
  Running scriptlet: mod_http2-1.15.7-3.module+el8.4.0+8625+d397f3da.x86_64                  9/9
  Verifying        : apr-1.6.3-12.el8.x86_64                                                 1/9
  Verifying        : apr-util-1.6.1-6.el8.x86_64                                             2/9
  Verifying        : apr-util-bdb-1.6.1-6.el8.x86_64                                         3/9
  Verifying        : apr-util-openssl-1.6.1-6.el8.x86_64                                     4/9
  Verifying        : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64                     5/9
  Verifying        : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch          6/9
  Verifying        : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64               7/9
  Verifying        : mod_http2-1.15.7-3.module+el8.4.0+8625+d397f3da.x86_64                  8/9
  Verifying        : redhat-logos-httpd-84.5-1.el8.noarch                                    9/9
Installed products updated.

Removed:
  apr-1.6.3-12.el8.x86_64
  apr-util-1.6.1-6.el8.x86_64
  apr-util-bdb-1.6.1-6.el8.x86_64
  apr-util-openssl-1.6.1-6.el8.x86_64
  httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
  httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
  httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
  mod_http2-1.15.7-3.module+el8.4.0+8625+d397f3da.x86_64
  redhat-logos-httpd-84.5-1.el8.noarch

Complete!

3、源码管理
下载tree源码包并编译安装。

4、进程管理
1)通过ps命令的两种选项形式查看进程信息

[root@server yum.repos.d]# ps -wa
    PID TTY          TIME CMD
   3225 tty2     00:00:00 Xorg
   3291 tty2     00:00:00 gnome-session-b
   3384 tty2     00:00:08 gnome-shell
   3404 tty2     00:00:00 ibus-daemon
   3409 tty2     00:00:00 ibus-dconf
   3410 tty2     00:00:00 ibus-extension-
   3414 tty2     00:00:00 ibus-x11
   3488 tty2     00:00:00 gsd-power
   3489 tty2     00:00:00 gsd-print-notif
   3491 tty2     00:00:00 gsd-rfkill
   3492 tty2     00:00:00 gsd-screensaver
   3500 tty2     00:00:00 gsd-sharing
   3504 tty2     00:00:00 gsd-sound
   3508 tty2     00:00:00 gsd-xsettings


2)通过pgrep和pidof命令查看sshd服务的进程号

[root@server yum.repos.d]# pgrep sshd
1065
4193
4197
[root@server yum.repos.d]# pidof sshd
4197 4193 1065
[root@server yum.repos.d]#


3)使dd if=/dev/zero of=/root/file bs=1M count=8190 命令操作在前台运行

[root@server yum.repos.d]# dd if=/dev/zero of=/root/file bs=1M count=8190
1261+0 records in
1261+0 records out
1322254336 bytes (1.3 GB, 1.2 GiB) copied, 3.78097 s, 350 MB/s

[root@server yum.repos.d]#


4)将上题操作调入到后台并暂停

[root@server yum.repos.d]#  dd if=/dev/zero of=/root/file bs=1M count=8190

[1]+  Stopped                 dd if=/dev/zero of=/root/file bs=1M count=8190
[root@server yum.repos.d]#


5)使dd if=/dev/zero of=/root/file2 bs=1M count=1024 命令操作在后台运行


[root@server yum.repos.d]# dd if=/dev/zero of=/root/file2 bs=1M count=1024

[2]+  Stopped                 dd if=/dev/zero of=/root/file2 bs=1M count=1024
[root@server yum.repos.d]#


6)查看后台的任务列表

[root@server yum.repos.d]# jobs
[1]-  Stopped                 dd if=/dev/zero of=/root/file bs=1M count=8190
[2]+  Stopped                 dd if=/dev/zero of=/root/file2 bs=1M count=1024
[root@server yum.repos.d]#


7)恢复dd if=/dev/zero of=/root/file bs=1M count=8190 让其在后台继续运行

[root@server yum.repos.d]# bg %1
[1]- dd if=/dev/zero of=/root/file bs=1M count=8190 &
[root@server yum.repos.d]#


8)查询dd if=/dev/zero of=/root/file bs=1M count=8190 命令的进程并通过kill杀死

[root@server yum.repos.d]# kill %1
[root@server yum.repos.d]# jobs
[1]-  Terminated              dd if=/dev/zero of=/root/file bs=1M count=8190
[2]+  Stopped                 dd if=/dev/zero of=/root/file2 bs=1M count=1024
[root@server yum.repos.d]#

你可能感兴趣的:(linux,运维,服务器)