rpm yum crontab sed用法

1、简述rpm与yum命令的常见选项,并举例

rpm的安装

  • -i:安装(install)
  • -v:显示详细信息
  • -h:显示进度条,50个#,每个#为2%
  • -ivh:安装并显示详细信息和进度条
  • --test:只测试不安装
  • --nodeps:忽略依赖关系
  • --releasepkgs:重装(修改后的配置文件不会替换,除非删除才替换)
  • --noscripts:不执行安装脚本
    • rpm自带脚本分为
    • 1.安装前脚本:preinstall %pre --nopre 不执行
    • 2.安装后脚本:postinstall %post --nopost 不执行
    • 3.卸载前脚本:preuninstall %preun --nopreun 不执行
    • 4.卸载后脚本:postuninstall %postun --nopostun 不执行
  • --nosignature:不检查签名信息,不检查来源
  • --nodigest:不检查完整性

rpm的升级

  • -Uvh:升级或安装(没有安装旧版本就安装)(update)
  • -Fvh:升级(没有安装旧版本不安装)(freshen)
  • --oldpackage:降级安装
  • --force:强制升级(忽略包的依赖关系)
  • 注意
    • 1.不要对内核做升级操作,linux支持多内核共存,可以直接安装新内核
    • 2.原程序配置文件修改后,升级后不会覆盖,会把新版本文件命名为FILENAME.rpmnew后提供

rpm卸载

  • -e:卸载 (erase)
  • --allmatches:卸载所有的匹配指定名称的包
  • --nodeps:忽略依赖关系
  • --noscripts:不执行脚本
  • --test:测试

rpm查询

  • -q:查询是否安装及安装版本(query)
  • -qa:查询所有安装的包
  • -qf FILE_PATH:查询指定文件由哪个安装包生成
  • -qg:查询包组有哪些包
  • -qp FILE.rpm:查询未安装的rpm包中安装后能生成哪些文件
  • -q --whatprovides CAPABILITY:查询指定的CAPABILITY是哪些包提供
  • -q --whatrequires CAPABILITY:查询指定的CAPABILITY被哪些包依赖
  • -q --changelog:查询changelog
  • -ql:查询文件列表
  • -qi:查询info
  • -qc:查询配置文件
  • -qd:查询说明文档
  • -q --provides:查询有包内有哪些CAPABILITY
  • -qR:查询依赖关系
  • -q --scripts:查询脚本
[root@localhost ~]# rpm -qa|grep bash
bash-4.2.46-28.el7.x86_64
[root@localhost ~]# rpm -qf /bin/bash
bash-4.2.46-28.el7.x86_64
[root@localhost ~]# rpm -qg bash
group bash does not contain any packages
[root@localhost ~]# rpm -q --whatprovides /bin/bash
bash-4.2.46-28.el7.x86_64
[root@localhost ~]# rpm -q --whatrequires /bin/bash
nss-softokn-freebl-3.28.3-6.el7.x86_64
iproute-3.10.0-87.el7.x86_64
grubby-8.28-23.el7.x86_64
openssl-1.0.2k-8.el7.x86_64
rpm-4.11.3-25.el7.x86_64
openldap-2.4.44-5.el7.x86_64
policycoreutils-2.5-17.1.el7.x86_64
teamd-1.25-5.el7.x86_64
device-mapper-1.02.140-8.el7.x86_64
dracut-033-502.el7.x86_64
kmod-20-15.el7.x86_64
systemd-219-42.el7.x86_64
initscripts-9.49.39-1.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
dhclient-4.2.5-58.el7.centos.x86_64
dracut-network-033-502.el7.x86_64
ebtables-2.0.10-15.el7.x86_64
plymouth-scripts-0.8.9-0.28.20140113.el7.centos.x86_64
firewalld-0.4.4.4-6.el7.noarch
kexec-tools-2.0.14-17.el7.x86_64
audit-2.7.6-3.el7.x86_64
postfix-2.10.1-6.el7.x86_64
openssh-server-7.4p1-11.el7.x86_64
dracut-config-rescue-033-502.el7.x86_64
iprutils-2.4.14.1-1.el7.x86_64
man-db-2.6.3-9.el7.x86_64
selinux-policy-targeted-3.13.1-166.el7.noarch
[root@localhost ~]# rpm -q --changelog bash|head -3
* Tue Mar 07 2017 Kamil Dudka <kdudka@redhat.com - 4.2.46-28
- CVE-2016-9401 - Fix crash when '-' is passed as second sign to popd
  Resolves: #1429838

