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

【概述】html

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

~ $ 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


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

#  1 表示有;0 表示沒有
$ cat /sys/class/net/enp2s0/carrier
1


【查看網卡狀態】bash

$ 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
# 示例
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】ide

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

nameserver 223.5.5.5

  若是修改後又被改回 127.0.0.53,能夠停用 systemd-resolved 服務。spa

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


【路由】

# 查看路由
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,是由於出口地址對主機來講廣播不可達,具體來講有兩種可能狀況:一、出口地址與主機不在同一個網段;二、出口地址與主機在同一個網段,但廣播路由被無心刪除了。廣播路由示例(紅框內即爲廣播路由):

15294599881708.png


【相關閱讀】


*** walker ***

相關文章
相關標籤/搜索