關於 Ubuntu Server 18.04 的網絡(dchp/dns/route/PPPoE)

【概述】

Ubuntu Server 18.04 中用 netplan 取代了 ifupdown,配置文件在 /etc/netplan/ 目錄下,文件格式爲 yaml,使配置文件生效的命令爲 sudo netplan apply。

~ $ cat /etc/network/interfaces

ifupdown has been replaced by netplan(5) on this system. See

/etc/netplan for current configuration.

To re-enable ifupdown on this system, you can run:

sudo apt install ifupdown

auto dsl-provider
iface dsl-provider inet ppp
pre-up /bin/ip link set enp3s0 up # line maintained by pppoeconf
provider dsl-provider

auto enp3s0
iface enp3s0 inet manual

【檢查網卡是否已插入網線】

1 表示有;0 表示沒有

$ cat /sys/class/net/enp2s0/carrier
1

【查看網卡狀態】

$ sudo mii-tool enp2s0
enp2s0: negotiated 1000baseT-HD flow-control, link ok

【重啓指定網卡】

關閉

$ sudo ifconfig enp3s0 down

開啓

$ sudo ifconfig enp3s0 up

【典型配置】

/etc/netplan/50-cloud-init.yaml

network:
ethernets:
# 靜態IP
enp2s0:
dhcp4: false
addresses:
- 192.168.0.145/24
gateway4: 192.168.0.1
nameservers:
addresses:
- 223.5.5.5
search: []
optional: true
# 動態IP
enp4s0:
dhcp4: true
# 若沒有下面這一句
# 向服務器發送的“mac”地址會是類似
# “5de26c1500020000ab1102df86200698a807”
# 的奇怪字符串
# 實際上這是 DUID
dhcp-identifier: mac
optional: true
version: 2
多網卡下靜態路由無效(不生效)的一種解決方案:How can I configure default route metric with dhcp and netplan?

示例

network:
ethernets:
enp2s0:
dhcp4: false
addresses:
- 192.168.30.36/24
# gateway4: 192.168.30.1
routes:
- to: 192.168.0.0/16
via: 192.168.30.1
metric: 50
nameservers:
addresses:
- 223.5.5.5
search: []
optional: true
enp3s0:
dhcp4: true
dhcp-identifier: mac
optional: true
version: 2

【DNS】

如果上面配置的 dns 並沒有什麼卵用(可能是受 iptables 防火牆規則的影響),可以清空 iptables 命令排查,也可以直接修改 /etc/resolv.conf 文件。

nameserver 223.5.5.5
  如果修改後又被改回 127.0.0.53,可以停用 systemd-resolved 服務。

sudo systemctl disable systemd-resolved
sudo 很慢?在 /etc/hosts 添加 hostname 到 127.0.0.1 的映射。

清空 iptables 命令

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
sudo iptables -t mangle -F
sudo iptables -t mangle -X
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

【PPPoE 撥號上網】

安裝 pppoeconf

sudo apt install pppoeconf

配置

sudo pppoeconf

手動連接

sudo pon dsl-provider

手動斷開

sudo poff dsl-provider

查看狀態

sudo plog

查看接口信息

sudo ip addr show ppp0

【查看網絡接口信息】

sudo ifconfig -a

or

sudo ip addr show
linux 網卡ppp0與ppp1來回切換、不斷重連

ping 不正常工作原因

【路由】

摘自:Linux下路由配置梳理

查看路由

route -n

添加到主機的路由

route add -host 192.168.1.2 dev eth0:0
route add -host 10.20.30.148 gw 10.20.30.40

添加到網絡的路由

route add -net 10.20.30.40 netmask 255.255.255.248 eth0
route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
route add -net 192.168.1.0/24 eth1
route add -net 192.168.0.0/16 gw 192.168.30.1

添加默認路由

route add default gw 192.168.1.1

刪除路由

route del -host 192.168.1.2 dev eth0:0
route del -host 10.20.30.148 gw 10.20.30.40
route del -net 10.20.30.40 netmask 255.255.255.248 eth0
route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
route del -net 192.168.1.0/24 eth1
route del default gw 192.168.1.1 # route del default 刪除所有的默認路由

添加一條默認路由

route add default gw 10.0.0.1 # 默認只在內存中生效

開機自啓動可以追加到/etc/rc.local文件裏

echo “route add default gw 10.0.0.1” >>/etc/rc.local

添加一條靜態路由

route add -net 192.168.2.0/24 gw 192.168.2.254

要永久生效的話要這樣做:

echo “any net 192.168.2.0/24 gw 192.168.2.254” >>/etc/sysconfig/static-routes

添加到一臺主機的靜態路由

route add -host 192.168.2.2 gw 192.168.2.254

要永久生效的話要這樣做:

echo "any host 192.168.2.2 gw 192.168.2.254 " >>/etc/sysconfig/static-routes

注:Linux 默認沒有這個文件 ,得手動創建一個

添加路由報錯 SIOCADDRT: Network is unreachable,是因爲出口地址對主機來說廣播不可達,具體來說有兩種可能情況:1、出口地址與主機不在同一個網段;2、出口地址與主機在同一個網段,但廣播路由被無意刪除了。廣播路由示例(紅框內即爲廣播路由):

15294599881708.png

發佈了24 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章