Try   HackMD

如何在 M1 Macbook 上使用 Linux?

前情提要

因為出 OI 題用了 TPS,以往都用 Linux 執行,用 M1 Macbook 跑時遇到不少麻煩。

費盡千辛萬苦解決用 pandoc + latex engin 輸出題敘的問題後,剛要接觸下個生成測資的指令,果不其然又噴錯了:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這次是關於 C++ 的,我已經懶得、也沒能力解決了,只好尋求 VM 的協助。

UTM

macOS 上常用的 VM 平台有:VMware, VirtualBox, Parallels。其中只有 Parallels 支援 M1,但連玩手遊都沒課過金的我自然是不會選擇它。

UTM 是一款開源且免費,還能跑在 M1 上的 VM 軟件。在最近幾個月前的更新後,使用變的非常簡單。

設置一台 Ubuntu 20.04

安裝好 UTM 後,就能新建一台 Linux VM:

  1. 新增一台 VM,並選擇虛擬化。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 選擇 Linux。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 點擊瀏覽,新增 Ubuntu Server 的 iso 檔。特別注意的是,這裡必須使用 Server 版本。如果要 GUI,則會在把 Server 版安裝起來後再另外安裝 Desktop。官方有列出支援的 OS。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 記憶體和硬碟都可以調整。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 選擇共享資料夾。但這步還不能啟用共享資料夾的功能,後面需要再安裝東西。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 儲存。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接著,就會進入安裝 Ubuntu Server 的流程。

  1. 選擇 "Install Ubuntu Server"。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 然後一路點擊 "Done" 或 "Continue"。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 接著就設置使用者和伺服器名稱。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 他會問你要不要安裝 Open SSH server。因為我後面會用到,所以選擇安裝。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 繼續按 Done 後,會出現這個畫面。大概要等個三分鐘,他就會裝完了,屆時並且下面會出現 "Reboot now" 按鈕。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 點擊 Reboot 之後,會進入這個畫面。點擊右上角的光碟圖標,然後把你安裝用的 iso 退出來。沒有做這步的話,每次開機他都會問你要不要安裝 Ubuntu Server。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 退出虛擬機,然後再開一次。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 成功的話,會顯示登入畫面。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 測試一下網路是不是能用。
$ ping 8.8.8.8

成功的話會跳訊息。但 UTM 不支援 ping,我也不知道為什麼這裡能用。或是:

$ sudo apt update

看看有沒有提示連不上 server 的錯誤。或是:

$ dig @1.1.1.1 github.com

成功的話也會跳訊息。dig 可以指定一個 DNS,然後問他關於這個 domain 的資訊。

SSH 進 VM

原生的 Console 用著還停難受的,特別是在被非常 Fancy 的 Terminal 慣壞之後。一個解決方案是 SSH 進去我們的 VM。

設置步驟:

  1. 點擊右上角的編輯虛擬機圖標,選擇“網路”分頁。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 網路模式切換到“模擬VLAN”,點擊新增連接埠轉送,並且放上 port。客體連線埠要填 22,那是 SSH 的預設 port。主體連線埠就隨便找一個空的填。

💡連接埠轉發應該可以想像成,UTM 會去抓送到本機 22022 port 的資料,然後丟到 VM 裡的 22 port。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 儲存設置後,開啟 VM。這時再使用上面測試網路的指令,會發現唯獨 ping 不能使用了。

  2. 使用本機的 Terminal SSH 到 localhost,並且 port 指定為“主機連接埠”。如果 Server 上的 Username 跟現在的 Username 不一樣的話,需要以 <username>@<ip> 指定以哪個 User 登進去。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Issue

SSH 時顯示 REMOTE HOST IDENTIFICATION HAS CHANGED!

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
Please contact your system administrator.
Add correct host key in /home/phpini/.ssh/known_hosts to get rid of this message.
Offending key in /home/phpini/.ssh/known_hosts:10
RSA host key for IP_ADDRESS has changed and you have requested strict checking.
Host key verification failed.

