1、压缩和解压缩

1.1 compress和uncompress

安装包为ncompress 对应格式为.Z后缀

[root@centos8 ~]# yum install ncompress -y

格式:

compress options [file ...]
uncompress file.Z   # 解压缩

常用选项:

  • -d 解压缩,相当于uncompress
  • -c 结果输出至标准输出,不删除原文件
  • -v 显示详情
[root@centos8 ~]# ll m.txt 
-rw-------. 1 root root 289241 Mar 22 14:25 m.txt
[root@centos8 ~]# compress m.txt 
[root@centos8 ~]# ll m.txt.Z 
-rw-------. 1 root root 76057 Mar 22 14:25 m.txt.Z
[root@centos8 ~]# uncompress m.txt.Z 
[root@centos8 ~]# ls
1.sh  anaconda-ks.cfg  f2.sh  f2.txt  m.txt  reset.sh  test  test.log
[root@centos8 ~]# compress -c m.txt > m.txt.Z
[root@centos8 ~]# ls
1.sh  anaconda-ks.cfg  f2.sh  f2.txt  m.txt  m.txt.Z  reset.sh  test  test.log
[root@centos8 ~]# compress -dc m.txt.Z > m2.txt
[root@centos8 ~]# ls
1.sh  anaconda-ks.cfg  f2.sh  f2.txt  m2.txt  m.txt  m.txt.Z  reset.sh  test  test.log
[root@centos8 ~]# 

zcat file.Z 不显示解压缩的前提下查看文本文件内容

zcat file.Z >file   # 也可以解压
1.2 gzip和gunzip

来自于gzip包,对应文件为.gz后缀

常用选项:

  • -k  keep,保留原文件,Centos8新特性
  • -d  解压缩,相当于gunzip
  • -c  结果输出至标准输出,不删除原文件
  • -#  指定压缩比,#取值1-9,值越大压缩比越大
# 解压缩
gunzip file.gz
# 不显示解压缩的前提下查看文本文件内容
zcat file.gz

gzip -c message > message.gz
gzip -c -d message.gz > message
zcat message.gz > message
cat message | gzip > m.gz
1.3 bzip2和bunzip2

来自于bzip2包,对应文件后缀为.bz2

常用选项:

  • -k  keep,保留原文件
  • -d  解压缩,相当于bunzip2
  • -c  结果输出至标准输出,不删除原文件
  • -#  1-9,压缩比,默认为9
bunzip2 file.bz2   # 解压缩
bzcat file.bz2    # 不显示解压缩的前提下查看文本文件内容
1.4 xz和unxz

来自于xz包,对应文件后缀为.bz2

常用选项:

  • -k  keep,保留原文件
  • -d  解压缩,相当于unxz
  • -c  结果输出至标准输出,保留源文件不改变
  • -#  1-9,压缩比,默认为6
unxz file.xz    # 解压
xzcat file.xz   # 不显示解压缩的前提下查看文本文件内容

以上四种工具只能实现对单个文件压缩

1.5 zip和unzip

zip可以实现打包目录和多个文件成一个文件并压缩,但可能会丢失文件属性信息,如:所有者和组信息,一般建议使用tar代替

分别来自于zip和unzip包,对应的后后缀为.zip

# 打包目录并压缩
zip -r /backup/sysconfig.zip /etc/susconfig

# 不包括目录本身,只打包目录内的文件和子目录
cd /etc/sysconfig;zip -r /root/sysocnfig.zip *

# 默认解压缩至当前文件
unzip /backup/sysconfig/zip

# 解压至指定目录,如果指定目录不存在,会在其父目录(必须事先存在)下自动生成
unzip /backup/sysconfig/zip -d /tmp/config

# 接受输入-
cat /var/log/message | zip message -

# -p表示管道
unzip -p message.zip > message

交互式加密和解密

[root@centos8 ~]# zip -e m.zip m.txt
Enter password: 
Verify password: 
  adding: m.txt (deflated 86%)
[root@centos8 test]# unzip m.zip 
Archive:  m.zip
[m.zip] m.txt password: 
password incorrect--reenter: [root@centos8 test]# 
[root@centos8 test]# unzip m.zip 
Archive:  m.zip
[m.zip] m.txt password: 
  inflating: m.txt 

非交互式加密和解密 注意是大写P

[root@centos8 ~]# zip -P 111 m.zip m.txt
  adding: m.txt (deflated 86%)
