yum仓库的创建并编译安装httpd软件和sed用法详解

2021/02/08 11:45
阅读数 87

1、yum仓库

1.1 CenOS7系统yum仓库配置

1)自建centos7本地源

#挂载光盘至某目录,如/mnt/cdrom
[root@centos7 cd]# mount /dev/sr0 /mnt/cdrom
mount: mount point /mnt/cdrom does not exist
[root@centos7 cd]# mkdir /mnt/cdrom
[root@centos7 cd]# mount /dev/sr0 /mnt/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@centos7 cd]# ll
total 1668
-rw-r--r--.  1 root root      14 Oct 30 05:14 CentOS_BuildTag
drwxr-xr-x.  3 root root    2048 Oct 27 00:25 EFI
-rw-rw-r--. 17 root root     227 Aug 30  2017 EULA
-rw-rw-r--. 17 root root   18009 Dec 10  2015 GPL
drwxr-xr-x.  3 root root    2048 Oct 27 00:26 images
drwxr-xr-x.  2 root root    2048 Oct 27 00:25 isolinux
drwxr-xr-x.  2 root root    2048 Oct 27 00:25 LiveOS
drwxr-xr-x.  2 root root 1669120 Oct 29 22:39 Packages
drwxr-xr-x.  2 root root    4096 Oct 30 04:03 repodata
-rw-rw-r--. 17 root root    1690 Dec 10  2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 15 root root    1690 Dec 10  2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--.  1 root root    2883 Nov  2 23:15 TRANS.TBL

#创建配置文件
[root@centos7 yum.repos.d]# cat centos7.repo    #添加校验
[baseos-oneself]
name=centos7
baseurl=file:///mnt/cdrom
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#测试安装
[root@centos7 yum.repos.d]# yum install -y tree     #使用的是自建仓库baseof-oneself
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
======================================================================================
 Package            Arch       Version           Repository           Size
======================================================================================
Installing:
 tree              x86_64      1.6.0-10.el7      baseos-oneself        46 k

Transaction Summary
======================================================================================
Install  1 Package
Installed:
  tree.x86_64 0:1.6.0-10.el7
Complete!

2)自建centos7网络源

#在本地仓库基础上,添加aliyun网络源、extras源以及epel源
[root@repo-server yum.repos.d]# cat centos7.repo
[baseos-cd]
name=centos7 cd
baseurl=file:///var/www/html/centos/7
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[baseos-aliyun]
name=centos7 aliyun
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64
gpgcheck=0
enabled=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=centos7 extras
baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64
gpgcheck=0
enabled=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[epel]
name=centos7 epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

#测试,先清除缓存
[root@repo-server yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Cleaning repos: baseos-aliyun baseos-cd epel extras
Cleaning up list of fastest mirrors
Other repos take up 215 M of disk space (use --verbose for details)
[root@repo-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Determining fastest mirrors
baseos-aliyun                                       | 3.6 kB  00:00:00
baseos-cd                                           | 3.6 kB  00:00:00
epel                                                | 4.7 kB  00:00:00
extras                                              | 2.9 kB  00:00:00
(1/8): baseos-cd/group_gz                           | 153 kB  00:00:00
(2/8): baseos-cd/primary_db                         | 6.1 MB  00:00:00
(3/8): epel/group_gz                                |  95 kB  00:00:01
(4/8): baseos-aliyun/primary_db                     | 6.1 MB  00:00:03
(5/8): epel/updateinfo                              | 1.0 MB  00:00:03
(6/8): baseos-aliyun/group_gz                       | 153 kB  00:00:03
(7/8): extras/primary_db                            | 222 kB  00:00:02
(8/8): epel/primary_db                              | 6.9 MB  00:00:03
repo id                   repo name                     status
baseos-aliyun             centos7 aliyun                10,072
baseos-cd                 centos7 cd                    10,072
epel                      centos7 epel                  13,512
extras                    centos7 extras                   448
repolist: 34,104
[root@repo-server yum.repos.d]#

#测试extras源,安装epel
[root@repo-server yum.repos.d]# yum provides epel
No matches found
[root@repo-server yum.repos.d]# yum search epel
epel-release.noarch : Extra Packages for Enterprise Linux repository configuration
[root@repo-server yum.repos.d]# yum install epel-release
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
======================================================================================
 Package             Arch            Version            Repository            Size
======================================================================================
Installing:
 epel-release       noarch           7-11               extras                15 k

Transaction Summary
======================================================================================
Install  1 Package
Installed:
  epel-release.noarch 0:7-11
Complete!

#测试epel源安装sl软件
[root@repo-server yum.repos.d]# yum install sl -y
Resolving Dependencies
--> Running transaction check
---> Package sl.x86_64 0:5.02-1.el7 will be installed
--> Finished Dependency Resolution
======================================================================================
 Package             Arch              Version            Repository            Size
======================================================================================
Installing:
 sl                  x86_64            5.02-1.el7         epel                  14 k

Transaction Summary
======================================================================================
Install  1 Package
Installed:
  sl.x86_64 0:5.02-1.el7
Complete!

1.2 CenOS8系统yum仓库配置

1)centos8自建本地源

#挂载光盘到/mnt/cdrom
[root@centos8 yum.repos.d]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part
  ├─cl-root 253:0    0   18G  0 lvm  /
  └─cl-swap 253:1    0    1G  0 lvm  [SWAP]
sr0          11:0    1  7.7G  0 rom
[root@centos8 yum.repos.d]# mount /dev/sr0 /mnt/cdrom
mount: /mnt/cdrom: mount point does not exist.
[root@centos8 yum.repos.d]# mkdir /mnt/cdrom
[root@centos8 yum.repos.d]# mount /dev/sr0 /mnt/cdrom
mount: /mnt/cdrom: WARNING: device write-protected, mounted read-only.

#先备份源配置文件
[root@centos8 yum.repos.d]# find -name "*.repo" -exec mv {} {}.bak \;
[root@centos8 yum.repos.d]# ll
total 20
drwxr-xr-x. 2 root root 4096 Jan 24 07:37 backup
-rw-r--r--. 1 root root  187 Jan 24 03:43 centos-appstream.repo.bak
-rw-r--r--. 1 root root  180 Jan 24 03:48 centos-base.repo.bak
-rw-r--r--. 1 root root 2595 Dec 26 06:31 CentOS-Base.repo.bak
-rw-r--r--. 1 root root  698 Jan 24 06:41 epel8.repo.bak

#手动配置repo,centos8需要配置两个仓库,BaseOS和AppStream
[root@centos8 yum.repos.d]# cat centos8.repo
[baseos8-oneself]
name=centos8 baseos by oneself
baseurl=file:///mnt/cdrom/BaseOS
enabled=1

[appstream8-oneself]
name=centos8 appstream by oneself
baseurl=file:///mnt/cdrom/AppStream
enabled=1

#测试
[root@centos8 yum.repos.d]# yum install -y tree
Last metadata expiration check: 0:02:37 ago on Sun 24 Jan 2021 07:43:40 AM EST.
Dependencies resolved.
======================================================================================
 Package        Architecture        Version        Repository            Size
=======================================================================================Installing:
 tree          x86_64             1.7.0-15.el8     baseos8-oneself      59 k

Transaction Summary
======================================================================================
Install  1 Package
Installed:
  tree-1.7.0-15.el8.x86_64

Complete!

2)CentOS8配置 网络源

#和CentOS7系统的配置相同,在本地源的基础上添加网络源和epel、extras源
[root@centos8 ~]#cat /etc/yum.repos.d/base.repo
[baseos8-oneself]
name=centos8 baseos by oneself
baseurl=file:///misc/cd/BaseOS
enabled=1
gpgcheck=0

[appstream8-oneself]
name=centos8 appstream by oneself
baseurl=file:///misc/cd/AppStream
enabled=1
gpgcheck=0

[baseos8-aliyun]
name=centos8 baseos by aliyun
baseurl=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
gpgcheck=0

[appstream8-aliyun]
name=centos8 appstream by aliyun
baseurl=https://mirrors.aliyun.com/centos/8/AppStream/x86_64/os/
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

