天天看點

使用yum軟體源

1 案例1:使用yum軟體源

1.1 問題

本例要求為虛拟機 server0指定可用的yum軟體源,相關要求如下:

YUM軟體庫源為 http://content.example.com/rhel7.0/x86_64/dvd
将此配置為虛拟機 server0 的預設軟體倉庫           

1.2 步驟

實作此案例需要按照如下步驟進行。

步驟一:檢查現有yum倉庫,去除不可用的設定

1)列出yum庫

[root@server0 ~]# yum  repolist  
已加載插件:langpacks
rhel_dvd                                                    | 4.1 kB  00:00:00     
(1/2): rhel_dvd/group_gz                                    | 134 kB  00:00:00     
(2/2): rhel_dvd/primary_db                                  | 3.4 MB  00:00:00     
源辨別                       源名稱                                           狀态
rhel_dvd                     Remote classroom copy of dvd                     4,305
repolist: 4,305           

2)移除不可用的yum庫配置檔案

當執行yum repolist操作報錯時,才執行此步驟(否則此步可跳過)。

[root@server0 ~]# mkdir  /etc/yum.repos.d/repobak
[root@server0 ~]# mv  /etc/yum.repos.d/*.repo  /etc/yum.repos.d/repobak/           

步驟二:添加指定的yum倉庫配置

1)使用yum-config-manager工具建立新配置檔案

[root@server0 ~]# yum-config-manager  --add-repo  http://content.example.com/rhel7.0/x86_64/dvd 
已加載插件:langpacks
adding repo from: http://content.example.com/rhel7.0/x86_64/dvd
[content.example.com_rhel7.0_x86_64_dvd]
name=added from: http://content.example.com/rhel7.0/x86_64/dvd
baseurl=http://content.example.com/rhel7.0/x86_64/dvd
enabled=1           

2)修改建立的倉庫配置,添加gpgcheck=0以禁用GPG簽名檢查

[root@server0 ~]# vim  /etc/yum.repos.d/content.example.com_rhel7.0_x86_64_dvd
[content.example.com_rhel7.0_x86_64_dvd]
name=added from: http://content.example.com/rhel7.0/x86_64/dvd
baseurl=http://content.example.com/rhel7.0/x86_64/dvd
enabled=1
gpgcheck=0            

步驟三:确認新配置的yum源可用

[root@server0 ~]# yum  clean  all                          //清理緩存
已加載插件:langpacks
正在清理軟體源: content.example.com_rhel7.0_x86_64_dvd
Cleaning up everything
[root@server0 ~]# yum  repolist                          //重新列出可用的源
已加載插件:langpacks
content.example.com_rhel7.0_x86_64_dvd                      | 4.1 kB  00:00:00     
(1/2): content.example.com_rhel7.0_x86_64_dvd/group_gz      | 134 kB  00:00:00     
(2/2): content.example.com_rhel7.0_x86_64_dvd/primary_db    | 3.4 MB  00:00:00     
源辨別                                 源名稱                                 狀态
content.example.com_rhel7.0_x86_64_dvd added from: http://content.example.com 4,305
repolist: 4,305           

2 案例2:更新Linux核心

2.1 問題

本例要求為虛拟機 server0安裝更新版的新核心:

新版本的核心安裝檔案可以從以下位址擷取:
http://classroom/content/rhel7.0/x86_64/errata/Packages/
更新核心,并滿足下列要求:當系統重新啟動後,更新的新核心應該作為預設核心;原來的核心要被保留,并且仍然可以正常啟動           

2.2 步驟

步驟一:下載下傳新版核心的安裝檔案

1)确認新版核心的下載下傳位址

如果給定的下載下傳位址中未包含kernel-...rpm檔案路徑,則打開firefox浏覽器,通路指定的網址(如圖-5所示)。

圖-5

在打開的網頁上找到需要的核心檔案,右擊對應的連結,選擇“Copy Link Location”複制下載下傳位址(如圖-6所示)。

圖-6

2)下載下傳新版核心安裝檔案

根據前一步擷取到的核心下載下傳位址,使用wget指令下載下傳:

