1. 程式人生 > >Linux基本內容介紹(5) --rpm與yum的使用,at和crontab命令簡介,以及sed的常用操作

Linux基本內容介紹(5) --rpm與yum的使用,at和crontab命令簡介,以及sed的常用操作

rpm命令

概述

rpm軟體是用於在redhat系列的Linux發行版中管理軟體包的工具。rpm包中包含的是二進位制的可執行程式,與原始碼安裝的方式相比,更加便捷。

命令使用

命令格式:rpm OPTIONS PACKAGE_FILE
根據操作的用途,主要可分為6類:安裝,升級,解除安裝,查詢,校驗和資料庫維護

1.安裝 rpm {-i|–install} [OPTIONS] PACKAGE_FILE OPTIONS: -v 詳細資訊 -vv 輸出更詳細的資訊 -h 輸出進度條,每個#表示2%的進度 –test dry run,屬於測試安裝,不會真正的安裝包,用於測試依賴關係和衝突訊息 –nodeps 強制安裝,忽略依賴關係,如果軟體的使用需要這些缺失的依賴關係的話,會導致軟體無法使用,如果只是文件功能之類的非必要功能的依賴關係,則不影響軟體的使用 –replacepkgs 重新安裝,例如誤改了某配置檔案需要恢復,進行重灌即可,需要注意的是重灌前要刪除誤改的配置檔案 i.e.安裝軟體包lrzsz

[root@test ~]# rpm -ivh lrzsz-0.12.20-27.1.el6.x86_64.rpm 
warning: lrzsz-0.12.20-27.1.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:lrzsz                  ##########################################
# [100%]

2.升級 rpm {-U|–upgrade} [OPTIONS] PACKAGE_FILE 升級或安裝 rpm {-F|–freshen} [OPTIONS] PACKAGE_FILE 升級 OPTIONS –oldpackage 降級 –force 強制升級

3.解除安裝 rpm {-e|–erase} [OPTIONS] PACKAGE_FILE OPTIONS –allmatches 解除安裝所有匹配指定名稱的軟體包 –nodeps強制解除安裝,忽略依賴關係 –noscripts 不執行指令碼解除安裝 –test 測試解除安裝,不是真的解除安裝,dry run

4.查詢 rpm {-q|–query} [select options] [query options] select options -a, –all 查詢所有已安裝的包 -f FILE 查詢指定檔案由哪個軟體包產生 i.e.

[bhps@test ~]$ rpm -qf /etc/crontab
crontabs-1.10-33.el6.noarch

-p 對未安裝程式包執行查詢操作 –whatprovides CAPABILITY 檢視指定CAPABILITY由哪個包提供 –whatrequires CAPABILITY 檢視指定CAPABILITY被哪個包依賴

query options –changelog 檢視包的changelog -l, –l 檢視包安裝生成檔案列表 -i,–info 檢視包的資訊,包括大小,時間戳和所屬包組等 -c,–configfiles 檢視指定包的配置檔案 -d,–docfiles檢視指定包提供的man文件 –provides 檢視指定包提供的CAPABILITY -R,–requires 檢視指定包的依賴關係 –scripts 檢視指定包自帶的指令碼片段

5.校驗 rpm {-V|–verify} [select options] [verify options] verify options –nodeps不校驗依賴關係 –nodigest 不校驗摘要 –noscripts 不執行包自帶指令碼 –nosignature不校驗簽名

6.資料庫維護 rpm {–initdb|–rebuilddb} 初始化資料庫,重建資料庫

yum命令

概述

yum是一種基於rpm的,互動式的包管理工具。可以實現安裝、更新、解除安裝包的操作,並能自動分析解決依賴關係問題。

命令使用

命令格式:yum [options] [command] [package …] 1.COMMAND repolist [all|enabled|disabled]顯示倉庫列表 list [all|glob_exp1][glo_exp2][…] 顯示程式包 list {availabe|installed|updates} [glob_exp1] […]顯示程式包(可用的,已安裝的或者更新的) install package1 [package2] […] 安裝程式包 reinstall package1 [package2] […] 重新安裝程式包 update [package1] [package2] […] 升級程式包 downgrade package1 [package2] […] 降級程式包 check-update 檢查可用升級 remove|erase package1 [package2] […] 解除安裝程式包 info […]檢視程式包資訊 provides|whatprovides feature1 [feature2] […] 檢視提供某特性的包或者包提供了哪些特性 clean [packages|metadata|expire-cache|rpmdb|plugins|all] 清理本地快取 makecache 構建快取 search string1 [string2] […] 以指定關鍵字搜尋程式包或者summary資訊 deplist package1 [package2] […]檢視指定包的依賴資訊 i.e.檢視lrzsz的依賴資訊

