---
# System prepended metadata

title: 高教機(Colud Native Trainer)

---

# 高教機(Colud Native Trainer)

以下命令在 Ubuntu Server 18.04.5 執行

- 取消 sudo 輸入密碼
> sudo nano  /etc/sudoers
```
%sudo   ALL=(ALL:ALL)  NOPASSWD:ALL
```

> sudo nano /etc/resolv.conf
```
nameserver 127.0.0.53
nameserver 8.8.8.8
....
```

> sudo apt update -y

> sudo apt upgrade -y

- 設定檔裡面有 wifi的名稱跟密碼
> sudo apt install wpasupplicant


> sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
```
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

ap_scan=1

fast_reauth=1

country=TW


network={
    ssid="Xuan"
    psk="12345678"
}
```

> ifconfig -a
```
....
wlx74da383bec56: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 74:da:38:3b:ec:56  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
```

- 因有安裝 wifi 套件，可讀取到無線網卡
>> wlx74da383bec56

- 將原來讀取 wpa_supplicant.conf 的程序 wpa_supplicant 殺掉
> sudo kill -9 $(ps -ef | grep wpa | awk '{print $2}')

- 重新執行 wpa_supplicant，並讀取 wpa_supplicant.conf 設定
> sudo wpa_supplicant -B -i wlx74da383bec56 -c /etc/wpa_supplicant/wpa_supplicant.conf

- 使用設定檔測試無線網卡連線 Wifi (亂碼部分請改為無線網卡名稱)
> sudo wpa_supplicant -u -s -D wext -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlx74da383bec56

- 讀取設定檔，連 wifi
> sudo dhclient

> ifconfig –a
```
....
wlx74da383bec56: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.10.6  netmask 255.255.255.240  broadcast 172.20.10.15
        inet6 fe80::76da:38ff:fe3b:ec56  prefixlen 64  scopeid 0x20<link>
        inet6 2001:b400:e2d5:8495:2446:84e4:cb34:7098  prefixlen 64  scopeid 0x0<global>
        inet6 2001:b400:e2d5:8495:76da:38ff:fe3b:ec56  prefixlen 64  scopeid 0x0<global>
        ether 74:da:38:3b:ec:56  txqueuelen 1000  (Ethernet)
        RX packets 7  bytes 1492 (1.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 3444 (3.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
```

- 使用 Systemd 設定開機自動連線 Wifi
    - 指定網卡讀 wpa_supplicant.conf(指定 wifi)
> sudo nano /etc/systemd/system/wpa_supplicant.service
```
[Unit]
Description=WPA supplicant
Before=network.target
After=dbus.service
Wants=network.target
IgnoreOnIsolate=true

[Service]
Type=dbus
BusName=fi.w1.wpa_supplicant1
ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant/wpa_supplicant.conf -i  wlx74da383bec56

[Install]
WantedBy=multi-user.target  
Alias=dbus-fi.w1.wpa_supplicant1.service
```

- 使用 Systemd 設定開機自動連線 Wifi
    - 請求一個IP位址
> sudo nano /etc/systemd/system/dhclient.service
```
[Unit]
Description= DHCP Client
Before=network.target
After=wpa_supplicant.service

[Service]
Type=simple
ExecStart=/sbin/dhclient wlx74da383bec56

[Install]
WantedBy=multi-user.target
```

> sudo systemctl enable dhclient.service

> sudo reboot

> ifconfig -a
```
....
wlx74da383bec56: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.10.6  netmask 255.255.255.240  broadcast 172.20.10.15
        inet6 fe80::76da:38ff:fe3b:ec56  prefixlen 64  scopeid 0x20<link>
        inet6 2001:b400:e2d5:8495:76da:38ff:fe3b:ec56  prefixlen 64  scopeid 0x0                 
....
```

- 為了不要讓檔案導入 /run/systemd/resolve/stub-resolv.conf，需要讀取 dnsmasq
    - 把名稱解析關掉
> sudo systemctl disable systemd-resolved

> sudo systemctl stop systemd-resolved

- 檔案修改後會導入 ../run/systemd/resolve/stub-resolv.conf
> ls -lh /etc/resolv.conf
```
lrwxrwxrwx 1 root root 39 Aug  6 22:35 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
```

- remove the symlinked resolv.conf file
> sudo rm /etc/resolv.conf

> sudo nano /etc/resolv.conf
```
nameserver 127.0.0.1
nameserver 8.8.8.8
```

> sudo nano /etc/dnsmasq.conf
```
dhcp-range=192.168.10.100,192.168.10.150,24h
dhcp-option=option:router,192.168.10.254
dhcp-option=option:dns-server,192.168.10.254
dhcp-option=option:netmask,255.255.255.0
....
....
```

> sudo systemctl restart dnsmasq

> sudo nano .bashrc
```
....
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o wlx74da383bec56 -j MASQUERADE
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
```
- 192.168.10.0/24 來的 Network ID，偽裝成 wlx74da383bec56 網卡
>> sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o wlx74da383bec56 -j MASQUERADE

- 上一個螢幕的指令，寫進 /proc/sys/net/ipv4/ip_forward
>> echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

> sudo iptables -t nat -L -n
```
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  192.168.10.0/24      0.0.0.0/0
```

> sudo nano /etc/netplan/00-installer-config.yaml
```yaml=
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.10.254/24]
      nameservers:
        addresses: [8.8.8.8,8.8.4.4]
    wlx74da383bec56:
      dhcp4: yes
```

- 網路世界能通往路都是因為實體網路的 Mac Address(識別代碼)
    - 可通過 ping 的方式，再打 asp -a 即可知道 mac Address
    - asp 通訊協定: IP轉MAC

- 先透過 /etc/hosts，再來到 dns Server(/etc/resolv.conf) 去找 IP
    - dns Server 就是到 gw01 的 dnsmasq
    - 再來由 arp 協定查看 IP
> ping wka01

> arp -a
```
wka02 (192.168.10.21) at 00:07:32:4d:1e:99 [ether] on enp1s0
mas02 (192.168.10.11) at 00:07:32:4d:1e:33 [ether] on enp1s0
wka01 (192.168.10.20) at 00:07:32:4d:1e:3c [ether] on enp1s0
? (192.168.2.254) at f4:28:53:6c:11:74 [ether] on wlx74da383bec56
mas01 (192.168.10.10) at 00:07:32:4d:1e:47 [ether] on enp1s0
wka03 (192.168.10.22) at 00:07:32:4d:1e:44 [ether] on enp1s0
mas03 (192.168.10.12) at 00:07:32:4d:1e:3a [ether] on enp1s0
```


- 設定 6 台的固定 IP
> sudo nano /etc/dnsmasq.conf
```
dhcp-range=192.168.10.100,192.168.10.150,24h
dhcp-option=option:router,192.168.10.254
dhcp-option=option:dns-server,192.168.10.254
dhcp-option=option:netmask,255.255.255.0
dhcp-host=00:07:32:4d:1e:44,192.168.10.22
dhcp-host=00:07:32:4d:1e:47,192.168.10.10
dhcp-host=00:07:32:4d:1e:3c,192.168.10.20
dhcp-host=00:07:32:4d:1e:99,192.168.10.21
dhcp-host=00:07:32:4d:1e:3a,192.168.10.30
dhcp-host=00:07:32:4d:1e:33,192.168.10.23
....
....
```

> sudo systemctl restart dnsmasq

- 六台皆重啟