[root@server0 ~]# wget  http://classroom/content/rhel7.0/x86_64/errata/Packages/kernel-3.10.0-123.1.2.el7.x86_64.rpm
--2016-12-23 22:13:47--  http://classroom/content/rhel7.0/x86_64/errata/Packages/kernel-3.10.0-123.1.2.el7.x86_64.rpm
正在解析主機 classroom (classroom)... 172.25.254.254
正在連接配接 classroom (classroom)|172.25.254.254|:80... 已連接配接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:30266784 (29M) [application/x-rpm]
正在儲存至: “kernel-3.10.0-123.1.2.el7.x86_64.rpm”
100%[=========================================>] 30,266,784  40.4MB/s 用時 0.7s   
2016-12-23 22:13:47 (40.4 MB/s) - 已儲存 “kernel-3.10.0-123.1.2.el7.x86_64.rpm” [30266784/30266784])
[root@server0 ~]# ls  -lh  kernel-*.rpm                  //确認下載下傳結果
-rw-r--r--. 1 root root 29M 6月  11 2014 kernel-3.10.0-123.1.2.el7.x86_64.rpm           

步驟二:安裝新版核心

Linux系統支援安裝多個不同版本的核心,開機引導時可以選擇使用哪個版本。是以隻需要正常安裝新版核心即可。

1)檢視現有核心版本

[root@server0 ~]# uname  -r
3.10.0-123.el7.x86_64           

2)安裝新版本核心

[root@server0 ~]# rpm  -ivh  kernel-3.10.0-123.1.2.el7.x86_64.rpm
警告:kernel-3.10.0-123.1.2.el7.x86_64.rpm: 頭V3 RSA/SHA256 Signature, 密鑰 ID fd431d51: NOKEY
準備中...                          ################################# [100%]
正在更新/安裝...
   1:kernel-3.10.0-123.1.2.el7        ################################# [100%]
                                          //此處需耐心等待,千萬别強行終止
[root@server0 ~]#            

步驟三:确認新核心版本

1)重新開機系統

[root@server0 ~]# reboot
.. ..           

2)登入系統,确認使用的核心已是新版本

[root@server0 ~]# uname  -r
3.10.0-123.1.2.el7.x86_64           

3 案例3:配置靜态網絡位址

3.1 問題

本例要求為虛拟機 server 配置以下靜态位址參數:

主機名:server0.example.com
IP位址:172.25.0.11
子網路遮罩:255.255.255.0
預設網關:172.25.0.254
DNS伺服器:172.25.254.254           

3.2 方案

使用nmcli配置網絡連接配接時的基本操作,

檢視網絡連接配接、連接配接詳情:

nmcli con show
nmcli con show "連接配接名"           

修改網絡連接配接參數:

nmcli con modify "連接配接名" ipv4.method auto|manual
nmcli con modify "連接配接名" ipv4.addresses "IP位址/掩碼長度 [預設網關]" ipv4.dns DNS伺服器位址
nmcli con modify "連接配接名" connection.autoconnect yes|no           

3.3 步驟

步驟一:配置固定主機名

1)配置前, 檢查是否設定靜态主機名

[root@server0 ~]# hostnamectl 
   Static hostname: n/a                         //未設定靜态主機名
Transient hostname: server0.example.com           

2)設定為指定的主機名

[root@server0 ~]# vim  /etc/hostname              //建立主機名配置檔案
server0.example.com            

3)配置後,檢查結果

[root@server0 ~]# hostnamectl 
   Static hostname: server0.example.com          //已設定靜态主機名
         Icon name: computer
.. ..           

步驟二:配置靜态IP位址參數

1)檢視目前主機的網卡裝置、網絡連接配接

[root@server0 ~]# nmcli  connection   show
名稱         UUID                                  類型            裝置 
System eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  802-3-ethernet  eth0           

2)修改連接配接“System eth0”的配置

将配置方式指定為manual,指定IP位址、預設網關、DNS位址,并配置自動連接配接:

[root@server0 ~]# nmcli  connection  modify  "System eth0"  ipv4.method  manual  ipv4.addresses  "172.25.0.11/24  172.25.0.254"  ipv4.dns  172.25.254.254  connection.autoconnect  yes           

3)重新激活連接配接“System eth0”

通過up指令激活連接配接配置,必要時也可以先down再up:

[root@server0 ~]# nmcli  connection  up  "System eth0"          //激活連接配接
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)           

確定系統服務NetworkManager開機自啟:

[root@server0 ~]# systemctl  restart  NetworkManager
[root@server0 ~]# systemctl  enable  NetworkManager           

4)檢查修改結果,确認無誤

