首页 > 其他 > 详细

标准1.0

时间:2021-03-12 15:22:44      阅读:28      评论:0      收藏:0      [点我收藏+]

创建虚拟机

Tip:创建虚拟机严格按照要求创建,完成之后需要克隆以备后用。【指标1.0】

1、修改网卡名称

net.ifnames=0 biosdevname=0

2.优化步骤

配置yum仓库

rm -f /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all 
yum update -y
yum makecache

安装基础软件包

yum install net-tools vim tree htop iftop gcc gcc-c++ glibciotop lrzsz sl wget unzip telnet nmap nc psmisc dos2unix bash-completion bash-completion-extra sysstat rsync nfs-utils httpd-tools -y

关闭防火墙firewalld

systemctl disable firewalld
systemctl stop firewalld

关闭selinux

sed -i ‘/^SELINUX=/c SELINUX=disabled‘ /etc/selinux/config 

调整单个进程最大能打开文件的数量

echo ‘* - nofile 65535‘ >> /etc/security/limits.conf

以上是【指标1.0】

显示时间

sudo vim /etc/profile
export PS1=‘[\u@\h:\W \t]#‘
source /etc/profile

===================================================================================================================

RPM软件包概述

1、RPM软件包

RedHat Package Manager  #软件包管理

例子:
ant-antunit-1.2-10.el7.noarch.rpm

ant-antunit			#软件包名称  
1.2					#软件的版本 
10					#软件包编译发布的次数 
el7					#适用于7系列的操作系统 
el6					#适用于6系列操作系统
noarch				#适用于任何cpu架构  
x86_64				#硬件平台  位数  
.rpm				#包的后缀名 

2、软件包安装方式

rpm包		 预先编译好的,安装简单  		 版本过低  

源码包		    手动编译,安装繁琐		     版本随意 

二进制包	   绿色软件,解压即用            无法修改源代码   

3、软件包的获取方式

本地的ISO镜像     需要挂载使用 

联网下载  		  下载单个软件包

仓库获取 yum源     

4、rpm软件包管理工具

命令   管理软件包   

#挂载镜像

[root@BC ~]# ll /dev/sr0 
brw-rw---- 1 root cdrom 11, 0 Jul 27 16:29 /dev/sr0

[root@BC ~]# ll /dev/cdrom 
lrwxrwxrwx 1 root root 3 Jul 27 16:29 /dev/cdrom -> sr0

[root@BC ~]# mount   /dev/sr0   /mnt/
mount: /dev/sr0 is write-protected, mounting read-only

[root@BC ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  1.5G   97G   2% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.6M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sr0        4.3G  4.3G     0 100% /mnt

[root@BC ~]# ll  /mnt/Packages/  | wc -l
4023


#rpm安装

	-i		#安装 
	
	-v		#显示安装的过程
	
	-h		#显示安装的进度条 
	
	-ivh     组合使用

[root@BC ~]# rpm  -ivh  /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]


#删除 移除一个软件包

[root@BC ~]# rpm  -e  tree


#rpm安装太需要依赖了,不会自动寻找依赖,需要手动下载依赖    不建议使用  

#安装httpd  报错
[root@BC ~]# rpm  -ivh  /mnt/Packages/httpd-2.4.6-88.el7.centos.x86_64.rpm 
error: Failed dependencies:
	/etc/mime.types is needed by httpd-2.4.6-88.el7.centos.x86_64
	httpd-tools = 2.4.6-88.el7.centos is needed by httpd-2.4.6-88.el7.centos.x86_64
	libapr-1.so.0()(64bit) is needed by httpd-2.4.6-88.el7.centos.x86_64
	libaprutil-1.so.0()(64bit) is needed by httpd-2.4.6-88.el7.centos.x86_64
	
#根据提示安装相关依赖  报错
[root@BC ~]# rpm  -ivh  /mnt/Packages/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm 
error: Failed dependencies:
	libapr-1.so.0()(64bit) is needed by httpd-tools-2.4.6-88.el7.centos.x86_64
	libaprutil-1.so.0()(64bit) is needed by httpd-tools-2.4.6-88.el7.centos.x86_64
	
#查找相关依赖包

[root@BC ~]# ll  /mnt/Packages/  | grep  apr
-rw-rw-r-- 1 root root   105728 Nov 29  2017 apr-1.4.8-3.el7_4.1.x86_64.rpm
-rw-rw-r-- 1 root root   192652 Nov 29  2017 apr-devel-1.4.8-3.el7_4.1.x86_64.rpm
-rw-rw-r-- 1 root root    94132 Jul  4  2014 apr-util-1.5.2-6.el7.x86_64.rpm
-rw-rw-r-- 1 root root    78072 Jul  4  2014 apr-util-devel-1.5.2-6.el7.x86_64.rpm
-rw-rw-r-- 1 root root   854420 Nov 12  2018 haproxy-1.5.18-8.el7.x86_64.rpm

#安装相关依赖
[root@BC ~]# rpm -ivh  /mnt/Packages/apr-1.4.8-3.el7_4.1.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:apr-1.4.8-3.el7_4.1              ################################# [100%]
[root@BC ~]# rpm -ivh  /mnt/Packages/apr-util-1.5.2-6.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:apr-util-1.5.2-6.el7             ################################# [100%]
   
#尝试安装httpd-tools

[root@BC ~]# rpm  -ivh  /mnt/Packages/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:httpd-tools-2.4.6-88.el7.centos  ################################# [100%]
   
#再次安装httpd  报错 
[root@BC ~]# rpm  -ivh  /mnt/Packages/httpd-2.4.6-88.el7.centos.x86_64.rpm 
error: Failed dependencies:
	/etc/mime.types is needed by httpd-2.4.6-88.el7.centos.x86_64


#搜索相关文件的依赖包
[root@BC ~]# yum  provides  /etc/mime.types
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * epel: fedora.cs.nctu.edu.tw
 * extras: mirrors.aliyun.com
 * updates: mirrors.ustc.edu.cn
mailcap-2.1.41-2.el7.noarch : Helper application and MIME type associations for file types
Repo        : base
Matched from:
Filename    : /etc/mime.types

#用yum去搜索
yum search    /etc/mime.types
yum provides  /etc/mime.types


#安装依赖包
[root@BC ~]# rpm  -ivh  /mnt/Packages/mailcap-2.1.41-2.el7.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mailcap-2.1.41-2.el7             ################################# [100%]
   
#最终安装成功 

[root@BC ~]# rpm  -ivh  /mnt/Packages/httpd-2.4.6-88.el7.centos.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:httpd-2.4.6-88.el7.centos        ################################# [100%]
[root@BC ~]# 



#联网安装软件包 

[root@BC ~]# rpm  -ivh  https://mirrors.aliyun.com/centos/7.8.2003/os/x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/centos/7.8.2003/os/x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]


#升级安装 

-U		#升级  

[root@BC ~]# rpm  -ivh  https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
warning: /var/tmp/rpm-tmp.jseg5b: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-4.0.0-2.el7         ################################# [100%]

[root@BC ~]# zabbix_agentd -V
zabbix_agentd (daemon) (Zabbix) 4.0.0
Revision 85308 1 October 2018, compilation time: Oct  1 2018 08:41:36

Copyright (C) 2018 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.

#升级安装
[root@BC ~]# rpm  -Uvh  https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.0-1.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.0-1.el7.x86_64.rpm
warning: /var/tmp/rpm-tmp.e9rZWk: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-4.4.0-1.el7         ################################# [ 50%]
Cleaning up / removing...
   2:zabbix-agent-4.0.0-2.el7         ################################# [100%]
[root@BC ~]# zabbix_agentd -V
zabbix_agentd (daemon) (Zabbix) 4.4.0
Revision cfac660b25 7 October 2019, compilation time: Oct  7 2019 11:26:17

Copyright (C) 2019 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.

This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/).

Compiled with OpenSSL 1.0.1e-fips 11 Feb 2013
Running with OpenSSL 1.0.1e-fips 11 Feb 2013

#查询指定的软件包是否安装

[root@BC ~]# rpm  -q  tree
tree-1.6.0-10.el7.x86_64
[root@BC ~]# rpm  -q  ifconfig
package ifconfig is not installed
[root@BC ~]# rpm  -q  net-tools
net-tools-2.0-0.25.20131004git.el7.x86_64

#显示所有已经安装的软件包 

[root@BC ~]# rpm -qa  | grep  tree
tree-1.6.0-10.el7.x86_64


#查询软件包列表信息
[root@BC ~]# rpm  -ql  httpd
/etc/httpd
/etc/httpd/conf
/etc/httpd/conf.d
/etc/httpd/conf.d/README
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf

#查询相关配置文件信息
[root@BC ~]# rpm  -qc  httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd

#查询软件包本身信息

[root@BC ~]# rpm  -qi  httpd
Name        : httpd
Version     : 2.4.6
Release     : 88.el7.centos
Architecture: x86_64
Install Date: Thu 30 Jul 2020 09:03:08 AM CST
Group       : System Environment/Daemons
Size        : 9817309
License     : ASL 2.0
Signature   : RSA/SHA256, Mon 12 Nov 2018 10:28:53 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : httpd-2.4.6-88.el7.centos.src.rpm
Build Date  : Mon 05 Nov 2018 09:48:57 AM 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@BC ~]# rpm  -qf  `which  ping`
iputils-20160308-10.el7.x86_64
[root@BC ~]# which  ping
/usr/bin/ping
[root@BC ~]# rpm -qf  /usr/bin/ping
iputils-20160308-10.el7.x86_64


总结:

	-ivh		#安装
	
	-Uvh		#升级
	
	-e			#卸载
	
	-q			#查询指定软件包 
	
	-qa			#查询所有已经安装的软件包
	
	-qi			#查询软件包本身相关信息
	
	-ql			#查询软件包列表信息
	
	-qc			#查询软件包相关文件
	
	-qf			#查询命令属于哪个软件包  必须是绝对路径  前提本地要已经存在这个软件包

5、yum管理工具

yum源   yum仓库  


[root@BC ~]# ll  /etc/yum.repos.d/
total 40
-rw-r--r-- 1 root root 1664 Nov 23  2018 CentOS-Base.repo
-rw-r--r-- 1 root root 1309 Nov 23  2018 CentOS-CR.repo
-rw-r--r-- 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo
-rw-r--r-- 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r-- 1 root root 1331 Nov 23  2018 CentOS-Sources.repo
-rw-r--r-- 1 root root 5701 Nov 23  2018 CentOS-Vault.repo
-rw-r--r-- 1 root root  951 Oct  3  2017 epel.repo
-rw-r--r-- 1 root root 1050 Oct  3  2017 epel-testing.repo


