---
tags: ubuntu
---
# Ubuntu 網絡問題修復
```
Hardware: Realtek PCIe GBE
IP addr: DHCP (192.168.1.34)
Subnet mask: 255.255.255.0
Gateway: 192.168.1.1
DNS: Auto (192.168.1.1)
```
## 問題描述
發生在關閉WiFi, 只使用有線網絡的時候
使用瀏覽器可瀏覽部份網站(google, yahoo之類的大網站)
- `traceroute` 外部網站都會被default gateway吐`!N`
- `nslookup`要嘛是`REFUSED`要嘛就`no server is reachable`
- `ping`不用說當然也是不行,一直被gateway回覆dest net unreachable
- `$ip a`可以看到是有拿到ip位址的, MAC也有抓到
:point_right: [直接看結論](#又修好了!!!!!!!!!)
## 纏鬥過程
~~我是以下三者全都有做,但不知道真正有效的是哪個啦 Orz~~
> 後來又莫名其妙自己爆掉了 我哭
> 打勾:heavy_check_mark: 的是有保留的變化,打叉:x: 的是後來被我revert的
**1. 更新r8168** :heavy_check_mark:
1. 去[Realtek官網](https://www.realtek.com/en/component/zoo/category/network-interface-controllers-10-100-1000m-gigabit-ethernet-pci-express-software)下載driver
2. 解壓縮
3. 進去剛才解壓縮的目錄 `$ cd r8168xxxxx`
4. 執行自動安裝檔 `$ sudo ./auto-run.py`
:::warning
:warning: 注意output有無出現以下訊息
```
modprobe: ERROR: could not insert 'r8168': Operation not permitted
```
不介意的話可以進BIOS暫時關掉`secure_boot`,不然就要手動去生mok key\+sign module
:::
5. 檢查有沒有成功 `$ lsmod | grep r8168`
如果有輸出就OK :v:
6. ...重開機?(有必要ㄇ)
做到這裡還是不行
這時候我發現nslookup吐IPv6的位址給我
顯然不知道為何router很堅持要我用IPv6, IPv4直接被它擋下 (???)
**2. 禁用IPv6** :x:
參考:[link](https://www.linuxbabe.com/ubuntu/disable-ipv6-on-ubuntu)
- 編輯`99-sysctl.conf`檔案
```
$ sudo nano /etc/sysctl.d/99-sysctl.conf
```
- 把下列三行補在檔案最末端。可以用`#`加一些註解,方便以後自己回來修改
```
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
```
- 讓系統載入剛才做的修改
````
sudo sysctl -p
````
**3. /etc/resolv.conf 設定DNS** :x:
- 編輯`resolv.conf`
- 把原本的nameserver註解掉,加上google的
````
# nameserver 127.0.0.53
nameserver 8.8.8.8
nameserver 8.8.4.4
````
- ==其實做這根本白搭,重開機會直接恢復原樣==
**4. 設定network/interfaces** :x:
- `$sudo nano /etc/network/interfaces`
- 加入以下內容 (記得把`enp4s0f1`取代成網卡名字)
```
auto enp4s0f1
iface enp4s0f1 inet static
address 192.168.1.34
netmask 255.255.255.0
gateway 192.168.1.1
dns 8.8.8.8,8.8.4.4
```
- 重啟電腦 (根本overkill哈哈)
- 還是不行啊!!
- 我有試過的variation:
- dns設192.168.1.1
- dns設8.8.8.8
- dns不設
- 把`static`改成`dhcp`然後後面四行註解掉
---
## 結論
重開機!
就好了!
我也不知道為什麼!
![](https://i.imgur.com/ETTlk4W.png)
> [time=23 May 04:30]
---
#### 這篇筆記寫到一半的時候突然又自己斷線了!
又變回原本server unreachable... 為何?!! :disappointed:
不管總之我要先睡了
> [time=23 May 07:00]
---
### 現在突然又好了我也不知道做了什麼
我undo了第2步的東西(把`sysctl.conf`恢復原樣)
還有`/etc/network/interfaces`也恢復原樣了
然後用gui的network manager去把IPv6設成disable
![](https://i.imgur.com/9yNpT1J.png)
神奇的是,還是會拿到IPv6位址... :confused:
```
alex@DESKTOP-ULUTBFT:~$ ip a
(略)
2: enp4s0f1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 60:45:cb:2d:42:8e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.34/24 brd 192.168.1.255 scope global noprefixroute enp4s0f1
valid_lft forever preferred_lft forever
inet6 2001:b011:e000:5fe7:6245:cbff:fe2d:428e/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 73740sec preferred_lft 73740sec
inet6 fe80::6245:cbff:fe2d:428e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
```
`nslookup`也會抓到IPv6位址
```
alex@DESKTOP-ULUTBFT:~$ nslookup www.google.com
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
Name: www.google.com
Address: 172.217.160.100
Name: www.google.com
Address: 2404:6800:4012::2004
```
不過這次`traceroute` default gateway乖乖讓我用IPv4過了,為什麼?!
```
alex@DESKTOP-ULUTBFT:~$ traceroute www.google.com
traceroute to www.google.com (172.217.160.68), 30 hops max, 60 byte packets
1 I-040GW.cht.com.tw (192.168.1.1) 4.305 ms 4.286 ms 4.814 ms
2 h254.s98.ts.hinet.net (168.95.98.254) 3.051 ms 2.992 ms 2.993 ms
3 tne1-3301.hinet.net (168.95.54.10) 2.975 ms 3.462 ms 3.458 ms
4 tne1-3011.hinet.net (220.128.26.146) 10.349 ms tne1-3022.hinet.net (220.128.27.102) 7.579 ms tne1-3011.hinet.net (220.128.26.146) 10.316 ms
5 tne1-3021.hinet.net (220.128.26.162) 7.509 ms 7.503 ms pcpd-3212.hinet.net (220.128.14.74) 15.556 ms
6 pcpd-3211.hinet.net (220.128.14.70) 9.382 ms pcpd-3211.hinet.net (220.128.12.34) 6.489 ms 7.729 ms
7 72.14.218.142 (72.14.218.142) 9.757 ms 9.759 ms 72.14.218.140 (72.14.218.140) 9.271 ms
8 * * *
9 72.14.237.230 (72.14.237.230) 7.166 ms 108.170.244.129 (108.170.244.129) 7.826 ms 209.85.245.252 (209.85.245.252) 8.820 ms
10 209.85.243.197 (209.85.243.197) 8.783 ms tsa01s09-in-f4.1e100.net (172.217.160.68) 6.801 ms 108.170.244.139 (108.170.244.139) 7.796 ms
```
狀態欄的圖標依然是 ![](https://i.imgur.com/iEEKZG9.png)…
但是我現在瀏覽網站沒問題R!?而且點它展開是顯示 ![](https://i.imgur.com/DFaR6t3.png)
至於ping還是會用IPv6...而且是成功的!我一臉懵逼...
```
alex@DESKTOP-ULUTBFT:~$ ping www.google.com
PING www.google.com(tsa03s02-in-x04.1e100.net (2404:6800:4008:803::2004)) 56 data bytes
64 bytes from tsa03s02-in-x04.1e100.net (2404:6800:4008:803::2004): icmp_seq=1 ttl=55 time=44.3 ms
64 bytes from tsa03s02-in-x04.1e100.net (2404:6800:4008:803::2004): icmp_seq=2 ttl=55 time=12.6 ms
64 bytes from tsa03s02-in-x04.1e100.net (2404:6800:4008:803::2004): icmp_seq=3 ttl=55 time=31.7 ms
64 bytes from tsa03s02-in-x04.1e100.net (2404:6800:4008:803::2004): icmp_seq=4 ttl=55 time=10.6 ms
^C
--- www.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 10.671/24.860/44.379/13.967 ms
```
:::success
#### 總之就是這樣了!沒記錯的話,修改的部份如下
1. 更新網卡驅動至r8168 (原本是r8169)
- 可能需要關閉`secure_boot`
- 原本網速上限1000Mbps, 現在只有100Mbps (沒差啊我家網絡原本就只有80~90Mbps++ ww)
2. 用network manager修改網絡設定
- IPv4:
```
Method: Manual
Address: 192.168.1.34
Mask: 255.255.255.0
Gateway: 192.168.1.1
DNS: 192.168.1.1 (Auto: off)
Route: Auto
```
- IPv6: Disabled
為保險起見(?)我也有把DNS和Route的Automatic設成Off
:::
> 我還不敢重開機…先讓我享受一下短暫的勝利~和網絡~………
> [time=23 May 21:03]
>
---
啊…又壞了…我明明什麼都沒改,只是正常的在上網罷了…
我回到network manager, 把IPv4的DNS改成中華電信的`168.95.1.1`,`168.95.192.1`, 然後把IPv6全部設Automatic
現在的情況變成只能瀏覽支援IPv6的網站…也就是說如果nslookup只找得到IPv4位址的話,該網站就會連不上 ~沒想到現在還有這麼多網站是不支援IPv6的~
> [time=24 May 04:10]
---
### 又修好了!!!!!!!!!
:::success
而且這次是 ![](https://i.imgur.com/IEJ8UTM.png)!!感覺良好!!
這次做的事情是用wifi連進default gateway `192.168.1.1`, 然後設定路由器的alias ip `192.168.10.1`, 之後用network manager設定:
**ipv4位址** 隨便取一個(我設192.168.1.105),mask 255.255.255.0
**gateway** 192.168.10.1
然後把wired connection關掉重開就好了 :v:
:::
至於為什麼會這樣做,是因為我發現 **用wifi和用eth連到的192.168.1.1是不一樣的頁面**!
wifi的是alcatel, eth的是draytek 2960...
`$ arping 192.168.1.1` 也會回傳2個不一樣的mac ~(我之前以為這是正常現象orz)~ 所以發現原來有兩個路由器在打架… 把wifi關掉後`$ ip n show` 就會發現ipv4的gateway抓到draytek, ipv6則是抓到alcatel, 而draytek那台是沒辦法對外的,~~我不知道為什麼(有可能就是因為網址打架?)~~,所以才會有上述「只能連上IPv6網站」的問題!!!~~氣死我~~
我登不進去draytek(應該是帳密有改過),但是alcatel那台用網絡上找到的帳密就可以登進去了
:::spoiler alcatel 帳密
**帳號** admin
**密碼** 40gw+MAC末四碼(全小寫)
如果不知道mac可以用arping或ip neigh show看
不然的話先用user user登進去看也可以 (是看LAN的喔)
:::
我不想直接改掉alcatel的192.168.1.1, 怕連帶影響到其他房客/機器 ~~(房東的網絡配置不知道在複雜幾點的)~~ 所以只是設了alias `192.168.10.1`, mask `255.255.255.0`, 然後等它重啟,我很忐忑地等了很久,怕被我弄壞orz
這時候`192.168.1.1`變成了draytek那台,手動修改arp table把`192.168.1.1`的mac改回alcatel的,`192.168.1.1`就變回alcatel了,進`192.168.10.1`也可以進到alcatel就代表alias設定成功, 我非常感動!!
所以開始修改電腦的ipv4設定,把gateway指到`192.168.20.1`然後重啟wired connection
就!!好了!!!!!
> [time=24 May 08:37]
但是有點讓人擔心的是現在進alcatel後台卻看不到dhcp lease......
> 不知道lease過期後會不會出事啊...
> [time=24 May 09:16]
---
### ???
從嘉義回來後突然又出問題了…\_(:3 」∠ )\_
打開network manager整個ethernet直接不見
`$ ip a`也完全沒有ethernet的蹤影 :sob:
編輯`/etc/NetworkManager/NetworkManager.conf`可以看到`managed=true`所以也不是這個問題
於是用`$ lspci -v`檢視就發現
```
04:00.1 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 12)
Subsystem: ASUSTeK Computer Inc. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
Flags: bus master, fast devsel, latency 0, IRQ 255
I/O ports at d000 [size=256]
Memory at ef114000 (64-bit, non-prefetchable) [size=4K]
Memory at ef110000 (64-bit, non-prefetchable) [size=16K]
Capabilities: <access denied>
Kernel modules: r8168
```
咦我的driver r8168哪裡去了…只剩下kernel module r8168 (???)
```bash
$ lshw -c network
*-network UNCLAIMED
description: Ethernet controller
product: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
vendor: Realtek Semiconductor Co., Ltd.
physical id: 0.1
bus info: pci@0000:04:00.1
version: 12
width: 64 bits
clock: 33MHz
capabilities: bus_master cap_list
configuration: latency=0
resources: ioport:d000(size=256) memory:ef114000-ef114fff memory:ef110000-ef113fff
```
`UNCLAIMED`代表driver module unload了 ~(mystery...)~ 而且configuration那邊應該要有說`driver=xxxx`才對
來看看我有哪些driver module
```
$ ls /lib/modules/5.3.0-59-generic/kernel/drivers/net/ethernet/realtek/
8139cp.ko 8139too.ko atp.ko r8169.ko
```
> `5.3.0-59-generic`是kernel, 可以用`$ uname -r`查到
咦我的r8168怎麼整個不見了 (´\_ゝ\`) r8169倒是還在!那我就裝r8169吧…
```
$ sudo modprobe r8169
$ lspci -v
04:00.1 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 12)
Subsystem: ASUSTeK Computer Inc. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
Flags: bus master, fast devsel, latency 0, IRQ 19
I/O ports at d000 [size=256]
Memory at ef114000 (64-bit, non-prefetchable) [size=4K]
Memory at ef110000 (64-bit, non-prefetchable) [size=16K]
Capabilities: <access denied>
Kernel driver in use: r8169
Kernel modules: r8168
$ lshw -c network
*-network
description: Ethernet interface
product: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
vendor: Realtek Semiconductor Co., Ltd.
physical id: 0.1
bus info: pci@0000:04:00.1
logical name: enp4s0f1
version: 12
serial: 60:45:cb:2d:42:8e
size: 1Gbit/s
capacity: 1Gbit/s
width: 64 bits
clock: 33MHz
capabilities: bus_master cap_list ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
configuration: autonegotiation=on broadcast=yes driver=r8169 duplex=full firmware=rtl8411-2_0.0.1 07/08/13 ip=192.168.10.105 latency=0 link=yes multicast=yes port=MII speed=1Gbit/s
resources: irq:19 ioport:d000(size=256) memory:ef114000-ef114fff memory:ef110000-ef113fff
```
`lspci`和`lshw`都可以看到有抓到driver! 網絡也恢復了\~
![](https://i.imgur.com/IEJ8UTM.png) 真是個賞心悅目的圖案 :heart:
> 其實上次為了解決別的問題亂裝driver把原本的r8169裝成r8168,後來一直想換回r8169, 這次算是一個契機吧…
> [time=Mon, Jun 15, 2020 11:05 AM]
好像是重開機後driver module會不見。解決方法參考[這篇](https://askubuntu.com/questions/385351/ubuntu-12-04-loses-ethernet-driver-after-update-and-reboot),編輯`/etc/modules` (要用sudo)
```
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Realtek ethernet driver
r8169
```
> [time=Tue, Jun 16, 2020 20:03 PM]