Ubuntu

在 ubuntu 18 上正確配置 netplan

  • February 17, 2021

我正在嘗試以非標準方式在 linux 機器上配置 wifi 和乙太網埠。我以前從未接觸過這種東西,所以請原諒我的幼稚。我基本上只是 YOLO 首先進入了文件負責人,並且已經取得了相當大的進展並讓它開始工作。但是仍然有一些奇怪的地方,我不完全理解它為什麼會起作用。因此,如果可以,我想完整地描述這一點,那麼任何更多知識的人請糾正我並解釋我做錯了什麼。

我有一台帶有 2 個乙太網埠和 1 個 wifi 的迷你電腦。我正在嘗試對其進行配置,以便網際網路進入一個乙太網埠,然後在 WIFI 和另一個乙太網埠上廣播。基本上將其配置為像wifi路由器一樣工作。

所以。

這是我的 /etc/netplan/01-network-manager-all.yaml

network:
   version: 2
   renderer: networkd
   ethernets:
       enp0s31f6:
           dhcp4: true
       enp1s0:
           addresses:
           - 192.168.2.1/24
           dhcp4: false
           nameservers:
               addresses:
               - 8.8.8.8
               - 8.8.4.4
               search: []
       wlp2s0:
           addresses:
           - 10.42.0.1/24
           dhcp4: false
           nameservers:
               addresses:
               - 8.8.8.8
               - 8.8.4.4
               search: []
   bridges:
       br0:
           interfaces: [enp0s31f6, enp1s0, wlp2s0]
           dhcp4: true
           addresses:
           - 100.80.0.1/24
           parameters:
               stp: false
               forward-delay: 0

這是我的 /etc/default/dhcpd.conf

INTERFACES="enp1s0 wlp2s0"

這是我的 /etc/dhcp/dhcpd.conf

option domain-name "whatever.you.want";
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;
log-facility local7;
subnet 10.42.0.1 netmask 255.255.255.0 {
    range 10.42.0.101 10.42.0.200;
    option subnet-mask 255.255.255.0;
    option routers 10.42.0.1;
    option broadcast-address 10.42.0.255;
}
subnet 100.80.0.1 netmask 255.255.255.0 {
    range 100.80.0.101 100.80.0.200;
    option subnet-mask 255.255.255.0;
    option routers 100.80.0.1;
    option broadcast-address 100.80.0.255;
}
subnet 192.168.2.1 netmask 255.255.255.0 {
    range 192.168.2.101 192.168.2.200;
    option subnet-mask 255.255.255.0;
    option routers 192.168.2.1;
    option broadcast-address 192.168.2.255;
}

我已經執行了所有正確的命令(我認為)

sudo netplan --debug generate
sudo netplan apply
sudo systemctl restart isc-dhcp-server
sudo systemctl enable isc-dhcp-server
sudo systemctl status isc-dhcp-server

所以這行得通。但是我知道我可能做錯了什麼。它也有怪癖。隨機似乎中斷了幾次,彈出一個沒有說明的錯誤。此外,當我嘗試將其插入某個 4G 熱點時,它只是無法正常工作,但是該 4G 熱點成功地將網際網路傳輸到其他設備,它也可以很好地從其他乙太網源接收網際網路。

那麼這個配置正確嗎?我將如何更改這些文件以正確配置它?

也為了衡量我的 ifconfig

br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       inet 100.80.0.1  netmask 255.255.255.0  broadcast 100.80.0.255
       inet6 fe80::14a8:83ff:fe5b:a89c  prefixlen 64  scopeid 0x20<link>
       ether 16:a8:83:5b:a8:9c  txqueuelen 1000  (Ethernet)
       RX packets 666051  bytes 252795968 (252.7 MB)
       RX errors 0  dropped 59  overruns 0  frame 0
       TX packets 127129  bytes 14963222 (14.9 MB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       ether 00:01:2e:84:6a:13  txqueuelen 1000  (Ethernet)
       RX packets 691307  bytes 261567317 (261.5 MB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 242102  bytes 38886894 (38.8 MB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
       device interrupt 16  memory 0xdf300000-df320000  

enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       inet 192.168.2.1  netmask 255.255.255.0  broadcast 192.168.2.255
       ether 00:01:2e:84:6a:14  txqueuelen 1000  (Ethernet)
       RX packets 111556  bytes 21621626 (21.6 MB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 523333  bytes 80568249 (80.5 MB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
       inet 127.0.0.1  netmask 255.0.0.0
       inet6 ::1  prefixlen 128  scopeid 0x10<host>
       loop  txqueuelen 1000  (Local Loopback)
       RX packets 11909999  bytes 4519986237 (4.5 GB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 11909999  bytes 4519986237 (4.5 GB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       inet 10.42.0.1  netmask 255.255.255.0  broadcast 10.42.0.255
       ether 00:24:d6:fc:f5:e9  txqueuelen 1000  (Ethernet)
       RX packets 87180  bytes 10348796 (10.3 MB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 134948  bytes 148087795 (148.0 MB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

要將您的設備用作路由器(從客戶端的角度來看,此設備將是其預設網關),您將需要以下配置設置:

  1. 將想要通過路由器設備訪問 Internet 的設備設置為使用該設備作為預設網關。如果他們從該設備獲取 DHCP,則可以將其作為 DHCP 選項傳入。否則,您需要在設備本身上添加此路由。ip使用以下命令檢查他們的路由表:
$ ip route show
default via 172.31.100.1 dev eno2 proto static
...

您應該看到路由器主機的地址作為其預設網關路由。 2. 在您的路由器主機上啟用轉發:

$ echo 1 > /proc/sys/net/ipv4/ip_forward
  1. 設置從我們的“LAN”適配器到eth0我們的“WAN”適配器的路由:eth1``eth0``eth1
iptables -t nat -I POSTROUTING -o eth1 -j MASQUERADE
iptables -I FORWARD -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I FORWARD -i eth1  -j ACCEPT
  1. 測試。
  2. 要刪除這些設置:
iptables -t nat -D POSTROUTING -o eth1 -j MASQUERADE
iptables -D FORWARD -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i eth1  -j ACCEPT
echo 0 > /proc/sys/net/ipv4/ip_forward

所有這些命令都需要以超級使用者身份執行。

引用自:https://unix.stackexchange.com/questions/503016