[root@repo8-server yum.repos.d]# yum clean all
39 files removed
[root@repo8-server yum.repos.d]# yum repolist --all
repo id                   repo name                               status
appstream8-aliyun         centos8 appstream by aliyun             enabled
appstream8-oneself        centos8 appstream by oneself            enabled
baseos8-aliyun            centos8 baseos by aliyun                enabled   #aliyun仓库
baseos8-oneself           centos8 baseos by oneself               enabled   #本地
epel                      EPEL                                    enabled
extras                    extras                                  enabled
[root@repo8-server yum.repos.d]# yum provides tree
Last metadata expiration check: 0:01:55 ago on Thu 28 Jan 2021 08:24:21 PM CST.
tree-1.7.0-15.el8.x86_64 : File system tree viewer
Repo        : @System
Matched from:
Provide    : tree = 1.7.0-15.el8

tree-1.7.0-15.el8.x86_64 : File system tree viewer
Repo        : baseos8-oneself               #本地仓库
Matched from:
Provide    : tree = 1.7.0-15.el8

tree-1.7.0-15.el8.x86_64 : File system tree viewer
Repo        : baseos8-aliyun                #aliyun仓库
Matched from:
Provide    : tree = 1.7.0-15.el8

1.3 实现私用 yum仓库

下载所有yum仓库的相关包和meta 数据

#CentOS 8 dnf 工具集成
dnf reposync --help #查看帮助
#默认只下载rpm包,不下载meta数据,需要指定--download-metadata 才能下载元数据
dnf reposync --repoid=REPOID --download-metadata -p /path   ##下载到指定目录

#CentOS 7 以前版本,reposync工具来自于yum-utils包
reposync --repoid=REPOID --download-metadata -p /path

创建私有yum仓库:

createrepo [options] <directory>

范例:centos7初始化仓库和元数据

#1)使用reposync工具下载元数据
[root@centos7 yum.repos.d]# yum provides reposync
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
baseos-oneself/filelists_db         | 7.2 MB  00:00:00
yum-utils-1.1.31-54.el7_8.noarch : Utilities based around the yum package manager
Repo        : baseos-oneself
Matched from:
Filename    : /usr/bin/reposync

[root@centos7 yum.repos.d]# yum install -y yum-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Dependencies Resolved

========================================================================================================================================================================
     Package         Arch              Version          Repository             Size
========================================================================================================================================================================
Installing:
 yum-utils          noarch            1.1.31-54.el7_8   baseos-oneself         122 k
Installing for dependencies:
libxml2-python      x86_64            2.9.1-6.el7.5     baseos-oneself         247 k
 python-chardet     noarch            2.2.1-3.el7       baseos-oneself         227 k
 python-kitchen     noarch            1.1.1-5.el7       baseos-oneself         267 k
Updating for dependencies:
 libxml2            x86_64            2.9.1-6.el7.5     baseos-oneself         668 k

Transaction Summary
=========================================================================================
...省略...
Complete!
[root@centos7 yum.repos.d]#

实例1:centos7系统创建局域网的基于Base和extras的私有yum源

