建立 free5GC VM 及設定網路
此示範我們將練習:
- 複製現有 VM,建立一個新的 free5GC VM
- 設定 free5GC VM 網路環境
參考影片 複製 VM 與設定 IP
1. 檢查現有 VM
開啟 VirtualBox, 確認現有 Ubuntu VM (ubuntu) 可開機執行
- 用 SSH 登入, 進入後確認可以連網
- 確定做過
sudo apt update
及sudo apt upgrade
- 關機
sudo shutdown -P now
2. 建立 free5GC VM
先複製新的 VM:
- 選取現有 VM (ubuntu) 右方按鈕 / 快照 / 再製
- 名稱設為 “free5gc”
- MAC 位址原則為:為所有網卡產生新的 MAC 位址
- 選取連結再製(或先了解完整再製與連結再製的不同,你也可選則完整再製)
成功後
- 啟動新建立的 free5gc VM,使用相同帳號登入
- 用
ping
及ifconfig
確定連網及 Host-only 介面卡 IP 位址- 例如 IP 可能為
192.168.56.103
, 介面名稱為enp0s8
- 例如 IP 可能為
- 用 SSH 登入 free5gc 確認可以連線
3. 更改 hostname
複製而來的 VM hostname 仍為 ubuntu
(或你之前給的名字)。
我們練習將這台電腦取名為 free5gc
。 你可以用 vi
或 nano
編輯 /etc/hostname
檔案:
sudo nano /etc/hostname # or sudo vi /etc/hostname
將檔案裡的 ubuntu
改為 free5gc
。若你用 nano
編輯,可以按 Ctrl-O 儲存檔案,再
按 Ctrl-X 離開。
順便將 /etc/hosts
裡的 ubuntu
改成 free5gc
:
sudo nano /etc/hosts
新的 /etc/hosts
檔案內容像這樣:
127.0.0.1 localhost
127.0.1.1 free5gc
...
等待會重新開機時就會生效。
4. 設定靜態 IP 網址
預設的 Host-only 介面卡是藉由 DHCP 得到 IP 位址。為減少後面指令錯誤的發生,
這裡練習在 Ubuntu 為 Host-only 介面卡設定靜態 IP (將之固定為 192.168.56.101
).
$ cd /etc/netplan
$ ls
00-installer-config.yaml
$ cat 00-installer-config.yaml
原先的檔案內容為:
# This is the network config written by 'subiquity'
network:
ethernets:
enp0s3:
dhcp4: true
enp0s8:
dhcp4: true
version: 2
代表 VM 有兩張網路介面卡,由指令 ifconfig
我們得知 enp0s8
為 Host-only 介面卡。你可以
編輯此檔案:
sudo nano 00-installer-config.yaml
新的檔案內容:
# This is the network config written by 'subiquity'
network:
ethernets:
enp0s3:
dhcp4: true
enp0s8:
dhcp4: no
addresses: [192.168.56.101/24]
version: 2
先測試是否內容無誤:
sudo netplan try
再套用新的設定
sudo netplan apply
執行 ifconfig
檢查網路設定是否改變:
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::a00:27ff:fec4:254f prefixlen 64 scopeid 0x20<link>
ether 08:00:27:c4:25:4f txqueuelen 1000 (Ethernet)
RX packets 2 bytes 1180 (1.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 18 bytes 1894 (1.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.56.101 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::a00:27ff:fe7e:ada6 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:7e:ad:a6 txqueuelen 1000 (Ethernet)
RX packets 8420 bytes 531867 (531.8 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10887 bytes 823487 (823.4 KB)
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 6621 bytes 596035 (596.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6621 bytes 596035 (596.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
檢查 routing table:
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.2.2 0.0.0.0 UG 100 0 0 enp0s3
10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
10.0.2.2 0.0.0.0 255.255.255.255 UH 100 0 0 enp0s3
192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s8
可以知道 Host-only 網路 192.168.56.0/24
本身沒有連結到 Internet。(但我們可以
在本機與此 VM 連線)。連結到 Internet 是藉由 NAT 網路 10.0.2.0/24
,(gateway
為 10.0.2.2
)
現在可以在本機用 SSH 登入 free5gc VM 了。
ssh 192.168.56.101 -l ubuntu
以後和 free5gc 溝通都是藉由 SSH。