运行apt install为何卡顿 (by quqi99)
作者:张华 发表于:2025-05-29
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明
问题
我今天临时用媳妇的mi laptop pro时总感觉’apt install’有点卡顿,之前用家里别的机器一直好好的啊。
一些背景
家里有两个路由器:
- 一级路由器(192.168.99.0/24)
- 二级路由器(192.168.99.213, 192.168.3.0/24), 为 huawei router,
因为想用到华为路由器方便禁止孩子看电视的功能,所以没有将它设置为桥接
区别有二:
- 之前用的家里别的机器都是接在一级路由器下的,而这次小米笔记本是没有网卡的,只能通过wifi接在二级华为路由器下
- 之前用的家里别的机器都是接在一级路由器下的所以有ipv6地址,这次小米机器是没有的 (原因见:IPv6来啦 中的 section ‘二级路由器如何获得IPv6’ - https://blog.csdn.net/quqi99/article/details/81389399 )
- 之前用的家里别的机器都是接在一级路由器下, 从外面可以通过IPv6地址访问家里机器,现在可以将在华为路由器中打开DMZ, 此时直接ssh hua@192.168.99.213即可, 若不想打开DMZ添加NAT规则也可。 总之仅在一级路由器上设置静态路由(192.168.3.0/24 via 192.168.99.213 dev br-lan metric 1)可以ping但不能ssh )
解决过程
我怀疑慢跟下列apt优先使用了IPv6地址有关。
hua@mi:~$ sudo apt update
...
0% [正在等待报头] [正在连接 security.ubuntu.com (2620:2d:4000:1::101)]
我明明已经通过下列命令设置了IPv4优先, 显然这个命令对apt不好使 (对wget/curl好不好使还没试)
echo 'precedence ::ffff:0:0/96 100' |sudo tee -a /etc/gai.confhua@mi:~$ getent ahosts security.ubuntu.com |head -n2
185.125.190.82 STREAM security.ubuntu.com
185.125.190.82 DGRAM
用下列workaround好使:
echo 'Acquire::ForceIPv4 "true";' |sudo tee /etc/apt/apt.conf.d/99force-ipv4
晚一些,得知是ubuntu archive服务端的dns今天有的挂了。暂时先换成清华源吧。
sudo apt clean && sudo rm -rf /var/lib/apt/lists/* && sudo apt update
hua@mi:~$ cat /etc/apt/sources.list.d/ubuntu.sources
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/
Suites: noble noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
#URIs: http://archive.ubuntu.com/ubuntu/Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main universe multiverse restricted
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
使用apt-cacher-ng
之前apt-cacher-ng也因这问题也表现的莫名其妙地,现在OK了.
安装它:
sudo apt install apt-cacher-ng -y
echo 'PassThroughPattern: .*' |sudo tee -a /etc/apt-cacher-ng/acng.conf
用它:
echo 'Acquire::http::Proxy "Acquire::http::Proxy "http://127.0.0.1:3142"";' | sudo tee /etc/apt/apt.conf.d/01acng
echo 'Acquire::HTTP::Proxy::ppa.launchpadcontent.net "DIRECT";' |sudo tee -a /etc/apt/apt.conf.d/01acng
遇到古怪问题时:
sudo systemctl stop apt-cacher-ng.service && sudo rm -rf /var/cache/apt-cacher-ng/* && sudo systemctl start apt-cacher-ng.service
另已知的不work的一种情况是因为MTU
另一已知问题是关于swap:
#to avoid 'Cannot allocate memory' by apt-cacher-ng
#sudo fallocate -l 8G /swapfile
#sudo mkswap /swapfile && sudo swapon /swapfile
#echo '/swapfile none swap sw 0 0' |sudo tee -a /etc/fstab
#sudo swapon --show && free -h
#if failed to create the file in /tmp/, that's because root partiton is full
/swapfile none swap sw 0 0
autofs问题
今天在改用小笔记本后,还遇到另一问题,就是用了十几年用autofs连nas的方法忽然感觉表现莫名其妙的了。
之前遇到过下列问题,直接 sudo apt purge sssd 就好了。这次发现还要: sudo systemctl stop sssd
hua@mi:~$ sudo automount -f -v
Starting automounter version 5.1.9, master map /etc/auto.master
using kernel protocol version 5.05
reading dir master /etc/auto.master.d
reading sss master auto.master
setautomntent: lookup(sss): setautomountent: entry for map auto.master not found
reading sss map auto.direct
setautomntent: lookup(sss): setautomountent: entry for map auto.direct not found
而且今天还有其他问题,将sssd删除之后,autofs可以正常启动了,但访问nas上的文件时hang在那。于是改成(sudo mount -t nfs -o vers=4,proto=tcp,nolock nas:/Public /mnt/)调试也不能运行,改成vers=3就可以mount了但仍然访问文件时hangout在那。后来重启nas才得到解决。另外,去掉mi laptop上的usb外置网卡后打开文件刚开始会有些慢之后能正常使用。
Appendix - DDNS script
之前实现DDNS的一个脚本备忘如下:
$ cat myddns.sh
#!/bin/sh
# dig +tcp gw.xxx.publicvm.com @101.6.6.6 -p5353 AAAA
API_KEY="xxx"
IP=$(ip -6 addr show dev br-eth0 | grep 'scope global dynamic' | awk '{split($2, a, "/"); print a[1], a[2]}' | sort -k2 -n | tail -n1 | awk '{print $1}')
if [ ! -e ./previous_ip.txt ]; thenecho 'touch file'echo '0' > ./previous_ip.txt
fi
if [ -s ./previous_ip.txt ]; thenPREVIOUS_IP=$(cat ./previous_ip.txt)echo PREVIOUS_IP=$PREVIOUS_IPif [ "$IP" != "$PREVIOUS_IP" ]; then#for dnsexit.com, no double quotations hereecho https://api.dnsexit.com/dns/ud/?apikey=${API_KEY} -d host=gw.xxx.publicvm.com -d ip=${IP}curl https://api.dnsexit.com/dns/ud/?apikey=${API_KEY} -d host=gw.xxx.publicvm.com -d ip=${IP}sleep 1fi
elseecho 'wring IP to previous_ip.txt'echo "$IP" > ./previous_ip.txt
fi