怎么获取yum源  

本地的ISO镜像   

yum源仓库  

联网的方式下载 

本地yum仓库 


yum的种类  

base		#基础源 

epel		#扩展源  

软件官方源     # nginx   zabbix   docker  



阿里源  

清华源

中科大  

163源 


#更新源  

[root@BC ~]# rm -rf  /etc/yum.repos.d/*

[root@BC ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo


[root@BC ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo



[root@BC ~]# ll /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 2523 Jul 30 09:51 CentOS-Base.repo
-rw-r--r-- 1 root root  664 May 11  2018 epel.repo


#显示可用的yum仓库

[root@BC ~]# yum  repolist   
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,070
!epel/x86_64                               Extra Packages for Enterprise Linux 7 - x86_64                          13,422
!extras/7/x86_64                           CentOS-7 - Extras - mirrors.aliyun.com                                     412
!updates/7/x86_64                          CentOS-7 - Updates - mirrors.aliyun.com                                    900
repolist: 24,804


#显示所有的

[root@BC ~]# yum  repolist   all
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                                  enabled: 10,070
centosplus/7/x86_64                 CentOS-7 - Plus - mirrors.aliyun.com                                  disabled
contrib/7/x86_64                    CentOS-7 - Contrib - mirrors.aliyun.com                               disabled
!epel/x86_64                        Extra Packages for Enterprise Linux 7 - x86_64                        enabled: 13,422
epel-debuginfo/x86_64               Extra Packages for Enterprise Linux 7 - x86_64 - Debug                disabled
epel-source                         Extra Packages for Enterprise Linux 7 - x86_64 - Source               disabled
!extras/7/x86_64                    CentOS-7 - Extras - mirrors.aliyun.com                                enabled:    412
!updates/7/x86_64                   CentOS-7 - Updates - mirrors.aliyun.com                               enabled:    900
repolist: 24,804


[root@BC ~]# yum  install  -y  yum-utils


[root@BC ~]# yum-config-manager    --disable  epel


[root@BC ~]# yum  repolist  all
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                                  enabled: 10,070
centosplus/7/x86_64                 CentOS-7 - Plus - mirrors.aliyun.com                                  disabled
contrib/7/x86_64                    CentOS-7 - Contrib - mirrors.aliyun.com                               disabled
epel/x86_64                         Extra Packages for Enterprise Linux 7 - x86_64                        disabled
epel-debuginfo/x86_64               Extra Packages for Enterprise Linux 7 - x86_64 - Debug                disabled
epel-source                         Extra Packages for Enterprise Linux 7 - x86_64 - Source               disabled
extras/7/x86_64                     CentOS-7 - Extras - mirrors.aliyun.com                                enabled:    412
updates/7/x86_64                    CentOS-7 - Updates - mirrors.aliyun.com                               enabled:    900
repolist: 11,382

[root@BC ~]# yum-config-manager    --enable  epel



#软件包管理 

#显示所有可安装的包 

[root@BC ~]# yum  list

#显示系统中已经安装的包
[root@BC ~]# yum  list  installed 


[root@BC ~]# yum  list  | grep  ^tree
tree.x86_64                               1.6.0-10.el7                 @base    
treelayout.noarch                         1.0.3-4.el7                  epel     
treelayout-demo.noarch                    1.0.3-4.el7                  epel     
treelayout-javadoc.noarch                 1.0.3-4.el7                  epel     

#显示可更新的包

[root@BC ~]# yum  check-update

[root@BC ~]# yum    list   updates  


[root@BC ~]# rpm  -qa  httpd
httpd-2.4.6-88.el7.centos.x86_64

#更新某个软件包 

[root@BC ~]# yum   update  httpd  -y


[root@BC ~]# rpm  -qa  httpd
httpd-2.4.6-93.el7.centos.x86_64


#安装软件包


[root@BC ~]# yum  install   tree		#交互式  麻烦  


[root@BC ~]# yum  install   tree   -y		#免交互


#卸载  

[root@BC ~]# yum  remove   tree  -y

[root@BC ~]# yum  erase   tree  -y


#联网安装软件包 

[root@BC ~]# yum  install  -y  https://mirrors.aliyun.com/centos/7.8.2003/os/x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm


#本地安装 

[root@BC ~]# yum localinstall  -y  /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 



[root@BC ~]# systemctl  start httpd
[root@BC ~]# systemctl  status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-07-30 10:36:26 CST; 6s ago


[root@BC ~]# rm -f /etc/httpd/conf/httpd.conf
[root@BC ~]# systemctl  restart httpd
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

#重新安装 

[root@BC ~]# yum  reinstall  -y  httpd


#搜索 

[root@BC ~]# yum search  ifconfig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
================================================= N/S matched: ifconfig =================================================
python36-ifcfg.noarch : Python cross-platform network interface discovery (ifconfig/ipconfig/ip)

  Name and summary matches only, use "search all" for everything.

#联网搜索命令属于哪个软件包 

[root@BC ~]# yum  provides   ifconfig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
epel/x86_64/filelists_db                                                                          |  12 MB  00:00:04     
net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools
Repo        : @base
Matched from:
Filename    : /usr/sbin/ifconfig

#查看软件包相关信息 

[root@BC ~]# yum  info  tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
Name        : tree
Arch        : x86_64
Version     : 1.6.0
Release     : 10.el7
Size        : 87 k
Repo        : installed
Summary     : File system tree viewer
URL         : http://mama.indstate.edu/users/ice/tree/
License     : GPLv2+
Description : The tree utility recursively displays the contents of directories in a
            : tree-like format.  Tree is basically a UNIX port of the DOS tree
            : utility.

#组包的安装

[root@BC ~]# yum  groups  install   Python

#组包的卸载

[root@BC ~]# yum  groups  remove   Python

#yum缓存 

#清除包的缓存 

[root@BC ~]# yum   clean  packages  
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
0 package files removed

#清除插件的缓存 

[root@BC ~]# yum   clean  plugins
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
Cleaning up list of fastest mirrors


#清除所有缓存 

[root@BC ~]# yum   clean  all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates

#生成缓存 

[root@BC ~]# yum  makecache


#yum 历史记录 

[root@BC ~]# yum  history
Loaded plugins: fastestmirror
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    27 | reinstall -y httpd       | 2020-07-30 10:37 | Reinstall      |    1   
    26 | localinstall -y /mnt/Pac | 2020-07-30 10:11 | Install        |    1   
    25 | erase tree -y            | 2020-07-30 10:10 | Erase          |    1   
    24 | install -y https://mirro | 2020-07-30 10:09 | Install        |    1   


#显示所有历史记录 

[root@BC ~]# yum  history  list  all
Loaded plugins: fastestmirror
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    27 | root <root>              | 2020-07-30 10:37 | Reinstall      |    1   
    26 | root <root>              | 2020-07-30 10:11 | Install        |    1   
    25 | root <root>              | 2020-07-30 10:10 | Erase          |    1   
    24 | root <root>              | 2020-07-30 10:09 | Install        |    1   
    23 | root <root>              | 2020-07-30 10:08 | Erase          |    1   
    22 | root <root>              | 2020-07-30 10:08 | Install        |    1   

#查看某个ID的历史信息


[root@BC ~]# yum  history  info  15
Loaded plugins: fastestmirror
Transaction ID : 15
Begin time     : Wed Jul 22 08:51:56 2020
Begin rpmdb    : 372:6b59857cbbb441dc1cf9e1a96ab23013fd830365
End time       :                           (0 seconds)
End rpmdb      : 373:c16ad60498e5079097783959293418aeedfc68b5
User           : root <root>
Return-Code    : Success
Command Line   : install -y psmisc
Transaction performed with:
    Installed     rpm-4.11.3-35.el7.x86_64                      @anaconda
    Installed     yum-3.4.3-161.el7.centos.noarch               @anaconda
    Installed     yum-plugin-fastestmirror-1.1.31-50.el7.noarch @anaconda
Packages Altered:
    Install psmisc-22.20-16.el7.x86_64 @base
history info


#回滚历史ID为10的操作

[root@BC ~]# yum  history  undo  10  -y 


[root@BC ~]# yum  --setopt=history_list_view=commands  history  list all
Loaded plugins: fastestmirror
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    28 | history undo 10          | 2020-07-30 10:49 | Erase          |   21   
    27 | reinstall -y httpd       | 2020-07-30 10:37 | Reinstall      |    1   
    26 | localinstall -y /mnt/Pac | 2020-07-30 10:11 | Install        |    1   
    25 | erase tree -y            | 2020-07-30 10:10 | Erase          |    1   
    24 | install -y https://mirro | 2020-07-30 10:09 | Install        |    1   
    23 | erase tree -y            | 2020-07-30 10:08 | Erase          |    1   
    22 | install tree -y          | 2020-07-30 10:08 | Install        |    1   
    21 | remove tree -y           | 2020-07-30 10:08 | Erase          |    1   



[root@BC ~]# ll  /etc/yum.conf 


#只下载 不安装   指定下载的路径

[root@BC ~]# yum  install  -y  --downloadonly  --downloaddir=/opt   nginx


[root@BC ~]# ll /opt/
total 4380
-rw-r--r-- 1 root root   93872 Jul  4  2014 centos-indexhtml-7-9.el7.centos.noarch.rpm
-rw-r--r-- 1 root root   65788 Jul  4  2014 dejavu-fonts-common-2.33-6.el7.noarch.rpm
-rw-r--r-- 1 root root 1482820 Jul  4  2014 dejavu-sans-fonts-2.33-6.el7.noarch.rpm
-rw-r--r-- 1 root root  260512 Nov 12  2018 fontconfig-2.13.0-4.3.el7.x86_64.rpm
-rw-r--r-- 1 root root   10116 Jul  4  2014 fontpackages-filesystem-1.44-8.el7.noarch.rpm
-rw-r--r-- 1 root root  149492 Jul  4  2014 gd-2.0.35-26.el7.x86_64.rpm
-rw-r--r-- 1 root root  278636 Apr 25  2018 gperftools-libs-2.6.1-1.el7.x86_64.rpm
-rw-r--r-- 1 root root  137752 Aug 23  2019 libjpeg-turbo-1.2.90-8.el7.x86_64.rpm
-rw-r--r-- 1 root root  621364 Aug 23  2019 libX11-1.6.7-2.el7.x86_64.rpm
-rw-r--r-- 1 root root  168172 Aug 23  2019 libX11-common-1.6.7-2.el7.noarch.rpm
-rw-r--r-- 1 root root   29784 Jul  4  2014 libXau-1.0.8-2.1.el7.x86_64.rpm
-rw-r--r-- 1 root root  218900 Nov 12  2018 libxcb-1.13-1.el7.x86_64.rpm
-rw-r--r-- 1 root root   56800 Aug 11  2017 libXpm-3.5.12-1.el7.x86_64.rpm
-rw-r--r-- 1 root root  575413 Oct  4  2019 nginx-1.16.1-1.el7.x86_64.rpm
-rw-r--r-- 1 root root   19857 Oct  4  2019 nginx-all-modules-1.16.1-1.el7.noarch.rpm
-rw-r--r-- 1 root root   21089 Oct  4  2019 nginx-filesystem-1.16.1-1.el7.noarch.rpm
-rw-r--r-- 1 root root   30445 Oct  4  2019 nginx-mod-http-image-filter-1.16.1-1.el7.x86_64.rpm
-rw-r--r-- 1 root root   39849 Oct  4  2019 nginx-mod-http-perl-1.16.1-1.el7.x86_64.rpm
-rw-r--r-- 1 root root   29613 Oct  4  2019 nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64.rpm
-rw-r--r-- 1 root root   58301 Oct  4  2019 nginx-mod-mail-1.16.1-1.el7.x86_64.rpm
-rw-r--r-- 1 root root   86465 Oct  4  2019 nginx-mod-stream-1.16.1-1.el7.x86_64.rpm

[root@BC ~]# cat /etc/yum.conf
[main]
#cachedir=/var/cache/yum/$basearch/$releasever
cachedir=/opt		#指定下载保存路径
keepcache=1			#开启缓存 


[root@BC ~]# yum install  -y  nginx 


[root@BC ~]# ll /opt/
total 12
drwxr-xr-x 4 root root  256 Jul 30 11:03 base
drwxr-xr-x 4 root root 4096 Jul 30 11:03 epel
drwxr-xr-x 4 root root  161 Jul 30 11:03 extras
-rw-r--r-- 1 root root  117 Jul 30 11:04 timedhosts
-rw-r--r-- 1 root root  107 Jul 30 11:03 timedhosts.txt
drwxr-xr-x 4 root root  161 Jul 30 11:03 updates
[root@BC ~]# find  /opt/  -name  "*.rpm"
/opt/base/packages/centos-indexhtml-7-9.el7.centos.noarch.rpm
/opt/base/packages/dejavu-fonts-common-2.33-6.el7.noarch.rpm
/opt/base/packages/fontconfig-2.13.0-4.3.el7.x86_64.rpm
/opt/base/packages/dejavu-sans-fonts-2.33-6.el7.noarch.rpm
/opt/base/packages/fontpackages-filesystem-1.44-8.el7.noarch.rpm
/opt/base/packages/gd-2.0.35-26.el7.x86_64.rpm
/opt/base/packages/gperftools-libs-2.6.1-1.el7.x86_64.rpm
/opt/base/packages/libX11-1.6.7-2.el7.x86_64.rpm
/opt/base/packages/libX11-common-1.6.7-2.el7.noarch.rpm
/opt/base/packages/libXau-1.0.8-2.1.el7.x86_64.rpm
/opt/base/packages/libXpm-3.5.12-1.el7.x86_64.rpm
/opt/base/packages/libjpeg-turbo-1.2.90-8.el7.x86_64.rpm
/opt/base/packages/libxcb-1.13-1.el7.x86_64.rpm
/opt/epel/packages/nginx-all-modules-1.16.1-1.el7.noarch.rpm
/opt/epel/packages/nginx-1.16.1-1.el7.x86_64.rpm
/opt/epel/packages/nginx-filesystem-1.16.1-1.el7.noarch.rpm
/opt/epel/packages/nginx-mod-http-image-filter-1.16.1-1.el7.x86_64.rpm
/opt/epel/packages/nginx-mod-http-perl-1.16.1-1.el7.x86_64.rpm
/opt/epel/packages/nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64.rpm
/opt/epel/packages/nginx-mod-mail-1.16.1-1.el7.x86_64.rpm
/opt/epel/packages/nginx-mod-stream-1.16.1-1.el7.x86_64.rpm

yum总结:

	repolist		#针对仓库操作 
	
		all			#显示所有 
		
	list			#显示所有可安装的软件包 
	
		installed	#显示已经安装的
		
		updates		#显示可更新的
		
	check-update	#显示可更新的
	
	update			#更新
    
    install			#安装  
	
	localinstall	#本地安装 
	
	reinstall		#重新安装 
	
	remove			#卸载
	
	erase			#卸载 
	
	info			#显示软件包信息
	
	search			#搜索
	
	provides		#查询命令属于哪个软件包 
	
	groups			#组包管理
	
		install		#安装
		
		remove		#移除
		
	clean			#清空
	
		all			#清空所有 
		
		packages	#清空软件包
		
		plugins		#清空插件 
		
	makecahce		#生成缓存
	
	history			#历史记录
	
		list	all		#显示所有
		
		info 	ID		#显示某次记录的信息
		
		undo	ID		#回滚这次的操作 
		
		
		
		
yum-config-manager		#yum仓库管理工具 

	--disable		#禁用仓库
	
	--enable		#启用仓库 

6、搭建本地yum仓库

#把之前的仓库全部将其失效

[root@BC ~]# ll /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 2523 Jul 30 09:51 CentOS-Base.repo
-rw-r--r-- 1 root root  664 Jul 30 09:57 epel.repo
[root@BC ~]# gzip  /etc/yum.repos.d/*
[root@BC ~]# ll /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 601 Jul 30 09:51 CentOS-Base.repo.gz
-rw-r--r-- 1 root root 254 Jul 30 09:57 epel.repo.gz

#挂载镜像源

[root@BC ~]# mount  /dev/sr0   /mnt/

[root@BC ~]# df -h  | grep  mnt
/dev/sr0        4.3G  4.3G     0 100% /mnt




file://			#本地

ftp://			#文件传输地址

http://			#网络协议地址

https://		#网络协议地址 


#编写yum源文件

[root@BC ~]# cat /etc/yum.repos.d/lcoal.repo
[BC]			#仓库名称  不能有空格
name=this  is  local  repo		#仓库说明信息
baseurl=file:///mnt				#仓库地址
enabled=1					#启用仓库
gpgcheck=0					#不检查其合法性  


#测试是否可用

[root@BC ~]# yum  clean  all
Loaded plugins: fastestmirror
Cleaning repos: BC
Cleaning up list of fastest mirrors
Other repos take up 309 M of disk space (use --verbose for details)


[root@BC ~]# yum  makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
BC                                                                                               | 3.6 kB  00:00:00     
(1/4): BC/group_gz                                                                               | 166 kB  00:00:00     
(2/4): BC/filelists_db                                                                           | 3.2 MB  00:00:00     
(3/4): BC/primary_db                                                                             | 3.1 MB  00:00:00     
(4/4): BC/other_db                                                                               | 1.3 MB  00:00:00     
Metadata Cache Created


[root@BC ~]# yum install  -y  tree

7、将yum仓库共享给其他主机

10.0.0.100		#本地仓库地址    服务端 


10.0.0.101		#客户端   

#客户端修改主机名 
[root@BC ~]# hostnamectl   set-hostname  client



# 在10.0.0.100 主机上面操作 

[root@BC ~]# mount  /dev/cdrom   /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@BC ~]# df -h | grep mnt
/dev/sr0        4.3G  4.3G     0 100% /mnt

#安装文件传输工具

[root@BC ~]# yum  install  -y  vsftpd

#启动
[root@BC ~]# systemctl  start  vsftpd
[root@BC ~]# systemctl  enable  vsftpd

#关闭防火墙
[root@BC ~]# systemctl  stop  firewalld

#关闭Selinux
[root@BC ~]# setenforce  0


#测试

#浏览器输出

ftp://10.0.0.100/

#访问的是这个目录

[root@BC ~]# ll /var/ftp/
total 0
drwxr-xr-x 2 root root 6 Oct 31  2018 pub


[root@BC ~]# mkdir  /var/ftp/local-base
[root@BC ~]# ll /var/ftp/
total 0
drwxr-xr-x 2 root root 6 Jul 30 19:55 local-base
drwxr-xr-x 2 root root 6 Oct 31  2018 pub
[root@BC ~]# cp  -rp  /mnt/Packages/*  /var/ftp/local-base/

#下载管理工具

[root@BC ~]# yum  install  -y  createrepo

#告诉系统,这是个镜像yum源仓库 

[root@BC ~]# createrepo   /var/ftp/local-base/
Spawning worker 0 with 4021 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete


#修改yum配置文件

[root@BC ~]# cat /etc/yum.repos.d/lcoal.repo
[BC]
name=this  is  local  repo
baseurl=ftp://10.0.0.100/local-base/
enabled=1
gpgcheck=0


[root@BC ~]# yum  repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                           repo name                                                        status
BC                                               this  is  local  repo                                            4,021
repolist: 4,021





#让其他主机使用这个仓库

在10.0.0.101主机上面操作


[root@client ~]# systemctl  stop  firewalld
[root@client ~]# setenforce  0



[root@client ~]# gzip  /etc/yum.repos.d/*
[root@client ~]# ll /etc/yum.repos.d/
total 28
-rw-r--r--. 1 root root 549 Nov 23  2018 CentOS-Base.repo.gz
-rw-r--r--. 1 root root 735 Nov 23  2018 CentOS-CR.repo.gz
-rw-r--r--. 1 root root 426 Nov 23  2018 CentOS-Debuginfo.repo.gz
-rw-r--r--. 1 root root 232 Nov 23  2018 CentOS-fasttrack.repo.gz
-rw-r--r--. 1 root root 381 Nov 23  2018 CentOS-Media.repo.gz
-rw-r--r--. 1 root root 506 Nov 23  2018 CentOS-Sources.repo.gz
-rw-r--r--. 1 root root 633 Nov 23  2018 CentOS-Vault.repo.gz


#编写yum源配置文件

[root@client ~]# cat /etc/yum.repos.d/lcoal.repo
[BC]
name=this  is  local  repo
baseurl=ftp://10.0.0.100/local-base/
enabled=1
gpgcheck=0



#测试

[root@client ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: BC
[root@client ~]# yum  makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
BC                                                                                               | 2.9 kB  00:00:00     
(1/3): BC/filelists_db                                                                           | 3.2 MB  00:00:00     
(2/3): BC/primary_db                                                                             | 3.2 MB  00:00:00     
(3/3): BC/other_db                                                                               | 1.3 MB  00:00:00     
Metadata Cache Created


[root@client ~]# yum  install  vim -y

源码包管理

?


1. 要有源码包  下载源码包

[root@BC ~]# wget  http://nginx.org/download/nginx-1.18.0.tar.gz



[root@BC ~]# ll
total 1016
-rw-r--r-- 1 root root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz

2. 解压软件包 

[root@BC ~]# tar  xf  nginx-1.18.0.tar.gz 
[root@BC ~]# ll
total 1016
drwxr-xr-x 8 user05 1001     158 Apr 21 22:09 nginx-1.18.0
-rw-r--r-- 1 root   root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz


3. 进入这个目录,进行预编译操作   编译设置  

[root@BC nginx-1.18.0]# ./configure   --prefix=/opt/nginx-1.18.0   --with-http_ssl_module
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found		#报错   缺少编译软件 gcc



#按照依赖

[root@BC nginx-1.18.0]# yum install  -y  gcc



#再次执行报错 

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.


#解决:

[root@BC nginx-1.18.0]# yum install  -y  pcre  pcre-devel 


#再次执行报错

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.


#解决:

[root@BC nginx-1.18.0]# yum  install  -y  openssl  openssl-devel


#4. 编译

[root@BC nginx-1.18.0]# make


5. 将编译的结果拷贝到指定的位置 

[root@BC nginx-1.18.0]# make  install


[root@BC nginx-1.18.0]# ll /opt/
drwxr-xr-x 6 root root   54 Jul 31 16:54 nginx-1.18.0


6. 做个软链接

[root@BC ~]# ln -s  /opt/nginx-1.18.0/  /opt/nginx


7. 启动 

[root@BC ~]# /opt/nginx/sbin/nginx 

定时任务

1、定时任务的概念

设定指定的时间周期性执行你的计划或者任务 
	
	crond		# 守护进程       分钟级别  
	
	两种:
	
		系统级别定时任务:	定时清理文件   收集系统信息  定时切割日志   
		
		用户级别定时任务:	同步时间    定时备份数据 
	

2、定时任务相关介绍

[root@BC ~]# ll  /etc/cron*  -d
drwxr-xr-x. 2 root root  21 Jul 29 08:56 /etc/cron.d		#定时任务的统一存放目录  /**.d配置文件的总目录
drwxr-xr-x. 2 root root  57 Jul 29 08:56 /etc/cron.daily	#系统每天执行的定时任务
-rw-------  1 root root   0 Apr 11  2018 /etc/cron.deny		#定时任务的黑名单 
drwxr-xr-x. 2 root root  22 Jul 29 08:56 /etc/cron.hourly	#系统每小时执行的定时任务
drwxr-xr-x. 2 root root   6 Jun 10  2014 /etc/cron.monthly	#系统每月执行的定时任务
-rw-r--r--  1 root root 451 Jun 10  2014 /etc/crontab		#定时任务主配置文件
drwxr-xr-x. 2 root root   6 Jun 10  2014 /etc/cron.weekly	#系统每周执行的定时任务 



[root@BC ~]# cat /etc/crontab
SHELL=/bin/bash			#定时任务所使用的命令解释器 
PATH=/sbin:/bin:/usr/sbin:/usr/bin		#定时任务所能用到的命令路径
MAILTO=root				#接收邮件 

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)		#分钟   
# |  .------------- hour (0 - 23)		#小时  
# |  |  .---------- day of month (1 - 31)	#日期 
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...	#月份 
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |			#  周 
# *  *  *  *  * user-name  command to be executed

分时日月周      用户    命令 


#跟定时任务相关的文件 

[root@BC ~]# ll /var/spool/cron/root		#存放root用户定时任务的配置文件  
total 0

[root@BC ~]# ll /var/log/cron		#定时任务执行的过程   日志 


[root@BC ~]# ll /var/spool/mail/	#用户的邮件  


怎样书写定时任务 


crontab			#书写定时任务的命令  

选项:

	-e			#编辑定时任务    ===   vi  /var/spool/cron/root
	
	-l			#查看定时任务    ===   cat  /var/spool/cron/root

1. 语法检查 

2. 方便简单 


#定时任务的规则 

*	# 每(分时日月周)都执行  

*/5	# 每 5 (分时日月周)执行  每隔多长时间  

/5	

1-3		#时间范围  1-3   连续的时间  1点到3点

1,3		#不连续的时间  1点和3点 


00 02 * * *   	#每天的凌晨2点整  

00 02 1 * * 	#每个月的1号凌晨2点整  

00 02 14 2 * 	#每年的2月14日凌晨2点整   

00 02 * * 7 	#每周日的凌晨2点整  

00 02 * 6 5 	#每年的6月份的每周五的凌晨2点整    

00 02 14 * 7 	#每个月的14号或者周日的凌晨2点整

00 02 14 2 7  	#每年的2月份的14号或者周日的凌晨2点整

*/10  02 * * * 	#每天的凌晨2点每隔10分钟  

* * * * *   	#每分钟 

00 00 14 2 *  	#每年的2月份14号的凌晨0点整

*/5 * * * *   	#每隔5分钟 

00 02 * 1,5,8 *  #每年的1和5和8月的每天的凌晨2点整

00 02 1-8 * *   #每个月的1到8号的凌晨2点整

00 21 * * *   	#每天晚上21点整

45 4 1,10,22 * * 	#每个月的1,10,22号 的凌晨4点45分 

45 4 1-10 * *  		#每个月的1到10号的凌晨4点45分

3,15 8-11 */2 * * 	#每个月每隔两天的8到11点的3分和15分的时候 

0 23-7/2 * * *   	#每天的23点到7点的每隔2个小时的整点 

15 21 * * 1-5		#每周一到周五的晚上21点15分

3、定时任务与案例

1. 定时同步系统时间 每分钟同步 

[root@BC ~]# ntpdate  ntp.aliyun.com
31 Jul 10:27:12 ntpdate[13673]: step time server 203.107.6.88 offset -28797.933639 sec
[root@BC ~]# date
Fri Jul 31 10:27:18 CST 2020


定时任务最好加上注释  作者  时间 

[root@BC ~]# crontab  -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@BC ~]# crontab  -l
#同步系统时间 BC 20200731_10
* * * * *  ntpdate  ntp.aliyun.com

#修改时间
[root@BC ~]# date -s  20200730
Thu Jul 30 00:00:00 CST 2020
[root@BC ~]# date
Thu Jul 30 00:00:02 CST 2020

#查看定时任务的执行过程

[root@BC ~]# tailf  /var/log/cron
Jul 31 17:01:01 BC run-parts(/etc/cron.hourly)[13622]: finished 0anacron
Jul 31 17:52:01 BC crontab[13651]: (root) LIST (root)
Jul 31 18:01:01 BC CROND[13656]: (root) CMD (run-parts /etc/cron.hourly)
Jul 31 18:01:01 BC run-parts(/etc/cron.hourly)[13656]: starting 0anacron
Jul 31 18:01:01 BC run-parts(/etc/cron.hourly)[13665]: finished 0anacron
Jul 31 10:27:49 BC crontab[13675]: (root) BEGIN EDIT (root)
Jul 31 10:30:30 BC crontab[13675]: (root) REPLACE (root)
Jul 31 10:30:30 BC crontab[13675]: (root) END EDIT (root)
Jul 31 10:30:36 BC crontab[13677]: (root) LIST (root)
Jul 30 00:00:03 BC CROND[13682]: (root) CMD (ntpdate  ntp.aliyun.com)

#查看接收的邮件发现了报错  说命令找不到 

[root@BC ~]# ll  /var/spool/mail/root 
-rw------- 1 root mail 3541 Jul 30 00:01 /var/spool/mail/root
[root@BC ~]# tailf /var/spool/mail/root
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20200729160201.064E8802C9EE@BC.localdomain>
Date: Thu, 30 Jul 2020 00:02:01 +0800 (CST)

/bin/sh: ntpdate: command not found


#重新编写定时任务 

[root@BC ~]# crontab -l
#同步系统时间 BC 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com


[root@BC ~]# tailf  /var/log/cron
Jul 30 00:01:01 BC run-parts(/etc/cron.hourly)[13694]: starting 0anacron
Jul 30 00:01:02 BC anacron[13707]: Anacron started on 2020-07-30
Jul 30 00:01:02 BC anacron[13707]: Normal exit (0 jobs run)
Jul 30 00:01:02 BC run-parts(/etc/cron.hourly)[13709]: finished 0anacron
Jul 30 00:02:01 BC CROND[13714]: (root) CMD (ntpdate  ntp.aliyun.com)
Jul 30 00:03:01 BC CROND[13722]: (root) CMD (ntpdate  ntp.aliyun.com)
Jul 30 00:03:31 BC crontab[13728]: (root) BEGIN EDIT (root)
Jul 30 00:03:38 BC crontab[13728]: (root) REPLACE (root)
Jul 30 00:03:38 BC crontab[13728]: (root) END EDIT (root)
Jul 30 00:03:43 BC crontab[13730]: (root) LIST (root)



Jul 30 00:04:01 BC crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 30 00:04:01 BC CROND[13734]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com)
Jul 31 10:35:04 BC CROND[13741]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com)