[root@centos8 test]# unzip -P 111 m.zip 
Archive:  m.zip
  inflating: m.txt                   
[root@centos8 test]# ls
m.txt  m.zip

2 打包和解压

2.1 tar

即Tape ARchive磁带归档,可以对目录和多个文件打包一个文件,并且可以压缩,保留文件属性不丢失,常用于备份功能,推荐使用,对应的后缀为.tar

1)创建归档

tar -cpvf /PATH/FILE.tar FILE ...

2)追加文件至归档;注:不支持对压缩文件追加

tar -rf /PATH/FILE.tar FILE ...

3)查看归档文件中的文件列表

tar -t -f /PATH/FILE.tar

4)展开归档

tar xf /PATH/FILE.tar tar xf /PATH/FILE.tar -C /PATH/

5)结合压缩工具实现;归档并压缩

  • -z  相当于gzip压缩工具
  • -j  相当于bzip2压缩工具
  • -J  相当于xz压缩工具
[root@centos8 test]# tar zcvf etc.tar.gz /etc/
[root@centos8 test]# tar jcvf etc.tar.bz2 /etc/
[root@centos8 test]# tar Jcvf etc.tar.xz /etc/ 
[root@centos8 test]# ll etc.tar.*
-rw-r--r--. 1 root root 3616727 Mar 22 16:13 etc.tar.bz2
-rw-r--r--. 1 root root 5085179 Mar 22 16:12 etc.tar.gz
-rw-r--r--. 1 root root 3072816 Mar 22 16:13 etc.tar.xz

只打包目录内文件,不包括目录本身

[root@centos8 ~]# cd /etc/
[root@centos8 etc]# tar zcvf /root/test/etc.tar.gz ./

利用tar进行文件复制

[root@centos8 ~]# tar -c /etc/ | tar x -C /backup 

--exclude 排除文件

[root@centos8 ~]# tar zcvf /root/a.tgz --exclude=/app/hosts1 --exclude=/app/hosts2 /app
  • -T  指定输入文件
  • -X  指定包含要排除的文件列表
[root@centos8 ~]# tar zcvf mybackup.tar.gz -T /root/includefilelist -X /root/excludefilelist
2.2 split 将一个文件分割为多个文件
# 分割大的tar文件为多份小文件
split -b size -d tar-file-name prefix-name
# 示例:
split -b 1M mybackup.tar.gz mybackup-parts
# 切换成的多个小文件使用数字后缀
split -b 1M -d mybackup.tar.gz mybackup-parts

将多个切割的小文件合并成一个大文件

cat mybackup-parts* > backup.tar.gz
2.3 cpio

cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以.cpio或者.tar结尾的文件

常用选项:

  • -o  output模式,打包,将标准输入传入的文件名打包后发送到标准输出
  • -i   input模式,解包,对标准输入传入的打包文件名解包到当前目录
  • -t   预览,查看标准输入传入的打包文件中包合的文件列表
  • -O filename  输出到指定的归档文件名
  • -A  向已存在的归档文件中追加文件
  • -I filename  对指定的归档文件名解压
  • -F filename  使用指定的文件名替代标准输入或输出
  • -d  解包生成目录,在cpio还原时,自动的建立目录
  • -v  显示打包过程中的文件名称
# 将etc目录备份
find ./etc/ -print | cpio -ov > bak.cpio
# 将/data内容追加bak.cpio
find /data | cpio -oA bak.cpio
# 内容预览
cpio -tv < etc.cpio
# 解包文件
cpio -idv < etc.cpio

3 yum仓库

Centos使用yum,dnf解决rpm的包依赖关系
YUM:Yelodog Update Modifier,rpm的前端程序,可解决软件包相关依赖性

3.1 yum/dnf工作原理

yum/dnf是基于C/S模式

3.2 yum客户端配置

yum客户端配置文件

