Daydreaming in Brookline, MA

MacからVirtualBoxのFedora VMにsshする

1 はじめに

私はMacにVirtualBoxを入れ、Fedora VM上でドキュメント(orgファイル)の編集やpythonのスクリプト開発を行っていますが、 このワークアラウンド (2. VirtualBox in Low Resolution) のために解像度が粗く、見た目がやや残念なことになっています。そこで、Mac上のターミナルからFedora VMにssh接続することを考えました。

2 VMにsshで入るための設定

2.1 ssh serverの設定

まずはVirtualBoxのFedora 35 VM上でsshサーバーの設定をします。

sudo systemctl status sshd  # sshサーバーが入っていないことを確認する
sudo dnf install openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd  # 設定が終わりました
ssh <username>@localhost  # 自分自身にsshで入れることを確認する

2.2 VMネットワークの設定

次に、VirtualBoxのVMに、ホストであるMacからつながるようにネットワーク設定をします。

  • VMがpowerd offであることを確認 > Settings > Network
  • > Advancedを展開 > Port Forwardingをクリック
  • > + 付きのネットワークカードのようなアイコンをクリック
    • Name: <自由に付ける>
    • Protocol: TCP
    • Host IP: 127.0.0.1
    • Host Port: 2222 # Mac側はポート2222番を使います
    • Guest IP: <空欄>
    • Guest Port: 22
  • > OKをクリック > VM起動

実際にMacのターミナルからsshしてみます。ポート2222番を指定します。

~ % ssh 127.0.0.1 -p 2222
The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
ECDSA key fingerprint is SHA256:/oidgNx7M..<snip>..5TB7x6Q/A.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[127.0.0.1]:2222' (ECDSA) to the list of known hosts.
<username>@127.0.0.1's password: 
Last login: Mon May  9 11:32:14 2022
[<username>@fedora ~]$

パスワードを聞かれましたが、無事にsshでログインできることが確認できました。

2.3 sshキーのインポート

パスワードレスでssh接続したいので、Mac上のsshパブリックキーをVMにコピーします。まずは.sshディレクトリを作成してそこにsshキーを生成します。すでに作成済みであればスキップしてください。

~ % mkdir .ssh
~ % chmod 700 .ssh  # パーミッションを700に設定します
~ % cd ~/.ssh
~/.ssh % ssh-keygen -t rsa -C "myname@local" -b 4096 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/<xxxxx>/.ssh/id_rsa): /Users/<xxxxx>/.ssh/id_rsa_fedora35
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/<xxxxx>/.ssh/id_rsa_fedora35.
Your public key has been saved in /Users/<xxxxx>/.ssh/id_rsa_fedora35.pub.
The key fingerprint is:
SHA256:WK05aFso3..<snip>...gUQo9TZjBmdes myname@local
The key's randomart image is:
+---[RSA 4096]----+
| .O*X*+..        |
| ooX=. . +       |
<snip>
|       .         |
|                 |
+----[SHA256]-----+
~/.ssh/ %

そして、作成したパブリックキーをsshサーバーであるFedora VMにコピーします。 そのためには ssh-copy-id コマンドを使います。ポート番号は -p オプションで与えます。

~/.ssh % ssh-copy-id -i id_rsa <username>@127.0.0.1 -p 2222
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
<username>@127.0.0.1's password: <パスワードを入力します>

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh -p '2222' '<username>@127.0.0.1'"
and check to make sure that only the key(s) you wanted were added.

~/.ssh %

ログインしてみます。

~/.ssh % ssh 127.0.0.1 -p 2222
Last login: Mon May  9 12:56:51 2022 from 10.0.2.2
[<username>@fedora ~]$

パスワード入力無しでssh接続できました。