Ubuntu apt下载缓慢的解决办法


一. apt软件源

由于apt-get默认的软件源位于国外,如欧洲等,因此默认情况下使用apt安装软件往往下载速度极慢。

在Ubuntu和所有其他基于Debian的发行版中,apt软件存储库在/etc/apt/sources.list文件中或者在/etc/apt/sources.list.d/目录下使用单独文件定义。国内用户通常需要对其进行调整,将软件源修改为国内的服务器,这是一个基本操作,本文不再赘述。

二. apt第三方软件源

apt软件源当然不可能包括完整的软件信息,有时候我们需要使用第三方软件源。例如docker、nvidia显卡驱动等,都属于第三方软件源。

除了手动编辑/etc/apt/sources.list文件,或者在/etc/apt/sources.list.d/目录下使用单独文件增加第三方软件源的信息之外,我们通常使用add-apt-repository命令来管理第三方软件源。

add-apt-repository是Python脚本,用于将APT存储库添加到/etc/apt/sources.list中,或者作为单个文件添加到/etc/apt/sources.list.d目录中。

三. apt第三方软件源下载缓慢的问题


当第三方软件源位于国外等网络情况不理想的地方,而又找不到网络条件好的镜像软件源作为替代时,apt安装软件会变得非常缓慢。

1. 网络代理能解决问题吗?


当第三方软件源网络条件差,甚至其中的软件下载地址被qiang时,首先想到的便是使用网络代理。

配置网络代理:

export http_proxy=http://xx.108.121.xx:9707
export https_proxy=http://xx.108.121.xx:9707
export HTTP_PROXY=http://xx.108.121.xx:9707
export HTTPS_PROXY=http://xx.108.121.xx:9707

然而,配置网络代理后,尽管wget、curl等执行正常,而apt却完全挂掉了。

1.1. Could not handshake: Error in the pull function. [IP: xx.108.121.xx 9707]

以安装nvidia显卡驱动为例:

apt-get install nvidia-driver-545

执行出错:

E: Failed to fetch https://ppa.launchpadcontent.net/graphics-drivers/ppa/ubuntu/pool/main/n/nvidia-graphics-drivers-545/nvidia-driver-545_545.23.06-0ubuntu0%7egpu22.04.3_amd64.deb  Could not handshake: Error in the pull function. [IP: xx.108.121.xx 9707]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

1.2. 403  Forbidden [IP: xx.108.121.xx 9707]


已安装nginx为例:

Err:1 http://mirrors.tencentyun.com/ubuntu jammy-updates/main amd64 nginx amd64 1.18.0-6ubuntu14.4
  403  Forbidden [IP: xx.108.121.xx 9707]
E: Failed to fetch http://mirrors.tencentyun.com/ubuntu/pool/main/n/nginx/nginx_1.18.0-6ubuntu14.4_amd64.deb  403  Forbidden [IP: xx.108.121.xx 9707]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

总之,使用网络代理之后,apt变成残废了,一会403,一会SSL失败。

2. “离线”安装


此处所谓的离线安装,不是真正的离线安装。在使用apt安装某个软件时,apt下载的可能不是单一的软件包,而是一系列的软件包,而且apt还能协助处理软件依赖、升级替换等一系列麻烦事,因此,我们不希望脱离apt来执行真正的离线安装。

这里apt的“离线”安装是指,我们提前借助其他网络条件,将apt在安装软件时需要的软件包提前下载好,然后放置到apt的软件包缓存目录中。这样,apt在安装软件时就能跳过下载过程,从而避开网络问题。为了实现这一点,需要满足两个条件。

2.1. apt软件包缓存位置


/var/cache/apt/archives是apt-get命令在系统上存储已下载的软件包文件的目录。当您使用apt-get命令下载软件包时,它会将下载的软件包文件存储在这个目录中,以便以后安装或升级使用。您可以使用这些软件包文件来进行离线安装、备份或共享给其他系统。这些软件包文件通常以.deb扩展名结尾。

如果需要清除该目录中的已下载软件包文件,您可以使用以下命令:

apt-get clean

这将从/var/cache/apt/archives目录中删除所有被apt-get命令下载的软件包文件。

2.2. apt软件包的下载地址


使用apt安装软件时,apt会打印一些软件包的下载信息,但这些信息并不完整,我们无法提取到软件包的具体地址。

以安装nginx为例:

# apt-get install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libevent-pthreads-2.1-7 libflashrom1 libftdi1-2 libopts25 sntp
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 40 not upgraded.
Need to get 3,872 B of archives.
After this operation, 50.2 kB of additional disk space will be used.
Get:1 http://mirrors.tencentyun.com/ubuntu jammy-updates/main amd64 nginx amd64 1.18.0-6ubuntu14.4 [3,872 B]
Fetched 3,872 B in 0s (45.9 kB/s) 
Selecting previously unselected package nginx.
(Reading database ... 141544 files and directories currently installed.)
Preparing to unpack .../nginx_1.18.0-6ubuntu14.4_amd64.deb ...
Unpacking nginx (1.18.0-6ubuntu14.4) ...
Setting up nginx (1.18.0-6ubuntu14.4) ...
Scanning processes...                                                                                                                                                                              
Scanning linux images...

在apt的输出内容中我们可以看到nginx软件包的一些网络地址信息,但这并不是软件包的具体下载地址:

Get:1 http://mirrors.tencentyun.com/ubuntu jammy-updates/main amd64 nginx amd64 1.18.0-6ubuntu14.4 [3,872 B]

只有在下载出现某些错误的情况下,apt才会将软件包的下载地址打印出来,而很多时候,我们是没工夫等待apt报错的。例如,apt以每秒1kb的速度下载100mb的文件,等待报错或者下载完成都是很耗时的。

