Ubuntu 22.04 LTS执行sudo apt-get update命令失败求助
Ubuntu 22.04 LTS执行sudo apt-get update命令失败求助
看起来你在执行sudo apt-get update时碰到了网络连接问题,错误提示全都是无法连接到Ubuntu镜像源的IPv6地址,显示“Network is unreachable”(网络不可达)。先把你遇到的错误信息贴出来方便参考:
ubuntu@ip-10-0-2-185:~$ sudo apt-get update Cannot initiate the connection to us-east-1.ec2.archive.ubuntu.com:8080 (2600:1f18:5c55:4c02:b55:260f:8030:b812). - connect (101: Network is unreachable) Cannot initiate the connection to us-east-1.ec2.archive.ubuntu.com:8080 (2600:1f18:5c55:4c02:7669:c120:50a2:9af4). - connect (101: Network is unreachable) Cannot initiate the connection to us-east-1.ec2.archive.ubuntu.com:8080 (2600:1f18:5c55:4c03:edb9:571f:f204:4ac8). - connect (101: Network is unreachable) Cannot initiate the connection to us-east-1.ec2.archive.ubuntu.com:8080 (2600:1f18:5c55:4c04:b8fe:122c:bb55:6b84). - connect (101: Network is unreachable) Cannot initiate the connection to us-east-1.ec2.archive.ubuntu.com:8080 (2600:1f18:5c55:4c05:17c8:ad29:43bf:eb48). - connect (101: Network is unreachable) Cannot initiate the connection to us-east-1.ec2.archive.ubuntu.com:8080 (2600:1f18:5c55:4c00:2637:a32b:ccef:30d5). - conn...
我整理了几个实用的解决思路,都是社区里常用或者我自己试过有效的方法,你可以挨个试试:
方法一:临时禁用IPv6执行更新
大概率是你的网络环境不支持IPv6,导致apt优先尝试IPv6连接失败。可以临时关掉IPv6再跑命令:
- 执行以下命令临时禁用IPv6:
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
- 再重新执行更新:
sudo apt-get update
- 如果成功了,想恢复IPv6的话,执行:
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0 sudo sysctl -w net.ipv6.conf.default.disable_ipv6=0
方法二:配置apt强制使用IPv4
如果临时禁用IPv6有用,你可以让apt默认用IPv4,省得以后每次手动切换:
- 创建并编辑apt的配置文件:
sudo nano /etc/apt/apt.conf.d/99force-ipv4
- 在文件里添加一行内容:
Acquire::ForceIPv4 "true";
- 保存退出(按Ctrl+O回车,再Ctrl+X),然后执行
sudo apt-get update试试。
方法三:更换国内Ubuntu镜像源
有时候AWS的EC2镜像源访问不稳定,换成国内镜像源会更顺畅,比如阿里云或者清华源:
- 先备份原来的源文件,防止出错:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
- 编辑源文件:
sudo nano /etc/apt/sources.list
- 把里面的内容替换成阿里云的22.04 LTS源:
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
- 保存退出后执行
sudo apt-get update。
方法四:检查EC2安全组和服务器防火墙
因为是在EC2实例上,还要确认安全组和防火墙的出站规则:
- 检查服务器本身的ufw防火墙状态:
sudo ufw status
如果有规则限制了8080端口的出站,需要调整放行。
2. 登录AWS控制台,查看对应EC2实例的安全组,确保出站规则允许到0.0.0.0/0和::/0的8080端口流量。
你可以先从方法一开始试,操作最简单,看看能不能解决问题,不行再试后面的方法。
备注:内容来源于stack exchange,提问作者user1841636
相关产品推荐
相关产品推荐

