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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章