1、yum服务器配置
#1)确保selinux和防火墙关闭,否则httpd服务无法访问
[root@centos7 ~]# grep '^SELINUX=' /etc/selinux/config
SELINUX=disabled
[root@centos7 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

#2)修改主机名,并配置httpd服务
[root@centos7 ~]# hostnamectl set-hostname repo-server
[root@centos7 ~]# logout
[root@repo-server ~]# yum install -y httpd          ##安装httpd软件
[root@repo-server ~]# systemctl enable --now httpd  ##设置开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@repo-server ~]# mkdir /var/www/html/centos/7 -p   #新建存放centos光盘的目录
[root@repo-server ~]# mount /dev/sr0 /mnt/              ##挂载光盘
[root@repo-server ~]# cp -a /mnt/* /var/www/html/centos/7   #生产环境中,要复制光盘内容

2、yum客户端配置
#1)配置yum源
[root@repo-client ~]# cd /etc/yum.repos.d
[root@repo-client yum.repos.d]# mkdir backup
[root@repo-client yum.repos.d]# mv *.repo backup
[root@repo-client yum.repos.d]# ll
drwxr-xr-x  2 root root  259 Jan 24 22:14 backup
-rw-r--r--. 1 root root 2523 Jun 16  2018 CentOS-Base.repo.backup
[root@repo-client yum.repos.d]# vim centos7-100.12.repo
[root@repo-client yum.repos.d]# cat centos7-100.12.repo
[BaseOS]
name=Centos7 BaseOS by 100.12
baseurl=http://192.168.100.12/centos/7
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#2)通过局域网中的yum源服务器,安装httpd
[root@repo-client yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: BaseOS
Cleaning up list of fastest mirrors
[root@repo-client yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
BaseOS                          | 3.6 kB  00:00:00
(1/2): BaseOS/group_gz          | 166 kB  00:00:00
(2/2): BaseOS/primary_db        | 6.0 MB  00:00:00
repo id       repo name                         status
BaseOS        Centos7 BaseOS by 100.12          10,019
repolist: 10,019
[root@repo-client yum.repos.d]# yum install httpd
...省略...
Installed:
  httpd.x86_64 0:2.4.6-95.el7.centos
Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7    apr-util.x86_64 0:1.5.2-6.el7    httpd-tools.x86_64 0:2.4.6-95.el7.centos    mailcap.noarch 0:2.1.41-2.el7
Complete!

3、下载阿里云的extras源,制作私有yum源
#下载extras仓库文件到var/www/html下
[root@repo-server yum.repos.d]# ll /var/www/html/
total 4
drwxr-xr-x. 3 root root 15 Jan 30 20:35 centos
-rw-r--r--. 1 root root 18 Jan 24 22:28 index.html
[root@repo-server yum.repos.d]# ll
total 12
drwxr-xr-x. 3 root root 4096 Jan 30 20:38 backup
-rw-r--r--  1 root root  746 Jan 30 18:01 centos7.repo
-rw-r--r--  1 root root 1946 Jun 16  2020 elrepo.repo
[root@repo-server yum.repos.d]# yum-config-manager --disable extras epel updates-aliyun
[root@repo-server yum.repos.d]# cat centos7.repo
[baseos-cd]
name=centos7 cd
baseurl=file:///var/www/html/centos/7
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[baseos-aliyun]
name=centos7 aliyun
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64
gpgcheck=0
enabled=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[updates-aliyun]
name=centos7 updates by aliyun
baseurl=https://mirrors.aliyun.com/centos/7/updates/x86_64/
gpgcheck=0
enabled = 0
[extras]
name=centos7 extras
baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[epel]
name=centos7 epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

#下载extras仓库及元数据到/var/www/html
[root@repo-server yum.repos.d]# reposync --repoid=extras --download-metadata -p /var/www/html
baseos-aliyun                                          | 3.6 kB  00:00:00
baseos-cd                                              | 3.6 kB  00:00:00
extras                                                 | 2.9 kB  00:00:00
(1/448): WALinuxAgent-2.2.38-2.el7_7.noarch.rpm        | 383 kB  00:00:02
(448/448): uboot-images-armv8-2019.07-3.el7.noarch.rpm | 4.8 MB  00:00:02
[root@repo-server yum.repos.d]# ls /var/www/html/extras/    #查看只有仓库,没有元数据
Packages
#需要安装createrepo工具来下载元数据
[root@repo-server yum.repos.d]# yum install createrepo  
[root@repo-server yum.repos.d]# createrepo /var/www/html/extras     #下载元数据
Spawning worker 0 with 224 pkgs
Spawning worker 1 with 224 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@repo-server yum.repos.d]# ls /var/www/html/extras/
Packages  repodata
[root@repo-server yum.repos.d]# ls /var/www/html/extras/repodata
2d6b84c87bbf6432ed264aebc8b21c14632bc0bed664e301277f2a82e0b1a864-primary.xml.gz
2e9fd48ed164af0d6a80c2a07dc67c09d733ea94fbde75f81cc7076405c90124-other.sqlite.bz2
6de1755ab3e4e9bd0ee8ff31b6c979fe8a2e132b66629bf4b659b0a7878fe70f-primary.sqlite.bz2
7449a9b5a4425d411cdad1ad46053117dc89c533912206598158785b910aa289-other.xml.gz
810451b024ee98636b6f0147eebde7988743f45a7c8e7478ed993ac3a102d612-filelists.sqlite.bz2
b7b314c13b2869254f021e1de291807842417150189451661839dd919036220d-filelists.xml.gz
repomd.xml

4、repo7-client客户端测试extras仓库
[root@repo-client yum.repos.d]# yum provides epel-release   #查不到epel-release包
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
BaseOS                                 | 3.6 kB  00:00:00
No matches found

#配置client中的yum源,指向服务器的extras目录
[root@repo-client yum.repos.d]# cat centos7-100.12.repo
[BaseOS]
name=Centos7 BaseOS by 100.12
baseurl=http://192.168.100.12/centos/7
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[extras]
name=centos7 extras by 100.12
baseurl=http://192.168.100.12/extras    #服务器中的extras仓库目录
gpgcheck=0
#测试,清除缓存,并重建缓存
[root@repo-client yum.repos.d]# yum clean all
[root@repo-client yum.repos.d]# du -sh /var/cache/yum/x86_64/7
0       /var/cache/yum/x86_64/7
[root@repo-client yum.repos.d]# yum makecache
...省略...
Metadata Cache Created
[root@repo-client yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                     repo name                           status
BaseOS                      Centos7 BaseOS by 100.12            10,072
extras                      centos7 extras by 100.12               448
repolist: 10,520

#client客户机与服务器端的仓库比较
[root@repo-client yum.repos.d]# du -sh /var/cache/yum/x86_64/7/*  #客户机的base和extras包
112M    /var/cache/yum/x86_64/7/BaseOS
3.9M    /var/cache/yum/x86_64/7/extras
4.0K    /var/cache/yum/x86_64/7/timedhosts
0       /var/cache/yum/x86_64/7/timedhosts.txt

[root@repo-server html]# du -sh /var/cache/yum/x86_64/7/*       #服务器中的base和extras包
112M    /var/cache/yum/x86_64/7/baseos-cd
3.9M    /var/cache/yum/x86_64/7/extras

#可以查到epel-release包的仓库是extras
[root@repo-client yum.repos.d]# yum provides epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
BaseOS                                 | 3.6 kB  00:00:00
extras                                 | 2.9 kB  00:00:00
epel-release-7-11.noarch : Extra Packages for Enterprise Linux repository configuration
Repo        : extras
[root@repo-client yum.repos.d]# yum provides epel-release   #可以安装

epel源的创建和extras相同,因文件太大,约13000多个包,未下载。

实例2:centos8系统创建局域网的基于Base的私有yum源

1)centos8系统创建局域网的本地源

1、服务器端配置yum源(本地、aliyun、epel、extras)
[root@repo8-server ~]# hostnamectl set-hostname repo8-server
[root@repo8-server ~]# logout
[root@repo8-server yum.repos.d]# cat centos8.repo
[baseos8-oneself]
name=centos8 baseos by oneself
baseurl=file:///misc/cd/BaseOS
enabled=1
gpgcheck=0
[appstream8-oneself]
name=centos8 appstream by oneself
baseurl=file:///misc/cd/AppStream
#https://mirrors.aliyun.com/centos/8/AppStream/x86_64/os/
enabled=1
gpgcheck=0
[baseos8-aliyun]
name=centos8 baseos by aliyun
baseurl=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
gpgcheck=0
[appstream8-aliyun]
name=centos8 appstream by aliyun
baseurl=https://mirrors.aliyun.com/centos/8/AppStream/x86_64/os/
gpgcheck=0
[epel]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
#http://mirrors.huaweicloud.com/epel/$releasever/Everything/$basearch
gpgcheck=0
enabled=1
[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@repo8-server yum.repos.d]# cd /var/www/html
[root@repo8-server html]# mkdir centos8
[root@repo8-server html]# mount /dev/sr0 ./centos8/
mount: /var/www/html/centos8: WARNING: device write-protected, mounted read-only.

2、客户端配置yum源
[root@repo8-client yum.repos.d]# cat centos8.repo
[baseos-repo8]
name=centos8 baseos by repo8-server
baseurl=http://192.168.100.200/centos8/BaseOS
enabled=1
[appstream-repo8]
name=centos8 appstream by repo8-server
baseurl=http://192.168.100.200/centos8/AppStream
enabled=1
#[epel]
#name=EPEL
#baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
#       http://mirrors.huaweicloud.com/epel/$releasever/Everything/$basearch
#gpgcheck=0
#enabled=1
[extras-repo8]      ##extras先禁用
name=extras by repo8-server
baseurl=http://192.168.100.200/extras
gpgcheck=0
enabled=0

3、测试查看是否有BaseOS和AppStream仓库
[root@repo8-client yum.repos.d]# yum makecache
centos8 baseos by repo8-server                     31 MB/s | 2.2 MB     00:00
centos8 appstream by repo8-server                  25 MB/s | 5.7 MB     00:00
Last metadata expiration check: 0:00:02 ago on Sat 30 Jan 2021 10:40:03 PM CST.
Metadata cache created.
[root@repo8-client yum.repos.d]# yum repolist --all
repo id               repo name                                  status
appstream-repo8       centos8 appstream by repo8-server         enabled
baseos-repo8          centos8 baseos by repo8-server            enabled
extras-repo8          extras by repo8-server                   disabled
[root@repo-client ~]#yum install httpd -y   ##安装httpd,可以正常安装

2)下载阿里云的extras源,制作私有yum源

4、服务器端下载extras仓库和元数据到/var/www/html
[root@repo8-server ~]#yum repolist
Last metadata expiration check: 0:10:14 ago on Wed 08 Apr 2020 05:42:07 PM CST.
repo id                 repo name           status
AppStream               AppStream           4,755
BaseOS                  BaseOS              1,659

[root@repo8-server html]# dnf reposync --repoid=extras --download-metadata -p /var/www/html/        ##下载extras仓库和元数据到/var/www/html下
extras                  6.8 kB/s | 1.5 kB     00:00
extras                   22 kB/s |  13 kB     00:00
(1/30): centos-release-ceph-nautilus-1.2-2.el8.noarch.rpm  28 kB/s | 8.8 kB     00:00
...省略...
(30/30): epel-release-8-8.el8.noarch.rpm                  237 kB/s |  23 kB     00:00
[root@repo8-server html]# ll
total 2
dr-xr-xr-x. 7 root root 2048 Jun  9  2020 centos8
drwxr-xr-x. 4 root root   38 Jan 30 22:50 extras
[root@repo8-server html]# ll extras/
total 8
drwxr-xr-x. 2 root root 4096 Jan 30 22:50 Packages
drwxr-xr-x. 2 root root 4096 Jan 30 22:50 repodata

5、客户端配置extras及测试
[root@repo8-client yum.repos.d]# yum repolist --all   ##先查询仓库只有BaseOS和AppStream
repo id               repo name                                  status
appstream-repo8       centos8 appstream by repo8-server         enabled
baseos-repo8          centos8 baseos by repo8-server            enabled
extras-repo8          extras by repo8-server                   disabled

[root@repo8-client yum.repos.d]# cat centos8.repo       ##增加extras
[extras-repo8]      ##extras启用
name=extras by repo8-server
baseurl=http://192.168.100.200/extras
gpgcheck=0
enabled=1

[root@repo8-client yum.repos.d]# yum repolist --all  ##配置好再查询,仓库增加了extras
repo id               repo name                                  status
appstream-repo8       centos8 appstream by repo8-server         enabled
baseos-repo8          centos8 baseos by repo8-server            enabled
extras-repo8          extras by repo8-server                    enabled
[root@repo8-client yum.repos.d]# du -sh /var/cache/dnf/*
5.8M    /var/cache/dnf/appstream-repo8-d5da99a3c5bda91f
4.3M    /var/cache/dnf/appstream-repo8-filenames.solvx
2.0M    /var/cache/dnf/appstream-repo8.solv
2.3M    /var/cache/dnf/baseos-repo8-8577e6388ac9c250
0       /var/cache/dnf/baseos-repo8-d0cce61d03fe8a13
1.3M    /var/cache/dnf/baseos-repo8-filenames.solvx
1.4M    /var/cache/dnf/baseos-repo8.solv
4.0K    /var/cache/dnf/expired_repos.json
16K     /var/cache/dnf/extras-repo8-ed5d482c423f166d
4.0K    /var/cache/dnf/extras-repo8-filenames.solvx
16K     /var/cache/dnf/extras-repo8.solv
620K    /var/cache/dnf/packages.db

#禁用其他仓库,只启用extras仓库,并查找可用的包
[root@repo8-client yum.repos.d]# yum --disablerepo=* --enablerepo="extras*" list available
Last metadata expiration check: 0:02:57 ago on Sat 30 Jan 2021 10:58:19 PM CST.
Available Packages
...省略...
centos-release-virt-common.noarch      1-2.el8                        extras-repo8
cpaste.x86_64                          1.0.0-3.el8                    extras-repo8
elrepo-release.noarch                  8.1-1.el8.elrepo      c        extras-repo8
[root@repo8-client yum.repos.d]#

#测试安装epel,来自配置的extras-repo8源
[root@repo8-client yum.repos.d]# yum install elrepo-release
Last metadata expiration check: 0:04:30 ago on Sat 30 Jan 2021 10:58:19 PM CST.
Dependencies resolved.
======================================================================================
 Package     Architecture           Version             epository              Size
=======================================================================================Installing:
 elrepo-release          noarch     8.1-1.el8.elrepo    extras-repo8           14 k

Transaction Summary
=======================================================================================Install  1 Package

3)下载阿里云的EPEL源,制作私有yum源