[root@localhost ~]# rpm -ql bash
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
/usr/bin/alias
/usr/bin/bash
/usr/bin/bashbug
/usr/bin/bashbug-64
/usr/bin/bg
/usr/bin/cd
/usr/bin/command
/usr/bin/fc
/usr/bin/fg
/usr/bin/getopts
/usr/bin/jobs
/usr/bin/read
/usr/bin/sh
/usr/bin/umask
/usr/bin/unalias
/usr/bin/wait
/usr/share/doc/bash-4.2.46
/usr/share/doc/bash-4.2.46/COPYING
/usr/share/info/bash.info.gz
/usr/share/locale/af/LC_MESSAGES/bash.mo
/usr/share/locale/bg/LC_MESSAGES/bash.mo
/usr/share/locale/ca/LC_MESSAGES/bash.mo
/usr/share/locale/cs/LC_MESSAGES/bash.mo
/usr/share/locale/de/LC_MESSAGES/bash.mo
/usr/share/locale/en@boldquot/LC_MESSAGES/bash.mo
/usr/share/locale/en@quot/LC_MESSAGES/bash.mo
/usr/share/locale/eo/LC_MESSAGES/bash.mo
/usr/share/locale/es/LC_MESSAGES/bash.mo
/usr/share/locale/et/LC_MESSAGES/bash.mo
/usr/share/locale/fi/LC_MESSAGES/bash.mo
/usr/share/locale/fr/LC_MESSAGES/bash.mo
/usr/share/locale/ga/LC_MESSAGES/bash.mo
/usr/share/locale/hu/LC_MESSAGES/bash.mo
/usr/share/locale/id/LC_MESSAGES/bash.mo
/usr/share/locale/ja/LC_MESSAGES/bash.mo
/usr/share/locale/lt/LC_MESSAGES/bash.mo
/usr/share/locale/nl/LC_MESSAGES/bash.mo
/usr/share/locale/pl/LC_MESSAGES/bash.mo
/usr/share/locale/pt_BR/LC_MESSAGES/bash.mo
/usr/share/locale/ro/LC_MESSAGES/bash.mo
/usr/share/locale/ru/LC_MESSAGES/bash.mo
/usr/share/locale/sk/LC_MESSAGES/bash.mo
/usr/share/locale/sv/LC_MESSAGES/bash.mo
/usr/share/locale/tr/LC_MESSAGES/bash.mo
/usr/share/locale/uk/LC_MESSAGES/bash.mo
/usr/share/locale/vi/LC_MESSAGES/bash.mo
/usr/share/locale/zh_CN/LC_MESSAGES/bash.mo
/usr/share/locale/zh_TW/LC_MESSAGES/bash.mo
/usr/share/man/man1/..1.gz
/usr/share/man/man1/:.1.gz
/usr/share/man/man1/[.1.gz
/usr/share/man/man1/alias.1.gz
/usr/share/man/man1/bash.1.gz
/usr/share/man/man1/bashbug-64.1.gz
/usr/share/man/man1/bashbug.1.gz
/usr/share/man/man1/bg.1.gz
/usr/share/man/man1/bind.1.gz
/usr/share/man/man1/break.1.gz
/usr/share/man/man1/builtin.1.gz
/usr/share/man/man1/builtins.1.gz
/usr/share/man/man1/caller.1.gz
/usr/share/man/man1/cd.1.gz
/usr/share/man/man1/command.1.gz
/usr/share/man/man1/compgen.1.gz
/usr/share/man/man1/complete.1.gz
/usr/share/man/man1/compopt.1.gz
/usr/share/man/man1/continue.1.gz
/usr/share/man/man1/declare.1.gz
/usr/share/man/man1/dirs.1.gz
/usr/share/man/man1/disown.1.gz
/usr/share/man/man1/enable.1.gz
/usr/share/man/man1/eval.1.gz
/usr/share/man/man1/exec.1.gz
/usr/share/man/man1/exit.1.gz
/usr/share/man/man1/export.1.gz
/usr/share/man/man1/fc.1.gz
/usr/share/man/man1/fg.1.gz
/usr/share/man/man1/getopts.1.gz
/usr/share/man/man1/hash.1.gz
/usr/share/man/man1/help.1.gz
/usr/share/man/man1/history.1.gz
/usr/share/man/man1/jobs.1.gz
/usr/share/man/man1/let.1.gz
/usr/share/man/man1/local.1.gz
/usr/share/man/man1/logout.1.gz
/usr/share/man/man1/mapfile.1.gz
/usr/share/man/man1/popd.1.gz
/usr/share/man/man1/pushd.1.gz
/usr/share/man/man1/read.1.gz
/usr/share/man/man1/readonly.1.gz
/usr/share/man/man1/return.1.gz
/usr/share/man/man1/set.1.gz
/usr/share/man/man1/sh.1.gz
/usr/share/man/man1/shift.1.gz
/usr/share/man/man1/shopt.1.gz
/usr/share/man/man1/source.1.gz
/usr/share/man/man1/suspend.1.gz
/usr/share/man/man1/times.1.gz
/usr/share/man/man1/trap.1.gz
/usr/share/man/man1/type.1.gz
/usr/share/man/man1/typeset.1.gz
/usr/share/man/man1/ulimit.1.gz
/usr/share/man/man1/umask.1.gz
/usr/share/man/man1/unalias.1.gz
/usr/share/man/man1/unset.1.gz
/usr/share/man/man1/wait.1.gz
[root@localhost ~]# rpm -qi bash
Name        : bash
Version     : 4.2.46
Release     : 28.el7
Architecture: x86_64
Install Date: Wed 09 May 2018 10:53:47 PM CST
Group       : System Environment/Shells
Size        : 3663637
License     : GPLv3+
Signature   : RSA/SHA256, Thu 10 Aug 2017 11:03:40 PM CST, Key ID 24c6a8a7f4a80e                                          b5
Source RPM  : bash-4.2.46-28.el7.src.rpm
Build Date  : Thu 03 Aug 2017 05:13:21 AM CST
Build Host  : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.gnu.org/software/bash
Summary     : The GNU Bourne Again shell
Description :
The GNU Bourne Again shell (Bash) is a shell or command language
interpreter that is compatible with the Bourne shell (sh). Bash
incorporates useful features from the Korn shell (ksh) and the C shell
(csh). Most sh scripts can be run by bash without modification.
[root@localhost ~]# rpm -qc bash
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
[root@localhost ~]# rpm -qd bash
/usr/share/doc/bash-4.2.46/COPYING
/usr/share/info/bash.info.gz
/usr/share/man/man1/..1.gz
/usr/share/man/man1/:.1.gz
/usr/share/man/man1/[.1.gz
/usr/share/man/man1/alias.1.gz
/usr/share/man/man1/bash.1.gz
/usr/share/man/man1/bashbug-64.1.gz
/usr/share/man/man1/bashbug.1.gz
/usr/share/man/man1/bg.1.gz
/usr/share/man/man1/bind.1.gz
/usr/share/man/man1/break.1.gz
/usr/share/man/man1/builtin.1.gz
/usr/share/man/man1/builtins.1.gz
/usr/share/man/man1/caller.1.gz
/usr/share/man/man1/cd.1.gz
/usr/share/man/man1/command.1.gz
/usr/share/man/man1/compgen.1.gz
/usr/share/man/man1/complete.1.gz
/usr/share/man/man1/compopt.1.gz
/usr/share/man/man1/continue.1.gz
/usr/share/man/man1/declare.1.gz
/usr/share/man/man1/dirs.1.gz
/usr/share/man/man1/disown.1.gz
/usr/share/man/man1/enable.1.gz
/usr/share/man/man1/eval.1.gz
/usr/share/man/man1/exec.1.gz
/usr/share/man/man1/exit.1.gz
/usr/share/man/man1/export.1.gz
/usr/share/man/man1/fc.1.gz
/usr/share/man/man1/fg.1.gz
/usr/share/man/man1/getopts.1.gz
/usr/share/man/man1/hash.1.gz
/usr/share/man/man1/help.1.gz
/usr/share/man/man1/history.1.gz
/usr/share/man/man1/jobs.1.gz
/usr/share/man/man1/let.1.gz
/usr/share/man/man1/local.1.gz
/usr/share/man/man1/logout.1.gz
/usr/share/man/man1/mapfile.1.gz
/usr/share/man/man1/popd.1.gz
/usr/share/man/man1/pushd.1.gz
/usr/share/man/man1/read.1.gz
/usr/share/man/man1/readonly.1.gz
/usr/share/man/man1/return.1.gz
/usr/share/man/man1/set.1.gz
/usr/share/man/man1/sh.1.gz
/usr/share/man/man1/shift.1.gz
/usr/share/man/man1/shopt.1.gz
/usr/share/man/man1/source.1.gz
/usr/share/man/man1/suspend.1.gz
/usr/share/man/man1/times.1.gz
/usr/share/man/man1/trap.1.gz
/usr/share/man/man1/type.1.gz
/usr/share/man/man1/typeset.1.gz
/usr/share/man/man1/ulimit.1.gz
/usr/share/man/man1/umask.1.gz
/usr/share/man/man1/unalias.1.gz
/usr/share/man/man1/unset.1.gz
/usr/share/man/man1/wait.1.gz
[root@localhost ~]# rpm -q --provides bash
/bin/bash
/bin/sh
bash = 4.2.46-28.el7
bash(x86-64) = 4.2.46-28.el7
config(bash) = 4.2.46-28.el7
[root@localhost ~]# rpm -qR bash
/bin/sh
config(bash) = 4.2.46-28.el7
libc.so.6()(64bit)
libc.so.6(GLIBC_2.11)(64bit)
libc.so.6(GLIBC_2.14)(64bit)
libc.so.6(GLIBC_2.15)(64bit)
libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6(GLIBC_2.3)(64bit)
libc.so.6(GLIBC_2.3.4)(64bit)
libc.so.6(GLIBC_2.4)(64bit)
libc.so.6(GLIBC_2.8)(64bit)
libdl.so.2()(64bit)
libdl.so.2(GLIBC_2.2.5)(64bit)
libtinfo.so.5()(64bit)
rpmlib(BuiltinLuaScripts) <= 4.2.2-1
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rtld(GNU_HASH)
rpmlib(PayloadIsXz) <= 5.2-1
[root@localhost ~]# rpm -q --scripts bash
postinstall scriptlet (using <lua>):
nl        = '\n'
sh        = '/bin/sh'..nl
bash      = '/bin/bash'..nl
f = io.open('/etc/shells', 'a+')
if f then
  local shells = nl..f:read('*all')..nl
  if not shells:find(nl..sh) then f:write(sh) end
  if not shells:find(nl..bash) then f:write(bash) end
  f:close()
end
postuninstall scriptlet (using <lua>):
-- Run it only if we are uninstalling
if arg[2] == "0"
then
  t={}
  for line in io.lines("/etc/shells")
  do
    if line ~= "/bin/bash" and line ~= "/bin/sh"
    then
      table.insert(t,line)
    end
  end

  f = io.open("/etc/shells", "w+")
  for n,line in pairs(t)
  do
    f:write(line.."\n")
  end
  f:close()
end

rpm检验

  • -V:(verify)
  • 若没有返回值,则没被修改

rpm包的来源合法性和完整性验证

  • 合法性通过数字签名来验证(开发者用私钥加密单项提取的特征码做成数字签名)
  • 使用者拿到公钥,如能匹配私钥则证明来源合法
  • 如果两个特征码相同,则证明程序完整
  • 关键点:合法获取公钥
  • 获取并导入信任的包制作者公钥:rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • 验证方式:
    • 1.安装时自动验证
    • 2.手工验证:rpm -K

数据库重建

  • 数据库在/var/lib/rpm
  • --initbuild:初始化数据库
  • --rebuilddb:重新构建数据库

yum命令使用

  • yum repolist [all|enabled|disabled]显示仓库列表,默认enabled
  • yum list [php*|all|available|installed|updates]显示安装包,默认all
  • yum install 安装程序包
  • yum update 更新程序包
  • yum check-update 检查可用更新程序包
  • yum remove|erase 卸载安装包 会删除有依赖关系的包
  • yum info 查看info
  • yum provides 查询有CAPABILITY的包
  • yum clean all 清理缓存
  • yum makecache 生成缓存
  • yum search 搜索(根据报名,summary信息模糊匹配) yum makecache后可以使用 yum -C search yum -C provides查询
  • yum reinstall 重装
  • yum downgrade 降级
  • yum deplist 查看依赖关系
  • yum history 查看事务历史
  • yum localinstall|localupdate 安装本地rpm,解决依赖关系
  • yum grouplist|groupinstall|groupupdate|groupinfo|groupremove 包组管理
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base 9,911
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,581
extras/7/x86_64 CentOS-7 - Extras 305
updates/7/x86_64 CentOS-7 - Updates 654
[root@localhost ~]# yum list installed|head
Loaded plugins: fastestmirror
Installed Packages
GeoIP.x86_64 1.5.0-11.el7 @anaconda
NetworkManager.x86_64 1:1.8.0-9.el7 @anaconda
NetworkManager-libnm.x86_64 1:1.8.0-9.el7 @anaconda
NetworkManager-team.x86_64 1:1.8.0-9.el7 @anaconda
NetworkManager-tui.x86_64 1:1.8.0-9.el7 @anaconda
NetworkManager-wifi.x86_64 1:1.8.0-9.el7 @anaconda
acl.x86_64 2.2.51-12.el7 @anaconda
aic94xx-firmware.noarch 30-6.el7 @anaconda
[root@localhost ~]# yum check-update kernel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com

kernel.x86_64 3.10.0-862.3.2.el7 updates
[root@localhost ~]# yum provides bash
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
bash-4.2.46-30.el7.x86_64 : The GNU Bourne Again shell
Repo : base



bash-4.2.46-28.el7.x86_64 : The GNU Bourne Again shell
Repo : @anaconda
[root@localhost ~]# yum grouplist
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.cn99.com
 * updates: mirrors.aliyun.com
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Cinnamon Desktop
   MATE Desktop
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Educational Software
   Electronic Lab
   Fedora Packager
   General Purpose Desktop
   Graphical Administration Tools
   Haskell
   Legacy UNIX Compatibility
   Milkymist
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
   TurboGears application framework
   Xfce
Done

yum命令选项

  • --nogpgcheck 禁止gpgcheck
  • -y 自动回答yes
  • -q 启用静默模式
  • --disablerepo=repoid 临时禁用此处指定repo
  • --noplugins 禁用所有插件

yum在repo配置文件中可用到的变量

  • $releasever:os 版本号
  • $arch:平台
  • $basearch:基础平台
  • $YUM0-9

创建yum仓库

  • yum install createrepo
  • createrepo rpm目录,生成repodata目录
  • 配置仓库

2、自建yum仓库,分别为网络源和本地源

安装网络源

[root@localhost ~]# yum -y install wget
Loaded plugins: fastestmirror
base | 3.6 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
(1/4): base/7/x86_64/primary_db | 5.9 MB 00:09
(2/4): extras/7/x86_64/primary_db | 147 kB 00:09
(3/4): base/7/x86_64/group_gz | 166 kB 00:09
(4/4): updates/7/x86_64/primary_db | 2.0 MB 00:11
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-15.el7_4.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package Arch Version Repository Size
================================================================================
Installing:
 wget x86_64 1.14-15.el7_4.1 base 547 k

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

Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
wget-1.14-15.el7_4.1.x86_64.rpm | 547 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-15.el7_4.1.x86_64 1/1
  Verifying : wget-1.14-15.el7_4.1.x86_64 1/1

Installed:
  wget.x86_64 0:1.14-15.el7_4.1

Complete!
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
[root@localhost yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2018-06-12 09:36:44-- http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 183.60.159.232, 183.60.159.227, 202.105.176.237, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|183.60.159.232|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2573 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[======================================>] 2,573 --.-K/s in 0.003s

2018-06-12 09:36:44 (727 KB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2573/2573]

[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Sources.repo
CentOS-Base.repo.bak CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
--2018-06-12 09:37:33-- http://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 202.105.176.227, 202.105.176.237, 202.105.176.236, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|202.105.176.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/epel.repo’

100%[======================================>] 664 --.-K/s in 0s

2018-06-12 09:37:33 (52.0 MB/s) - ‘/etc/yum.repos.d/epel.repo’ saved [664/664]

[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Sources.repo
CentOS-Base.repo.bak CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo epel.repo
[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
base | 3.6 kB 00:00
epel | 3.2 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
(1/18): epel/x86_64/group_gz | 88 kB 00:00
(2/18): base/7/x86_64/group_gz | 166 kB 00:00
(3/18): epel/x86_64/updateinfo | 932 kB 00:04
(4/18): epel/x86_64/prestodelta | 1.4 kB 00:00
(5/18): epel/x86_64/primary | 3.5 MB 00:17
(6/18): base/7/x86_64/primary_db | 5.9 MB 00:23
(7/18): epel/x86_64/other | 2.2 MB 00:06
base/7/x86_64/filelists_db FAILED
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/f35d0029ad59e9fca0823be708150f5ee28ee1707cdc9bdaf420da676daafe28-filelists.sqlite.bz2: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/f35d0029ad59e9fca0823be708150f5ee28ee1707cdc9bdaf420da676daafe28-filelists.sqlite.bz2: (28, 'Connection timed out after 30000 milliseconds')
Trying other mirror.
base/7/x86_64/other_db FAILED
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/ede157be4cf5c030483ab639f8f18c6daf268d4c233037a1578b3ed8258fa461-other.sqlite.bz2: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/ede157be4cf5c030483ab639f8f18c6daf268d4c233037a1578b3ed8258fa461-other.sqlite.bz2: (28, 'Connection timed out after 30001 milliseconds')
Trying other mirror.
(8/18): extras/7/x86_64/other_db | 95 kB 00:00
(9/18): extras/7/x86_64/primary_db | 147 kB 00:00
(10/18): updates/7/x86_64/prestodelta | 237 kB 00:01
(11/18): epel/x86_64/filelists | 10 MB 00:34
(12/18): updates/7/x86_64/filelists_db | 1.3 MB 00:05
(13/18): updates/7/x86_64/other_db | 231 kB 00:00
(14/18): updates/7/x86_64/primary_db | 2.0 MB 00:08
(15/18): base/7/x86_64/other_db | 2.5 MB 00:10
(16/18): base/7/x86_64/filelists_db | 6.9 MB 00:14
extras/7/x86_64/filelists_db FAILED
http://mirrors.aliyuncs.com/centos/7/extras/x86_64/repodata/cd697d6a6d62dc517544660b2165ab5c5e89e0dfd9205078ae57f341dcb7856d-filelists.sqlite.bz2: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/extras/x86_64/repodata/cd697d6a6d62dc517544660b2165ab5c5e89e0dfd9205078ae57f341dcb7856d-filelists.sqlite.bz2: (28, 'Connection timed out after 30003 milliseconds')
Trying other mirror.
(17/18): extras/7/x86_64/filelists_db | 519 kB 00:00
extras/7/x86_64/prestodelta FAILED
http://mirrors.aliyuncs.com/centos/7/extras/x86_64/repodata/bd200a0ecb76986b2a6667904f4918b717594faa19e66adcc57ec50af853aa4e-prestodelta.xml.gz: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/extras/x86_64/repodata/bd200a0ecb76986b2a6667904f4918b717594faa19e66adcc57ec50af853aa4e-prestodelta.xml.gz: (28, 'Connection timed out after 30003 milliseconds')
Trying other mirror.
(18/18): extras/7/x86_64/prestodelta | 48 kB 00:00
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
epel 12581/12581
epel 12581/12581
epel 12581/12581
Metadata Cache Created
[root@localhost yum.repos.d]# yum repo list
Loaded plugins: fastestmirror
No such command: repo. Please use /usr/bin/yum --help
[root@localhost yum.repos.d]# 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 9,911
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,581
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 305
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 654
repolist: 23,451

安装本地源

[root@localhost ~]# mount -r /dev/sr0 /media/cdrom/
[root@localhost ~]# ls /media/cdrom/
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
[root@localhost ~]# vim /etc/yum.repos.d/local.repo
[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[local]
name=Local Packages for Enterprise Linux 7
baseurl=file:///media/cdrom
gpgcheck=0
[root@localhost ~]# 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 9,911
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,581
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 305
local Local Packages for Enterprise Linux 7 3,894
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 654
repolist: 27,345

3、简述at和crontab命令,制定 每周三凌晨三、五点10分执行某个脚本,输出当前时间,时间格式为 2017-12-28 10:00:00

at

  • at [OPTION] ... TIME
  • TIME: HH:MM [YYYY-mm-dd]
  • 或:noon,midnight,teatime
  • 或:HH:MM tomorrow
  • 或:now+#UNIT UNIT:minutes hours days
  • ctrl+D 提交
  • 结果将发送到邮件
  • at -f /PATH/TO/SOMEFILE TIME 使用文件任务,不使用用户交互
  • at -l 查看任务列表
  • at -d 编号:删除对应编号的任务
  • at -c 编号:查看指定编号作业内容
  • 安装 atd ,启动atd
  • yum -y install at
  • service atd start/ systemctl start atd.service
  • service atd status/ systemctl status atd.service
[root@localhost scripts]# vim echoAt.sh
[root@localhost scripts]# cat echoAt.sh
#!/bin/bash
echo "hello world"
[root@localhost scripts]# at -f /root/scripts/echoAt.sh now+1min
job 2 at Sat Jun 9 11:46:00 2018
[root@localhost scripts]# at -l
2 Sat Jun 9 11:46:00 2018 a root
[root@localhost scripts]# at -l
2 Sat Jun 9 11:46:00 2018 a root
[root@localhost scripts]# at -l
[root@localhost scripts]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Sat Jun 9 11:46 14/505 "Output from your job "
& 1
Message 1:
From root@localhost.localdomain Sat Jun 9 11:46:00 2018
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Subject: Output from your job 2
To: root@localhost.localdomain
Date: Sat, 9 Jun 2018 11:46:00 +0800 (CST)
From: root@localhost.localdomain (root)
Status: R

hello world

& q
Held 1 message in /var/spool/mail/root
You have mail in /var/spool/mail/root
[root@localhost scripts]# at -f /root/scripts/echoAt.sh now+1min
job 4 at Sat Jun 9 11:49:00 2018
[root@localhost scripts]# at -l
4 Sat Jun 9 11:49:00 2018 a root
[root@localhost scripts]# at -d 4
[root@localhost scripts]# at -l
[root@localhost scripts]# at -f /root/scripts/echoAt.sh now+1min
job 5 at Sat Jun 9 11:49:00 2018
[root@localhost scripts]# at -c 5
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=1; export XDG_SESSION_ID
HOSTNAME=localhost.localdomain; export HOSTNAME
SHELL=/bin/bash; export SHELL
HISTSIZE=1000; export HISTSIZE
SSH_CLIENT=192.168.1.103\ 53797\ 22; export SSH_CLIENT
SSH_TTY=/dev/pts/0; export SSH_TTY
USER=root; export USER
LS_COLORS=rs=0:di=01\;34:ln=01\;36:mh=00:pi=40\;33:so=01\;35:do=01\;35:bd=40\;33\;01:cd=40\;33\;01:or=40\;31\;01:mi=01\;05\;37\;41:su=37\;41:sg=30\;43:ca=30\;41:tw=30\;42:ow=34\;42:st=37\;44:ex=01\;32:\*.tar=01\;31:\*.tgz=01\;31:\*.arc=01\;31:\*.arj=01\;31:\*.taz=01\;31:\*.lha=01\;31:\*.lz4=01\;31:\*.lzh=01\;31:\*.lzma=01\;31:\*.tlz=01\;31:\*.txz=01\;31:\*.tzo=01\;31:\*.t7z=01\;31:\*.zip=01\;31:\*.z=01\;31:\*.Z=01\;31:\*.dz=01\;31:\*.gz=01\;31:\*.lrz=01\;31:\*.lz=01\;31:\*.lzo=01\;31:\*.xz=01\;31:\*.bz2=01\;31:\*.bz=01\;31:\*.tbz=01\;31:\*.tbz2=01\;31:\*.tz=01\;31:\*.deb=01\;31:\*.rpm=01\;31:\*.jar=01\;31:\*.war=01\;31:\*.ear=01\;31:\*.sar=01\;31:\*.rar=01\;31:\*.alz=01\;31:\*.ace=01\;31:\*.zoo=01\;31:\*.cpio=01\;31:\*.7z=01\;31:\*.rz=01\;31:\*.cab=01\;31:\*.jpg=01\;35:\*.jpeg=01\;35:\*.gif=01\;35:\*.bmp=01\;35:\*.pbm=01\;35:\*.pgm=01\;35:\*.ppm=01\;35:\*.tga=01\;35:\*.xbm=01\;35:\*.xpm=01\;35:\*.tif=01\;35:\*.tiff=01\;35:\*.png=01\;35:\*.svg=01\;35:\*.svgz=01\;35:\*.mng=01\;35:\*.pcx=01\;35:\*.mov=01\;35:\*.mpg=01\;35:\*.mpeg=01\;35:\*.m2v=01\;35:\*.mkv=01\;35:\*.webm=01\;35:\*.ogm=01\;35:\*.mp4=01\;35:\*.m4v=01\;35:\*.mp4v=01\;35:\*.vob=01\;35:\*.qt=01\;35:\*.nuv=01\;35:\*.wmv=01\;35:\*.asf=01\;35:\*.rm=01\;35:\*.rmvb=01\;35:\*.flc=01\;35:\*.avi=01\;35:\*.fli=01\;35:\*.flv=01\;35:\*.gl=01\;35:\*.dl=01\;35:\*.xcf=01\;35:\*.xwd=01\;35:\*.yuv=01\;35:\*.cgm=01\;35:\*.emf=01\;35:\*.axv=01\;35:\*.anx=01\;35:\*.ogv=01\;35:\*.ogx=01\;35:\*.aac=01\;36:\*.au=01\;36:\*.flac=01\;36:\*.mid=01\;36:\*.midi=01\;36:\*.mka=01\;36:\*.mp3=01\;36:\*.mpc=01\;36:\*.ogg=01\;36:\*.ra=01\;36:\*.wav=01\;36:\*.axa=01\;36:\*.oga=01\;36:\*.spx=01\;36:\*.xspf=01\;36:; export LS_COLORS
MAIL=/var/spool/mail/root; export MAIL
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin; export PATH
PWD=/root/scripts; export PWD
LANG=en_US.UTF-8; export LANG
HISTCONTROL=ignoredups; export HISTCONTROL
SHLVL=1; export SHLVL
HOME=/root; export HOME
LOGNAME=root; export LOGNAME
SSH_CONNECTION=192.168.1.103\ 53797\ 192.168.1.107\ 22; export SSH_CONNECTION
LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN
XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR
OLDPWD=/root; export OLDPWD
cd /root/scripts || {
         echo 'Execution directory inaccessible' >&2
         exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER519a1011'
#!/bin/bash
echo "hello world"

marcinDELIMITER519a1011

cron 周期性任务计划

  • 服务程序:cronie,守护进程crond daemon
  • systemctl status crond./sercie crond status
  • 与atd不同:使用专用配置文件,有固定格式,周期性任务
  • cron任务分为:系统cron,用户cron
  • 系统cron配置/etc/crontab
  • 系统cron特点:时间点(* * * * *) 用户身份 任务命令,需要自定义PATH,执行结果邮件发送给MAILTO指定用户
  • 用户cron配置使用命令crontab -e:修改/var/spool/cron/USERNAME文件
  • 用户cron特点:时间点 任务命令,需要自定义PATH,执行结果邮件发送给当前用户

时间表示法

  • 1.特定值:给定时间内有效取值范围内的值
    • 分(0-59),时(0-23),日(1-31),月(1-12),周(1-7)
    • 周与日不能同时使用
  • 2.通配符:* 给定时间内有效取值范围内的所有值,表示每
  • 3.离散:, 用逗号隔开
  • 4.连续:-
  • 5.指定时间内,定义步长,*/2 每隔两分钟
  • 注意:
    • 1.指定时间点不能被步长整除,则没有意义
    • 2.最小时间单位为分钟,秒级需要借助脚本循环

crontab命令

  • -e:编辑任务,添加#注释可以删除一个任务
  • 运行结果以邮件发送给用户,如果不接收需要使用 &> /dev/null
  • -l:列出任务
  • -r:移除所有任务
  • -u :root为指定用户管理cron
  • 定义COMMAND中如果有%,需要转义,或加''
  • 某任务在指定时间点因关机未能执行,则下次开机不会自动执行
  • 如果需要开机自动执行,需要使用anacron实现
  • 脚本前加入 . /etc/profile .~/.bash_profile
[root@localhost ~]# crontab -l
* * * * * /bin/bash /root/a.sh
* * * * * /bin/bash /root/b.sh
[root@localhost ~]# cat a.sh
. /etc/profile
. ~/.bash_profile
echo $PATH
[root@localhost ~]# cat b.sh
echo $PATH
[root@localhost ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 2 messages 2 new
>N 1 (Cron Daemon) Sat Jun 9 12:26 25/861 "Cron <root@localhost>"
 N 2 (Cron Daemon) Sat Jun 9 12:26 25/897 "Cron <root@localhost>"
& 1
Message 1:
From root@localhost.localdomain Sat Jun 9 12:26:01 2018
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> /bin/bash /root/b.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=44>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
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>
Date: Sat, 9 Jun 2018 12:26:01 +0800 (CST)
Status: R

/usr/bin:/bin
& 2
Message 2:
From root@localhost.localdomain Sat Jun 9 12:26:01 2018
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> /bin/bash /root/a.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=45>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
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>
Date: Sat, 9 Jun 2018 12:26:01 +0800 (CST)
Status: R

/usr/local/sbin:/usr/sbin:/usr/bin:/bin:/root/bin
& q
Held 2 messages in /var/spool/mail/root

制定 每周三凌晨三、五点10分执行某个脚本,输出当前时间,时间格式为 2017-12-28 10:00:00

crontab -e
10 3,5 * * 3 /bin/bash /root/scripts/nowtime.sh
vim /root/scripts/nowtime.sh
#!/bin/bash
# description: 输出当前时间,时间格式为 2017-12-28 10:00:00
. /etc/profile
. ~/.bash_profile
date +'%F %T'

4、简述sed常用操作命令,并举例

sed

  • 流编辑器 行编辑器
  • 工作空间:pattern space,hold space
  • 每一行匹配pattern,匹配到的编辑命令修改,没有匹配到的到标准输出
  • sed 'script' file

选项

  • -n:不输出pattern space的标准输出
  • -e:指定多个编辑
  • -f:/PATH/TO/SED_SCRIPTS_FILE 指定sed脚本
  • -r:使用扩展正则表达式
  • -i:编辑原文件

地址定界

  • 1.不给地址:全文处理
  • 2.单地址:#:指定行 /PATTERN/:此模式匹配到的每一行
  • 3.地址范围
    • #,#
    • #,+#
    • #,/pattern/
    • /pattern1/,/pattern2/
  • 4.步近
    • 1~2:奇数行
    • 2~2:偶数行

编辑命令

  • d:删除
  • p:显示
  • a \TEXT:(append)在行后面追加,\n多行
  • i \TEXT:(insert)在行前插入
  • c \TEXT:(change)匹配行替换
  • w :保存
  • r:读取,文件合并
  • =:打印行号
  • !:取反
  • s///:查找替换,g:全局替换 w:替换成功保存到文件 p:只显示替换行
[root@localhost ~]# cat /etc/fstab |sed -r -e 's/^#[[:space:]]*//g' -e '/^[[:space:]]*$/d'
/etc/fstab
Created by anaconda on Wed May 9 22:52:20 2018
Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
UUID=4f8f9662-22a0-446d-8044-465d2a00b42d / xfs defaults 0 0
UUID=2ffce847-9957-4a54-a48f-ff466fa8a6fa /boot xfs defaults 0 0
UUID=200259c9-e2bb-4768-a136-4e6f2d287686 swap swap defaults 0 0
[root@localhost ~]# sed '7s#disabled#enforcing#g' !$
sed '7s#disabled#enforcing#g' /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

高级编辑命令

  • h:模式覆盖保持
  • H:模式追加保持
  • g:保持覆盖模式
  • G:保持追加模式
  • x:互换
  • n:覆盖匹配下一行到模式空间
  • N:追加匹配下一行至模式空间
  • d:删除模式行
  • D:删除多行模式空间的所有行
  • sed '/^$/d;G' 删除所有空白行,原有每行后方添加空白行
posted @ 2018-06-12 10:52  徐鲁川  阅读(859)  评论(1编辑  收藏  举报