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

相关文章

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据