6、下载阿里云的epel源
[root@repo8-server ~]# cat /etc/yum.repos.d/centos8.repo    #配置中增加aliyun的epel源
[epel]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
        http://mirrors.huaweicloud.com/epel/$releasever/Everything/$basearch
gpgcheck=0
enabled=1

[root@centos8 ~]#dnf repolist --all
repo id               repo name                                  status
appstream8-aliyun     centos8 appstream by aliyun               enabled
appstream8-oneself    centos8 appstream by oneself              enabled
baseos8-aliyun        centos8 baseos by aliyun                  enabled
baseos8-oneself       centos8 baseos by oneself                 enabled
epel                  EPEL                                      enabled
extras                extras                                    enabled
[root@repo8-server ~]# du -sh /var/www/html/*
7.8G    /var/www/html/centos8
448K    /var/www/html/extras
[root@repo8-server ~]#  dnf reposync --repoid=epel --download-metadata -p /var/www/html
EPEL                                 36 kB/s | 4.7 kB     00:00
EPEL                                4.4 MB/s |  20 MB     00:04
(1/6979): 3proxy-0.8.13-1.el8.x86_64.rpm                   506 kB/s | 175 kB     00:00
...省略...
(6979/6979): xrootd-doc-5.0.3-2.el8.noarch.rpm             3.1 MB/s |  80 MB     00:26
[root@repo8-server ~]# du -sh /var/www/html/*
7.8G    /var/www/html/centos8
9.9G    /var/www/html/epel              ##epel仓库占9.9G空间
448K    /var/www/html/extras
[root@repo8-server ~]# ll /var/www/html/epel    ##仓库和元数据都下载了
total 4
drwxr-xr-x 29 root root  249 Jan 31 09:26 Packages
drwxr-xr-x  2 root root 4096 Jan 31 09:25 repodata
#下面两个步骤只有没meta数据才需要执行
#[root@repo8-server ~]# dnf -y install createrepo httpd
#[root@repo8-server ~]# createrepo /var/www/html/epel/

#查看是否有key文件,没有可以使用wget下载
[root@repo8-server ~]# find / -name "RPM*EPEL*"
/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
#下载相关的key文件
[root@repo-server ~]#wget -P /var/www/html/epel/ https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8

7、客户端配置epel及测试
[root@repo-client ~]#cat /etc/yum.repos.d/centos8.repo
[baseos-repo8]
name=centos8 baseos by repo8-server
baseurl=http://192.168.100.200/centos8/BaseOS
enabled=1
[appstream-repo8]
name=centos8 appstream by repo8-server
baseurl=http://192.168.100.200/centos8/AppStream
enabled=1
[epel-repo8]
name=epel by repo8-server
baseurl=http://192.168.100.200/epel
gpgcheck=1
enabled=1
[extras-repo8]
name=extras by repo8-server
baseurl=http://192.168.100.200/extras
gpgcheck=0
enabled=1
#测试open***包,来自配置的extras-repo8源
[root@repo8-client ~]# yum provides open***
epel by repo8-server                                21 MB/s | 8.8 MB     00:00
Last metadata expiration check: 0:00:04 ago on Sun 31 Jan 2021 10:38:42 AM CST.
open***-2.4.10-1.el8.x86_64 : A full-featured SSL *** solution
Repo        : epel-repo8
Matched from:
Provide    : open*** = 2.4.10-1.el8

2、安装编译httpd

2.1 源代码编译安装过程

利用编译工具,通常只需要三个大的步骤:

  • ./configure

    1)通过选项传递参数,指定安装路径、启用特性等;执行时会参考用户的指定以及Makefile.in文件生成Makefile

    2)检查依赖到的外部环境,如依赖的软件包

  • make

    根据Makefile文件,会检测依赖的环境,进行构建应用程序

    make -j 2 ##如果有两颗cpu,就可以用-j选项,可以并行多个任务,加速执行编译

  • make install

    复制文件到相应路径

注意:安装前可以通过查看README,INSTALL获取帮助

2.2 编译安装准备

准备:安装相关的依赖包

  • 开发工具:make, gcc (c/c++编译器GNU C Complier)
  • 开发环境:开发库(glibc:标准库),头文件,可安装开发包组 Development Tools
  • 软件相关依赖包

生产实践:基于最小化安装的系统建议安装下面相关包

[root@centos8 ~]# 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

2.3 编译安装

第一步:运行 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

2.4 安装后的配置

  • 二进制程序目录导入至PATH环境变量中,编辑文件/etc/profile.d/NAME.sh
export PATH=/PATH/TO/BIN:$PATH
  • 相关用户及文件

    有些开源软件编译完成后,还需要创建相关的用户及文件

  • 导入帮助手册

    编辑/etc/man.config|man_db.conf文件,添加一个MANPATH

实例1:CentOS 8 编译安装 cmatrix

#1 安装相关包,未安装,会提示错误
[root@centos8 ~]# dnf install gcc make autoconf ncurses-devel
#2 下载并解压缩包
[root@centos8 ~]# cd /usr/local/src
[root@centos8 ~]# wget https://github.com/abishekvashok/cmatrix/releases/download/v2.0/cmatrix-v2.0-Butterscotch.tar
[root@centos8 ~]# tar xvf cmatrix-v2.0-Butterscotch.tar
#3 配置
[root@centos8 ~]# cd cmatrix
[root@centos8 cmatrix]# ./configure --prefix=/apps/cmatrix  ##所有的文件都安装在/apps/cmatrix目录下,也可以分开指定bin的安装路径
#4 编译并安装
[root@centos8 cmatrix]# make
#如果make或./configure过程中出现错误,根据错误提示来安装所需的库文件,然后rm -rf /usr/local/src/cmatrix这个目录,重新执行解压缩和./configure
[root@centos8 cmatrix]# make install

#5 配置环境
[root@centos8 ~]# echo 'PATH=/apps/cmatrix/bin:$PATH' > /etc/profile.d/cmatrix.sh
[root@centos8 ~]# . /etc/profile.d/cmatrix.sh
#或者用软链接实现
[root@centos8 ~]# ln -sv /apps/cmatrix/bin/cmatrix /usr/local/bin/

#6运行,显示***帝国的界面
[root@centos8 ~]# cmatrix -a -b -C yellow

#7帮助
[root@centos8 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /apps/cmatrix/share/man
[root@centos8 ~]# man cmatrix

#8怎么部署在多台机器,机器配置相同,可以直接拷贝安装目录
scp -r /apps/ 192.168.100.201:/ ##把整个apps目录都复制到其他机器上
ln -s /apps/cmatrix/bin/cmatrix /usr/local/bin
cmatrix #看是否能运行,如果提示错误,一般是缺少库文件,yum search 库名,然后安装即可

实例2:centos 编译安装 httpd-2.4.46

1、centos8 编译安装 httpd-2.4.46
#安装前准备:关闭防火墙和SELinux
#1 依赖的安装包
[root@centos8 ~]# dnf install gcc wget make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
#2 下载并解压缩包
[root@centos8 ~]# wget -P /usr/local/src https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
[root@centos8 ~]# cd /usr/local/src
[root@centos8 src]# tar xvf httpd-2.4.46.tar.bz2
#3 配置
[root@centos8 ~]# cd /usr/local/src/httpd-2.4.46/
[root@centos8 httpd-2.4.46]# ./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl
#配置过程中可能会提示缺乏某些库,使用yum search 库名 或 dnf list 库名,搜索到后,一般安装-devel版本,比如
yum search apr* 
yum search pcre* 
dnf list openssl*

#4 编译并安装
[root@centos8 httpd-2.4.46]# make -j 4 && make install
#5 配置环境
[root@centos8 ~]# echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
[root@centos8 ~]# . /etc/profile.d/httpd.sh
#6运行
[root@centos8 ~]# apachectl start
#7 指定用apache用户运行
[root@centos8 ~]# useradd -r -s /sbin/nologin -d /apps/httpd -c Apache -u 48 apache
[root@centos8 ~]# vim /etc/httpd/httpd.conf     ##查看配置文件,修改daemon为apache
user apache
group apache
[root@centos8 ~]# sed -ri.bak 's/daemon$/apache/' /etc/httpd/httpd.conf

#7生效和验证
[root@repo8-server ~]# ps aux|grep httpd
root   17670  0.0  0.5 117588  5624 ?   Ss   16:48   0:00 /apps/httpd//bin/httpd -k start
daemon 17671  0.0  0.7 878316  7672 ?   Sl   16:48   0:00 /apps/httpd//bin/httpd -k start
daemon 17672  0.0  0.8 878316  7976 ?   Sl   16:48   0:00 /apps/httpd//bin/httpd -k start
daemon 17673  0.0  0.7 878316  7672 ?   Sl   16:48   0:00 /apps/httpd//bin/httpd -k start
root   18507  0.0  0.1  12108   984 pts/0    S+   17:00   0:00 grep --color=auto httpd
[root@repo8-server ~]# apachectl restart
[root@repo8-server ~]# ps aux|grep httpd    #同上

2、centos7编译安装httpd2.4.46
[root@centos7-http ~]# cd 
[root@centos7-http ~]# rpm -q httpd &>/dev/null && yum remove -y httpd
[root@centos7-http ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
[root@centos7-http ~]# yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
[root@centos7-http ~]# cd /usr/local/src
[root@centos7-http src]# tar xf httpd-2.4.46.tar.bz2
[root@centos7-http src]# cd httpd-2.4.46
[root@centos7-http httpd-2.4.46]# ./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl
[root@centos7-http httpd-2.4.46]# echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
[root@centos7-http httpd-2.4.46]# . /etc/profile.d/httpd.sh
[root@centos7-http httpd-2.4.46]# apachectl start
[root@centos7-http httpd-2.4.46]# curl localhost
<html><body><h1>It works!</h1></body></html>

[root@centos7-http httpd-2.4.46]# useradd -r -s /sbin/nologin -d /apps/httpd -c Apache -u 48 apache
[root@centos7-http httpd-2.4.46]# sed -i.bak 's/daemon$/apache/' /etc/httpd/httpd.conf
[root@centos7-http httpd-2.4.46]# apachectl restart
[root@centos7-http httpd-2.4.46]# ps aux|grep httpd
root   18721  0.0  0.3 103756  3304 ?   Ss   17:30   0:00 /apps/httpd//bin/httpd -k start
apache 18838  0.0  0.4 390584  4324 ?   Sl   17:35   0:00 /apps/httpd//bin/httpd -k start
apache 18839  0.0  0.4 456120  4324 ?   Sl   17:35   0:00 /apps/httpd//bin/httpd -k start
apache 18840  0.0  0.4 390584  4324 ?   Sl   17:35   0:00 /apps/httpd//bin/httpd -k start
root   18923  0.0  0.0 112812   968 pts/1  S+   17:35   0:00 grep --color=auto httpd
[root@centos7-http httpd-2.4.46]#

实例3:脚本编译安装http2.4,实现可以正常访问

[root@centos7-http httpd-2.4.46]# vi /scripts/httpd.sh
#!/bin/bash
#查看是否有httpd并删除
rpm -q httpd  &>/dev/null && { yum -y remove httpd &>/dev/null; echo "remove is ok"; }
#httpd是否启动并停止
ps -ef |grep ^apache &>/dev/null && apachectl stop &>/dev/null
#httpd目录是否存在并删除
find /apps -type d -name "httpd" &>/dev/null && { rm /apps/httpd /etc/profile.d/httpd.sh /etc/httpd -rf; echo "rm httpd is ok"; }
#安装相关库文件
yum install -y gcc make wget tar apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config &>/dev/null || { echo install package failure; exit 10; }
#下载httpd软件包并解压缩
cd /usr/local/src/
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2 &>/dev/null || { echo download failue; exit 20; }
rpm -q bzip2 &>/dev/null || yum install -y bzip2 &>/dev/null
tar xf httpd-2.4.46.tar.bz2 &>/dev/null  && echo "tar is ok" || { echo tar failure; exit 30; }
#编译安装
cd httpd-2.4.46
./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl &>/dev/null && echo "configure is ok"|| { echo configure failure;exit 40; }
make &>/dev/null || { echo make failure; exit 50; }
make install &>/dev/null && echo "make install is ok" || { echo install failure; exit 60; }
#添加路径并启动
echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
apachectl start &>/dev/null && echo "start is ok" || { echo "apachectl start failure"; exit 70; }
#查看是否有apache用户并添加
getent passwd apache &>/dev/null && userdel -r apache &>/dev/null
useradd -r -s /sbin/nologin -d /apps/httpd -c Apache -u 48 apache &>/dev/null  && echo "useradd apache is ok" ||  { echo "useradd failure"; exit 80; }
#修改配置文件中的daemon用户为apache
sed -i 's/daemon$/apache/' /etc/httpd/httpd.conf &>/dev/null
#重启
apachectl restart &>/dev/null && echo "restart is ok" || { echo "apachectl start failure"; exit 90; }

#测试
[root@centos7-http httpd-2.4.46]# ps -ef |grep ^apache
apache    10903  10902  0 20:08 ?        00:00:00 /apps/httpd/bin/httpd -k start
apache    10904  10902  0 20:08 ?        00:00:00 /apps/httpd/bin/httpd -k start
apache    10905  10902  0 20:08 ?        00:00:00 /apps/httpd/bin/httpd -k start
[root@centos7-http httpd-2.4.46]# . /scripts/httpd.sh
rm httpd is ok
tar is ok
configure is ok
make install is ok
start is ok
useradd apache is ok
restart is ok
[root@centos7-http httpd-2.4.46]# curl localhost
<html><body><h1>It works!</h1></body></html>

3 文本处理三剑客之 sed

3.1 sed 工作原理

sed 即 Stream EDitor,和 vi 不同,sed是行编辑器

Sed是从文件或管道中读取一行,处理一行,输出一行;再读取一行,再处理一行,再输出一行,直到最后一行。

每当处理一行时,把当前处理的行存储在临时缓冲区中,称为模式空间(PatternSpace),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。一次处理一行的设计模式使得sed性能很高,sed在读取大文件时不会出现卡顿的现象。如果使用vi命令打开几十M上百M的文件,明显会出现有卡顿的现象,这是因为vi命令打开文件是一次性将文件加载到内存,然后再打开。Sed就避免了这种情况,一行一行的处理,打开速度非常快,执行速度也很快

参考网站:http://www.gnu.org/software/sed/manual/sed.html

3.2 sed 基本用法

格式:

sed [option]... 'script;script;...' inputfile...

grep sed /var/log/anaconda/packaging.log    #这个日志是系统安装时安装了哪些文件

常用选项:

  • -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       #显示替换成功的行
I,i     #忽略大小写
w  /PATH/FILE   #将替换成功的行保存至文件中

范例:

1、全局:
[root@centos8 ~]# sed ''        ##后面未跟文件,表示标准输入
welcome     ##输入welcome
welcome     ##打印输出welcome
to
to
magedu
magedu
[root@centos8 ~]# sed '' /etc/issue ##''中没有任何script(地址和命令),默认执行p命令,打印文件
\S
Kernel \r on an \m
[root@centos8 ~]# sed 'p' /etc/issue    #因有默认打印,再使用p命令,就打印2次了
\S
\S
Kernel \r on an \m
Kernel \r on an \m
[root@centos8 ~]# sed -n '' /etc/issue  ##-n关闭自动打印,所以没有任何显示
[root@centos8 ~]# sed -n 'p' /etc/issue 
\S
Kernel \r on an \m

2、单地址:
[root@centos8 ~]# cp /etc/passwd /data/
[root@centos8 ~]# sed '3p' /data/passwd     #打印passwd文件,并且打印2遍第3行
[root@centos8 ~]# sed -n '3p' /data/passwd  #只打印第3行
daemon:x:2:2:daemon:/sbin:/sbin/nologin

[root@centos8 ~]# ifconfig eth0 | sed '2p'      #打印2遍第2行
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.209.111  netmask 255.255.255.0  broadcast 192.168.209.255
[root@centos8 ~]# ifconfig eth0 | sed -n '2p'   ##只打印第2行
        inet 192.168.209.111  netmask 255.255.255.0  broadcast 192.168.209.255
[root@centos8 ~]# sed -n '$p' /data/passwd      ##打印最后一行,等同于tail -n1 /data/passwd
apache:x:48:48:Apache:/apps/httpd:/sbin/nologin

[root@centos8 ~]# sed -n '/^[^#]/p' /etc/fstab  ##打印不是#开头的行,支持正则表达式
UUID=6cda6325-3f54-4ade-b27f-28d9f4e5c9c7 /              xfs     defaults        0 0
UUID=99e34388-a48e-48d4-a76b-910442e0a697 /boot          ext4    defaults        1 2
UUID=711befdf-2afa-4911-9202-0fbd22548371 /data          xfs     defaults        0 0
UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8 swap           swap    defaults        0 0
[root@centos8 ~]# grep '^[^#]' /etc/fstab       #显示同上

[root@centos8 ~]# sed -n '/root/p' /data/passwd #包含root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos8 ~]# grep 'root' /data/passwd      #显示同上
[root@centos8 ~]# sed -n '/^root/p' /data/passwd        #root开头的行
root:x:0:0:root:/root:/bin/bash
[root@centos8 ~]# grep '^root' /data/passwd     #显示同上

[root@centos8 ~]# df | sed -n '/^\/dev\/sd/p'
/dev/sda2    104806400 4872956  99933444  5% /
/dev/sda3    52403200  398860  52004340  1% /data
/dev/sda1     999320  848568   81940  92% /boot

3、地址范围:
[root@centos8 ~]# seq 10 | sed -n '3,6p'        #第3~6行
3
4
5
6
[root@centos8 ~]# seq 10 | sed -n '3,+4p'   #从第3行开始,再加4行,即3~7行
3
4
5
6
7
[root@centos8 ~]# seq 10 | sed -n '3,$p'        #从第3行开始到末行
3
4
5
6
7
8
9
10
[root@centos8 ~]# sed -n '/^d/,/^s/p' /etc/passwd   #打印d开头到s开头的行
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin

4、步进:
[root@centos8 ~]# seq 10 |sed -n '1~2p'     #奇数行
1
3
5
7
9
[root@centos8 ~]# seq 10 |sed -n '2~2p'     #偶数行
2
4
6
8
10
[root@centos8 ~]# sed 10 |sed -n '1~3p'     #步进3
1
4
7
10
[root@centos8 ~]# seq 10 |sed  '1~2d'   #与sed -n '1~2p'取反
2
4
6
8
10
[root@centos8 ~]# seq 10 |sed  '2~2d'
1
3
5
7
9

5、常用命令:
#d删除
[root@centos8 ~]# sed '/root/d' /etc/passwd     #把模式空间中的root行删除打印,即不显示root行
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
...省略...
[root@centos8 ~]# sed -n '/root/!p' /etc/passwd #同上,不打印root行

#a i c插入文本
[root@centos8 ~]# sed 6 |sed '3a abc'   #在第n行后插入文本
1
2
3
abc
4
5
6

[root@centos8 ~]# sed 6 |sed '2,4a abc' #在第m~n行后都插入文本
1
2
abc
3
abc
4
abc
5
6
[root@centos8 ~]# sed 6 |sed '3a     abc'       ##有无空格或多少个空格效果一样
1
2
3
abc
4
5
6
[root@centos8 ~]# sed 6 |sed '3a\  abc'     ##加\,显示空格
1
2
3
  abc
4
5
6
[root@centos8 ~]# sed 6 |sed '3a\  abc\n    123'    ##\n换行
1
2
3
  abc
    123
4
5
6
[root@centos8 ~]# sed 6 |sed '3i\  abc'     ##a是行后添加,i是行前添加,c是替换所选行
1
2
  abc
3
4
5
6

#w写入文件
[root@centos8 ~]# sed '3w /data/sed.log' /etc/passwd    #passwd的第3行写入sed.log中
[root@centos8 ~]# cat /data/sed.log
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@centos8 ~]# sed -n '/^root/w /data/sed.log' /etc/passwd
[root@centos8 ~]# cat /data/sed.log
root:x:0:0:root:/root:/bin/bash
[root@centos8 ~]# sed -n '3,6w /data/sed.log' /etc/passwd   #写入第3~6行

#r从文件读入
[root@centos8 ~]# seq 5|sed '3r /etc/issue'     #读取issue文件,插入到第3行后
1
2
3
\S
Kernel \r on an \m

4
5

#=显示行号
[root@centos8 ~]# sed -n '/root/=' /data/passwd
1
10
[root@centos8 ~]# sed -n '/root/=p' /data/passwd
sed: -e expression #1, char 8: extra characters after command
[root@centos8 ~]# sed '/root/=' /data/passwd
1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10
operator:x:11:0:operator:/root:/sbin/nologin

#-e多点编辑
[root@centos8 ~]# sed 6 |sed -e '3a\  abc' -e '3i\ 123'
1
2
 123
3
  abc
4
5
6
[root@centos8 ~]# seq 10| sed -e '2d' -e '5d'   #只删除第2行和第5行
1
3
4
6
7
8
9
10
[root@centos8 ~]# seq 10| sed '2d;5d'       #同上
[root@centos8 ~]# seq 10| sed '2d;5aabc'    #可以同时使用两个命令
1
3
4
5
abc
6
7
8
9
10
[root@centos8 ~]# seq 10| sed '2immm;5aabc'     #这样不行,只能用-e选项
1
mmm;5aabc
2
3
4
5
6
7
8
9
10

6、查找替换-i
[root@centos8 ~]#  sed -i.bak '/root/d' /data/passwd
[root@centos8 ~]# cat /data/passwd          ##已删除包含root的行
[root@centos8 ~]# cat /data/passwd.bak      ##passwd的备份

[root@centos8 ~]#seq 10 > seq.log
[root@centos8 ~]# sed -i.bak '2d;4d' seq.log
[root@centos8 ~]# cat seq.log.bak
1
2
3
4
5
6
7
8
9
10
[root@centos8 ~]#cat seq.log
1
3
5
6
7
8
9
10

#在httpd.conf的Listen 80行后添加Listen 8080
[root@centos8 ~]# sed -i.bak '/^Listen/aListen 8080' /etc/httpd/httpd.conf

#删除所有以#开头的行
[root@centos8 ~]# sed -i '/^#/d' /data/fstab
[root@centos8 ~]# cat /data/fstab

UUID=6cda6325-3f54-4ade-b27f-28d9f4e5c9c7 /         xfs     defaults        0 0
UUID=99e34388-a48e-48d4-a76b-910442e0a697 /boot     ext4    defaults        1 2
UUID=711befdf-2afa-4911-9202-0fbd22548371 /data     xfs     defaults        0 0
UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8 swap      swap    defaults        0 0

#只显示非#开头的行
[root@centos8 ~]# sed -n '/^[^#]/p' /data/fstab     #空行不显示
UUID=6cda6325-3f54-4ade-b27f-28d9f4e5c9c7 /         xfs     defaults        0 0
UUID=99e34388-a48e-48d4-a76b-910442e0a697 /boot     ext4    defaults        1 2
UUID=711befdf-2afa-4911-9202-0fbd22548371 /data     xfs     defaults        0 0
UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8 swap      swap    defaults        0 0
[root@centos8 ~]# sed -n '/^#/!p' /data/fstab       ##空行也显示

UUID=6cda6325-3f54-4ade-b27f-28d9f4e5c9c7 /         xfs     defaults        0 0
UUID=99e34388-a48e-48d4-a76b-910442e0a697 /boot     ext4    defaults        1 2
UUID=711befdf-2afa-4911-9202-0fbd22548371 /data     xfs     defaults        0 0
UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8 swap      swap    defaults        0 0

7、s/pattern/string/修饰符 #查找替换,支持使用其它分隔符,可以是其它形式:s@@@,s###
1)修改网卡配置
[root@centos8 ~]# cat /etc/default/grub.bak
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="resume=UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8 net.ifnames=0 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true
[root@centos8 ~]# sed -i '/net.if/s/net.ifnames=0//' /etc/default/grub.bak  
#去掉net.ifnames=0
[root@centos8 ~]# cat /etc/default/grub.bak
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="resume=UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8  rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true
#添加到最后,用扩展表达式,需要使用r或E选项
[root@centos8 ~]# sed -i '/^GRUB_CMDLINE/s/(.*)"$/\1 net.ifnames=0"/' /etc/default/grub.bak
sed: -e expression #1, char 42: invalid reference \1 on `s' command's RHS
[root@centos8 ~]# sed -ri '/^GRUB_CMDLINE/s/(.*)"$/\1 net.ifnames=0"/' /etc/default/grub.bak
[root@centos8 ~]# cat /etc/default/grub.bak
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="resume=UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8  rhgb quiet net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

2)替换账号
#编译安装完httpd2.4.46后,需要新建apache系统帐号,并将httpd.conf中的daemon替换为apache
[root@centos8 ~]# sed -i.bak  -e '/^User daemon/cUser apache' -e '/^Group daemon/cGroup apache' /etc/httpd/httpd.conf.bak
[root@centos8 ~]# sed -ri.bak -e 's/^User.*/User apache/' -e 's#^(Group).*#\1 apache#' /etc/httpd/httpd.conf.bak
或
[root@centos8 ~]# sed -n '/daemon$/p' /etc/httpd/httpd.conf.bak
User daemon
Group daemon
[root@centos8 ~]# sed -i 's/daemon$/apache/' /etc/httpd/httpd.conf.bak
[root@centos8 ~]# sed -n '/apache$/p' /etc/httpd/httpd.conf.bak
User apache
Group apache

#修改selinux中的enforcing为disabled
[root@centos8 ~]# sed -i.bak '/^SELINUX=enforcing/cSELINUX=disabled' /etc/selinux/config
或
[root@centos8 ~]# sed -Ei.bak 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config
[root@centos8 ~]# cat /etc/selinux/config|sed -n '/^[^#]/p' 
SELINUX=disabled
SELINUXTYPE=targeted

范例:

sed '2p' /etc/passwd                #第2行打印两遍,其他行打印一遍
sed  -n '2p' /etc/passwd            #第2行
sed  -n '1,4p' /etc/passwd          #1~4行
sed  -n '/root/p' /etc/passwd       #包含root的行
sed  -n '2,/root/p' /etc/passwd     #从2行到包含root的行
sed  -n '/^$/=' /etc/issue          #显示空行行号
[root@centos8 ~]# sed -n '/^$/=' /etc/issue
3
[root@centos8 ~]# sed -n -e '/^$/p' -e '/^$/=' /etc/fstab

