ubuntu apt-get update 失败解决。安装新的ubuntu时出现 mirror连接不上

当运行apt-get update后出现如下错误时:
E: Some index files failed to download, they have been ignored, or old ones used instead.

可以将目录下/var/lib/apt/lists/partial/所有的文件清掉,再次运行apt-get update即可!自带源在大陆不好。


出现以下错误:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /etc/apt$ sudo apt-get update  
  2. Err http://security.ubuntu.com precise-security InRelease  
  3.     
  4. Err http://security.ubuntu.com precise-security Release.gpg  
  5.   Temporary failure resolving 'security.ubuntu.com'  
  6. Err http://cn.archive.ubuntu.com precise InRelease  
  7.     
  8. Err http://cn.archive.ubuntu.com precise-updates InRelease  
  9.     
  10. Err http://cn.archive.ubuntu.com precise-backports InRelease  
  11.     
  12. Err http://cn.archive.ubuntu.com precise Release.gpg  
  13.   Temporary failure resolving 'cn.archive.ubuntu.com'  
  14. Err http://cn.archive.ubuntu.com precise-updates Release.gpg  
  15.   Temporary failure resolving 'cn.archive.ubuntu.com'  
  16. Err http://cn.archive.ubuntu.com precise-backports Release.gpg  
  17.   Temporary failure resolving 'cn.archive.ubuntu.com'  
  18. Reading package lists... Done  
  19. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/InRelease    
  20.   
  21. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-updates/InRelease    
  22.   
  23. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/InRelease    
  24.   
  25. W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/InRelease    
  26.   
  27. W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/precise-security/Release.gpg  Temporary failure resolving 'security.ubuntu.com'  
  28.   
  29. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'  
  30.   
  31. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-updates/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'  
  32.   
  33. W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/Release.gpg  Temporary failure resolving 'cn.archive.ubuntu.com'  
  34.   
  35. W: Some index files failed to download. They have been ignored, or old ones used instead.  
  36. xinju@xinjuOffice:/etc/apt$ cd ../network  


阿里源:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. deb http://mirrors.aliyun.com/ubuntu/ raring main restricted universe multiverse  
  2. deb http://mirrors.aliyun.com/ubuntu/ raring-security main restricted universe multiverse  
  3. deb http://mirrors.aliyun.com/ubuntu/ raring-updates main restricted universe multiverse  
  4. deb http://mirrors.aliyun.com/ubuntu/ raring-proposed main restricted universe multiverse  
  5. deb http://mirrors.aliyun.com/ubuntu/ raring-backports main restricted universe multiverse  
  6. deb-src http://mirrors.aliyun.com/ubuntu/ raring main restricted universe multiverse  
  7. deb-src http://mirrors.aliyun.com/ubuntu/ raring-security main restricted universe multiverse  
  8. deb-src http://mirrors.aliyun.com/ubuntu/ raring-updates main restricted universe multiverse  
  9. deb-src http://mirrors.aliyun.com/ubuntu/ raring-proposed main restricted universe multiverse  
  10. deb-src http://mirrors.aliyun.com/ubuntu/ raring-backports main restricted universe multiverse  

修改ip:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # This file describes the network interfaces available on your system  
  2. # and how to activate them. For more information, see interfaces(5).  
  3.   
  4. # The loopback network interface  
  5. auto lo  
  6. iface lo inet loopback  
  7.   
  8. # The primary network interfacea  
  9. auto eth0  
  10. iface eth0 inet static  
  11. address 192.168.2.170  
  12. netmask 255.255.255.0  
  13. broadcast 192.168.2.255  
  14. gateway 192.168.2.1  
  15. dns-nameservers 192.168.2.1  


修改dns:

1,重启生效:

sudo vi /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)

在里面插入:
nameserver 8.8.8.8
nameserver 8.8.4.4

如果有多个DNS就一行一个

修改好保存,然后执行

sudo resolvconf -u

再看/etc/resolv.conf,最下面就多了2行:

cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
nameserver 8.8.4.4

可以看到我们的设置已经加上了,然后再ping一个域名,当时就可以解析了,无需重启。


2,重启失效:

配置文件地址 /etc/resolv.conf

使用编辑器打开

改为如下内容:
search localdomain
nameserver 202.96.128.86 希望修改成的DNS
nameserver 202.96.128.166 备用DNS

重启网络:sudo /etc/init.d/networking restart。即可

### Ubuntu 中使用 `apt-get` 更新系统的正确方法 在 Ubuntu 系统中,`apt-get` 是一种强大的包管理工具,用于安装、升级和删除软件包。以下是关于如何正确使用 `apt-get` 进行系统更新以及一些常见的问题解决方案。 #### 正确使用 `apt-get` 的流程 1. **编辑源列表** 打开 `/etc/apt/sources.list` 文件并添加或修改所需的镜像源地址。例如,可以将默认的源替换为清华大学开源镜像站或其他可靠的国内镜像站点[^3]。 ```bash sudo nano /etc/apt/sources.list ``` 2. **更新本地索引** 在更改了源之后,运行以下命令来同步最新的软件包信息至本地缓存: ```bash sudo apt-get update ``` 3. **执行系统升级** 升级已安装的软件包到最新版本可以通过下面这条指令完成: ```bash sudo apt-get upgrade ``` 如果希望同处理依赖关系并自动移除再需要的老化库,则应改用 dist-upgrade 命令代替 simple upgrade: ```bash sudo apt-get dist-upgrade ``` 4. **清理旧版数据 (可选)** 清理掉下载后未使用的 .deb 软件包文件以释放磁盘空间: ```bash sudo apt-get autoremove && sudo apt-get clean ``` #### 解决常见问题 - **无法找到某些特定软件包** 当尝试通过 `apt-get install <package>` 安装某个程序失败,可能是因为当前设置中的仓库包含该软件包或者其名称拼写有误。此需确认 sources.list 是否包含了正确的组件(main, universe, restricted 和 multiverse),并且再次调用 `update` 刷新数据库[^1]^。 - **网络连接错误** 若遇到因网速慢而导致的操作中断情况,考虑切换成速度更快的镜像服务器比如阿里云提供的服务[^4]^。具体操作就是按照前述方式重新指定新的 URL 地址作为资源入口点。 - **E: Sub-process returned an error code 错误消息** 可能由于先前存在的损坏安装引起冲突所致;先试着修复破损状态再继续其他动作之前最好先解决问题根源所在之处: ```bash sudo dpkg --configure -a sudo apt-get -f install ``` --- ### 示例脚本:一键优化 APT 设置与更新过程 为了简化上述多步手动干预环节,这里提供一段简单的 bash shell script 来实现自动化部署工作流: ```bash #!/bin/bash # Backup original source list before modification. cp /etc/apt/sources.list /etc/apt/sources.list.bak echo "Updating to use TUNA mirror..." cat <<EOF | sudo tee /etc/apt/sources.list >/dev/null deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse ... EOF sudo apt-get update && \ (sudo apt-get full-upgrade || echo 'Full Upgrade Failed') && \ sudo apt-get autoclean && \ sudo apt-get autoremove ``` 此脚本会备份原始配置以防万一出现问题便于恢复,并且替换成清华 TUNA 镜像链接,最后依次完成整个更新周期内的各项任务。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值