#邮件正在一直接收信息 导致邮件过大 

[root@BC ~]# ll /var/spool/mail/root 
-rw------- 1 root mail 7028 Jul 31 10:35 /var/spool/mail/root
[root@BC ~]# ll /var/spool/mail/root 
-rw------- 1 root mail 7929 Jul 31 10:36 /var/spool/mail/root

#停掉邮件服务 日志不在发生变化 

[root@BC ~]# systemctl  stop postfix
[root@BC ~]# ll /var/spool/mail/root 
-rw------- 1 root mail 8829 Jul 31 10:37 /var/spool/mail/root

#但是会一直生成小文件 

[root@BC ~]# ll /var/spool/postfix/maildrop/
total 12
-rwxr--r-- 1 root postdrop 601 Jul 31 10:38 7FF40C0CD48D
-rwxr--r-- 1 root postdrop 600 Jul 31 10:39 A8919C0CD48E
-rwxr--r-- 1 root postdrop 601 Jul 31 10:40 CD943C0CD48F


#重新编写定时任务

[root@BC ~]# crontab  -l
#同步系统时间 BC 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null

[root@BC ~]# systemctl  start postfix

[root@BC ~]# ll /var/spool/mail/root 
-rw------- 1 root mail 13469 Jul 31 10:43 /var/spool/mail/root


