Linux

Ubuntu Linux 設定 apt 使用 proxy 更新套件教學與範例

介紹如何在 Ubuntu Linux 中設定 apt 透過代理伺服器(proxy)更新系統套件。

SOCKS 代理伺服器

如果想要讓另外一台 Linux 伺服器作為臨時的代理伺服器使用,可以使用以下 SSH 指令建立 SOCKS 代理伺服器:

# 建立 SOCKS 代理伺服器
ssh -D 9000 myuser@remote.server.com

執行上面這行 SSH 指令之後,我們就可以透過 localhost:9000 來使用這個 SOCKS 代理伺服器了。

接著在 /etc/apt/apt.conf.d 目錄中新增一個 12proxy 設定檔,填入以下 SOCKS 代理伺服器設定:

# SOCKS 代理伺服器設定
Acquire::http::proxy "socks5h://localhost:9000";

這樣就完成 apt 的 SOCKS 代理伺服器的設定了,之後執行 apt 指令時,就會自動透過 remote.server.com 來下載更新的資料。

HTTP 代理伺服器

若要讓 apt 透過其他的 HTTP 代理伺服器下載資料,可以將 /etc/apt/apt.conf.d/12proxy 設定檔改為以下這樣的 HTTP 代理伺服器設定:

Acquire::http::Proxy "http://your.proxy.com:3128";
Acquire::https::Proxy "http://your.proxy.com:3128";
Acquire::ftp::Proxy "http://your.proxy.com:3128";

手動匯入 GPG 金鑰

如果遇到 apt-key 無法透過代理伺服器匯入金鑰的情況,可以採用手動匯入的方式來解決,以下次操作步驟。
Step 1
Ubuntu 的金鑰伺服器上輸入金鑰的 ID(記得開頭要加上 0x),例如 0xABF5BD827BD9BF62,查詢自己需要的金鑰。
Step 2
在查詢結果中點選要下載的金鑰,並取得金鑰的內容。以 0xABF5BD827BD9BF62 這個金鑰來說,網址為 https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xabf5bd827bd9bf62,而內容則為:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Comment: Hostname:
Version: Hockeypuck ~unreleased

xsBNBE5OMmIBCAD+FPYKGriGGf7NqwKfWC83cBV01gabgVWQmZbMcFzeW+hMsgxH
W6iimD0RsfZ9oEbfJCPG0CRSZ7ppq5pKamYs2+EJ8Q2ysOFHHwpGrA2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iCBqAPoKIan65zq9sKF
waiyyYOU5mbemH39E9l4fNWulo/ovr9w754IF89D3FhfVl3hx71Fkq7rguqCkTel
gqreHFJFQtw0g/x56fihF+VE0kfdQcrYoLnO3OnrMVyQSZym30wx4guOuN0vZcrW
EKUvfOpBvROxmeuqykqEz2OpdbmnX87UcmDRBtnmoXCSLX607ymEbzq/drMkGput
iVXjkXXSRyhtqVjLeTeg6ATtz4Qnwjpdw75UGCmrYkQRnJYvo6cty8uS7QBtjLvk
xUei85xgukr0lEGBlpKFNRlfhxl0h17kngcsGZikupvoHMI+9tZy0R0nZMdqaKMw
1715J3S3tDmek4dhkMA87rEDWtZJtYI0FGQ4/Opw3qJrMLN1TPMT7xL9gNRLt2LT
kJjWoa8hZu19/HLagdKzqP+6wm4ASuJPwQ==
=Gqvk
-----END PGP PUBLIC KEY BLOCK-----

將金鑰的內容下載後儲存至檔案中,檔案名稱可以自由命名,此處我們將其命名為 pubkey.gpg
Step 3
使用以下指定匯入下載的公開金鑰 pubkey.pgp

# 匯入公開金鑰
sudo apt-key add pubkey.pgp

參考資料

Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

1 年 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

1 年 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

1 年 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

1 年 ago