[[email protected] bhps]# yum deplist lrzsz
Loaded plugins: product-id, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Finding dependencies: 
package: lrzsz.x86_64 0.12.20-27.1.el6
  dependency: libnsl.so.1()(64bit)
   provider: glibc.x86_64 2.12-1.149.el6
  dependency: libc.so.6(GLIBC_2.7)(64bit)
   provider: glibc.x86_64 2.12-1.149.el6
  dependency: rtld(GNU_HASH)
   provider: glibc.i686 2.12-1.149.el6
   provider: glibc.x86_64 2.12-1.149.el6

history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats] 檢視事務歷史 localinstall rpmfile1 [rpmfile2] […] 本地安裝 localupdate rpmfile1 [rpmfile2] […] 本地升級 groupinstall group1 [group2] […] 包組安裝 groupupdate group1 [group2] […] 包組升級 grouplist [hidden] [groupwildcard] […] 展示組 groupremove group1 [group2] […] 移除組 groupinfo group1 [group2] […] 檢視組資訊

2.OPTION –nogpgcheck 禁止進行gpg check -y 自動應答yes -q 靜默模式 –disablerepo=repoidglog 臨時禁止指定repo –enablerepo=repoidlob –noplugins 禁用所有外掛 i.e. 1.建立源為網路的yum 編輯檔案/etc/yum.repos.d/base.repo 使用內網自建源http://10.161.34.169:8080/rhel6.6/http://10.161.34.169:8080/epel/

[[email protected] yum.repos.d]# cat base.repo
[base]
name=rhel6.6
enabled=1
gpgcheck=0
baseurl=http://XXX.XXX.XXX.XXX:8080/rhel6.6/

[epel]
name=epel
baseurl=http://XXX.XXX.XXX.XXX:8080/epel/
gpgcheck=0
enabled=1
[[email protected] yum.repos.d]# yum make cache

2.建立源為本地的yum 在上面的基礎上新增包含glusterfs rpm包的源

[[email protected] glusterfs]# ls
glusterfs-3.6.6-1.el6.x86_64.rpm            glusterfs-debuginfo-3.6.6-1.el6.x86_64.rpm      glusterfs-geo-replication-3.6.6-1.el6.x86_64.rpm  repodata
glusterfs-api-3.6.6-1.el6.x86_64.rpm        glusterfs-devel-3.6.6-1.el6.x86_64.rpm          glusterfs-libs-3.6.6-1.el6.x86_64.rpm
glusterfs-api-devel-3.6.6-1.el6.x86_64.rpm  glusterfs-extra-xlators-3.6.6-1.el6.x86_64.rpm  glusterfs-rdma-3.6.6-1.el6.x86_64.rpm
glusterfs-cli-3.6.6-1.el6.x86_64.rpm        glusterfs-fuse-3.6.6-1.el6.x86_64.rpm           glusterfs-server-3.6.6-1.el6.x86_64.rpm
[[email protected] glusterfs]# pwd
/usr/local/src/glusterfs
[[email protected] yum.repos.d]# cat >> /etc/yum.repos.d/base.repo <<EOF
> [glusterfs]
> name=glusterfs
> baseurl=file:///usr/local/src/glusterfs/
> gpgcheck=0
> enabled=1
> EOF
[[email protected] yum.repos.d]# yum makecache
Loaded plugins: product-id, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base                                                                                                                                 | 2.9 kB     00:00     
base/filelists_db                                                                                                                    | 4.2 MB     00:00     
base/primary_db                                                                                                                      | 3.3 MB     00:00     
base/other_db                                                                                                                        | 1.7 MB     00:00     
epel                                                                                                                                 | 3.0 kB     00:00     
epel/filelists_db                                                                                                                    | 9.0 MB     00:00     
epel/primary_db                                                                                                                      | 6.1 MB     00:00     
epel/other_db                                                                                                                        | 3.6 MB     00:00     
glusterfs                                                                                                                            | 2.9 kB     00:00 ... 
glusterfs/filelists_db                                                                                                               |  18 kB     00:00 ... 
glusterfs/primary_db                                                                                                                 |  11 kB     00:00 ... 
glusterfs/other_db                                                                                                                   | 3.3 kB     00:00 ... 
Metadata Cache Created

at命令