/etc/yum.conf   # 为所有仓库提供公共配置
/etc/yum.repo.d/*.repo   # 为每个仓库提供配置文件

帮助参考man 5 yum.conf

repo仓库配置文件指向的定义:

[repositoryID]
name=Some name for this repository
baseurl=url://path/to/repository/
enabled={1|0}
gpgcheck={1|0}
gpgkey=URL
enablegroups={1|0}
failovermenthod={roundrobin|priority}
    roundrobin:意为随机挑选,默认值
    priority:按顺序访问
cost=   默认为1000

yum服务器的baseurl形式

  • file:// 本地路径
  • http://
  • https://
  • ftp://

相关变量:

  • $releasever   当前os的发行版的主版本号,如:6,7,8
  • $arch      CPU架构,如:arch64,i586,i686,x86_64等
  • $basearch    系统基础平台:i386,x86_64
  • $contentdir   表示目录,比如:centos-8,centos-7
  • $YUM0-$YUM9  自定义变量

例:

http://server/centos/$releasever/$basearch
http://server/centos/7/x86_64
http://server/centos/6/i386

Centos8的配置文件

[root@centos8 ~]# ll /etc/yum.conf 
lrwxrwxrwx. 1 root root 12 Nov 12  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   # 升级时,自动选择安装最新版,即使缺少包的依赖

Centos7的配置文件

[root@centos7 ~]# ll /etc/yum.conf  
-rw-r--r--. 1 root root 970 Aug  8  2019 /etc/yum.conf
[root@centos7 ~]# cat /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
3.3 实现私用yum仓库

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

# Centos8 dnf工具集成
dnf reposync --help 查看帮助
# 默认只下载rpm包,不下载meta数据,需要指定--download-metadata才能下载meta
dnf reposync --repoid=REPOID --download--metadata -p /path
# Centos7以前版本,reposync工具来自于yum-utils包
reposync --repoid=REPOID --download-metadata -p /path

常见私有仓库

createrepo [options] <directory>

常见局域网的基于Base的私有yum源

# 仓库服务器配置
[root@centos8 ~]# yum install httpd -y
[root@centos8 ~]# systemctl enable --now httpd
[root@centos8 ~]# mkdir /var/www/html/centos/8 -pv
[root@centos8 ~]# mount /dev/sr0 /mnt/
[root@centos8 ~]# cp -a /mnt/* /var/www/html/centos/8
# yum客户端配置
[root@centos8 ~]# 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

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

[root@centos8 ~]# yum repolist 
Repository epel is listed more than once in the configuration
BaseOS                                                 3.8 MB/s | 3.9 kB     00:00    
AppStream                                              2.7 MB/s | 4.3 kB     00:00    
extras                                                  15 kB/s | 1.5 kB     00:00    
repo id            repo name                                                     status
AppStream          AppStream                                                     4,755
BaseOS             BaseOS                                                        1,659
extras             extras                                                           32

[root@centos8 ~]# dnf reposync --repoid=extras --download-metadata -p /var/www/html/centos
Repository epel is listed more than once in the configuration
extras                                                  23 kB/s | 1.5 kB     00:00    
extras                                                  75 kB/s |  14 kB     00:00    
(1/32): centos-release-ceph-nautilus-1.2-2.el8.noarch. 115 kB/s | 8.8 kB     00:00    
(2/32): centos-release-ansible-29-1-2.el8.noarch.rpm    62 kB/s | 8.4 kB     00:00    
(3/32): centos-release-ceph-octopus-1.0-1.el8.noarch.r 131 kB/s | 8.8 kB     00:00    
(4/32): centos-release-advanced-virtualization-1.0-2.e 104 kB/s |  15 kB     00:00    
(5/32): centos-release-configmanagement-1-1.el8.noarch 162 kB/s | 8.7 kB     00:00    
(6/32): centos-release-gluster6-1.0-1.el8.noarch.rpm   144 kB/s | 9.3 kB     00:00    
(7/32): centos-release-gluster7-1.0-2.el8.noarch.rpm    99 kB/s | 9.5 kB     00:00    
(8/32): centos-release-gluster8-1.0-1.el8.noarch.rpm   126 kB/s | 9.3 kB     00:00    
(9/32): centos-release-gluster9-1.0-1.el8.noarch.rpm   113 kB/s | 9.3 kB     00:00    
(10/32): centos-release-messaging-1-2.el8.noarch.rpm   141 kB/s | 9.4 kB     00:00    
(11/32): centos-release-nfs-ganesha28-1.0-3.el8.noarch  96 kB/s | 8.7 kB     00:00    
(12/32): centos-release-nfs-ganesha30-1.0-2.el8.noarch  88 kB/s | 8.6 kB     00:00    
(13/32): centos-release-nfv-common-1-2.el8.noarch.rpm  105 kB/s | 8.7 kB     00:00    
(14/32): centos-release-nfv-extras-1-2.el8.noarch.rpm  108 kB/s | 8.4 kB     00:00    
(15/32): centos-release-nfv-openvswitch-1-2.el8.noarch 122 kB/s | 8.4 kB     00:00    
(16/32): centos-release-openstack-train-2-1.el8.noarch 161 kB/s |  11 kB     00:00    
(17/32): centos-release-openstack-ussuri-1-5.el8.noarc 135 kB/s |  11 kB     00:00    
(18/32): centos-release-openstack-victoria-1-2.el8.noa 159 kB/s |  10 kB     00:00    
(19/32): centos-release-ovirt44-1.0-1.el8.noarch.rpm   207 kB/s |  15 kB     00:00    
(20/32): centos-release-qpid-proton-1-2.el8.noarch.rpm 107 kB/s | 8.3 kB     00:00    
(21/32): centos-release-rabbitmq-38-1-2.el8.noarch.rpm 144 kB/s | 8.3 kB     00:00    
(22/32): centos-release-opstools-1-10.el8.noarch.rpm    49 kB/s | 9.9 kB     00:00    
(23/32): centos-release-samba411-1.0-1.el8.noarch.rpm  114 kB/s | 8.8 kB     00:00    
(24/32): centos-release-storage-common-2-2.el8.noarch. 628 kB/s | 9.4 kB     00:00    
(25/32): centos-release-samba412-1.0-1.el8.noarch.rpm  141 kB/s | 8.8 kB     00:00    
(26/32): centos-release-samba413-1.0-1.el8.noarch.rpm   87 kB/s | 8.8 kB     00:00    
(27/32): centos-release-virt-common-1-2.el8.noarch.rpm 136 kB/s | 8.9 kB     00:00    
(28/32): centos-release-stream-8.1-1.1911.0.7.el8.x86_ 112 kB/s |  11 kB     00:00    
(29/32): centos-stream-repos-8-2.el8.noarch.rpm        269 kB/s |  19 kB     00:00    
(30/32): elrepo-release-8.1-1.el8.elrepo.noarch.rpm    235 kB/s |  14 kB     00:00    
(31/32): epel-release-8-8.el8.noarch.rpm               1.1 MB/s |  23 kB     00:00    
(32/32): cpaste-1.0.0-3.el8.x86_64.rpm                 329 kB/s |  42 kB     00:00    
[root@centos8 ~]# ls /var/www/html/centos/
extras
[root@centos8 ~]# ls /var/www/html/centos/extras/
Packages  repodata
[root@centos8 ~]# 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/extras

[root@centos8 ~]# yum --disablearepo=* --enablerepo=extras list available 
[root@centos8 ~]# yum install epel-release -y

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

[root@centos8 ~]# cat /etc/yum.repos.d/base.repo 
[epel]
name=epel
baseurl=http://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
gpgcheck=0

[root@centos8 ~]# dnf repolist
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:07:43 ago on Tue 23 Mar 2021 08:31:21 PM CST.
repo id             repo name                                                    status
AppStream           AppStream                                                    4,755
BaseOS              BaseOS                                                       1,659
extras              extras                                                          32
# 下载相关仓库和元数据
[root@centos8 ~]# dnf reposync--repoid=epel --download-metadata -p /var/www/html
# --download-metadata 加此选项可以下载元数据
# 下载相关的key文件
[root@centos8 ~]# wget -P /var/www/html/epel https://mirrors.aliyun.com/rprl/RPM-GPG-KEY-EPEL-8

#下面两个步骤只有没meta数据才需要执行
# [root@centos8 ~]# dnf install createrepo -y
# [root@centos8 ~]# createrepo /var/www/html/epel

[root@centos8 ~]# ls /var/www/html/epel/
Packages  repodata
[root@centos8 ~]# systemctl restart httpd
[root@centos8 ~]# 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/extras
[epel]
name=epel
baseurl=http://10.0.0.8/epel
gpgkey=http://10.0.0.8/epel/RPM-GPG-KEY-EPEL-8
[root@centos8 ~]# dnf repolist
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:16:47 ago on Tue 23 Mar 2021 08:31:21 PM CST.
repo id             repo name                                                    status
AppStream           AppStream                                                    4,755
BaseOS              BaseOS                                                       1,659
extras              extras                                                          32