Ubuntu18.04突然不能更新apt源?实践出真理ack比grep香太多了!

2024-01-20 14:30

本文主要是介绍Ubuntu18.04突然不能更新apt源?实践出真理ack比grep香太多了!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

喵哥好久不更新了,前段时间一直忙于秋招,然后就是实验室搬家。最近这两天搞了一个服务器玩玩,总算遇到了一个不错的素材。

乍看标题,似乎莫名其妙,其实是在解决更新apt源失败的问题过程中得出的经验之谈。这篇博客可能会比较“松散”,因为这里主要是说明解决问题的过程,算是一个记录,方便后面复习。

1、问题描述

在搭建服务器的过程中,使用wget下载文件,或是apt install安装软件,或是apt update更新apt源都失败。具体如下图:

遇到这种问题简直要搞死人,想做的事情都做不了,服务器基本就没啥用了。这种情况持续了四五天,我也是在这四五天里一直在尝试各种解决方案。

2、尝试的解决方案

2.1修改apt源

最早发现这个问题是在安装软件的时候,当时以为是用的apt源有问题,所以就去/etc/apt/sources.list.d/sources.list里修改了apt源,当然在修改前需要做好备份:

sudo cp /etc/apt/sources.list.d/sources.list /etc/apt/sources.list.d/sources.list.bak
sudo vim /etc/apt/sources.list.d/sources.list

阿里源:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

中科大源:

deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

163源:

deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

清华源:

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

可是修改了sources.list文件后,没法去更新apt源。喵哥想到因为每次都是尝试去连接192.168.187.145,有可能跟DNS服务器有关。

2.2修改DNS服务器

修改DNS服务器是在/etc/resolv.conf里,但是这里修改的DNS会在服务器重启,或者执行 resolvconf -u 后会变为默认的设置。那么在哪设置默认的DNS呢?

可以在/etc/resolvconf/resolv.conf.d/base里面添加:

nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 8.8.8.8
nameserver 8.8.4.4

 

然后执行 resolvconf -u去刷新/etc/resolv.conf。

然而,这么尝试了还是行不通。第一次我放弃了,我把服务器给重置了,然后又重新开始配置服务器。

但是,第二次配置服务器的时候又遇到这个问题,总不能一直重置服务器吧。

3、解决方案

因为不管是wget下载文件,还是apt安装软件、更新apt源,都是尝试去连接192.168.187.145这个IP,所以这个应该跟系统中的某个地方的设置有关,但是在哪呢?

喵哥第一反应是用grep 去找

grep 192.168.187.145 /

但是用grep在全局查找实在是太慢了,不想等。记得前段时间看到ack,ag比grep快很多,于是就用ack去尝试搜索:

ack 192.168.187.145 /

发现快很多,一下就出了结果:

 

可以看到,在/root/.zshrc中有一行代理设置用到了这个IP,难怪。。。

找到后就简单了,注释掉这一行就好了。

附1:ack的用法

ack --help-types             # 打印所有type类型
ack "include|RTSP" .        # ack 'pattern1|pattern2|pattern3' 可以搜索含有 pattern1 或者 pattern2 或者 pattern3 的行
ack-grep hello
ack-grep -i hello
ack-grep -v hello             # 反向选择,即过滤掉含 hello 的行
ack-grep -w hello
ack-grep -Q 'hello*'
ack -f --html                # 打印类型是html的文件,不做具体的搜索,相当于find命令,html类型的文件大致包含: .html, .htm.  使用 ack -f --html -w index 方式是不对的,不能同时搜文件和文件内容。
ack -w --type=nojs css     #查找不是js类型之外的其它文件,只要该文件含有单词css
ack -n "(^9999)|(^000)"    #这种正则表达式和grep的类似,这里只是查找文件,不进入子目录进行递归(-n),它显示含有以9999或者000开头的行
ack -g log --cc         # -g代表只搜索文件(会递归进子目录),这里的意思是搜索c类型的文件,其文件名相对路径含有字符log,输入如 *log*.c  *log*.h 这样的文件。 ack -g log --cc -w aa,这样使用是不对的,不能同时搜文件和文件内容。ack --type-add=html:ext:shtml,xhtml   #只影响当前的命令,必须配合搜索命令一起使用,如:ack --type-add=html:ext:shtml,xhtml --html -w body,或者 ack --type-add=html:ext:shtml,xhtml -f --html
echo "--type-add=html:ext:shtml,xhtml" >> ~/.ackrc     #这个可以将增加某类型的类型定义保存起来,以后每次使用ack都会生效。--type-set=example:is:example.txt     # 这个是改变或新建某类型的类型文件。
echo "--type-set=bashcnf:match:/.bash(rc|_profile)/" >> ~/.ackrc     #这个是用正则式来定义类型参数,定义里一个叫bashcnf的类型,该类型匹配 .bashrc 或 .bash_profile 两个文件。
ack 'string1|string2'     #搜索string1或string2.

附2:.zshrc文件

这个zsh配置文件还不错:

https://download.csdn.net/download/ruibin_cao/11966891

这篇关于Ubuntu18.04突然不能更新apt源?实践出真理ack比grep香太多了!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/626303

相关文章

Redis中6种缓存更新策略详解

《Redis中6种缓存更新策略详解》Redis作为一款高性能的内存数据库,已经成为缓存层的首选解决方案,然而,使用缓存时最大的挑战在于保证缓存数据与底层数据源的一致性,本文将介绍Redis中6种缓存更... 目录引言策略一:Cache-Aside(旁路缓存)策略工作原理代码示例优缺点分析适用场景策略二:Re

Pandas利用主表更新子表指定列小技巧

《Pandas利用主表更新子表指定列小技巧》本文主要介绍了Pandas利用主表更新子表指定列小技巧,通过创建主表和子表的DataFrame对象,并使用映射字典进行数据关联和更新,实现了从主表到子表的同... 目录一、前言二、基本案例1. 创建主表数据2. 创建映射字典3. 创建子表数据4. 更新子表的 zb

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

Python 中的 with open文件操作的最佳实践

《Python中的withopen文件操作的最佳实践》在Python中,withopen()提供了一个简洁而安全的方式来处理文件操作,它不仅能确保文件在操作完成后自动关闭,还能处理文件操作中的异... 目录什么是 with open()?为什么使用 with open()?使用 with open() 进行

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio