2014年10月11日 星期六

CentOS 7 yum 使用&鏡像站設定

與 Debian 不同,CentOS 安裝(更新)套件的指令如下:
# yum install [套件名]
# yum remove [套件名]
# yum update [套件名]
# yum update (全部套件)
存放 repo 設定檔在 /etc/yum.repo.d/ 的目錄下。

到設定檔的目錄並觀看裡面有哪些檔案:
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
total 16
-rw-r--r--. 1 root root 1664 Aug 23 00:21 CentOS-Base.repo
-rw-r--r--. 1 root root  649 Aug 23 00:21 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 1331 Aug 23 00:21 CentOS-Sources.repo
-rw-r--r--. 1 root root  156 Aug 23 00:21 CentOS-Vault.repo
這些是安裝好系統後內建的設定檔,我們可以注意到鏡像站的設定檔都是 .repo 結尾的
所以在建立我們自己的設定檔時,要記得命名的規則,不然 yum 是不會抓到設定檔的。

再來介紹一下設定檔的語法:

[名字]
name=同上即可  
baseurl=鏡像站路徑
enabled=將這個鏡像站清單打開
gpgcheck=不使用gpg key


接下來請建立一個 old 資料夾把舊的檔案丟進去,指令如下:
[root@localhost yum.repos.d]# mkdir old
[root@localhost yum.repos.d]# mv CentOS-* old/

鏡像站我建議使用國家高速網路( twaren )的 ftp 站:

ftp://ftp.twaren.net/Linux/CentOS/7/os/x86_64/
ftp://ftp.twaren.net/Linux/CentOS/7/updates/x86_64/

在上述兩個路徑中,都會找的一個叫做 repodata 的資料夾,repo檔的路徑必須指到這個檔案才能抓到套件清單。


建立新的 repo 檔:

[root@localhost yum.repos.d]# vim mybase.repo myupdate.repo
兩個檔案的內容如下:
1. mybase.repo
[root@localhost yum.repos.d]# cat mybase.repo 
[mybase]
name=mybase
baseurl=ftp://ftp.twaren.net/Linux/CentOS/7/os/x86_64/
enabled=1
gpgcheck=0
2. myupdate.repo
[root@localhost yum.repos.d]# cat myupdate.repo 
[myupdate]
name=myupdate
baseurl=ftp://ftp.twaren.net/Linux/CentOS/7/updates/x86_64/
enabled=1
gpgcheck=0
最後請下下面的指令來確認更新是否成功:
[root@localhost yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras google-chrome mybase myupdate updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum update
Loaded plugins: fastestmirror, langpacks
base                                                     | 3.6 kB     00:00     
extras                                                   | 3.4 kB     00:00     
google-chrome                                            |  951 B     00:00     
mybase                                                   | 3.6 kB     00:00     
myupdate                                                 | 3.4 kB     00:00     
updates                                                  | 3.4 kB     00:00     
base/7/x86_64/primary_db       FAILED                                          
http://ftp.twaren.net/Linux/CentOS/7.0.1406/os/x86_64/repodata/efa521576f53587de26616ea1e45f902993abcd9d67e707b8993b5f29bd15956-primary.sqlite.bz2: [Errno 14] HTTP Error 403 - Forbidden
Trying other mirror.
(1/7): base/7/x86_64/group_gz                              | 157 kB   00:00     
(2/7): extras/7/x86_64/primary_db                          |  26 kB   00:00     
(3/7): base/7/x86_64/primary_db                            | 4.9 MB   00:00     
(4/7): updates/7/x86_64/primary_db                         | 3.6 MB   00:00     
(5/7): mybase/group_gz                                     | 157 kB   00:01     
(6/7): myupdate/primary_db                                 | 3.6 MB   00:02     
(7/7): mybase/primary_db                                   | 4.9 MB   00:02     
google-chrome/primary                                      | 1.9 kB   00:00     
Determining fastest mirrors
 * base: ftp.stust.edu.tw
 * extras: ftp.stust.edu.tw
 * updates: ftp.stust.edu.tw
google-chrome                                                               3/3
No packages marked for update