如果新增第二台 VM,然後再度 SSH 進去就會發生這種狀況。這通常發生在伺服器重灌之類的時候,因為 host key 跟之前紀錄的不一樣了。只要把 ~/.ssh/known_hosts 裡的對應主機刪掉就好了,或是整個檔案砍掉也行。

💡host key 是用在 SSH protocal 裡的一對公私鑰。

kex_exchange_identification Connection Closed by Remote Host

很多狀況都會導致 kex_exchange_identification Connection Closed by Remote Host。可以檢查一下 server 端有沒有裝 openssh-server

關閉 GUI 模式

當你用不到 GUI,切換到“只有主控台”模式有一些好處。首先,可以複製貼上到 VM 裡面了;另外,也能增加效能。

設置步驟:

  1. 進入編輯虛擬機/“顯示器”分頁,選擇“只有主控台”

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

此時網路大概是不能用的。進到 VM 裡不管是 digapt udpate 都會沒反應。因為在不同模式下,網卡(Network adapter)會是不同名字。

  1. 查看現在的網卡名稱
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether ae:5b:4b:d6:2a:50 brd ff:ff:ff:ff:ff:ff
    inet 192.168.64.8/24 brd 192.168.64.255 scope global dynamic enp0s8
       valid_lft 86091sec preferred_lft 86091sec
    inet6 fd40:b442:8ba5:a1a7:ac5b:4bff:fed6:2a50/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 2591990sec preferred_lft 604790sec
    inet6 fe80::ac5b:4bff:fed6:2a50/64 scope link 
       valid_lft forever preferred_lft forever

enp0s8 就是我現在的網卡,而我在 GUI 模式下的網卡是 enp0s10,跟現在不一樣。

  1. 修改 netplan 內容
$ sudo vim /etc/netplan/00-installer-config.yaml

原本的檔案內容長這樣,dhcp4 設成 true 時會自己去抓 ipv4 來用。

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s10:
      dhcp4: true
  version: 2

把它再加上我現在的網卡(enp0s8)的設置就好了。

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s10:
      dhcp4: true
    enp0s8:
      dhcp4: true
  version: 2

有一個奇怪的地方是,切換成主控台模式之後,shutdown 需要加 sudo 了。這應該是因為主控台模式,可以視為 terminal。如果在 GUI 模式下,接觸到的就直接是 console 了。

共享資料夾

有了共享資料夾會方便很多,不然可能要把全部東西都丟到 GitHub 才行。我直接把整個家目錄都共享了。可惜的是,主控台模式下不能使用共享功能。

設置步驟:

  1. 在 VM 上安裝套件
$ sudo apt install spice-webdavd davfs2

💡SPICE 是用在虛擬化環境裡的通訊協定。

💡WebDAV 是 HTTP 的擴充,用來遠端操控 Server 上的資源。我們可以透過支持 WebDAV 的工具送出 COPYLOCKMOVE 等請求,讓 WebDAV Server 執行對應動作。

💡davfs2 則可以讓我們透過本地硬碟存取 WebDAV Server 上的資料。

  1. 右上角編輯虛擬機,開啟共享功能。然後在主畫面選擇要共享的資料夾。

  1. 把 WebDAV server 掛載上

資料夾會被當成一個 WebDAV server,在 http://127.0.0.1:9843/ 上。我們要把它掛載到 /mnt 上。

首先創個資料夾,叫什麼都好,這裡會東西最後會掛載到這裡裡面。

$ sudo mkdir /mnt/dav
$ sudo vim /etc/davfs2/secrets

這是 davfs2 用來儲存登入資料的檔案,在加上這行:

http://127.0.0.1:9843/ username password
$ sudo vim /etc/fstab

電腦在開機時,他會根據 fstab,自動對特定類型的檔案系統進行 mount 操作,這樣就不用每次都手動 mount 了。加上這行:

http://127.0.0.1:9843/ /mnt/dav davfs defaults,uid=username,gid=username,_netdev,auto 0 0
  1. 重新開機,就會發現它已經掛載上了。

後記

只是為了跑 linux 折騰了我兩天🥵,終於可以開始出題了zzzz。
希望等等不要再遇到什麼困難。

更:結果真的出現一樣的錯誤訊息了。