一、查看當前源
在終端中輸入以下命令查看當前源:
cat /etc/apt/sources.list
在Ubuntu中,默認源是官方源。輸出結果中以「#」字元開頭的行是注釋,不需要管它們。
deb http://cn.archive.ubuntu.com/ubuntu/ bionic main restricted
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
deb http://cn.archive.ubuntu.com/ubuntu/ bionic universe
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates universe
deb http://cn.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://security.ubuntu.com/ubuntu bionic-security universe
deb http://security.ubuntu.com/ubuntu bionic-security multiverse
二、備份當前源
在修改前,先備份一下當前的源文件:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
這樣可以在修改出現問題時,恢復到原來的狀態。
三、選擇合適的源
阿里雲會定時從Ubuntu官方源同步數據,然後提供自己的Ubuntu鏡像。我們可以根據自己所在的區域選擇合適的源,以加快下載速度。建議選擇與自己所在地區相近的源。比如在中國大陸地區,選擇cn的源。
阿里雲的Ubuntu源地址為:https://opsx.alibaba.com/mirror。
# 阿里雲Ubuntu源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
複製上面的內容,在終端中輸入以下命令將其粘貼到源文件中:
sudo vim /etc/apt/sources.list
這裡以vim編輯器為例。如果你不熟悉vim,可以使用其他編輯器,比如nano。在終端中輸入以下命令打開nano編輯器:
sudo nano /etc/apt/sources.list
將阿里雲源的內容粘貼進去,保存並退出編輯器。這樣就已經完成了源的修改。
四、更新源信息
剛剛我們只是更改了源文件,並沒有實際更新源信息。運行以下命令,更新源信息:
sudo apt-get update
這個命令會檢查源文件,看看有沒有修改,並更新軟體包列表。
五、測試源是否生效
我們可以進行一個小測試,看看是否成功更換源。在終端中輸入以下命令:
sudo apt-get install cowsay
cowsay是一個終端小工具,可以讓mac電腦說話。如果阿里雲源生效,這個小工具應該可以正常安裝。如果沒有生效,會提示找不到軟體包。如果出現這種情況,請先檢查你的源文件是否按照規範進行修改。
六、小結
以上就是在Ubuntu中更換阿里雲源的方法。我們可以根據自己的需求,選擇不同的源,以提高下載效率。如果嘗試多次後,還是無法完成源的更新,可以嘗試更換其他鏡像。
原創文章,作者:HLQXI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/366219.html