总结:

1. 定时任务要有注释 作者 时间 

2. 定时任务的命令一定要在命令行上面执行成功

3. 定时任务要使用绝对路径 

4. 定时任务写命令的时候,尽量复制之前执行成功的命令 减少出错率

5. 定时任务的执行结果定向到指定的文件中或者定向到空


2. 把系统的时间追加到一个文件中


[root@BC ~]# date +%F_%T >> /root/time.txt
[root@BC ~]# cat /root/time.txt
2020-07-31_10:51:45


[root@BC ~]# crontab -l
#同步系统时间 BC 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
* * * * *  /usr/bin/date +%F_%T >> /root/time.txt  


[root@BC ~]# tailf  /var/log/cron
Jul 31 10:54:01 BC crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 31 10:54:01 BC CROND[14046]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null)
Jul 31 10:54:01 BC CROND[14047]: (root) CMD (/usr/bin/date +)


#修改定时任务

[root@BC ~]# crontab -l
#同步系统时间 BC 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
* * * * *  /usr/bin/date +\%F_\%T >> /root/time.txt  



[root@BC ~]# tailf  /var/log/cron
Jul 31 10:56:01 BC crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 31 10:56:01 BC CROND[14074]: (root) CMD (/usr/bin/date +%F_%T >> /root/time.txt  )