命令格式:at [OPTION] … TIME TIME     HH:MM [YYYY-mm-dd]     noon,midnight,teatime     tomorrow     now+# UNIT:minutes,hours,days or weeks OPTION     -l 檢視作業佇列,相當於atq     -f /PATH/FROM/SOMEFILE,從指定檔案中讀取作業任務,不需要互動式輸入     -d刪除指定作業,相當於atrm     -c檢視指定作業的具體內容     -q QUEUE指明佇列

crontab命令

系統cron配置檔案格式,編輯該檔案需要root許可權

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# 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

注意: (1)每行定義一個cron任務 (2)crontab中環境變數與使用者登陸環境變數不同,使用命令時使用絕對路徑 (3)crontab執行結果郵件傳送給使用者 六個時間欄位: (1)特定值:給定值,某個時間點,day of week和day of month一般不同時使用 (2)*:表示該欄位所有的可選值 (3)離散值:離散值用逗號分割,表示多個時間點 (4)連續值:連續值使用-連線兩個數字,表示連續的時間段 (5)定義步長:在指定時間上定義步長,/#。指定時間不能被步長整除,是沒有意義的,步長最小單位為分鐘,秒級的實現需要在指令碼中實現

普通使用者crontab命令 需要使用root使用者將需要使用crontab命令的使用者名稱稱加入到/etc/cron.allow檔案中,每個使用者一行;/etc/cron.deny是不允許使用crontab的使用者,優先順序低於/etc/cron.allow i.e. 1.每週三凌晨三、五點10分執行某個指令碼,輸出當前時間,時間格式為 2017-12-28 10:00:00

[[email protected] test]$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# 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
10 3,5 * * 3 bhps /bin/bash /home/bhps/test/show_time.sh &> /dev/null
[[email protected] test]$ pwd 
/home/bhps/test
[[email protected] test]$ cat show_time.sh 
#!/bin/bash
PATH=/usr/lib64/qt-3.3/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin:/home/bhps/bin
export PATH
cur_time=$(date +"%Y-%d-%m %H:%M:%S")
echo $cur_time

sed命令

概述

流式編輯器,可用於過濾或編輯文件

命令使用

命令格式:sed [OPTION]… {script-only-if-no-other-script} [input-file]… OPTION:    -n:不輸出模式空間的內容到螢幕,預設是輸出所有讀入到模式空間內容的。    -e script|–expression=script:多點編輯,-e script -e script    -f /PATH/TO/SED_SCRIPT_FILE 從檔案中讀取script,每行一個script    -r|–regexp-extend:支援使用擴充套件正則表示式,+和?不需要轉義    -i|–in-place:直接編輯原檔案

script:地址界定編輯命令 編輯命令:    d:刪除    p:顯示模式空間內容    a \text:在行後追加text內容    i \text:在行前插入文字text    c \text:用text替換匹配到的行    w /PATH/TO/SOMEFILE:將模式空間匹配到的內容儲存到檔案    r /PATH/FROM/SOMEFILE:讀取指定檔案內容至當前檔案模式匹配的行後    =:為匹配到的模式列印行號    !:條件取反,地址定界!編輯命令    s///:查詢替換,分隔符可自己指定,例如[email protected]@@,s###       替換標記:g:全域性替換,w /path/to/file 將替換結果儲存至檔案,p:顯示替換成功的行 高階命令: h:模式空間的內容覆蓋至保持空間 H:模式空間的內容追加至保持空間 g:保持空間內容覆蓋至模式空間 G:保持空間內容追加至模式空間 x:模式空間和保持空間內容互換 n:將讀取匹配到的行的下一行覆蓋至模式空間 N:將讀取匹配到的行的下一行追加至模式空間 d:刪除模式空間的行 D:刪除多行模式空間所有行 i.e. 1.顯示偶數行

[bhps@test ~]$ cat t.txt 
first line
two line
third line
fourth line
fifth line
[bhps@test ~]$ sed -n 'n;p' t.txt 
two line
fourth line

2.刪除以#開頭的行的#字元和#字元後面的所有空格符

[[email protected] ~]$ sed '[email protected]^#[[:space:]]*@@g' grub.conf 
grub.conf generated by anaconda

Note that you do not have to rerun grub after making changes to this file
NOTICE:  You have a /boot partition.  This means that
all kernel and initrd paths are relative to /boot/, eg.
root (hd0,0)
kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
initrd /initrd-[generic-]version.img
boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux 6 (2.6.32-504.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-504.el6.x86_64.img