1. 程式人生 > >【Ubuntu】搭建環境安裝軟體前apt-get update失敗,怎麼辦?

【Ubuntu】搭建環境安裝軟體前apt-get update失敗,怎麼辦?

**我一般更新或安裝軟體使用的都是root許可權,自己的測試虛擬機器無所謂,如果你使用伺服器請慎用root許可權。**

命令列下輸入

sudo apt-get update
報了一堆錯誤:
Err http://cn.archive.ubuntu.com trusty InRelease
Err http://cn.archive.ubuntu.com trusty-updates InRelease
Err http://cn.archive.ubuntu.com trusty-backports InRelease
Err http://cn.archive.ubuntu.com trusty Release.gpg     
W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/trusty/InRelease

根據提示的錯誤顯示,可能是資源有問題,應該是訪問被阻止,沒有梯子。

推薦幾個方案:

方案一:修改Ubuntu中/etc/hosts檔案,新增可翻牆hosts(資源可以google或百度一下,這就不介紹了)

方案二:修改/etc/apt/source.list檔案。將源改成163的或者阿里的:備份系統中的源列表,開啟終端,輸入:

sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup

/etc/apt/source.list檔案中新增,下面內容,但彆著急先看後面
deb http://mirrors.163.com/ubuntu/ vivid main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ vivid-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ vivid-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ vivid-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ vivid-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ vivid main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ vivid-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ vivid-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ vivid-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ vivid-backports main restricted universe multiverse

可是這個source.list檔案是隻讀許可權,無法進行修改。

(1.桌面版sudo gedit /etc/apt/source.list修改,但是gedit 指令沒有裝,使用sudo apt-get install gedit又安裝不了。

 2.伺服器版vim /etc/apt/source.list修改,又沒有許可權

方案三:修改DNS(可行)

首先檢視/etc/resolv.conf

cat /etc/resolv.conf

隨後命令列執行:

sudo vi /etc/resolvconf/resolv.conf.d/base

在裡面插入(如果使用終端,可以直接複製貼上):

nameserver 8.8.8.8
nameserver 8.8.4.4
修改好,【!wq】退出儲存,

然後執行:
sudo resolvconf -u
執行結束後,檢視/etc/resolv.conf檔案,最下面就多了2行:
cat /etc/resolv.conf
結果顯示:
#這塊顯示原來的資訊
nameserver 8.8.8.8
nameserver 8.8.4.4
此時執行:
sudo apt-get update

可以執行,然後可以使用sudo apt-get install **指令了。

=================================================================

問題又來了:下載倉庫資訊失敗


實際上,有兩種型別的下載倉庫資訊失敗錯誤。如果你的錯誤是這樣的:

W:Failed to fetch bzip2:/var/lib/apt/lists/partial/in.archive.ubuntu.comubuntudistsoneiricrestrictedbinary-i386Packages Hash Sum mismatch,
W:Failed to fetch bzip2:/var/lib/apt/lists/partial/in.archive.ubuntu.comubuntudistsoneiricmultiversebinary-i386Packages Hash Sum mismatch,
E:Some index files failed to download. They have been ignored, or old ones used instead

那麼,你可以用以下命令修復:

sudo rm -rf /var/lib/apt/lists/*
sudo apt-get update

=====================================================================

問題又來了:部分更新錯誤


在終端中執行更新會出現部分更新錯誤:

Not all updates can be installed
Run a partial upgrade, to install as many updates as possible

在終端中執行以下命令來修復該錯誤:

sudo apt-get install -f

=====================================================================

同方案三:

當執行apt-get update後出現如下錯誤時: 
出現了某些故障(-5 - 沒有與主機名關聯的地址) 

E: Some index files failed to download, they have been ignored, or old ones used instead. 

可以將目錄下/var/lib/apt/lists/partial/所有的檔案清掉(感覺不靠譜),再次執行apt-get update即可!

 
如果還有問題,請檢查resolvconf服務的配置檔案: 

/etc/resolvconf/resolv.conf.d/base

很可能是DNS的問題,可以使用公共的dns地址(操作參考方案三): 
nameserver 8.8.8.8 
nameserver 4.2.2.2


Ubuntu12.04自帶源在大陸不好。可以更換其他源: 
如果是其它ubuntu版本,只需把precise改為相應的版本的名字。hardy(8.04),lucid(10.04),maverick(10.10),oneiric(11.10),quantal(12.10),precise(12.04) trusty(14.04) 

在/etc/apt/sources.list新增的內容有以下幾種:

163

deb http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse 
deb http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse 
deb http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse 
deb http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse 
deb http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse 
deb-src http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse 
deb-src http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse 
deb-src http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse 
deb-src http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse 
deb-src http://mirrors.163.com/ubuntu/precise-backports main restricted universe multiverse

sohu
deb http://mirrors.sohu.com/ubuntu/ precise main restricted universe multiverse 
deb http://mirrors.sohu.com/ubuntu/ precise-security main restricted universe multiverse 
deb http://mirrors.sohu.com/ubuntu/ precise-updates main restricted universe multiverse 
deb http://mirrors.sohu.com/ubuntu/ precise-proposed main restricted universe multiverse 
deb http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse 
deb-src http://mirrors.sohu.com/ubuntu/ precise main restricted universe multiverse 
deb-src http://mirrors.sohu.com/ubuntu/ precisec-security main restricted universe multiverse 
deb-src http://mirrors.sohu.com/ubuntu/ precise-updates main restricted universe multiverse 
deb-src http://mirrors.sohu.com/ubuntu/ precise-proposed main restricted universe multiverse 
deb-src http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse

ustc
deb http://mirrors.ustc.edu.cn/ubuntu/ precise main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-security main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-security main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse


備註:文中方案一、三,嘗試過可行,解決方案來自網路。