网络班第六周
重要知识点回顾一
1.软件运行和编译
1.1软件相关概念
1.1.1ABI
ABI即 Application Binary Interface
Windows与Linux不兼容
ELF(Executable and Linkable Format)
PE(Portable Executable)
库级别的虚拟化:
Linux: WINE
Windows: Cygwin
1.1.2API
API即Application Programming Interface,API可以在各种不同的操作系统上实现给应用程序提供完全相同的接口,而它们本身在这些系统上的实现却可能迥异,主流的操作系统有两种,一种是Windows系统,另一种是Linux系统。由于操作系统的不同,API又分为Windows API和Linux API。在Windows平台开发出来的软件在Linux上无法运行,在Linux上开发的软件在Windows上又无法运行,这就导致了软件移植困难,POSIX 标准的出现就是为了解决这个问题。
POSIX:Portable Operating System Interface 可移植操作系统接口,定义了操作系统应该为应用程序提供的接口标准,是IEEE为要在各种UNIX操作系统上运行的软件而定义的一系列API标准的总称。Linux和windows都要实现基本的posix标准,程序就在源代码级别可移植了 。
1.1.3 开发语言
系统级开发
汇编语言
C
C++
应用级开发
java
Python
go
php
perl
delphi
basic
ruby
bash
1.2模块(库)文件
查看二进制程序所依赖的库文件
ldd /PATH/TO/BINARY_FILE
管理及查看本机装载的库文件
# 加载配置文件中指定的库文件
ldconfig
# 显示本机已经缓存的所有可用库文件名及文件路径映射关系
/sbin/ldconfig –p
配置文件:
/etc/ld.so.conf, /etc/ld.so.conf.d/*.conf
缓存文件:
/etc/ld.so.cache
范例:库文件破坏后,将导致依赖的程序无法正常运行
[root@centos8 ~]#ldd /bin/ls linux-vdso.so.1 (0x00007ffc509fd000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fc6ef24a000) libcap.so.2 => /lib64/libcap.so.2 (0x00007fc6ef044000) libc.so.6 => /lib64/libc.so.6 (0x00007fc6eec81000) libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007fc6ee9fd000) libdl.so.2 => /lib64/libdl.so.2 (0x00007fc6ee7f9000) /lib64/ld-linux-x86-64.so.2 (0x00007fc6ef698000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc6ee5d9000) [root@centos8 ~]#ldd /bin/cat linux-vdso.so.1 (0x00007ffe335dd000) libc.so.6 => /lib64/libc.so.6 (0x00007fa34749e000) /lib64/ld-linux-x86-64.so.2 (0x00007fa347a6b000) [root@centos8 ~]#mv /lib64/libc.so.6 /tmp [root@centos8 ~]#ls ls: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory [root@centos8 ~]#cat cat: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
2 软件包和包管理器
2.1 软件包介绍
开源软件最初只提供了.tar.gz的打包的源码文件,用户必须自已编译每个想在GNU/Linux上运行的软件。用户急需系统能提供一种更加便利的方法来管理这些软件,当Debian诞生时,这样一个管理工具dpkg也就应运而生,可用来管理deb后缀的"包"文件。从而著名的“package”概念第一次出现在GNU/Linux系统中,稍后Red Hat才开发自己的rpm包管理系统。
2.1.1 软件包中的文件分类
二进制文件
库文件
配置文件
帮助文件
范例:利用 cpio工具查看包文件列表
rpm2cpio 包文件|cpio –itv 预览包内文件 rpm2cpio 包文件|cpio –id “*.conf” 释放包内文件
2.1.2 程序包管理器
软件包管理器功能:
将编译好的应用程序的各组成文件打包一个或几个程序包文件,利用包管理器可以方便快捷地实现程序包的安装、卸载、查询、升级和校验等管理操作
主流的程序包管理器
redhat:rpm文件, rpm 包管理器,rpm:Redhat Package Manager,RPM Package Manager
debian:deb文件, dpkg 包管理器
2.1.3 包命名
源代码打包文件命名:
name-VERSION.tar.gz|bz2|xz VERSION: major.minor.release
rpm包命名方式:
name-VERSION-release.arch.rpm
VERSION: major.minor.release
release:release.OS
常见的arch:
x86: i386, i486, i586, i686
x86_64: x64, x86_64, amd64
powerpc: ppc跟平台无关:noarch
2.1.4 分类和拆包
软件包为了管理和使用的便利,会将一个大的软件分类,放在不同的子包中。
包的分类
Application-VERSION-ARCH.rpm: 主包
Application-devel-VERSION-ARCH.rpm 开发子包
Application-utils-VERSION-ARHC.rpm 其它子包
Application-libs-VERSION-ARHC.rpm 其它子包
2.1.5 包的依赖
软件包之间可能存在依赖关系,甚至循环依赖,即:A包依赖B包,B包依赖C包,C包依赖A包
安装软件包时,会因为缺少依赖的包,而导致安装包失败。
解决依赖包管理工具:
yum:rpm包管理器的前端工具
dnf:Fedora 18+ rpm包管理器前端管理工具,CentOS 8 版代替 yum
apt:deb包管理器前端工具
zypper:suse上的rpm前端管理工具
2.1.6 程序包管理器相关文件
包文件组成 (每个包独有)
包内的文件
元数据,如:包的名称,版本,依赖性,描述等
可能会有包安装或卸载时运行的脚本
数据库(公共):/var/lib/rpm
程序包名称及版本
依赖关系
功能说明
包安装后生成的各文件路径及校验码信息
2.1.7 获取程序包的途径
软件包需要事先将源码进行编译后打包形成,获取包的途径如下:
系统发版的光盘或官方网站
CentOS镜像:
https://www.centos.org/download/
Ubuntu 镜像:
389 i686
211 noarch
1061 x86_64http://cdimage.ubuntu.com/releases/
第三方组织提供
Fedora-EPEL:Extra Packages for Enterprise Linux
https://fedoraproject.org/wiki/EPEL
https://mirrors.aliyun.com/epel/?spm=a2c6h.13651104.0.0.3bc47dfaZpesAr
Rpmforge:官网:http://repoforge.org/, RHEL推荐,包很全,即将关闭
Community Enterprise Linux Repository:http://www.elrepo.org,支持最新的内核和硬件相关包
软件项目官方站点
http://yum.mariadb.org/10.4/centos8-amd64/rpms/
http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/
搜索引擎
注意:第三方包建议要检查其合法性,来源合法性,程序包的完整性
自己制作
将源码文件,利用工具,如:rpmbuild,fpm等工具制作成rpm包文件
3 包管理器rpm
3.1 安装
格式:
rpm {-i|--install} [install-options] PACKAGE_FILE…
选项:
-v: verbose -vv: -h: 以#显示程序包管理执行进度
常用组合:
rpm -ivh PACKAGE_FILE ...
rpm包安装[install-options]
--test: 测试安装,但不真正执行安装,即dry run模式 --nodeps:忽略依赖关系 --replacepkgs | replacefiles --nosignature: 不检查来源合法性 --nodigest:不检查包完整性 --noscripts:不执行程序包脚本 %pre: 安装前脚本 --nopre %post: 安装后脚本 --nopost %preun: 卸载前脚本 --nopreun %postun: 卸载后脚本 --nopostun
3.2 升级和降级
rpm {-U|--upgrade} [install-options] PACKAGE_FILE...
rpm {-F|--freshen} [install-options] PACKAGE_FILE...
upgrade:安装有旧版程序包,则“升级”,如果不存在旧版程序包,则“安装”
freshen:安装有旧版程序包,则“升级”, 如果不存在旧版程序包,则不执行升级操作
--oldpackage:降级
--force: 强制安装
常用组合
rpm -Uvh PACKAGE_FILE ...
rpm -Fvh PACKAGE_FILE ...
升级注意项:
(1) 不要对内核做升级操作;Linux支持多内核版本并存,因此直接安装新版本内核。
(2) 如果原程序包的配置文件安装后曾被修改,升级时,新版本提供的同一个配置文件不会直接覆盖老版本的配置文件,而把新版本文件重命名(FILENAME.rpmnew)后保留 。
3.3 包查询
rpm {-q|--query} [select-options] [query-options]
[select-options] -a:所有包 -f:查看指定的文件由哪个程序包安装生成 -p rpmfile:针对尚未安装的程序包文件做查询操作 [query-options] --changelog:查询rpm包的changelog -c:查询程序的配置文件 -d:查询程序的文档 -i:information -l:查看指定的程序包安装后生成的所有文件 --scripts:程序包自带的脚本 # 和CAPABILITY相关 --whatprovides CAPABILITY:查询指定的CAPABILITY由哪个包所提供 --whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖 --provides:列出指定程序包所提供的CAPABILITY -R:查询指定的程序包所依赖的CAPABILITY
常用查询用法:
-qa -q PACKAGE -qi PACKAGE -qc PACKAGE -ql PACKAGE -qd PACKAGE -q --scripts PACKAGE -qf FILE -qpi PACKAGE_FILE -qpl PACKAGE_FILE, ...
3.4 包卸载
格式:
rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--notriggers] [--test]
PACKAGE_NAME ...
注意:当包卸载时,对应的配置文件不会删除, 以FILENAME.rpmsave形式保留
范例:强行删除rpm包,并恢复
[root@centos7 ~]#rpm -e rpm --nodeps # 重启进入rescue模式 # mkdir /mnt/cdrom # mount /dev/sr0 /mnt/cdrom # rpm -ivh /mnt/cdrom/Packages/rpm-4.11.3-40.el7.x86_64.rpm --root=/mnt/sysimage # reboot
3.5 包校验
在安装包时,系统也会检查包的来源是否是合法的
检查包的完整性和签名
rpm -K|--checksig rpmfile
在检查包的来源和完整性前,必须导入所需要公钥
范例:
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 rpm -qa “gpg-pubkey*”
范例:CentOS 8
[root@centos8 ~]#rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37- 16.module_el8.1.0+256+ae790463.x86_64.rpm /misc/cd/AppStream/Packages/httpd-2.4.37- 16.module_el8.1.0+256+ae790463.x86_64.rpm: digests SIGNATURES NOT OK [root@centos8 ~]#cat /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [root@centos8 ~]#rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [root@centos8 rpm-gpg]#rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37- 16.module_el8.1.0+256+ae790463.x86_64.rpm /misc/cd/AppStream/Packages/httpd-2.4.37- 16.module_el8.1.0+256+ae790463.x86_64.rpm: digests signatures OK [root@centos8 ~]#rpm -qa "gpg-pubkey*" gpg-pubkey-8483c65d-5ccc5b19
范例:校验包文件
[root@centos8 ~]#rpm -K /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm: digests signatures OK [root@centos8 ~]#cp /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm /data [root@centos8 ~]#cd /data [root@centos8 data]#ll total 60 -r--r--r-- 1 root root 60780 Apr 8 10:11 tree-1.7.0-15.el8.x86_64.rpm [root@centos8 data]#echo >>tree-1.7.0-15.el8.x86_64.rpm [root@centos8 data]#ll tree-1.7.0-15.el8.x86_64.rpm -r--r--r-- 1 root root 60781 Apr 8 10:11 tree-1.7.0-15.el8.x86_64.rpm [root@centos8 data]#cd [root@centos8 ~]#rpm -K /data/tree-1.7.0-15.el8.x86_64.rpm /data/tree-1.7.0-15.el8.x86_64.rpm: DIGESTS SIGNATURES NOT OK
软件在安装时,会将包里的每个文件的元数据,如:大小,权限,所有者,时间等记录至rpm相关的数据库中,可以用来检查包中的文件是否和当初安装时有所变化 。
rpm {-V|--verify} [select-options] [verify-options]
# 示例:
[root@centos8 ~]#rpm -V centos-release
S.5....T. c /etc/issue
S file Size differs
M Mode differs (includes permissions and file type)
5 digest (formerly MD5 sum) differs
D Device major/minor number mismatch
L readLink(2) path mismatch
U User ownership differs
G Group ownership differs
T mTime differs
P capabilities differ
. (点) 表示当前位置代表的字符含义一致
c 所在的位置表示文件类型
c 配置文件
d 文件数据文件
g 该文件不属于某个文件(极少情况)
l 许可证文件(license file)
r 自述文件(READ ME)
3.6 数据库
rpm包安装时生成的信息,都放在rpm数据库中
/var/lib/rpm
可以重建数据库
rpm {--initdb|--rebuilddb}
initdb: 初始化,如果事先不存在数据库,则新建之,否则,不执行任何操作
rebuilddb:重建已安装的包头的数据库索引目录
4 yum和dnf
CentOS 使用 yum, dnf 解决rpm的包依赖关系
YUM: Yellowdog Update Modifier,rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,up2date的替代工具,CentOS 8 用dnf 代替了yum ,不过保留了和yum的兼容性,配置也是通用的
4.1 yum/dnf 工作原理
yum/dnf 是基于C/S 模式
yum 服务器存放rpm包和相关包的元数据库
yum 客户端访问yum服务器进行安装或查询等
yum 实现过程
先在yum服务器上创建 yum repository(仓库),在仓库中事先存储了众多rpm包,以及包的相关的元数据文件(放置于特定目录repodata下),当yum客户端利用yum/dnf工具进行安装时包时,会自动下载repodata中的元数据,查询远数据是否存在相关的包及依赖关系,自动从仓库中找到相关包下载并安装
yum服务器的仓库可以多种形式存在:
file:// 本地路径
http://
https://
ftp://
注意:yum仓库指向的路径一定必须是repodata目录所在目录
4.2 yum客户端配置
yum客户端配置文件
/etc/yum.conf #为所有仓库提供公共配置 /etc/yum.repos.d/*.repo: #为每个仓库的提供配置文件
帮助参考: man 5 yum.conf
相关变量
yum的repo配置文件中可用的变量: $releasever: 当前OS的发行版的主版本号,如:8,7,6 $arch: CPU架构,如:aarch64, i586, i686,x86_64等 $basearch:系统基础平台;i386, x86_64 $contentdir:表示目录,比如:centos-8,centos-7 $YUM0-$YUM9:自定义变量
范例:CentOS 8 配置文件
[root@centos8 ~]# ll /etc/yum.conf lrwxrwxrwx. 1 root root 12 May 14 2019 /etc/yum.conf -> dnf/dnf.conf [root@centos8 ~]#cat /etc/yum.conf [main] gpgcheck=1 #安装包前要做包的合法和完整性校验 installonly_limit=3 #同时可以安装3个包,最小值为2,如设为0或1,为不限制 clean_requirements_on_remove=True #删除包时,是否将不再使用的包删除 best=True #升级时,自动选择安装最新版,即使缺少包的依赖
baseurl指向的路径
阿里云提供了写好的CentOS和ubuntu的仓库文件下载链接
http://mirrors.aliyun.com/repo/
CentOS系统的yum源
# 阿里云 https://mirrors.aliyun.com/centos/$releasever/os/x86_64/ # 华为云 https://mirrors.huaweicloud.com/ # 清华大学 https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/x86_64/
EPEL的yum源
# 阿里云 https://mirrors.aliyun.com/epel/$releasever/x86_64
阿里巴巴开源软件
https://opsx.alibaba.com/
范例:为CentOS7用系统安装光盘作的本地yum仓库
# 挂载光盘至某目录,如/mnt/cdrom mount /dev/cdrom /mnt/cdrom # 创建配置文件 [root@centos7 ~]#vim /etc/yum.repos.d/centos7.repo [CentOS7] name=CentOS 7 baseurl=file:///mnt/cdrom gpgcheck=0 enabled=1
范例:为CentOS 8 配置 yum 的系统和EPEL源仓库
[root@centos8 ~]#cat /etc/yum.repos.d/base.repo [BaseOS] name=BaseOS baseurl=file:///misc/cd/BaseOS gpgcheck=1 gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [AppStream] name=AppStream baseurl=file:///misc/cd/AppStream gpgcheck=0 [epel] name=EPEL baseurl=http://mirrors.aliyun.com/epel/$releasever/Everything/$basearch gpgcheck=0 enabled=1 [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os gpgcheck=0
注意:与之前的版本不同,CentOS 8 系统有两个yum 源:BaseOS和AppStream ,需要分别设置两个仓库
yum-config-manager命令
可以生成yum仓库的配置文件及启用或禁用仓库,来自于yum-utils包
格式:
# 增加仓库 yum-config-manager --add-repo URL或file # 禁用仓库 yum-config-manager --disable “仓库名" # 启用仓库 yum-config-manager --enable “仓库名”
范例:创建仓库配置
[root@centos8 ~]#rpm -qf `which yum-config-manager ` dnf-utils-4.0.2.2-3.el8.noarch [root@centos8 ~]#yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo Adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/dockerce.repo [root@centos8 ~]#ls /etc/yum.repos.d/ backup base.repo docker-ce.repo
范例:创建仓库配置
# 生成172.16.0.1_cobbler_ks_mirror_8_.repo [root@centos8 ~]#yum-config-manager --addrepo=http://172.16.0.1/cobbler/ks_mirror/8/ Adding repo from: http://172.16.0.1/cobbler/ks_mirror/8/ [root@centos8 ~]#cat /etc/yum.repos.d/172.16.0.1_cobbler_ks_mirror_8_.repo [172.16.0.1_cobbler_ks_mirror_8_] name=created by dnf config-manager from http://172.16.0.1/cobbler/ks_mirror/8/ baseurl=http://172.16.0.1/cobbler/ks_mirror/8/ enabled=1
范例:创建仓库配置
[root@centos8 ~]#ls /etc/yum.repos.d/ backup base.repo [root@centos8 ~]#yum-config-manager --add-repo /data/docker-ce.repo Adding repo from: file:///data/docker-ce.repo [root@centos8 ~]#ls /etc/yum.repos.d/ backup base.repo docker-ce.repo
范例:启用和禁用仓库
[root@centos8 ~]#yum repolist [root@centos8 ~]#yum-config-manager --disable epel [root@centos8 ~]#cat /etc/yum.repos.d/base.repo [BaseOS] name=BaseOS baseurl=file:///misc/cd/BaseOS gpgcheck=0 [AppStream] name=AppStream baseurl=file:///misc/cd/AppStream gpgcheck=0 [epel] name=EPEL baseurl=http://mirrors.aliyun.com/epel/$releasever/Everything/$basearch http://mirrors.huaweicloud.com/epel/$releasever/Everything/$basearch gpgcheck=0 enabled=0 [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os http://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/os gpgcheck=0 enabled=1 [root@centos8 ~]#yum repolist BaseOS 3.8 MB/s | 3.9 kB 00:00 AppStream 4.2 MB/s | 4.3 kB 00:00 extras 10 kB/s | 1.5 kB 00:00 repo id repo name status AppStream AppStream 4,755 BaseOS BaseOS 1,659 extras extras 12 [root@centos8 ~]#yum-config-manager --disable extras [root@centos8 ~]#yum repolist BaseOS 3.8 MB/s | 3.9 kB 00:00 AppStream 4.2 MB/s | 4.3 kB 00:00 repo id repo name status AppStream AppStream 4,755 BaseOS BaseOS 1,659 [root@centos8 ~]#yum-config-manager --enable extras [root@centos8 ~]#yum repolist BaseOS 3.8 MB/s | 3.9 kB 00:00 AppStream 4.2 MB/s | 4.3 kB 00:00 extras 12 kB/s | 1.5 kB 00:00 repo id repo name status AppStream AppStream 4,755 BaseOS BaseOS 1,659 extras extras 12
4.3 yum命令
yum命令的用法:
yum [options] [command] [package ...]
yum的命令行选项:
-y #自动回答为“yes” -q #静默模式 --nogpgcheck #禁止进行gpg check --enablerepo=repoidglob #临时启用此处指定的repo,支持通配符,如:”*“ --disablerepo=repoidglob #临时禁用此处指定的repo,和上面语句同时使用,放在后面的生效
4.3.1 显示仓库列表
yum repolist [all|enabled|disabled]
范例:显示仓库列表
[root@centos8 ~]#yum repolist [root@centos8 ~]#yum repolist all Last metadata expiration check: 0:01:32 ago on Sun 29 Dec 2019 12:13:10 AM CST. repo id repo name status AppStream AppStream enabled: 4,681 BaseOS BaseOS enabled: 1,655 epel EPEL disabled extras extras disabled [root@centos8 ~]#yum --enablerepo=ep* --disablerepo=A* repolist Last metadata expiration check: 0:01:18 ago on Sun 29 Dec 2019 12:13:27 AM CST. repo id repo name status BaseOS BaseOS 1,657 epel EPEL 3,733
4.3.2 显示程序包
yum list yum list [all | glob_exp1] [glob_exp2] [...] yum list {available|installed|updates} [glob_exp1] [...]
4.3.3 安装程序包
yum install package1 [package2] [...] yum reinstall package1 [package2] [...] #重新安装
4.3.4 卸载程序包
yum remove | erase package1 [package2] [...]
4.3.5 升级和降级
yum update [package1] [package2] [...] yum downgrade package1 [package2] [...] (降级)
4.3.6 查询
查看程序包information:
yum info [...]
查看指定的特性(可以是某文件)是由哪个程序包所提供:
yum provides | whatprovides feature1 [feature2] [...]
注意:文件要写全路径,而不只是文件名,否则无法查询到
范例:查询文件所属包
[root@centos8 ~]# ll /etc/vsftpd/vsftpd.conf ls: cannot access '/etc/vsftpd/vsftpd.conf': No such file or directory # 注意要写文件全路径才能查询到 [root@centos8 ~]#yum provides vsftpd.conf Last metadata expiration check: 0:56:45 ago on Fri 10 Apr 2020 11:24:00 AM CST. Error: No Matches found [root@centos8 ~]# yum provides /etc/vsftpd/vsftpd.conf Last metadata expiration check: 0:33:13 ago on Fri 27 Dec 2019 03:47:34 PM CST. vsftpd-3.0.3-28.el8.x86_64 : Very Secure Ftp Daemon Repo : AppStream Matched from: Filename : /etc/vsftpd/vsftpd.conf
以指定的关键字搜索程序包名及summary信息
yum search string1 [string2] [...]
查看指定包所依赖的capabilities:
yum deplist package1 [package2] [...]
范例:查询包依赖
[root@centos8 ~]#yum deplist httpd Last metadata expiration check: 0:41:15 ago on Wed 08 Apr 2020 03:15:02 PM CST. package: httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64 dependency: /bin/sh provider: bash-4.4.19-10.el8.x86_64 dependency: /etc/mime.types provider: mailcap-2.1.48-3.el8.noarch dependency: httpd-filesystem provider: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch dependency: httpd-filesystem = 2.4.37-16.module_el8.1.0+256+ae790463 provider: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch dependency: httpd-tools = 2.4.37-16.module_el8.1.0+256+ae790463 provider: httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.x86_64 dependency: libapr-1.so.0()(64bit) provider: apr-1.6.3-9.el8.x86_64 dependency: libaprutil-1.so.0()(64bit) provider: apr-util-1.6.1-6.el8.x86_64 dependency: libbrotlienc.so.1()(64bit) provider: brotli-1.0.6-1.el8.x86_64 dependency: libc.so.6(GLIBC_2.14)(64bit) provider: glibc-2.28-72.el8.x86_64 dependency: libcrypt.so.1()(64bit) provider: libxcrypt-4.1.1-4.el8.x86_64 dependency: libcrypt.so.1(XCRYPT_2.0)(64bit) provider: libxcrypt-4.1.1-4.el8.x86_64 dependency: libdl.so.2()(64bit) provider: glibc-2.28-72.el8.x86_64 dependency: libexpat.so.1()(64bit) provider: expat-2.2.5-3.el8.x86_64 dependency: liblua-5.3.so()(64bit) provider: lua-libs-5.3.4-11.el8.x86_64 dependency: libm.so.6()(64bit) provider: glibc-2.28-72.el8.x86_64 dependency: libpcre.so.1()(64bit) provider: pcre-8.42-4.el8.x86_64 dependency: libpthread.so.0()(64bit) provider: glibc-2.28-72.el8.x86_64 dependency: libpthread.so.0(GLIBC_2.2.5)(64bit) provider: glibc-2.28-72.el8.x86_64 dependency: libselinux.so.1()(64bit) provider: libselinux-2.9-2.1.el8.x86_64 dependency: libsystemd.so.0()(64bit) provider: systemd-libs-239-18.el8.x86_64 dependency: libsystemd.so.0(LIBSYSTEMD_209)(64bit) provider: systemd-libs-239-18.el8.x86_64 dependency: libz.so.1()(64bit) provider: zlib-1.2.11-10.el8.x86_64 dependency: mod_http2 provider: mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64 dependency: rtld(GNU_HASH) provider: glibc-2.28-72.el8.i686 provider: glibc-2.28-72.el8.x86_64 dependency: system-logos-httpd provider: centos-logos-httpd-80.5-2.el8.noarch dependency: systemd-units provider: systemd-239-18.el8.i686 provider: systemd-239-18.el8.x86_64
4.3.7 仓库缓存
清除目录/var/cache/yum/缓存
yum clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
构建缓存:
yum makecache
范例:管理缓存
[root@centos7 ~]#du -sh /var/cache/yum 93M /var/cache/yum [root@centos7 ~]#ls /var/cache/yum/x86_64/7/ base epel extras timedhosts timedhosts.txt [root@magedu ~]#yum clean all Loaded plugins: fastestmirror Cleaning repos: base epel extras Cleaning up list of fastest mirrors [root@centos7 ~]#du -sh /var/cache/yum 4.0K /var/cache/yum [root@centos7 ~]#yum makecache ...... Metadata Cache Created [root@centos7 ~]#du -sh /var/cache/yum 276M /var/cache/yum
4.3.8 查看yum事务历史
yum 执行安装卸载命令会记录到相关日志中
日志文件:
# CentOS 7以前版本日志 /var/log/yum.log # CentOS 8 版本日志 /var/log/dnf.rpm.log /var/log/dnf.log
日志命令:
yum history [info|list|packages-list|packages-info|summary|addoninfo|redo|undo|rollback|new|sync|stats]
4.3.9 安装及升级本地程序包
yum localinstall|install rpmfile1 [rpmfile2] [...] yum localupdate|update rpmfile1 [rpmfile2] [...]
4.3.10 包组管理的相关命令
yum grouplist [hidden] [groupwildcard] [...] yum groupinstall group1 [group2] [...] yum groupupdate group1 [group2] [...] yum groupremove group1 [group2] [...] yum groupinfo group1 [...]
范例:最小化安装的系统安装图形环境
[root@centos8 ~]#yum grouplist Last metadata expiration check: 0:21:21 ago on Sun 22 Dec 2019 01:56:36 PM CST. Available Environment Groups: Server with GUI Server Workstation KDE Plasma Workspaces Virtualization Host Custom Operating System Installed Environment Groups: Minimal Install Available Groups: Container Management .NET Core Development RPM Development Tools Smart Card Support Development Tools Graphical Administration Tools Headless Management Legacy UNIX Compatibility Network Servers Scientific Support Security Tools System Tools Fedora Packager [root@centos8 ~]#yum groupinfo "Server with GUI" Last metadata expiration check: 0:32:00 ago on Wed 08 Apr 2020 04:35:02 PM CST. Environment Group: Server with GUI Description: An integrated, easy-to-manage server with a graphical interface. no group 'dns-server' from environment 'graphical-server-environment' Mandatory Groups: Common NetworkManager submodules Container Management Core Fonts GNOME Guest Desktop Agents Hardware Monitoring Utilities Hardware Support Headless Management Internet Browser Multimedia Printing Client Server product core Standard base-x Optional Groups: Basic Web Server Debugging Tools FTP Server File and Storage Server Guest Agents Infiniband Support Mail Server Network File System Client Network Servers Performance Tools Remote Desktop Clients Remote Management for Linux Virtualization Client Virtualization Hypervisor Virtualization Tools Windows File Server [root@centos8 ~]#dnf groupinstall GNOME -y [root@centos8 ~]#init 5
4.3.11 实现私用 yum仓库
下载所有yum仓库的相关包和meta 数据
# CentOS 8 dnf 工具集成 dnf reposync --help #查看帮助 # 默认只下载rpm包,不下载meta数据,需要指定--download-metadata 才能下载meta dnf reposync --repoid=REPOID --download-metadata -p /path # CentOS 7 以前版本,reposync工具来自于yum-utils包 reposync --repoid=REPOID --download-metadata -p /path
创建私有yum仓库:
createrepo [options] <directory>
范例:创建局域网的基于Base的私有yum源
# 仓库服务器配置 [root@repo-server ~]#yum -y install httpd [root@repo-server ~]#systemctl enable --now httpd [root@repo-server ~]#mkdir /var/www/html/centos/8 -pv [root@repo-server ~]#mount /dev/sr0 /mnt/ [root@repo-server ~]#cp -a /mnt/* /var/www/html/centos/8 # yum客户端配置 [root@repo-client ~]#cat /etc/yum.repos.d/test.repo [BaseOS] name=BaseOS baseurl=http://10.0.0.8/centos/8/BaseOS gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [AppStream] name=Appstream baseurl=http://10.0.0.8/centos/8/AppStream/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
范例:下载阿里云的EPEL源,制作私有yum源
[root@centos8 ~]#cat /etc/yum.repos.d/base.repo [epel] name=EPEL baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/ gpgcheck=0 [root@centos8 ~]#dnf repolist Last metadata expiration check: 0:07:40 ago on Sun 22 Dec 2019 03:14:16 PM CST. repo id repo name status AppStream AppStream 4,681 BaseOS BaseOS 1,655 epel EPEL 3,707 # 下载相关仓库包和元数据 [root@centos8 ~]#dnf reposync --repoid=epel --download-metadata -p /var/www/html # --download-metadata 加此选项可以下载元数据 # 下载相关的key文件 [root@repo-server ~]#wget -P /var/www/html/epel/ https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8 # 下面两个步骤只有没meta数据才需要执行 #[root@centos8 ~]#dnf -y install createrepo httpd #[root@centos8 ~]#createrepo /var/www/html/epel/ [root@centos8 ~]#ls /var/www/html/epel/ Packages repodata [root@centos8 ~]#systemctl start httpd [root@repo-client ~]#cat /etc/yum.repos.d/test.repo [BaseOS] name=BaseOS baseurl=http://10.0.0.8/centos/8/BaseOS gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [AppStream] name=Appstream baseurl=http://10.0.0.8/centos/8/AppStream/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [extras] name=extras baseurl=http://10.0.0.8/centos/extras/ [epel] name=epel baseurl=http://10.0.0.8/epel/ gpgkey=http://10.0.0.8/epel/RPM-GPG-KEY-EPEL-8 [root@repo-client ~]#yum repolist extras 1.6 MB/s | 4.9 kB 00:00 epel 88 MB/s | 6.2 MB 00:00 repo id repo name status AppStream Appstream 4,755 BaseOS BaseOS 1,659 epel epel 5,206 extras extras 12 [root@repo-client ~]#dnf install openvpn
4.3.12 DNF 介绍
DNF,即DaNdiFied,是新一代的RPM软件包管理器。DNF 发行日期是2015年5月11日,DNF 包管理器采用Python 编写,发行许可为GPL v2,首先出现在Fedora 18 发行版中。在 RHEL 8.0 版本正式取代了 YUM,DNF包管理器克服了YUM包管理器的一些瓶颈,提升了包括用户体验,内存占用,依赖分析,运行速度等 。
配置文件:
/etc/dnf/dnf.conf
仓库文件:
/etc/yum.repos.d/ *.repo
日志文件:
/var/log/dnf.rpm.log
/var/log/dnf.log
DNF 使用帮助:man dnf
dnf 用法与yum一致
dnf [options] <command> [<arguments>...] dnf --version dnf repolist dnf reposync dnf install httpd dnf remove httpd dnf clean all dnf makecache dnf list installed dnf list available dnf search nano dnf history undo 1
4.3.13 yum Troubleshooting
yum 和 dnf 失败最主要原因:
yum的配置文件格式或路径错误
解决方法:检查/etc/yum.repos.d/*.repo文件格式
yum cache
解决方法:yum clean all
网络不通:
解决方法:网卡配
练习一
自建yum仓库,分别为网络源和本地源
在windows上安装软件时我们一般需要将软件从网页上下载下来,然后安装。在linux,想要安装软件,方法比较多。
出于安全起见,有些企业内部服务器使用本地局域网而不连接外网,为了安装一些服务,就需要搭建本地的yum仓库。
在系统安装盘内,已经自带了大量必要的安装包,可以用做本地yum仓库的源;
1,如果是软件所在位置来看,有本地,或者其他yun服务器。
2,按照软件包的文件形式来看,有源码包和rpm软件包。
rpm包安装有直接的rpm包安装或者现在为了解决rpm包安装时的依赖问题的yum(dnf)安装。yum安装本质上还是底层调用rpm包安装。
接下来为第一种情况来配置yum源,在yum安装软件包时,yum时根据系统的yum源的配置来安装软件的。yum源所指是本地的文件就是本地源,指向其他服务器就是网络源。
在yum安装软件时就会去yum所指的文件路径中去寻找软件,找不到就会报错。
yum源配置文件主要有2个。
/etc/yum.conf:为所有仓库提供公共配置
/etc/yum.repos.d/*.repo 这里面的*.repo文件。为仓库的指向提供配置
配置本地源
# 挂载光盘至某目录,例如/mnt/cdrom [root@centos7 ~]# mkdir /mnt/cdrom [root@centos7 ~]# mount /dev/cdrom /mnt/cdrom mount: /dev/sr0 is write-protected, mounting read-only # 查看repodata仓库 [root@centos7 ~]# cd /mnt/cdrom/ [root@centos7 cdrom]# pwd /mnt/cdrom [root@centos7 cdrom]# ls CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7 EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL # 创建配置文件 [root@centos7 cdrom]# vim /etc/yum.repos.d/CentOS-local.repo [root@centos7 cdrom]# cat /etc/yum.repos.d/CentOS-local.repo [cdrom] name=cdrom baseurl=file:///mnt/cdrom/ gpfcheck=0 # 查看仓库列表 [root@centos7 ~]# yum repolist enabled Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.ustc.edu.cn * extras: mirrors.163.com * updates: mirrors.163.com repo id repo name status cdrom cdrom 4,071 base/7/x86_64 CentOS-7 - Base 10,072 extras/7/x86_64 CentOS-7 - Extras 448 updates/7/x86_64 CentOS-7 - Updates 1,155 repolist: 15,746 [root@centos7 ~]#
配置网络源
# 从阿里云官方下载对应的CentOS-Base.repo [root@centos7 ~]# ls /etc/yum.repos.d/ CentOS-local.repo [root@centos7 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2523 100 2523 0 0 6618 0 --:--:-- --:--:-- --:--:-- 6604 [root@centos7 ~]# ls /etc/yum.repos.d/ CentOS-Base.repo CentOS-local.repo [root@centos7 ~]# cat /etc/yum.repos.d/CentOS-Base.repo # CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # # [base] name=CentOS-$releasever - Base - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #released updates [updates] name=CentOS-$releasever - Updates - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #contrib - packages by Centos Users [contrib] name=CentOS-$releasever - Contrib - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 # 查看仓库列表 [root@centos7 ~]# yum clean all Loaded plugins: fastestmirror Cleaning repos: base cdrom extras updates Cleaning up list of fastest mirrors Other repos take up 19 M of disk space (use --verbose for details) [root@centos7 ~]# yum makecache Loaded plugins: fastestmirror Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; No route to host" Trying other mirror. http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error" Trying other mirror. base | 3.6 kB 00:00:00 cdrom | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/14): base/7/x86_64/group_gz | 153 kB 00:00:01 (2/14): base/7/x86_64/primary_db | 6.1 MB 00:00:11 (3/14): cdrom/group_gz | 153 kB 00:00:00 (4/14): cdrom/primary_db | 3.3 MB 00:00:00 (5/14): cdrom/other_db | 1.3 MB 00:00:00 (6/14): cdrom/filelists_db | 3.3 MB 00:00:00 (7/14): base/7/x86_64/filelists_db | 7.2 MB 00:00:14 (8/14): extras/7/x86_64/filelists_db | 224 kB 00:00:02 (9/14): extras/7/x86_64/primary_db | 222 kB 00:00:03 (10/14): extras/7/x86_64/other_db | 134 kB 00:00:00 (11/14): base/7/x86_64/other_db | 2.6 MB 00:00:03 (12/14): updates/7/x86_64/filelists_db | 2.7 MB 00:00:06 (13/14): updates/7/x86_64/other_db | 334 kB 00:00:00 (14/14): updates/7/x86_64/primary_db | 4.7 MB 00:00:09 Metadata Cache Created [root@centos7 ~]# yum repolist enabled Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com repo id repo name status base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072 cdrom cdrom 4,071 extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 448 updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,155 repolist: 15,746 [root@centos7 ~]#
重要知识点回顾二
程序包编译
1.源码编译介绍
程序包编译安装:
源代码-->预处理-->编译-->汇编-->链接-->执行
多文件:文件中的代码之间,很可能存在跨文件依赖关系
虽然有很多开源软件将软件打成包,供人们使用,但并不是所有源代码都打成包,如果想使用开源软件,可能需要自已下载源码,进行编译安装。另外即使提供了包,但是生产中需要用于软件的某些特性,仍然需要自行编译安装。但是利用源代码编译安装是比较繁琐的,庆幸的是有相关的项目管理工具可以大大减少编译过程的复杂度 。
2.开源程序源代码的获取
项目官方自建站点:
apache.org (ASF:Apache Software Foundation)
mariadb.org
...
代码托管:
Github.com
gitee.com
SourceForge.net
code.google.com
3.编译源码的项目工具
C、C++的源码编译:使用 make 项目管理器
configure脚本 --> Makefile.in --> Makefile
相关开发工具:
autoconf: 生成configure脚本
automake:生成Makefile.in
java的源码编译: 使用 maven
4.C语言源代码编译安装过程
利用编译工具,通常只需要三个大的步骤
./configure
(1) 通过选项传递参数,指定安装路径、启用特性等;执行时会参考用户的指定以及Makefile.in文
件生成Makefile
(2) 检查依赖到的外部环境,如依赖的软件包
make 根据Makefile文件,会检测依赖的环境,进行构建应用程序,
make install 复制文件到相应路径
注意:安装前可以通过查看README,INSTALL获取帮助
4.1编译安装准备
准备:安装相关的依赖包
开发工具:make, gcc (c/c++编译器GNU C Complier) 开发环境:开发库(glibc:标准库),头文件,可安装开发包组 Development Tools 软件相关依赖包
生产实践:基于最小化安装的系统建议安装下面相关包
yum install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages
4.2 编译安装
第一步:运行 configure 脚本,生成Makefile 文件
其选项主要功能:
可以指定安装位置
指定启用的特性
获取其支持使用的选项
./configure --help
选项分类:
安装路径设定:
--prefix=/PATH:指定默认安装位置,默认为/usr/local/
--sysconfdir=/PATH:配置文件安装位置
System types:支持交叉编译
软件特性和相关指定:
Optional Features: 可选特性
--disable-FEATURE
--enable-FEATURE[=ARG]
Optional Packages: 可选包
--with-PACKAGE[=ARG] 依赖包
--without-PACKAGE 禁用依赖关系
注意:通常被编译操作依赖的程序包,需要安装此程序包的“开发”组件,其包名一般类似于namedevel-VERSION
第二步:make
第三步:make install
4.3安装后的配置
1. 二进制程序目录导入至PATH环境变量中
编辑文件/etc/profile.d/NAME.sh
export PATH=/PATH/TO/BIN:$PATH
2. 相关用户及文件
有些开源软件编译完成后,还需要创建相关的用户及文件
3. 导入帮助手册
编辑/etc/man.config|man_db.conf文件,添加一个MANPATH
范例:centos8 编译安装 httpd-2.4.43
# 安装前准备:关闭防火墙和SELinux # 1 安装包 [root@centos8 ~]#dnf install gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config # 2 下载并解压缩包 [root@centos8 ~]#tar xvf httpd-2.4.43.tar.bz2 -C /usr/local/src # 3 配置 [root@centos8 ~]#cd /usr/local/src/httpd-2.4.43/ [root@centos8 httpd-2.4.43]#./configure --prefix=/apps/httpd24 -- sysconfdir=/etc/httpd24 --enable-ssl # 4 编译并安装 [root@centos8 httpd-2.4.43]#make -j 4 && make install # 5 配置环境 [root@centos8 ~]#echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/httpd24.sh [root@centos8 ~]#. /etc/profile.d/httpd24.sh # 6运行 [root@centos8 ~]#apachectl # 7 指定用apache用户运行 [root@centos8 ~]#useradd -r -s /sbin/nologin -d /var/www -c Apache -u 48 apache [root@centos8 ~]#vim /etc/httpd24/httpd.conf user apache group apache # 7生效和验证 [root@centos8 ~]#apachectl restart # 查看 [root@centos8 ~]#ps aux
练习二
编译安装http2.4
第一步:查看httpd的基本信息
[root@centos7 ~]# rpm -qi httpd package httpd is not installed [root@centos7 ~]# yum provides httpd Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com httpd-2.4.6-93.el7.centos.x86_64 : Apache HTTP Server Repo : cdrom httpd-2.4.6-95.el7.centos.x86_64 : Apache HTTP Server Repo : base httpd-2.4.6-97.el7.centos.x86_64 : Apache HTTP Server Repo : updates [root@centos7 ~]# mount /dev/cdrom /mnt/cdrom/ mount: /dev/sr0 is write-protected, mounting read-only [root@centos7 ~]# find /mnt/cdrom/ -name httpd* /mnt/cdrom/Packages/httpd-2.4.6-93.el7.centos.x86_64.rpm /mnt/cdrom/Packages/httpd-devel-2.4.6-93.el7.centos.x86_64.rpm /mnt/cdrom/Packages/httpd-manual-2.4.6-93.el7.centos.noarch.rpm /mnt/cdrom/Packages/httpd-tools-2.4.6-93.el7.centos.x86_64.rpm [root@centos7 ~]# rpm -qpi /mnt/cdrom/Packages/httpd-2.4.6-93.el7.centos.x86_64.rpm Name : httpd Version : 2.4.6 Release : 93.el7.centos Architecture: x86_64 Install Date: (not installed) Group : System Environment/Daemons Size : 9821040 License : ASL 2.0 Signature : RSA/SHA256, Sat 04 Apr 2020 04:53:10 AM CST, Key ID 24c6a8a7f4a80eb5 Source RPM : httpd-2.4.6-93.el7.centos.src.rpm Build Date : Thu 02 Apr 2020 09:15:44 PM CST Build Host : x86-01.bsys.centos.org Relocations : (not relocatable) Packager : CentOS BuildSystem <http://bugs.centos.org> Vendor : CentOS URL : http://httpd.apache.org/ Summary : Apache HTTP Server Description : The Apache HTTP Server is a powerful, efficient, and extensible web server. [root@centos7 ~]#
第二步:从官方网址http://httpd.apache.org/下载2.4版本的源码包到/usr/local/sr
[root@centos7 ~]# ls /usr/local/src/ [root@centos7 ~]# wget -O /usr/local/src/httpd-2.4.46.tar.bz2 https://mirror.bit.edu.cn/apache//httpd/httpd-2.4.46.tar.bz2 --2021-01-03 06:37:07-- https://mirror.bit.edu.cn/apache//httpd/httpd-2.4.46.tar.bz2 Resolving mirror.bit.edu.cn (mirror.bit.edu.cn)... 114.247.56.117, 2001:da8:204:1205::22 Connecting to mirror.bit.edu.cn (mirror.bit.edu.cn)|114.247.56.117|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 7187805 (6.9M) [application/octet-stream] Saving to: ‘/usr/local/src/httpd-2.4.46.tar.bz2’ 100%[=======================================================>] 7,187,805 1.02MB/s in 6.8s 2021-01-03 06:37:14 (1.01 MB/s) - ‘/usr/local/src/httpd-2.4.46.tar.bz2’ saved [7187805/7187805] [root@centos7 ~]# ll -h /usr/local/src/ total 6.9M -rw-r--r-- 1 root root 6.9M Aug 5 19:32 httpd-2.4.46.tar.bz2 [root@centos7 ~]# cd /usr/local/src/ [root@centos7 src]# pwd /usr/local/src [root@centos7 src]# tar -xf httpd-2.4.46.tar.bz2 -C /usr/local/src [root@centos7 src]# ll -h total 6.9M drwxr-sr-x 11 root 40 4.0K Aug 1 22:11 httpd-2.4.46 -rw-r--r-- 1 root root 6.9M Aug 5 19:32 httpd-2.4.46.tar.bz2 [root@centos7 src]#
第三步:安装前准备:关闭防火墙和SELinux,并安装基本包
[root@centos7 src]# yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile ... ... ... Nothing to do [root@centos7 httpd-2.4.46]# mkdir -p /apps/httpd24
第四步:编译安装,并解决依赖包
[root@centos7 httpd-2.4.46]# ./configure --prefix=/apps/httpd24 --sysconfdir=/etc/http24 --enable-ssl checking for chosen layout... Apache ... ... ... checking for APR... no configure: error: APR not found. Please read the documentation. [root@centos7 httpd-2.4.46]# yum search apr Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com ======================================= N/S matched: apr ======================================== apr-devel.x86_64 : APR library development kit apr-devel.i686 : APR library development kit apr-util-devel.i686 : APR utility library development kit apr-util-devel.x86_64 : APR utility library development kit apr-util-ldap.x86_64 : APR utility library LDAP support apr-util-mysql.x86_64 : APR utility library MySQL DBD driver apr-util-nss.x86_64 : APR utility library NSS crytpo support apr-util-odbc.x86_64 : APR utility library ODBC DBD driver apr-util-openssl.x86_64 : APR utility library OpenSSL crytpo support apr-util-pgsql.x86_64 : APR utility library PostgreSQL DBD driver apr-util-sqlite.x86_64 : APR utility library SQLite DBD driver pcp-pmda-haproxy.x86_64 : Performance Co-Pilot (PCP) metrics for HAProxy apr.x86_64 : Apache Portable Runtime library apr.i686 : Apache Portable Runtime library apr-util.i686 : Apache Portable Runtime Utility library apr-util.x86_64 : Apache Portable Runtime Utility library haproxy.x86_64 : TCP/HTTP proxy and load balancer for high availability environments Name and summary matches only, use "search all" for everything. [root@centos7 httpd-2.4.46]# yum -y install apr-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Installed: apr-devel.x86_64 0:1.4.8-7.el7 Complete! [root@centos7 httpd-2.4.46]# ./configure --prefix=/apps/httpd24 --sysconfdir=/etc/http24 --enable-ssl ... ... ... checking for APR-util... no configure: error: APR-util not found. Please read the documentation. [root@centos7 httpd-2.4.46]# yum search APR-util Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com ===================================== N/S matched: APR-util ===================================== apr-util.i686 : Apache Portable Runtime Utility library apr-util.x86_64 : Apache Portable Runtime Utility library apr-util-devel.i686 : APR utility library development kit apr-util-devel.x86_64 : APR utility library development kit apr-util-ldap.x86_64 : APR utility library LDAP support apr-util-mysql.x86_64 : APR utility library MySQL DBD driver apr-util-nss.x86_64 : APR utility library NSS crytpo support apr-util-odbc.x86_64 : APR utility library ODBC DBD driver apr-util-openssl.x86_64 : APR utility library OpenSSL crytpo support apr-util-pgsql.x86_64 : APR utility library PostgreSQL DBD driver apr-util-sqlite.x86_64 : APR utility library SQLite DBD driver Name and summary matches only, use "search all" for everything. [root@centos7 httpd-2.4.46]# yum -y install apr-util-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Installed: apr-util-devel.x86_64 0:1.5.2-6.el7 Complete! [root@centos7 httpd-2.4.46]# ./configure --prefix=/apps/httpd24 --sysconfdir=/etc/http24 --enable-ssl checking for chosen layout... Apache ... ... ... config.status: creating build/config_vars.sh config.status: creating include/ap_config_auto.h config.status: executing default commands configure: summary of build options: Server Version: 2.4.46 Install prefix: /apps/httpd24 C compiler: gcc -std=gnu99 CFLAGS: -pthread CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE LDFLAGS: LIBS: C preprocessor: gcc -E [root@centos7 httpd-2.4.46]# ll -t total 2364 -rw-r--r-- 1 root 40 109230 Jan 3 07:23 config.log drwxr-sr-x 6 root 40 4096 Jan 3 07:23 build drwxr-sr-x 28 root 40 4096 Jan 3 07:23 modules -rw-r--r-- 1 root 40 11381 Jan 3 07:23 Makefile -rwxr-xr-x 1 root 40 43745 Jan 3 07:23 config.status -rw-r--r-- 1 root 40 1238 Jan 3 07:23 modules.c -rwxr-xr-x 1 root 40 131 Jan 3 07:23 config.nice -rwxr-xr-x 1 root 40 1208523 Aug 1 22:11 configure -rw-r--r-- 1 root 40 17313 Aug 1 22:11 httpd.spec drwxr-sr-x 10 root 40 145 Aug 1 22:11 docs -rw-r--r-- 1 root 40 290399 Aug 1 22:06 CHANGES -rw-r--r-- 1 root 40 25656 Apr 23 2020 acinclude.m4 -rw-r--r-- 1 root 40 720 Jan 2 2020 NOTICE -rw-r--r-- 1 root 40 47641 Oct 25 2019 CMakeLists.txt -rw-r--r-- 1 root 40 403 Nov 22 2004 emacs-style [root@centos7 httpd-2.4.46]# make -j 4 && make install ... ... ... make[2]: Entering directory `/usr/local/src/httpd-2.4.46/support' make[2]: Leaving directory `/usr/local/src/httpd-2.4.46/support' Installing configuration files mkdir /etc/http24 mkdir /apps/httpd24/build Installing man pages and online manual mkdir /apps/httpd24/man mkdir /apps/httpd24/man/man1 mkdir /apps/httpd24/man/man8 mkdir /apps/httpd24/manual make[1]: Leaving directory `/usr/local/src/httpd-2.4.46' [root@centos7 httpd-2.4.46]#
第五步:修改PATH变量 ,指定用apache用户运行 ,运行man帮助
[root@centos7 httpd-2.4.46]# ls /apps/httpd24/ bin build cgi-bin error htdocs icons include logs man manual modules [root@centos7 httpd-2.4.46]# ls /apps/httpd24/bin/ ab apxs dbmmanage envvars-std htcacheclean htdigest httpd logresolve apachectl checkgid envvars fcgistarter htdbm htpasswd httxt2dbm rotatelogs [root@centos7 httpd-2.4.46]# echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/httpd24.sh [root@centos7 httpd-2.4.46]# . /etc/profile.d/httpd24.sh [root@centos7 httpd-2.4.46]# useradd -r -s /sbin/nologin -d /var/www -c Apachee -u 48 apache [root@centos7 httpd-2.4.46]# vim /etc/http24//httpd.conf user apache group apache [root@centos7 httpd-2.4.46]# apachectl restart AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::a2d4:689b:ab91:9a35. Set the 'ServerName' directive globally to suppress this message [root@centos7 httpd-2.4.46]# ps aux | grep httpd root 21606 0.0 0.1 103756 3304 ? Ss 07:43 0:00 /apps/httpd24/bin/httpd apache 21702 0.0 0.2 456120 4328 ? Sl 07:46 0:00 /apps/httpd24/bin/httpd apache 21703 0.0 0.2 456120 4324 ? Sl 07:46 0:00 /apps/httpd24/bin/httpd apache 21704 0.0 0.2 456120 4328 ? Sl 07:46 0:00 /apps/httpd24/bin/httpd root 21787 0.0 0.0 112812 968 pts/0 S+ 07:46 0:00 grep --color=auto httpd [root@centos7 httpd-2.4.46]# tree /apps/httpd24/ -d /apps/httpd24/ ├── bin ├── build ├── cgi-bin ├── error │ └── include ├── htdocs ├── icons │ └── small ├── include ├── logs ├── man │ ├── man1 │ └── man8 ├── manual │ ├── developer │ ├── faq │ ├── howto │ ├── images │ ├── misc │ ├── mod │ ├── platform │ ├── programs │ ├── rewrite │ ├── ssl │ ├── style │ │ ├── css │ │ ├── lang │ │ ├── latex │ │ ├── scripts │ │ └── xsl │ │ └── util │ └── vhosts └── modules 33 directories [root@centos7 httpd-2.4.46]# vim /etc/man_db.conf MANDATORY_MANPATH /apps/httpd24/man [root@centos7 httpd-2.4.46]# man httpd HTTPD(8) httpd HTTPD(8) NAME httpd - Apache Hypertext Transfer Protocol Server SYNOPSIS httpd [ -d serverroot ] [ -f config ] [ -C directive ] [ -c directive ] [ -D parameter ] [ -e level ] [ -E file ] [ -k start|restart|graceful|stop|graceful-stop ] [ -h ] [ -l ] [ -L ] [ -S ] [ -t ] [ -v ] [ -V ] [ -X ] [ -M ] [ -T ] On Windows systems, the following additional arguments are available: httpd [ -k install|config|uninstall ] [ -n name ] [ -w ] SUMMARY httpd is the Apache HyperText Transfer Protocol (HTTP) server program. It is designed to be run as a standalone daemon process. When used like this it will create a pool of child processes or threads to handle requests. In general, httpd should not be invoked directly, but rather should be invoked via apachectl on Unix-based systems or as a service on Windows NT, 2000 and XP and as a console application on Windows 9x and ME. OPTIONS -d serverroot Set the initial value for the ServerRoot directive to serverroot. This can be overridden by the ServerRoot directive in the configuration file. The default is Manual page httpd(8) line 1 (press h for help or q to quit) [root@centos7 httpd-2.4.46]#
第六步:生效和验证
[root@centos7 httpd-2.4.46]# apachectl restart AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::a2d4:689b:ab91:9a35. Set the 'ServerName' directive globally to suppress this message [root@centos7 httpd-2.4.46]# ps aux | grep httpd root 21606 0.0 0.1 103756 3304 ? Ss 07:43 0:00 /apps/httpd24/bin/httpd apache 21702 0.0 0.2 456120 4328 ? Sl 07:46 0:00 /apps/httpd24/bin/httpd apache 21703 0.0 0.2 456120 4324 ? Sl 07:46 0:00 /apps/httpd24/bin/httpd apache 21704 0.0 0.2 456120 4328 ? Sl 07:46 0:00 /apps/httpd24/bin/httpd root 21787 0.0 0.0 112812 968 pts/0 S+ 07:46 0:00 grep --color=auto httpd [root@centos7 httpd-2.4.46]# curl http://10.0.0.10/ <html><body><h1>It works!</h1></body></html> [root@centos7 httpd-2.4.46]#
重要知识点回顾三
1.sed 工作原理
sed 即 Stream EDitor,和 vi 不同,sed是行编辑器
Sed是从文件或管道中读取一行,处理一行,输出一行;再读取一行,再处理一行,再输出一行,直到
最后一行。每当处理一行时,把当前处理的行存储在临时缓冲区中,称为模式空间(Pattern
Space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下
一行,这样不断重复,直到文件末尾。一次处理一行的设计模式使得sed性能很高,sed在读取大文件时
不会出现卡顿的现象。如果使用vi命令打开几十M上百M的文件,明显会出现有卡顿的现象,这是因为
vi命令打开文件是一次性将文件加载到内存,然后再打开。Sed就避免了这种情况,一行一行的处理,
打开速度非常快,执行速度也很快
2.sed 基本用法
sed [option]... 'script;script;...' inputfile...
常用选项:
-n 不输出模式空间内容到屏幕,即不自动打印
-e 多点编辑
-f /PATH/SCRIPT_FILE 从指定文件中读取编辑脚本
-r, -E 使用扩展正则表达式
-i.bak 备份文件并原处编辑
script格式:
'地址命令'
地址格式:
1. 不给地址:对全文进行处理 2. 单地址: #:指定的行,$:最后一行 /pattern/:被此处模式所能够匹配到的每一行 3. 地址范围: #,# #从#行到第#行,3,6 从第3行到第6行 #,+# #从#行到+#行,3,+4 表示从3行到第7行 /pat1/,/pat2/ #,/pat/ 4. 步进:~ 1~2 奇数行 2~2 偶数行
命令
p 打印当前模式空间内容,追加到默认输出之后 Ip 忽略大小写输出 d 删除模式空间匹配的行,并立即启用下一轮循环 a [\\]text 在指定行后面追加文本,支持使用\n实现多行追加 i [\\]text 在行前面插入文本 c [\\]text 替换行为单行或多行文本 w /path/file 保存模式匹配的行至指定文件 r /path/file 读取指定文件的文本至模式空间中匹配到的行后 = 为模式空间中的行打印行号 ! 模式空间中匹配行取反处理 s/pattern/string/ 修饰符 查找替换,支持使用其它分隔符,可以是其它形式:s@@@,s###替换修饰符: g 行内全局替换 p 显示替换成功的行 w /PATH/FILE 将替换成功的行保存至文件中 I,i 忽略大小写
范例:常见用法
[root@centos7 ~]# seq 10 | sed -n '3,6p' 3 4 5 6 [root@centos7 ~]# nl /etc/passwd | sed -n '3,6p' 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync [root@centos7 ~]# nl /etc/passwd | sed -n '3,+3p' 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync [root@centos7 ~]# sed -n '/^d/,/^s/p' /etc/passwd | nl 1 daemon:x:2:2:daemon:/sbin:/sbin/nologin 2 adm:x:3:4:adm:/var/adm:/sbin/nologin 3 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 4 sync:x:5:0:sync:/sbin:/bin/sync 5 dbus:x:81:81:System message bus:/:/sbin/nologin 6 polkitd:x:999:998:User for polkitd:/:/sbin/nologin 7 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [root@centos7 ~]# seq 10 | sed -n '1~2p' 1 3 5 7 9 [root@centos7 ~]# seq 10 | sed -n '2~2p' 2 4 6 8 10 [root@centos7 ~]# sed -n '/ROOT/Ip' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos7 ~]# sed -n '/^ROOT/Ip' /etc/passwd root:x:0:0:root:/root:/bin/bash [root@centos7 ~]# seq 10 | sed '5a abc' 1 2 3 4 5 abc 6 7 8 9 10 [root@centos7 ~]# seq 10 | sed '5aabc' 1 2 3 4 5 abc 6 7 8 9 10 [root@centos7 ~]# seq 10 | sed '5a\ abc' 1 2 3 4 5 abc 6 7 8 9 10 [root@centos7 ~]# seq 10 | sed '5c\abc' 1 2 3 4 abc 6 7 8 9 10 [root@centos7 ~]# seq 10 | sed '5cabc' 1 2 3 4 abc 6 7 8 9 10 [root@centos7 ~]# seq 10 | sed -e '5a\abc' -e '5i\123' 1 2 3 4 123 5 abc 6 7 8 9 10 [root@centos7 ~]# sed -n '/root/w /data/sed.log' /etc/passwd [root@centos7 ~]# cat /data/sed.log root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos7 ~]# cat /etc/issue \S Kernel \r on an \m [root@centos7 ~]# seq 10 | sed '3,6r /etc/issue' 1 2 3 \S Kernel \r on an \m 4 \S Kernel \r on an \m 5 \S Kernel \r on an \m 6 \S Kernel \r on an \m 7 8 9 10 [root@centos7 ~]# # 删除所有以#开头的行 [root@centos8 ~]#sed -i '/^#/d' fstab # 只显示非#开头的行 [root@centos8 ~]#sed -n '/^#/!p' fstab
备注:以上指令只是显示sed模式空间,不对源文件进行修改。
范例:备份文件并原处编辑
[root@centos7 ~]# cp -a /etc/passwd /data/ [root@centos7 ~]# cd /data [root@centos7 data]# ls a1sh a3sh a.txt disk.sh passwd sed.log test a2sh a4sh CentOS-local.repo f1.txt script systeminfo.sh user.txt [root@centos7 data]# sed -n '/root/p' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos7 data]# sed -i.bak '/root/d' passwd [root@centos7 data]# ls a1sh a3sh a.txt disk.sh passwd script systeminfo.sh user.txt a2sh a4sh CentOS-local.repo f1.txt passwd.bak sed.log test [root@centos7 data]# sed -n '/root/p' passwd [root@centos7 data]# sed -n '/root/p' passwd.bak root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos7 data]#
范例:在文件中指定行追加内容并备份
[root@centos7 data]# cp -a /root/.bashrc /data [root@centos7 data]# ls -a . a2sh a.txt disk.sh httpd.conf.bak script test .. a3sh .bashrc f1.txt passwd sed.log user.txt a1sh a4sh CentOS-local.repo httpd.conf passwd.bak systeminfo.sh [root@centos7 data]# cat -n .bashrc 1 # .bashrc 2 3 # User specific aliases and functions 4 5 alias rm='rm -i' 6 alias cp='cp -i' 7 alias mv='mv -i' 8 9 # Source global definitions 10 if [ -f /etc/bashrc ]; then 11 . /etc/bashrc 12 fi [root@centos7 data]# sed -i.bak '7a\alias test=ls' .bashrc [root@centos7 data]# ls -a . a2sh a.txt CentOS-local.repo httpd.conf passwd.bak systeminfo.sh .. a3sh .bashrc disk.sh httpd.conf.bak script test a1sh a4sh .bashrc.bak f1.txt passwd sed.log user.txt [root@centos7 data]# cat -n .bashrc 1 # .bashrc 2 3 # User specific aliases and functions 4 5 alias rm='rm -i' 6 alias cp='cp -i' 7 alias mv='mv -i' 8 alias test=ls 9 10 # Source global definitions 11 if [ -f /etc/bashrc ]; then 12 . /etc/bashrc 13 fi [root@centos7 data]# cat -n .bashrc.bak 1 # .bashrc 2 3 # User specific aliases and functions 4 5 alias rm='rm -i' 6 alias cp='cp -i' 7 alias mv='mv -i' 8 9 # Source global definitions 10 if [ -f /etc/bashrc ]; then 11 . /etc/bashrc 12 fi [root@centos7 data]#
范例:修改文件中指定行的内容并备份
[root@centos7 data]# cp /etc/selinux/config /data [root@centos7 data]# ls a1sh a4sh config httpd.conf passwd.bak systeminfo.sh a2sh a.txt disk.sh httpd.conf.bak script test a3sh CentOS-local.repo f1.txt passwd sed.log user.txt [root@centos7 data]# cat config 1 2 # This file controls the state of SELinux on the system. 3 # SELINUX= can take one of these three values: 4 # enforcing - SELinux security policy is enforced. 5 # permissive - SELinux prints warnings instead of enforcing. 6 # disabled - No SELinux policy is loaded. 7 SELINUX=disabled 8 # SELINUXTYPE= can take one of three values: 9 # targeted - Targeted processes are protected, 10 # minimum - Modification of targeted policy. Only selected processes are protected. 11 # mls - Multi Level Security protection. 12 SELINUXTYPE=targeted 13 14 [root@centos7 data]# sed -Ei.bak 's#^(SELINUX=).*#\1enforcing#' config [root@centos7 data]# ls a1sh a4sh config f1.txt passwd sed.log user.txt a2sh a.txt config.bak httpd.conf passwd.bak systeminfo.sh a3sh CentOS-local.repo disk.sh httpd.conf.bak script test [root@centos7 data]# cat -n config 1 2 # This file controls the state of SELinux on the system. 3 # SELINUX= can take one of these three values: 4 # enforcing - SELinux security policy is enforced. 5 # permissive - SELinux prints warnings instead of enforcing. 6 # disabled - No SELinux policy is loaded. 7 SELINUX=enforcing 8 # SELINUXTYPE= can take one of three values: 9 # targeted - Targeted processes are protected, 10 # minimum - Modification of targeted policy. Only selected processes are protected. 11 # mls - Multi Level Security protection. 12 SELINUXTYPE=targeted 13 14 [root@centos7 data]# cat -n config.bak 1 2 # This file controls the state of SELinux on the system. 3 # SELINUX= can take one of these three values: 4 # enforcing - SELinux security policy is enforced. 5 # permissive - SELinux prints warnings instead of enforcing. 6 # disabled - No SELinux policy is loaded. 7 SELINUX=disabled 8 # SELINUXTYPE= can take one of three values: 9 # targeted - Targeted processes are protected, 10 # minimum - Modification of targeted policy. Only selected processes are protected. 11 # mls - Multi Level Security protection. 12 SELINUXTYPE=targeted 13 14 [root@centos7 data]#
范例:提取网卡ip
[root@centos7 data]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:32:b5:7e brd ff:ff:ff:ff:ff:ff inet 10.0.0.10/8 brd 10.255.255.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::a2d4:689b:ab91:9a35/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@centos7 data]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255 inet6 fe80::a2d4:689b:ab91:9a35 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:32:b5:7e txqueuelen 1000 (Ethernet) RX packets 3530 bytes 284157 (277.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2420 bytes 306142 (298.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos7 data]# ifconfig ens33 | sed -n '2p' inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255 # 此处inet后有空格,且空格表示这一行中inet后所有内容 [root@centos7 data]# ifconfig ens33 | sed -n '2p' | sed 's#.*inet ##' 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255 # 此处.*前面有空格,且空格表示此行第一个空格后的所有内容 [root@centos7 data]# ifconfig ens33 | sed -n '2p' | sed 's#.*inet ##' | sed 's# .*##' 10.0.0.10 [root@centos7 data]#
范例:提取网卡ip(通用写法)
[root@centos7 data]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255 inet6 fe80::a2d4:689b:ab91:9a35 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:32:b5:7e txqueuelen 1000 (Ethernet) RX packets 4160 bytes 334278 (326.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2834 bytes 354472 (346.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos7 data]# ifconfig ens33 | sed -En '2s#^([^0-9]+)([0-9.]+).*#\2#p' 10.0.0.10 [root@centos7 data]# ifconfig ens33 | sed -En '2s#^([^0-9]+)([0-9.]+)(.*)$#\2#p' 10.0.0.10 [root@centos7 data]#
范例:取基名和目录名
# 取目录名 [root@centos7 data]# echo "/etc/sysconfig/network-scripts/" |sed -r 's#(^/.*/)([^/]+/?)#\1#' /etc/sysconfig/ # 取基名 [root@centos7 data]# echo "/etc/sysconfig/network-scripts/" |sed -r 's#(^/.*/)([^/]+/?)#\2#' network-scripts/ # 取第二个跟之前的内容 [root@centos7 data]# echo "/etc/sysconfig/network-scripts/" |sed -rn 's#(^/[^/]+)(.*)#\1#p' /etc [root@centos7 data]#
3.sed 高级用法
sed 中除了模式空间,还另外还支持保持空间(Hold Space),利用此空间,可以将模式空间中的数
据,临时保存至保持空间,从而后续接着处理,实现更为强大的功能。
常见的高级命令
P 打印模式空间开端至\n内容,并追加到默认输出之前
h 把模式空间中的内容覆盖至保持空间中
H 把模式空间中的内容追加至保持空间中
g 从保持空间取出数据覆盖至模式空间
G 从保持空间取出内容追加至模式空间
x 把模式空间中的内容与保持空间中的内容进行互换
n 读取匹配到的行的下一行覆盖至模式空间
N 读取匹配到的行的下一行追加至模式空间
d 删除模式空间中的行
D 如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输
入行,而使用合成的模式空间重新启动循环。如果模式空间不包含换行符,则会像发出d命令那样
启动正常的新循环
范例
sed -n 'n;p' FILE sed '1!G;h;$!d' FILE sed ‘N;D’FILE seq 10 |sed '3h;9G;9!d' sed '$!N;$!D' FILE sed '$!d' FILE sed ‘G’ FILE sed ‘g’ FILE sed ‘/^$/d;G’ FILE sed 'n;d' FILE sed -n '1!G;h;$p' FILE
打印偶数行
[Sat Dec 12 root@centos8 19:58~]#seq 10 | sed -n 'n;p' 2 4 6 8 10 [Sat Dec 12 root@centos8 19:58~]# #步进的写法 [Sat Dec 12 root@centos8 20:01~]#seq 10 | sed -n '2~2p' 2 4 6 8 10 [Sat Dec 12 root@centos8 20:02~]#
倒叙显示文件
[Sat Dec 12 root@centos8 20:11~]#seq 10 | sed '1!G;h;$!d' 10 9 8 7 6 5 4 3 2 1 [Sat Dec 12 root@centos8 20:12~]#
练习三
利用sed 取出ifconfig命令中本机的IPv4地址
[root@centos7 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255 inet6 fe80::a2d4:689b:ab91:9a35 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:32:b5:7e txqueuelen 1000 (Ethernet) RX packets 6199 bytes 497121 (485.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4225 bytes 494742 (483.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos7 ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.10 netmask 255.0.0.0 broadcast 10.255.255.255 inet6 fe80::a2d4:689b:ab91:9a35 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:32:b5:7e txqueuelen 1000 (Ethernet) RX packets 6266 bytes 502703 (490.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4279 bytes 501030 (489.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos7 ~]# ifconfig ens33 | sed -En '2s#([^0-9]+)([0-9.]+)(.*$)#\2#p' 10.0.0.10 [root@centos7 ~]#
删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
[root@centos7 ~]# cp -a /etc/fstab /data/ [root@centos7 ~]# cat -n /data/fstab 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Wed Nov 25 06:31:52 2020 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=340dc09a-63ab-44af-8b95-78284eef123d / xfs defaults 0 0 10 UUID=5bbea95b-2d3f-49c8-be91-2d83143b83a9 /boot xfs defaults 0 0 11 UUID=d5965256-1676-4e28-a12a-eb409335ff66 /data xfs defaults 0 0 12 UUID=158f78b4-f449-4e0d-8eca-5f4c8afa7d7e swap swap defaults 0 0 [root@centos7 ~]# ls /data/ a1sh a4sh config f1.txt httpd.conf.bak script test a2sh a.txt config.bak fstab passwd sed.log user.txt a3sh CentOS-local.repo disk.sh httpd.conf passwd.bak systeminfo.sh [root@centos7 ~]# sed -nri.bak '/^#[[:blank:]]+/s@^#[[:blank:]]+(,*)@\1@p' /data/fstab [root@centos7 ~]# ls /data/ a1sh a4sh config f1.txt httpd.conf passwd.bak systeminfo.sh a2sh a.txt config.bak fstab httpd.conf.bak script test a3sh CentOS-local.repo disk.sh fstab.bak passwd sed.log user.txt [root@centos7 ~]# cat -n /data/fstab 1 /etc/fstab 2 Created by anaconda on Wed Nov 25 06:31:52 2020 3 Accessible filesystems, by reference, are maintained under '/dev/disk' 4 See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info [root@centos7 ~]# cat -n /data/fstab.bak 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Wed Nov 25 06:31:52 2020 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=340dc09a-63ab-44af-8b95-78284eef123d / xfs defaults 0 0 10 UUID=5bbea95b-2d3f-49c8-be91-2d83143b83a9 /boot xfs defaults 0 0 11 UUID=d5965256-1676-4e28-a12a-eb409335ff66 /data xfs defaults 0 0 12 UUID=158f78b4-f449-4e0d-8eca-5f4c8afa7d7e swap swap defaults 0 0 [root@centos7 ~]#
处理/etc/fstab路径,使用sed命令取出其目录名和基名
# 取目录名 [root@centos7 ~]# echo /etc/fstab | sed -En 's#(^/.*/)([^/]+/?)#\1#p' /etc/ # 取基名 [root@centos7 ~]# echo /etc/fstab | sed -En 's#(^/.*/)([^/]+/?)#\2#p' fstab [root@centos7 ~]#
浙公网安备 33010602011771号