E: Failed to fetch https://ppa.launchpadcontent.net/graphics-drivers/ppa/ubuntu/pool/main/n/nvidia-graphics-drivers-545/libnvidia-compute-545_545.23.06-0ubuntu0%7egpu22.04.3_amd64.deb  Connection timed out

当然,这里可以取巧,正如前文所言,网络代理会导致apt执行报错,并且是立即报错,而报错信息中将会包含软件包的实际下载地址,简直机智过人!

这里将通过apt的正常途径来打印软件包下载地址。

2.2.1. 通过apt-get -o Debug::Acquire::http=true命令打印软件包下载地址


apt-get -o Debug::Acquire::http=true这个命令设置了apt-get在执行HTTP下载时显示调试信息。这些调试信息将包括HTTP请求、响应头等。您可以在执行其他apt-get命令时添加此调试选项来查看相应的调试信息。

例如,使用apt-get -o Debug::Acquire::http=true update命令,将显示更新存储库时的HTTP请求和响应的详细信息。您可以根据需要将这个调试选项与其他apt-get命令和选项一起使用来自定义apt-get的行为和查看调试信息。还是以安装nginx为例:

# apt-get -o Debug::Acquire::http=true install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libevent-pthreads-2.1-7 libflashrom1 libftdi1-2 libopts25 sntp
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 40 not upgraded.
Need to get 3,872 B of archives.
After this operation, 50.2 kB of additional disk space will be used.
0% [Working]GET /ubuntu/pool/main/n/nginx/nginx_1.18.0-6ubuntu14.4_amd64.deb HTTP/1.1
Host: mirrors.tencentyun.com
User-Agent: Debian APT-HTTP/1.3 (2.4.10) non-interactive

Answer for: http://mirrors.tencentyun.com/ubuntu/pool/main/n/nginx/nginx_1.18.0-6ubuntu14.4_amd64.deb
HTTP/1.1 200 OK
Server: openresty/1.16.1.1
Date: Tue, 31 Oct 2023 02:34:08 GMT
Content-Type: application/octet-stream
Content-Length: 3872
Connection: keep-alive
Last-Modified: Wed, 28 Jun 2023 08:32:49 GMT
Accept-Ranges: bytes

Get:1 http://mirrors.tencentyun.com/ubuntu jammy-updates/main amd64 nginx amd64 1.18.0-6ubuntu14.4 [3,872 B]
Fetched 3,872 B in 0s (44.6 kB/s) 
Selecting previously unselected package nginx.
(Reading database ... 141544 files and directories currently installed.)
Preparing to unpack .../nginx_1.18.0-6ubuntu14.4_amd64.deb ...
Unpacking nginx (1.18.0-6ubuntu14.4) ...
Setting up nginx (1.18.0-6ubuntu14.4) ...
Scanning processes...                                                                                                                                                                              
Scanning linux images...

通过调试信息就可以看到nginx软件包的下载地址了:

Answer for: http://mirrors.tencentyun.com/ubuntu/pool/main/n/nginx/nginx_1.18.0-6ubuntu14.4_amd64.deb

2.3. apt“离线”安装步骤


至此,针对apt下载缓慢的情况,我们可以通过以下步骤来解决。

a. 借助apt-get -o Debug::Acquire::http=true命令打印软件包下载地址
借助apt-get -o Debug::Acquire::http=true将下载缓慢的软件包的实际下载地址打印出来后,中断apt执行过程。

b. 借助其他网络条件下载软件包
借助其他网络条件,例如位于海外的服务器,去下载需要的软件包。

c. 将下载好的软件包放置到apt软件包缓存目录/var/cache/apt/archives

d. 重复执行以上步骤
重复执行apt-get -o Debug::Acquire::http=true install,如果遇到有下载缓慢的软件包,重复以上步骤,直到所有软件包准备完毕。这时,再次执行apt-get install,apt将能够跳过软件包的下载过程,执行实际的安装操作。
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决Ubuntu网速慢的方法有多种。首先,可以通过检查网络硬件的详细信息来确定网络连接的情况,可以使用以下命令:sudo lshw -numeric -class network、sudo ip addr show、sudo ip route show、sudo traceroute forum.ubuntu.org.cn 。 其次,如果网速仍然很慢,尤其是网页加载速度缓慢,可能是DNS的问题。可以通过修改配置将本地设置为一个DNS解析服务器来解决这个问题 。具体操作如下: 1. 安装dnsmasq,用于做DNS缓存:sudo apt-get install dnsmasq 。 2. 编辑dnsmasq的配置文件:sudo vi /etc/dnsmasq.conf,在文件中找到"resolv-file=/etc/resolv.dnsmasq.conf"这一行,并修改为该行。 3. 确保/etc/resolv.conf文件中的DNS信息正确,然后执行sudo cp /etc/resolv.conf /etc/resolv.dnsmasq.conf。 4. 将/etc/resolv.conf文件中的DNS改为127.0.0.1:nameserver 127.0.0.1。 5. 如果使用adsl上网,需要修改/etc/ppp/peers/dsl-provider文件,注释掉"usepeerdns"这一行,以免它覆盖/etc/resolv.conf文件的内容。 6. 重启dnsmasq服务:sudo /etc/init.d/dnsmasq restart。 另外,如果遇到traceroute指令报错,请先安装traceroute:sudo apt-get install traceroute 。如果显示软件包被引用...的错误,请修改apt源,使用国内源的下载速度会更快。 综上所述,以上是解决Ubuntu网速慢的一些常用方法。希望对您有帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值