1
sed'/root/a\superman' /data/passwd          #行后插入uperman
sed '/root/i\superman' /data/passwd         #行前插入uperman
sed '/root/c\superman' /data/passwd         #用uperman代替root行
[root@centos8 ~]# sed '/root/cuperman' /data/passwd
uperman
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uperman

[root@centos8 ~]# sed '/^$/d' /etc/issue
\S
Kernel \r on an \m

[root@centos8 data]# sed '1,40d' passwd
gnome-initial-setup:x:983:979::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
[root@centos8 data]# nl passwd |sed '2,5d'
     1  root:x:0:0:root:/root:/bin/bash
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@centos8 data]# nl passwd |sed '2a tea'
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
tea
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin

sed -n 's/root/&superman/p' /etc/passwd     #在替换的单词后添加
[root@centos8 data]# sed -n 's/root/&superman/p' passwd
rootsuperman:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/rootsuperman:/sbin/nologin
sed -n 's/root/superman&/p' /etc/passwd     #在替换的单词前添加
[root@centos8 data]# sed -n 's/root/superman&/p' passwd
supermanroot:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/supermanroot:/sbin/nologin

范例:引用变量

[root@centos8111 ~]# echo |sed "s/^/$RANDOM.rmvb/"
10451.rmvb
[root@centos8111 ~]# sed "s/^/$RANDOM.rmvb/"    ##等待输入
abc
9046.rmvbabc
[root@centos8111 ~]# echo sed 's/^/$RANDOM.rmvb/'
sed s/^/$RANDOM.rmvb/
[root@centos8111 ~]# echo |sed 's/^/$RANDOM.rmvb/'
$RANDOM.rmvb
[root@centos8111 ~]# echo |sed 's/^/'''$RANDOM.rmvb'''/'
10038.rmvb
[root@centos8111 ~]# echo |sed 's/^/'''$RANDOM'''.rmvb/'
27788.rmvb

范例:修改配置文件

[root@centos6 ~]#sed  -e '/^#<VirtualHost/,/^#<\/VirtualHost>/s@#@@' -e
'/^#NameVirtualHost/s@#@@' /etc/httpd/conf/httpd.conf

[root@centos8111 ~]# sed -rn '/^# <VirtualHost/,/^#<\/VirtualHost>/p' /etc/httpd/httpd.conf.bak
# <VirtualHost> definition.  These values also provide defaults for
#</VirtualHost> containers you may define later in the file.
[root@centos8111 ~]# sed -rn '/^# <Virt.*/,/^#<\/Virt.*/p' /etc/httpd/httpd.conf.bak
# <VirtualHost> definition.  These values also provide defaults for
#</VirtualHost> containers you may define later in the file.
[root@centos8111 ~]# sed -rn '/^# <Virt.*/,/^#<\/Virt.*/s@#@@p' /etc/httpd/httpd.conf.bak
 <VirtualHost> definition.  These values also provide defaults for
</VirtualHost> containers you may define later in the file.

范例:替换中的强弱引用