[root@BC ~]# cat time.txt 
2020-07-31_10:56:01


总结:

	定时任务中,有些特殊字符不识别,需要转义  
	

3. 备份/etc/目录   压缩包名带有时间戳    保留最近的3天数据 

[root@BC ~]# cat backup.sh
#!/bin/bash
#重新定义环境变量 
export  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/.local/bin:/root/bin
#1.创建备份目录
mkdir -p  /backup
#2.开始备份
cd  /
tar czf  /backup/etc_$(date +%F_%M).tar.gz  etc/  
#3.删除3天以前的数据
find  /backup  -type f -mtime  +3   -name "*.tar.gz"  -delete


[root@BC ~]# sh  backup.sh
[root@BC ~]# ll /backup/
total 9972
-rw-r--r-- 1 root root 10210944 Jul 31 11:05 etc_2020-07-31_05.tar.gz

#批量执行 

[root@BC ~]# for i in {20..31};do date -s 2020/07/$i &&  sh /root/backup.sh ;done


#编写定时任务 
[root@BC ~]# crontab  -l
#同步系统时间 BC 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
* * * * *  /usr/bin/date +\%F_\%T >> /root/time.txt  
#备份
* * * * *  /bin/bash   /root/backup.sh  &>/dev/null


[root@BC ~]# tailf  /var/log/cron
Jul 31 11:10:01 BC crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 31 11:10:01 BC CROND[14327]: (root) CMD (/usr/bin/date +%F_%T >> /root/time.txt  )
Jul 31 11:10:01 BC CROND[14328]: (root) CMD (/bin/bash   /root/backup.sh  &>/dev/null)
Jul 31 11:10:01 BC CROND[14329]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null)


[root@BC ~]# ll  /backup/
total 59832
-rw-r--r-- 1 root root 10210944 Jul 28 00:00 etc_2020-07-28_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 29 00:00 etc_2020-07-29_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 30 00:00 etc_2020-07-30_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 31 00:00 etc_2020-07-31_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 31 11:05 etc_2020-07-31_05.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 31 11:10 etc_2020-07-31_10.tar.gz

4、定时发邮件

[root@BC ~]# yum install  -y  mailx

[root@BC ~]# vim /etc/mail.rc

#发件人

set from=xxx@qq.com
#邮件服务器

set smtp=smtp.qq.com
#发件人用户名

set smtp-auth-user=xxx@qq.com
#发件人密码(QQ邮箱不可以使用密码,只能使用授权码)

set smtp-auth-password=xxx
#登录方式

set smtp-auth=login
#邮件服务器协议及端口

set smtp=smtps://smtp.qq.com:465
#忽略证书

set ssl-verify=ignore
#指定证书位置

set nss-config-dir=/etc/pki/nssdb/

#或者指定别的证书位置,创建证书目录



#放到最后  
set from=xxx@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=xxx@qq.com
set smtp-auth-password=xxx		#客户端的授权码  
set smtp-auth=login
set smtp=smtps://smtp.qq.com:465
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/


[root@BC ~]# echo  "test"  | mail  -s "hello"  xxxx@qq.com
[root@BC ~]# Error in certificate: Peer‘s certificate issuer has been marked as not trusted by the.		#忽略这个错误 


[root@BC ~]# mkdir qingshu  
[root@BC ~]# cd qingshu
[root@BC qingshu]# vim  qingshu1.txt
[root@BC qingshu]# mail  -s "致亲爱的小姐姐"   xxx@qq.com  <  qingshu1.txt 
[root@BC qingshu]# Error in certificate: Peer‘s certificate issuer has been marked as not trusted by the.


#编写自动化脚本 

[root@BC qingshu]# cat /root/send_mail.sh
#!/bin/bash
Qingshu=$(ls -1  /root/qingshu/ |head -1)
mail  -s "致亲爱的小姐姐"  xxxx@qq.com  < /root/qingshu/$Qingshu  
mail  -s "致亲爱的小姐姐"  xxxx@qq.com  < /root/qingshu/$Qingshu

if  [ $? -eq 0 ];then
	rm -f /root/qingshu/$Qingshu
fi


[root@BC qingshu]# ll
total 20
-rw-r--r-- 1 root root  85 Jul 31 11:49 qingshu1.txt
-rw-r--r-- 1 root root 184 Jul 31 11:51 qingshu2.txt
-rw-r--r-- 1 root root 121 Jul 31 11:51 qingshu3.txt
-rw-r--r-- 1 root root 155 Jul 31 11:51 qingshu4.txt
-rw-r--r-- 1 root root  70 Jul 31 11:52 qingshu5.txt


#编写定时任务 

[root@BC qingshu]# crontab  -l
#同步系统时间 BC 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
#* * * * *  /usr/bin/date +\%F_\%T >> /root/time.txt  
#备份
#* * * * *  /bin/bash   /root/backup.sh  &>/dev/null
#xxxxxxxxx
* * * * *  /bin/bash   /root/send_mail.sh  &>/dev/null

磁盘

1、磁盘基础介绍

1.什么是磁盘?

磁盘是指利用磁记录技术存储数据的存储器,是所有硬式存储的统称,如最早出现的软盘,现在的硬盘,都是磁盘中的一部分。

2.什么是软盘?

磁盘开始指的是1.44MB的3.5英寸磁盘,这是很早时候的电脑储存盘,也叫软盘。

软盘在如今已经很少被使用了,使用软盘需要安装软盘驱动。软盘容量小,容易损坏。随着数据的大量增长,软盘已经不满足日益所需的数据存储需求,所以,硬盘便被研发了出来。

3.什么是硬盘?

硬盘就是一种最为常见的外存储器,它好比是数据的外部仓库一样。电脑除了要有"工作间",还要有专门存储东西的仓库。是计算机中的最大的存储装置了,我们会将磁盘的储存片装到硬质金属盒子里,这样就可以得到更好的保护,而且在使用寿命上也比软盘优越得多,只有低格的时候才会对硬盘有很大的伤害。

4.磁盘与软盘硬盘的关系

磁盘是一种统称,即是一开始的软盘,又是现如今的硬盘,但是在现在,说起磁盘,都指的是硬盘。

2、磁盘的根本结构

1.盘片

	盘片是硬盘中承载数据存储的介质。硬盘盘片是以坚固耐用的材料为盘基,将磁粉附着在铝合金(新材料也有用玻璃)圆盘片的表面上,表面被加工的相当平滑。这些磁粉被划分成为磁道的若干个同心圆,在每个同心圆的磁道上就好像有无数的任意排列的小磁铁,它们分别代表着0和1的状态。当这些小磁铁受到来自磁头的磁力影响时,其排列的方向会随之改变。

    利用磁头的磁力控制指定的一些小磁铁方向,使每个小磁铁都可以用来储存信息。硬盘是由多个盘片叠加在一起,互相之间由垫圈隔开。

    硬盘中一般会有多个盘片,每个盘片包含两个面,每个盘面都对应地有一个读/写磁头(Head,简写为H)。受到硬盘整体体积和生产成本的限制,盘片数量都受到限制,一般都在5片以内。盘片的编号自下向上从0开始,如最下边的盘片有0面和1面,再上一个盘片就编号为2面和3面。

2.磁道 

	当磁盘旋转时,磁头若保持在一个位置上,则每个磁头都会在磁盘表面划出一个圆形轨迹,这些圆形轨迹就叫做磁道。

    每个盘片的盘面在出厂的时候被划分出了多个同心圆环,数据就存储在这样的同心圆环上面,每个盘面可以划分多个磁道,但是肉眼不可见。通常一块盘面有成千上万个磁道。

    磁盘上的磁道是一组记录密度不同的同心圆。磁表面存储器是在不同形状(如盘状、带状等)的载体上。

3.扇区  

	在硬盘出厂时会对磁盘进行一次低级格式化,其实就是再将每个磁道划分为若干个弧段,每个弧段就是一个扇区(Sector)。若干个扇区就组成整个盘片,硬盘的读写以扇区为基本单位。这种以簇为最小分配单位的机制,使硬盘对数据的管理变得相对容易。现在每个扇区可存储512字节数据,已经成了业界的约定。

    低级格式化就是将空白的磁盘划分出柱面和磁道,再将磁道划分为若干个扇区,每个扇区又划分出标识部分ID、间隔区GAP和数据区DATA等。

    硬盘低级格式化的功用:硬盘低级格式化是对硬盘最彻底的初始化方式,经过低格后的硬盘,原来保存的数据将会全部丢失,所以一般来说低格硬盘是非常不可取的,只有非常必要的时候才能低格硬盘。而这个所谓的必要时候有两种,一是硬盘出厂前,硬盘厂会对硬盘进行一次低级格式化;另一个是当硬盘出现某种类型的坏道时,使用低级格式化能起到一定的缓解或者屏蔽作用。

4.柱面  

	柱面实际上就是我们抽象出来的一个逻辑概念,简单来说就是处于同一个垂直区域的磁道称为柱面,即各盘面上面相同位置的集合,这样数据如果存储到相同半径磁道上的同一扇区,这样可以实现并行读取,主要是减少磁头寻道时间。

5.磁头 

	磁头是硬盘中最昂贵的部件,也是硬盘技术中最重要和最关键的一环。主要作用是读取磁盘磁道上面的金属块,负责读或写入数据。

3、磁盘相关概念

1.磁盘的接口

IDE  SCSI 		#淘汰了


SATA III    SAS   #企业级 


SSD( SATA III    PCIE )


M2    超极本的接口标准     MSATA   

2.机械硬盘和固态硬盘的区别

	机械硬盘  HDD					固态硬盘   SSD

    容量大价格低						容量小价格贵  
    
    抗击打能力弱 						抗击打能力强  
    
    读写速度慢						 读写速度快  
    
    数据损坏恢复易						数据恢复难
    
    寿命一直使用						 使用期限有一定限制   

3.磁盘的命名方式:

/dev/sda1		#第一块物理硬盘的第一个分区

