rpm yum crontab sed用法

一、簡述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 禁用全部插件php

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

  • $releasever:os 版本號
  • $arch:平臺
  • $basearch:基礎平臺
  • $YUM0-9node

建立yum倉庫

  • yum install createrepo
  • createrepo rpm目錄,生成repodata目錄
  • 配置倉庫

二、自建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

三、簡述at和crontab命令,制定 每週三凌晨3、五點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

制定 每週三凌晨3、五點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'

四、簡述sed經常使用操做命令,並舉例

sed

  • 流編輯器 行編輯器
  • 工做空間:pattern space,hold space
  • 每一行匹配pattern,匹配到的編輯命令修改,沒有匹配到的到標準輸出
  • sed 'script' filelinux

    選項

  • -n:不輸出pattern space的標準輸出
  • -e:指定多個編輯
  • -f:/PATH/TO/SED_SCRIPTS_FILE 指定sed腳本
  • -r:使用擴展正則表達式
  • -i:編輯原文件git

地址定界

  • 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' 刪除全部空白行,原有每行後方添加空白行
相關文章
相關標籤/搜索