#defaults更换成#wangdefaults
NAME=wang;sed -Ei.bak '/default/s/(defaults)/$NAME\1/' /etc/fstab   ##''不行,强引用
NAME=wang;sed -Ei.bak "/default/s/(defaults)/$NAME\1/" /etc/fstab   ##""可行,弱引用
NAME=wang;sed -Ei.bak '/default/s/(defaults)/'''$NAME'''\1/' /etc/fstab     #可行

[root@centos8 /]# NAME=wang;sed -rn '/default/s/(defaults)/$NAME\1/p' /data/fstab
/dev/mapper/cl-root     /                       xfs     $NAMEdefaults        0 0
UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot ext4    $NAMEdefaults        1 2
/dev/mapper/cl-swap     swap                    swap    $NAMEdefaults        0 0
[root@centos8 /]# NAME=wang;sed -rn "/default/s/(defaults)/$NAME\1/p" /data/fstab
/dev/mapper/cl-root     /                       xfs     wangdefaults        0 0
UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot ext4    wangdefaults        1 2
/dev/mapper/cl-swap     swap                    swap    wangdefaults        0 0
[root@centos8 /]# NAME=wang;sed -rn '/default/s/(defaults)/'''$NAME'''\1/p' /data/fstab
/dev/mapper/cl-root     /                       xfs     wangdefaults        0 0
UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot ext4    wangdefaults        1 2
/dev/mapper/cl-swap     swap                    swap    wangdefaults        0 0
[root@centos8 /]#

实例1:利用sed 取出ifconfig命令中本机的IPv4地址

#centos7.8可用
[root@centos8 ~]# ifconfig eth0 | sed -n '2s/^.*inet //p' | sed -n 's/netmask.*//p'
192.168.100.200
[root@centos8 ~]# ifconfig eth0 | sed -n '2s/^.*inet //;s/ netmask.*//p'
192.168.100.200
[root@centos8 ~]# ifconfig eth0 | sed -rn '2s/(.*inet )([0-9].*)(netmask.*)/\2/p'
192.168.100.200

#centos6.7.8通用写法
[root@centos8 ~]# ifconfig eth0 |sed -nr "2s/[^0-9]+([0-9.]+).*/\1/p" 
192.168.100.200
[root@centos8 ~]# ifconfig eth0 |sed -rn '2s/^[^0-9]+([0-9.]+) .*$/\1/p'
192.168.100.200

实例2:取基名和目录名

1、取网卡的目录名和基名
#取目录名
[root@centos8 ~]# echo /etc/sysconfig/network-scripts/ifcfg-ens33 |sed -rn 's#(.*)/([^/]+)/?#\1#p'
/etc/sysconfig/network-scripts
[root@centos8 ~]# echo "/etc/sysconfig/network-scripts/ifcfg-ens33" |sed -r 's#(^/.*/)([^/]+/?)#\1#'
/etc/sysconfig/network-scripts/

#取基名
[root@centos8 ~]# echo /etc/sysconfig/network-scripts/ifcfg-ens33 |sed -rn 's#(.*)/([^/]+)/?#\2#p'
ifcfg-ens33
[root@centos8 ~]# echo "/etc/sysconfig/network-scripts/ifcfg-ens33" |sed -r 's#(^/.*/)([^/]+/?)#\2#'
ifcfg-ens33

2、处理/etc/fstab路径,使用sed命令取出其目录名和基名   

[root@centos8111 ~]# echo "/etc/fstab" |sed -r 's#(.*)/([^/]+)/?#\1#'
/etc
[root@centos8111 ~]# echo "/etc/fstab" |sed -r 's#(.*)/([^/]+)/?#\2#'
fstab

实例3:将非#开头的行加

[root@centos8 /]# cat /data/fstab
...省略...
/dev/mapper/cl-root     /                        xfs     defaults        0 0
UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot  ext4    defaults        1 2
/dev/mapper/cl-swap     swap                     swap    defaults        0 0
[root@centos8 /]# sed -rn '/^[^#]/s/(.*)/#\1/p' /data/fstab
#/dev/mapper/cl-root     /                       xfs     defaults        0 0
#UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot ext4    defaults        1 2
#/dev/mapper/cl-swap     swap                    swap    defaults        0 0

[root@centos8 /]# sed -rn 's/^[^#]/#&/p' /data/fstab            #同上
[root@centos8 /]# sed -rn 's/^[^#](.*)/#\1/p' /data/fstab       #同上
[root@centos8 /]# sed -rn '/^#/!s@^@#@p' /data/fstab            #空行也加上了#
#
#/dev/mapper/cl-root     /                       xfs     defaults        0 0
#UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot ext4    defaults        1 2
#/dev/mapper/cl-swap     swap                    swap    defaults        0 0

实例4:将#开头的行删除

#1)将#开头的行,只删除#
[root@centos8 /]# sed -ri.bak '/^#/s/#//' /data/fstab
[root@centos8 /]# cat /data/fstab

 /etc/fstab
 Created by anaconda on Sat Nov  7 05:25:08 2020

 Accessible filesystems, by reference, are maintained under '/dev/disk/'.
 See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.

 After editing this file, run 'systemctl daemon-reload' to update systemd
 units generated from this file.

/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot ext4    defaults        1 2
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
[root@centos8 /]#

#2)将#开头的行删除
[root@repo8-server ~]# sed -r '/^#|^$/d' /data/fstab
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=58adea0f-0cad-4f43-90c2-8cc6f310fdef /boot ext4    defaults        1 2
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
[root@repo8-server ~]#

实例5:删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

[root@centos8111 ~]# sed -rn '/^#/s/# +//p' /etc/fstab
/etc/fstab
Created by anaconda on Mon Nov 16 06:50:47 2020
Accessible filesystems, by reference, are maintained under '/dev/disk/'.
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
After editing this file, run 'systemctl daemon-reload' to update systemd
units generated from this file.

实例6:取分区利用率

[root@centos8 ~]# df | sed -nr '/^\/dev\/sd/s# .*([0-9]+)%.*# \1#p'
/dev/sda2 9
/dev/sda5 1
/dev/sda1 3
#备注:
/^\/dev\/sd/          #查找/dev/sd开头的行
空格.*([0-9]+)%.*     #从空格开始+任意字符到+多个数字%+任意字符
空格\1                #查找到的内容替换为"空格+分组1"

[root@centos8 ~]# df | sed -nr '/^\/dev\/sd/s#.*([0-9]+)%.*#\1#p'
9
1
3
#备注:
.*([0-9]+)%.*    #查找.*+数字%+任意字符,即匹配的整行
\1               #替换成分组1,即数字

实例7:修改内核参数

[root@centos8 ~]# sed -nr '/^GRUB_CMDLINE/s/"$/ net.ifnames=0"/p' /etc/default/grub.bak
GRUB_CMDLINE_LINUX="resume=UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8  rhgb quiet net.ifnames=0"

[root@centos8 ~]#sed -rn '/^GRUB_CMDLINE/s@(.*)"$@\1 net.ifnames=0"@p'/etc/default/grub.bak
GRUB_CMDLINE_LINUX="resume=UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8  rhgb quiet net.ifnames=0"

[root@centos8 ~]# sed -rn '/^GRUB_CMDLINE/s@"$@ net.ifnames=0"@p' /etc/default/grub
GRUB_CMDLINE_LINUX="resume=UUID=4c77e17e-4a42-4f9b-b8c8-b8e4d3f821e8 rhgb quiet net.ifnames=0"

#备注:
#/^GRUB_CMDLINE/ 查找匹配只能使用/,不能用#或@等符号,而替换时可以用#或@符号。

实例8:查看httpd.conf配置文件

#删除以“空格或#或空行”开头的行
[root@centos8111 ~]# sed -r '/^( |#|$)/d' /etc/httpd/httpd.conf
ServerRoot "/apps/httpd"
Listen 80
LoadModule authn_file_module modules/mod_authn_file.so
...省略...
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

[root@centos8111 ~]# sed -r '/^ |^#|^$/d' /etc/httpd/httpd.conf|wc -l
58
[root@centos8111 ~]# sed -r '/^( |#|$)/d' /etc/httpd/httpd.conf|wc -l
58
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部