檢查IP位址:

[root@server0 ~]# ifconfig  eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.0.11  netmask 255.255.255.0  broadcast 172.25.0.255
        inet6 fe80::5054:ff:fe00:b  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:00:0b  txqueuelen 1000  (Ethernet)
        RX packets 1394  bytes 138855 (135.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 944  bytes 98495 (96.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0           

檢查預設網關位址:

[root@server0 ~]# route  -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.0.254    0.0.0.0         UG    1024   0        0 eth0
172.25.0.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0           

檢查DNS伺服器位址:

[root@server0 ~]# cat  /etc/resolv.conf 
# Generated by NetworkManager
search example.com
nameserver 172.25.254.254           

如果在使用nmcli修改網絡連接配接時并未指定ipv4.dns,也可以直接修改DNS用戶端配置檔案/etc/resolv.conf,確定添加有上述記錄即可。

步驟三:驗證網絡配置結果

通過ssh遠端通路server0:

[root@room9pc13 ~]# ssh -X [email protected]
Warning: Permanently added 'server0.example.com' (ECDSA) to the list of known hosts.
Last login: Fri Dec 23 19:00:12 2016 from 172.25.0.250
[root@server0 ~]# hostname                             //确認自己的主機名
server0.example.com           

在虛拟機server0上,可以查詢server0、desktop0、content等站點:

[root@server0 ~]# host  server0.example.com
server0.example.com has address 172.25.0.11
[root@server0 ~]# host  desktop0.example.com
desktop0.example.com has address 172.25.0.10
desktop0.example.com mail is handled by 10 smtp0.example.com.
[root@server0 ~]# host  content.example.com
content.example.com has address 172.25.254.254           

4 案例4:查找并處理檔案

4.1 問題

本例要求采用不少于兩種方法完成以下任務:

找出所有使用者 student 擁有的檔案
把它們拷貝到 /root/findfiles/ 檔案夾中           

4.2 步驟

步驟一:确認能找到指定的檔案

[root@server0 ~]# find  /  -user  student  -type f  
find: ‘/proc/1853/task/1853/fdinfo/6’: 沒有那個檔案或目錄
find: ‘/proc/1853/fdinfo/6’: 沒有那個檔案或目錄
/var/spool/mail/student
/home/student/.bash_logout
/home/student/.bash_profile
/home/student/.bashrc
/home/student/.ssh/authorized_keys
/home/student/.config/gnome-initial-setup-done
/home/student/.config/monitors.xml           

對于上述操作中出現的/proc資訊忽略即可。

步驟二:處理找到的檔案

1)建立目标檔案夾

[root@server0 ~]# mkdir  /root/findfiles           

2)拷貝找到的檔案到目标檔案夾

以下兩種方法任選一種:

[root@server0 ~]# find  /  -user  student  -type f  -exec  cp  -p  {}  /root/findfiles/  \;
.. ..
或者
[root@server0 ~]# \cp  -p  $(find  /  -user  student  -type f)  /root/findfiles/
.. ..           

3)确認拷貝結果

[root@server0 ~]# ls  -lhA  /root/findfiles/
總用量 24K
-rw-------. 1 student student 1.7K 7月  11 2014 authorized_keys
-rw-r--r--. 1 student student   18 1月  29 2014 .bash_logout
-rw-r--r--. 1 student student  193 1月  29 2014 .bash_profile
-rw-r--r--. 1 student student  231 1月  29 2014 .bashrc
-rw-r--r--. 1 student student    4 7月  11 2014 gnome-initial-setup-done
-rw-r--r--. 1 student student 1.5K 7月  11 2014 monitors.xml
-rw-rw----. 1 student mail       0 7月  11 2014 student           

5 案例5:查找并提取檔案内容

5.1 問題

本例要求在檔案/usr/share/dict/words中查找到所有包含字元串seismic的行,并滿足下列要求:

将找到的行按原文順序拷貝到 /root/wordlist 檔案中
檔案 /root/wordlist 不要包含空行,并且其中所有行的内容必須是 /usr/share/dict/words 檔案中原始行的準确副本           

5.2 步驟

[root@serverX ~]# grep  'seismic'  /usr/share/dict/words  >  /root/wordlist           
[root@server0 ~]# cat /root/wordlist 
anaseismic
antiseismic
aseismic
aseismicity
bradyseismic
.. ..           

繼續閱讀