/dev/sdb3		#第二块物理硬盘的第三个分区 

/dev/vdc5		#第三块虚拟硬盘的第5个分区 	


sd		#物理硬盘
 
 
vd		#虚拟硬盘 


4.磁盘的相关单位和术语

尺寸:		2.5英寸    3.5英寸  

数据大小:	bit   B   KB   MB   GB   TB    PB    EB  

单位换算:   1024     1B=8b  

转速:	RPM    转/每分钟    5400    7200     10k     15k  

IOPS    衡量磁盘读写 IO的单位   每秒的输入输出 


5.两个分区表:

MBR  	适用于2TB      只能有4个主分区   扩展分区(逻辑分区 )  


		3个主分区 + 1个扩展分区( 逻辑分区...... )

MBR的位置:  0磁头0磁道1扇区      512字节  

		446字节  MBR引导记录    

		64字节4个主分区  

		2字节分区结束标识符  

GPT		适用于2TB   128个主分区 

6.分区工具:

fdisk		#适用于2TB的分区

gdisk		#适用于2TB以上

4、Fdisk分区

[root@BC ~]# ll  /dev/sd*
brw-rw---- 1 root disk 8, 0 Aug  3 09:34 /dev/sda
brw-rw---- 1 root disk 8, 1 Aug  3 09:34 /dev/sda1
brw-rw---- 1 root disk 8, 2 Aug  3 09:34 /dev/sda2
brw-rw---- 1 root disk 8, 3 Aug  3 09:34 /dev/sda3
[root@BC ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 97.5G  0 part /
sr0     11:0    1  4.3G  0 rom  /mnt


[root@BC ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 97.5G  0 part /
sdb      8:16   0  100G  0 disk 
sdc      8:32   0    3T  0 disk 
sr0     11:0    1  4.3G  0 rom 




[root@BC ~]# ll /dev/sd*
brw-rw---- 1 root disk 8,  0 Aug  3 17:38 /dev/sda
brw-rw---- 1 root disk 8,  1 Aug  3 17:38 /dev/sda1
brw-rw---- 1 root disk 8,  2 Aug  3 17:38 /dev/sda2
brw-rw---- 1 root disk 8,  3 Aug  3 17:38 /dev/sda3
brw-rw---- 1 root disk 8, 16 Aug  3 17:38 /dev/sdb
brw-rw---- 1 root disk 8, 32 Aug  3 17:38 /dev/sdc



#显示某块磁盘的分区信息  

[root@BC ~]# fdisk   -l  /dev/sdb

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@BC ~]# fdisk   -l  /dev/sda

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ae9a7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048     5220351     2097152   82  Linux swap / Solaris
/dev/sda3         5220352   209715199   102247424   83  Linux
[root@BC ~]# 



#1. 进行分区  创建分区  

[root@BC ~]# fdisk   /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x5b8d3e75.

Command (m for help): m

Command action 
a toggle a bootable flag    					 #切换分区启动标记
b edit bsd disklabel    						 #编辑sdb磁盘标签
c toggle the dos compatibility flag     		  #切换dos兼容模式
d delete a partition             				  #删除分区
l list known partition types     				  #显示分区类型
m print this menu    							 #显示帮助菜单
n add a new partition    						 #新建分区
o create a new empty DOS partition table 		   #创建新的空白分区表
p print the partition table     				  #显示分区表的信息
q quit without saving changes     				  #不保存退出
s create a new empty Sun disklabel    			   #创建新的Sun磁盘标签
t change a partitions system id         		   #修改分区ID,可以通过l查看id 
u change display/entry units     				  #修改容量单位,磁柱或扇区
v verify the partition table     				  #检验分区表
w write table to disk and exit     				  #保存退出
x extra functionality (experts only)     		   #拓展功能

Command (m for help): 

Command (m for help): l

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext‘d (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      



Command (m for help): n    		#创建分区    		#主分区的编号是1-4   逻辑分区从5开始 
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended

Select (default p): p			#创建主分区
Partition number (1-4, default 1): 1		#主分区的编号
First sector (2048-209715199, default 2048): 			#扇区的起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +10G		#扇区的结束位置   
Partition 1 of type Linux and of size 10 GiB is set


Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5b8d3e75

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux



Command (m for help): n			#再次创建一个分区 
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (20973568-209715199, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-209715199, default 209715199): +10G
Partition 2 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5b8d3e75

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    41945087    10485760   83  Linux


Command (m for help): d		#删除分区  
Partition number (1,2, default 2): 2		#指定分区编号  
Partition 2 is deleted

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5b8d3e75

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux


#创建扩展分区

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (2-4, default 2): 
First sector (20973568-209715199, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-209715199, default 209715199): +50G
Partition 2 of type Extended and of size 50 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5b8d3e75

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568   125831167    52428800    5  Extended



#创建一个逻辑分区

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (20975616-125831167, default 20975616): 
Using default value 20975616
Last sector, +sectors or +size{K,M,G} (20975616-125831167, default 125831167): +20G
Partition 5 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5b8d3e75

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568   125831167    52428800    5  Extended
/dev/sdb5        20975616    62918655    20971520   83  Linux



Command (m for help): w		#保存退出  
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


#检查

[root@BC ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 97.5G  0 part /
sdb      8:16   0  100G  0 disk 
├─sdb1   8:17   0   10G  0 part 
├─sdb2   8:18   0    1K  0 part 
└─sdb5   8:21   0   20G  0 part 
sdc      8:32   0    3T  0 disk 
sr0     11:0    1  4.3G  0 rom  


[root@BC ~]# ll /dev/sd*
brw-rw---- 1 root disk 8,  0 Aug  3  2020 /dev/sda
brw-rw---- 1 root disk 8,  1 Aug  3  2020 /dev/sda1
brw-rw---- 1 root disk 8,  2 Aug  3  2020 /dev/sda2
brw-rw---- 1 root disk 8,  3 Aug  3  2020 /dev/sda3
brw-rw---- 1 root disk 8, 16 Aug  3 09:53 /dev/sdb
brw-rw---- 1 root disk 8, 17 Aug  3 09:53 /dev/sdb1
brw-rw---- 1 root disk 8, 18 Aug  3 09:53 /dev/sdb2
brw-rw---- 1 root disk 8, 21 Aug  3 09:53 /dev/sdb5
brw-rw---- 1 root disk 8, 32 Aug  3  2020 /dev/sdc


#2. 创建文件系统    格式化


[root@BC ~]# mkfs.xfs   /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0


#3. 创建挂载点   

[root@BC ~]# mkdir   /fdisk01


#4.  挂载使用 

[root@BC ~]# mount  /dev/sdb1   /fdisk01
[root@BC ~]# df -h |  grep  ‘/fdisk01‘
/dev/sdb1        10G   33M   10G   1% /fdisk01

#测试是否可用
[root@BC ~]# dd   if=/dev/zero  of=/fdisk01/test.log  bs=10M  count=100
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB) copied, 10.8088 s, 97.0 MB/s
[root@BC ~]# df -h |  grep  ‘/fdisk01‘
/dev/sdb1        10G  1.1G  9.0G  11% /fdisk01




#5. 实现永久挂载 

[root@BC ~]# tail -1 /etc/fstab
/dev/sdb1				  /fdisk01                xfs     defaults        0 0
[root@BC ~]# 

[root@BC ~]# mount  -a		#重新挂载/etc/fstab文件中的挂载列表 
[root@BC ~]# 



#数据是跟着设备走的    目录只是一个挂载点   就是入口 

[root@BC ~]# ll /fdisk01/
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug  3 09:59 test.log
[root@BC ~]# umount   /dev/sdb1
[root@BC ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  5.5G   92G   6% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.5M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs           199M     0  199M   0% /run/user/0
[root@BC ~]# ll /fdisk01/
total 0
[root@BC ~]# mount  -a
[root@BC ~]# ll /fdisk01/
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug  3 09:59 test.log

5、Gdisk分区

[root@BC ~]# yum  install  -y  gdisk


[root@BC ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 97.5G  0 part /
sdb      8:16   0  100G  0 disk 
├─sdb1   8:17   0   10G  0 part /fdisk01
├─sdb2   8:18   0    1K  0 part 
└─sdb5   8:21   0   20G  0 part 
sdc      8:32   0    3T  0 disk 
sr0     11:0    1  4.3G  0 rom  


[root@BC ~]# gdisk  -l  /dev/sdc
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.
Disk /dev/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2BF39D49-315B-42C3-9C9C-C5A98D1EBB71
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 6442450877 sectors (3.0 TiB)

Number  Start (sector)    End (sector)  Size       Code  Name


#2. 创建分区 

[root@BC ~]# gdisk    /dev/sdc
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): m 
b    back up GPT data to a file                            #将GPT数据备份到文件中
c    change a partition‘s name                             #更改分区的名称
d    delete a partition                                    #删除分区
i    show detailed information on a partition              #显示分区的详细信息
l    list known partition types                            #列出已知的分区类型
n    add a new partition                                   #添加一个新的分区
o    create a new empty GUID partition table (GPT)         #创建一个新的空GUID分区表(GPT) 
p    print the partition table                             #打印分区表
q    quit without saving changes                           #没有保存更改就退出
r    recovery and transformation options (experts only)    #恢复和转换选项(仅限专家使用) 
s    sort partitions                                       #年代分类分区
t    change a partition‘s type code                        #不要更改分区的类型代码
v    verify disk                                           #验证磁盘
w    write table to disk and exit                          #将表写入磁盘并退出
x    extra functionality (experts only)                    #额外功能(仅限专家使用) 
?    print this menu                                       #打印菜单

Command (? for help): 



Command (? for help): n		#创建分区
Partition number (1-128, default 1): 	#分区编号
First sector (34-6442450910, default = 2048) or {+-}size{KMGTP}: 		#扇区起始位置
Last sector (2048-6442450910, default = 6442450910) or {+-}size{KMGTP}: 	#扇区结束位置
Current type is ‘Linux filesystem‘
Hex code or GUID (L to show codes, Enter = 8300): 		#选择分区类型 
Changed type of partition to ‘Linux filesystem‘

Command (? for help): p		#打印分区表信息
Disk /dev/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2E58E6D9-4971-4B2F-A455-16D3A5115002
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048      6442450910   3.0 TiB     8300  Linux filesystem

Command (? for help): 


Command (? for help): w		#保存退出

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y		#是否确认 
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
[root@BC ~]# 


#3. 格式化,创建文件系统

[root@BC ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 97.5G  0 part /
sdb      8:16   0  100G  0 disk 
├─sdb1   8:17   0   10G  0 part /fdisk01
├─sdb2   8:18   0    1K  0 part 
└─sdb5   8:21   0   20G  0 part 
sdc      8:32   0    3T  0 disk 
└─sdc1   8:33   0    3T  0 part 
sr0     11:0    1  4.3G  0 rom  


[root@BC ~]# mkfs.xfs   /dev/sdc1
meta-data=/dev/sdc1              isize=512    agcount=4, agsize=201326527 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=805306107, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=393215, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0



#4. 创建挂载点,进行挂载 
[root@BC ~]# mkdir   /gdisk01
[root@BC ~]# mount  /dev/sdc1  /gdisk01
[root@BC ~]# df -h  | grep  ‘/gdisk01‘
/dev/sdc1       3.0T   33M  3.0T   1% /gdisk01
[root@BC ~]# cp  /fdisk01/test.log   /gdisk01/
[root@BC ~]# df -h  | grep  ‘/gdisk01‘
/dev/sdc1       3.0T  1.1G  3.0T   1% /gdisk01
[root@BC ~]# 


#5. 永久挂载 
[root@BC ~]# tail  -1 /etc/fstab
/dev/sdc1			          /gdisk01                xfs     defaults        0 0
[root@BC ~]# mount -a
[root@BC ~]# 

6、磁盘挂载方式

在Linux系统中,所有的设备必须挂载才能使用 

mount		#挂载的命令

选项:

	-t		#指定文件系统     网络协议   nfs
	
	-o		#指定挂载的参数 	
	
	-a		#重新挂载/etc/fstab文件中的挂载列表 
	
[root@BC ~]# mount  -t  xfs  /dev/sdb1   /fdisk01/
[root@BC ~]# df  -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  5.5G   92G   6% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.5M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sdb1        10G  1.1G  9.0G  11% /fdisk01
[root@BC ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 97.5G  0 part /
sdb      8:16   0  100G  0 disk 
├─sdb1   8:17   0   10G  0 part /fdisk01
├─sdb2   8:18   0    1K  0 part 
└─sdb5   8:21   0   20G  0 part 
sdc      8:32   0    3T  0 disk 
└─sdc1   8:33   0    3T  0 part 
sr0     11:0    1  4.3G  0 rom  
[root@BC ~]# blkid
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="70df4a5b-091c-4aaa-9cc7-d745201ec394" TYPE="xfs" 
/dev/sda2: UUID="6aefd444-30dc-4c0e-9a8d-75a8b1d9856b" TYPE="swap" 
/dev/sda3: UUID="4aa02e85-ffe1-4783-9886-a328f923bee8" TYPE="xfs" 
/dev/sdb1: UUID="0668e9f1-7d37-42eb-9e79-980f6352c342" TYPE="xfs" 
/dev/sdc1: UUID="60d9a87f-fa86-4fdc-a1c9-2a88df756a24" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="71869797-d467-4293-807e-7c6f917c7422" 
[root@BC ~]# 


[root@BC ~]# mount  -o ro  /dev/sdb1  /fdisk01/
[root@BC ~]# cp /etc/hosts  /fdisk01/
cp: cannot create regular file ‘/fdisk01/hosts’: Read-only file system


umount		#卸载  

选项:

	-l		#强制卸载  
	
[root@BC ~]# umount   /dev/sdb1
[root@BC ~]# df -h | grep  ‘/dev/sdb1‘
[root@BC ~]# mount  /dev/sdb1  /fdisk01/
[root@BC ~]# cd /fdisk01/
[root@BC fdisk01]# umount /fdisk01		#不能在挂载的目录中进行卸载 
umount: /fdisk01: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@BC fdisk01]# umount /dev/sdb1
umount: /fdisk01: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@BC fdisk01]# umount  -l  /dev/sdb1
[root@BC fdisk01]# cd
[root@BC ~]# mount  /dev/sdb1  /fdisk01/
[root@BC ~]# umount   /fdisk01/
[root@BC ~]# df -h | grep  ‘/dev/sdb1‘
[root@BC ~]# 



#通过UUID进行挂载

[root@BC ~]# blkid
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="70df4a5b-091c-4aaa-9cc7-d745201ec394" TYPE="xfs" 
/dev/sda2: UUID="6aefd444-30dc-4c0e-9a8d-75a8b1d9856b" TYPE="swap" 
/dev/sda3: UUID="4aa02e85-ffe1-4783-9886-a328f923bee8" TYPE="xfs" 
/dev/sdb1: UUID="0668e9f1-7d37-42eb-9e79-980f6352c342" TYPE="xfs" 
/dev/sdc1: UUID="60d9a87f-fa86-4fdc-a1c9-2a88df756a24" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="71869797-d467-4293-807e-7c6f917c7422" 
[root@BC ~]# mount  UUID="0668e9f1-7d37-42eb-9e79-980f6352c342"   /fdisk01/
[root@BC ~]# df -h  | grep  ‘fdisk‘
/dev/sdb1        10G  1.1G  9.0G  11% /fdisk01
[root@BC ~]# 


[root@BC ~]# tail -1 /etc/fstab
UUID=0668e9f1-7d37-42eb-9e79-980f6352c342 /fdisk01/		  xfs     defaults        0 0
[root@BC ~]# mount -a


[root@BC ~]# tail -4 /etc/fstab
UUID=4aa02e85-ffe1-4783-9886-a328f923bee8 /                       xfs     defaults        0 0
UUID=70df4a5b-091c-4aaa-9cc7-d745201ec394 /boot                   xfs     defaults        0 0
UUID=6aefd444-30dc-4c0e-9a8d-75a8b1d9856b swap                    swap    defaults        0 0
UUID=0668e9f1-7d37-42eb-9e79-980f6352c342 /fdisk01/		  xfs     defaults        0 0


#以空白字符为分隔符,分为6列:
[root@BC ~]# cat /etc/fstab 
#设备名称    挂载的位置    文件系统类型    挂载的参数    是否备份    是否检查

/dev/sdb1    /data        xfs            defaults     0            0 


第一列:		挂载的设备  挂载的地址  网络设备地址  

第二列:		挂载点  挂载的目录  空目录 

第三列:		文件系统的类型   xfs 7默认的  ext4 6默认的  nfs  gfs

第四列:		挂载的参数  defaults

第五列:	是否允许dump进行备份  

		0	#不备份
		
		1	#每天备份
		
		2	#不定期备份 
		
第六列:	是否通过fsck命令检查磁盘 

		0	#不检查 
		
		1	#检查  检查顺序  先检查 /  
		
		2	#检查顺序 
		

#建议挂载时使用UUID

#1.怎样获取磁盘分区的UUID,使用blkid命令获取
[root@BC ~]# blkid|grep sdc1
/dev/sdc1: UUID="c7c940b3-277b-49fa-960b-eef5ed6e9b1b"
    
#2.手动测试是否能正常的挂载
[root@BC ~]# mount UUID="c7c940b3-277b-49fa-960b-eef5ed6e9b1b" /datac


#3.将信息写入到/etc/fstab文件中,保证下次启动系统自动挂载
UUID="c7c940b3-277b-49fa-960b-eef5ed6e9b1b" /datac xfs defaults 0 0


#4.卸载/datac挂载信息, 然后使用mount -a检查/etc/fstab文件是否ok
[root@BC ~]# mount -a


PS: 如果不检查写错了直接重启, 会导致系统无法启动. 			

7、Swap虚拟内存

物理内存不足,启用Swap。
Out of memory

OOM		#内存溢出   内存不足 


[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980         133        1625           9         221        1672
Swap:          2047           0        2047
[root@BC ~]# swapoff   -a
[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980         132        1627           9         220        1673
Swap:             0           0           0



虚拟内存 


#添加一个swap分区 


#创建一个1G的分区 

[root@BC ~]# fdisk   /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): p
Partition number (3,4, default 3): 
First sector (125831168-209715199, default 125831168): 
Using default value 125831168
Last sector, +sectors or +size{K,M,G} (125831168-209715199, default 209715199): +1G
Partition 3 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5b8d3e75

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568   125831167    52428800    5  Extended
/dev/sdb3       125831168   127928319     1048576   83  Linux
/dev/sdb5        20975616    62918655    20971520   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.


[root@BC ~]# partprobe		#通知内核,系统分区表发生变化 


#将这个分区变为swap分区

[root@BC ~]# mkswap   /dev/sdb3
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=8a9c926b-de0d-4b49-bcb8-39a9c0b834a8


#把这个分区加入到swap

[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980          77        1819           0          84        1774
Swap:          2047          69        1978
[root@BC ~]# swapon   -s
Filename				Type		Size	Used	Priority
/dev/sda2                              	partition	2097148	71168	-2
[root@BC ~]# swapon   /dev/sdb3
[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980          78        1818           0          84        1773
Swap:          3071          69        3002
[root@BC ~]# swapon   -s
Filename				Type		Size	Used	Priority
/dev/sda2                              	partition	2097148	70912	-2
/dev/sdb3                              	partition	1048572	0	-3


#移除swap

[root@BC ~]# swapoff   /dev/sdb3
[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980          78        1818           0          84        1773
Swap:          2047          69        1978

#禁用swap
[root@BC ~]# swapoff  -a
[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980         128        1759           9          92        1715
Swap:             0           0           0

#启用swap

[root@BC ~]# swapon  -a
[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980         129        1758           9          92        1713
Swap:          2047           0        2047



#生成一个大文件  作为swap


[root@BC ~]# dd  if=/dev/zero  of=/root/swap.txt  bs=100M  count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 8.33166 s, 126 MB/s
[root@BC ~]# ll
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug  3 11:21 swap.txt
[root@BC ~]# mkswap   swap.txt 
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=51e07b8a-971a-47ec-972e-36f8273ec3cd
[root@BC ~]# ll
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug  3 11:22 swap.txt
[root@BC ~]# file   swap.txt 
swap.txt: Linux/i386 swap file (new style), version 1 (4K pages), size 255999 pages, no label, UUID=51e07b8a-971a-47ec-972e-36f8273ec3cd


[root@BC ~]# chmod  600  swap.txt 


[root@BC ~]# swapon  /root/swap.txt 

[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980         132         727           9        1120        1663
Swap:          3047           0        3047


[root@BC ~]# swapoff   swap.txt 
[root@BC ~]# free  -m
              total        used        free      shared  buff/cache   available
Mem:           1980         130         729           9        1120        1665
Swap:          2047           0        2047

8、Raid 介绍


磁盘阵列技术 

做磁盘阵列的好处:

1. 获得更高的容量 

2. 获得更高的性能  

3. 让数据更安全  冗余  


如何做RAID:


软件实现   软RAID   不推荐    mdadm  


硬件RAID    RAID卡实现的      推荐    


做RAID什么时候做:

在装系统之前做


RAID的级别:

级别		别名      硬盘数量 		容量			性能				容错性

RAID0	条带卷		至少两块  		N*单块  		读写最快			 0

RAID1   镜像卷		只能两块       损失一半       写慢 读一般           50%

RAID5	校验卷 	至少3块		浪费一块       读写一般             N分之一 只能坏一块

RAID10	镜像阵列条带卷  至少4块    浪费一半        读写很快             50%  


9、LVM概述

1. 什么是LVM?

    LVM是Logical Volume Manager (逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。又译为逻辑卷宗管理器、逻辑扇区管理器、逻辑磁盘管理器,是Linux核心所提供的逻辑卷管理(Logical volume management)功能。它在硬盘的分区之上,又创建一个逻辑层,以方便系统管理硬盘分区系统。
    最先由IBM开发,在AIX系统上实现,OS/2 操作系统与 HP-UX也支持这个功能。在1998年,Heinz Mauelshagen 根据在 HP-UX 上的逻辑卷管理器,写作出第一个 Linux 版本的逻辑卷管理器。

2. LVM解决了什么问题

    当我们刚开始安装Linux操作系统是遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。
    普通的磁盘分区好之后就无法改变其大小,而遇到出现某个分区空间耗尽时,解决的方法可以是符号链接,或者调整分区大小的工具,但这只是暂时解决方法,没有从根本上解决问题。
    随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户可以在无需停机的请款下可以方便的调整各个分区大小。

3. LVM基本概念

物理卷(PV):(physical volume),把常规的磁盘设备通过pvcreate命令对其进行初始化,形成了物理卷。其实就是硬盘或分区。(面粉)

卷组(VG):(volume group),把多个物理卷组成一个逻辑的整体,这样卷组的大小就是多个硬盘之和。或者理解就是由一个或多个PV组成的整体。(面团)

逻辑卷(LV):(logical volume),从卷组中划分需要的空间大小出来。用户仅需对其格式化然后即可挂载使用。从VG中切割出的空间用于创建文件系统。(切成馒头)

基本单元(PE):(physical extend),分配的逻辑大小的最小单元,默认为4MB的基本块。(假设分配100MB逻辑空间,则需要创建25个PE)

4. 逻辑卷实践

1)创建物理卷,将普通磁盘转换为物理卷。
2)创建卷组,将物理卷加入到卷组中。
3)在卷组中划分逻辑卷,然后挂载使用。

1. 准备物理磁盘,如果是虚拟机需要关闭添加磁盘,以便更好的实验

[root@BC ~]# lsblk 
sdb 8:16 0 20G 0 disk 
sdc 8:32 0 20G 0 disk

2. 将磁盘转换为物理卷


#下载安装软件包
[root@BC ~]# yum install -y lvm2


[root@BC ~]# pvcreate /dev/sdb

3. 将/dev/sdb物理卷加入卷组(资源池)-->名字lvm_test


[root@BC ~]# vgcreate lvm_test /dev/sdb


#检查卷组
[root@BC ~]# vgs

4. 创建逻辑卷,分配名称,以及大小,指定卷组


选项:
    -L    #逻辑卷大小
    -n    #逻辑卷名字
    
[root@BC ~]# lvcreate -L 100M -n lv1 lvm_test    ##将lvm_test分配給lv1

5. 格式化逻辑卷


[root@BC ~]# mkfs.xfs /dev/lvm_test/lv1

6. 挂载使用


[root@BC ~]# mkdir /lv1


[root@BC ~]# mount /dev/lvm_test/lv1 /lv1/


[root@BC ~]# cp /etc/services /lv1/

02. 卷组管理,如何扩展卷组大小,如何删除卷组

1. 如何扩大VG卷组


#1.新硬盘加入pv
[root@BC ~]# pvcreate /dev/sdc


#2.先检查当前的vg卷组大小
[root@oldboyedu ~]# vgs
VG #PV #LV #SN Attr VSize VFree
lvm_test 1 1 0 wz--n- 1020.00m 920.00m


#3.使用vgextend扩展卷组
[root@BC ~]# vgextend lvm_test /dev/sdc
Volume group "lvm_test" successfully extended


#4.再次检查,发现卷组已经扩大
[root@oldboyedu ~]# vgs
VG #PV #LV #SN Attr VSize VFree
lvm_test 2 1 0 wz--n- 1.99g 1.89g



2. 假设想移除/dev/sdb磁盘,建议先将sdb磁盘数据先迁移到sdc磁盘,然后在移除。(同一卷组的磁盘才可以进行在线迁移),(注意卷组的名称---)


#1.检查当前逻辑卷VG中PV使用情况
[root@BC ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb vg1 lvm2 a -- 2.00g 1.76g 
/dev/sdc vg1 lvm2 a -- 2.00g 2.00g


#2.pvmove在线数据迁移,将sdb的数据迁移至sdc 
[root@BC ~]# pvmove /dev/sdb
/dev/sdb: Moved: 100.00%


#3.检查是否将sdb数据迁移至sdc 
[root@BC ~]# pvs
PV VG Fmt Attr PSize PFree 
/dev/sdb vg1 lvm2 a -- 2.00g 2.00g 
/dev/sdc vg1 lvm2 a -- 2.00g 1.76g


#4.从卷组中移除sdb磁盘
[root@BC ~]# vgreduce lvm_test /dev/sdb
Removed "/dev/sdb" from volume group "lvm_test"


3. 逻辑卷管理,动态的调整逻辑卷大小

01. 逻辑卷的扩展,是取决于vg卷中是否还有剩余的容量,注意扩展逻辑卷不能超过卷组VG的总大小


[root@BC ~]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 1 1 0 wz--n- 1020.00m 920.00m


#1.扩展lv逻辑卷,增加800M分配给逻辑卷 {注意: 800M +800M 不一样}
[root@BC ~]# lvextend -L +800M /dev/lvm_test/lv1


#或者可以选择分配磁盘池中多少百分比给逻辑卷
[root@BC ~]# lvextend -l +50%FREE /dev/lvm_test/lv1


#2.扩展逻辑卷后需要更新fs文件系统
[root@BC ~]# xfs_growfs /dev/lvm_testvg/lv1 #xfs文件格式扩容

02. 缩小逻辑卷,不要去操作,因为线上在使用的磁盘真的敢操作吗?

03. 删除逻辑卷,如果不想使用逻辑卷可以选择删除。


[root@BC ~]# umount /dev/lvm_test/lv1


[root@BC ~]# lvremove /dev/lvm_test/lv1
Do you really want to remove active logical volume lvm_test/lv1? 
Logical volume "lv1" successfully removed

注意:LVM如果有一个磁盘损坏,整个lvm都坏了, lvm只有动态扩展作用,底层用RAID + 上层LVM = 既有冗余又有动态扩展

10、硬盘故障

01. 磁盘block满故障

[root@BC ~]# fdisk   /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): 
Using default response p
Selected partition 4
First sector (127928320-209715199, default 127928320): 
Using default value 127928320
Last sector, +sectors or +size{K,M,G} (127928320-209715199, default 209715199): +2G
Partition 4 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5b8d3e75

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568   125831167    52428800    5  Extended
/dev/sdb3       125831168   127928319     1048576   83  Linux
/dev/sdb4       127928320   132122623     2097152   83  Linux
/dev/sdb5        20975616    62918655    20971520   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@BC ~]# 


#准备2G的分区 


[root@BC ~]# dd  if=/dev/zero  of=/opt/test.log  bs=100M  count=21
dd: error writing ‘/opt/test.log’: No space left on device
21+0 records in
20+0 records out
2099183616 bytes (2.1 GB) copied, 3.34918 s, 627 MB/s


[root@BC ~]# echo  "123" >> /opt/file.txt
-bash: echo: write error: No space left on device		#磁盘空间不足 


#解决:

检查磁盘空间

[root@BC ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  6.4G   92G   7% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.6M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sdb1        10G  1.1G  9.0G  11% /fdisk01
/dev/sdb4       2.0G  2.0G   20K 100% /opt


#找到这个大文件  确认好之后在删  

[root@BC ~]# du  -sh  /opt/*  | grep  ‘G‘
2.0G	/opt/test.log

一级一级往下找 
注意:如果该文件存在硬链接,一定要把硬链接文件删除。


02. 磁盘inode满故障

[root@BC ~]# echo  "123"  >>/opt/test.txt
-bash: /opt/test.txt: No space left on device
[root@BC ~]# 


[root@BC ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  6.4G   92G   7% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.6M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sdb1        10G  1.1G  9.0G  11% /fdisk01
/dev/sdb4       2.0G  582M  1.5G  29% /opt


[root@BC ~]# df -i
Filesystem       Inodes   IUsed    IFree IUse% Mounted on
/dev/sda3      51123712   46093 51077619    1% /
devtmpfs         250786     417   250369    1% /dev
tmpfs            253511       1   253510    1% /dev/shm
tmpfs            253511     774   252737    1% /run
tmpfs            253511      16   253495    1% /sys/fs/cgroup
/dev/sda1        256000     326   255674    1% /boot
tmpfs            253511       1   253510    1% /run/user/0
/dev/sdb1       5242880       4  5242876    1% /fdisk01
/dev/sdb4       1048576 1048576        0  100% /opt



[root@BC ~]# find  /opt/ -type  d   -size  +2M  | xargs  ls -lhd
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test01
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test02
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test03
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test04
drwxr-xr-x 2 root root 3.7M Aug  3 12:27 /opt/test/test05


[root@BC ~]# find  /opt/ -type  d   -size  +1M  | xargs  ls -lhd
drwxr-xr-x 2 root root 1.2M Aug  3 12:25 /opt/test/data01
drwxr-xr-x 2 root root 1.2M Aug  3 12:25 /opt/test/data02
drwxr-xr-x 2 root root 1.2M Aug  3 12:25 /opt/test/data03
drwxr-xr-x 2 root root 1.2M Aug  3 12:25 /opt/test/data04
drwxr-xr-x 2 root root 1.2M Aug  3 12:25 /opt/test/data05
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test01
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test02
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test03
drwxr-xr-x 2 root root 3.8M Aug  3 12:27 /opt/test/test04
drwxr-xr-x 2 root root 3.7M Aug  3 12:27 /opt/test/test05


[root@BC ~]# ll /opt/test/data01  | wc -l
50001


#找到之后,确认在删除  


[root@BC ~]# find  /opt/test/data01 -type  f  -delete


[root@BC ~]# rm -f  /opt/test/test01/*
-bash: /usr/bin/rm: Argument list too long


[root@BC ~]# ll  /opt/test/test01/ | wc -l
150001

标准1.0

原文:https://www.cnblogs.com/zxm1/p/14523461.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!