はじめに
VirtualBox上のゲストOSへ、ホストOSのMacからssh接続する方法について調べた。 web上では情報が錯綜しているため、ChatGPTを用いて動作を確認した。
環境
macOS; 12.6.8 VirtualBox; 7.0.8 r156879 ubuntu; 22.04.3
方法
(1)VirtualBoxの設定(ぽートフォワーディングを使用する)
VirtualBox のメインウィンドウで、対象の VM を選択して「設定」をクリックする。
左側のメニューから「ネットワーク」を選択する。
「アダプター1」タブ (または SSH を使用する予定のアダプター) を確認し、接続方法が「NAT」に設定されていることを確認する。
「詳細」を展開し、「ポートフォワーディング」ボタンをクリックする。
ポートフォワーディングルールウィンドウで、右側の緑の「+」ボタンをクリックして新しいルールを追加する。
以下の情報を入力または確認する:
- **名前**: 任意の名前 (例: "SSH") - **プロトコル**: TCP - **ホストIP**: 通常は空白のまま - **ホストポート**: 2222 (またはホストで使用したい他のポート) - **ゲストIP**: 通常は空白のまま - **ゲストポート**: 22 (SSHのデフォルトポート)
- 「OK」をクリックしてルールを保存し、再び「OK」をクリックして設定を閉じる。
(2)ssh接続する Macのターミナルを開く以下のコマンドを実行する。
ssh -p 2222 [ゲストマシンのユーザー名]@127.0.0.1 (注意:小文字のp) or ssh -p 2222 [ゲストマシンのユーザー名]@localhost
Introduction
I investigated how to make an ssh connection to a guest OS on VirtualBox from a Mac on the host OS. Since information on the web is mixed, I confirmed the operation using ChatGPT.
Environment
macOS; 12.6.8 VirtualBox; 7.0.8 r156879 ubuntu; 22.04.3
Method
(1) VirtualBox configuration (using pote forwarding)
in the main window of VirtualBox, select the target VM and click "Settings". 2.
Select "Network" from the menu on the left side. 3.
Check the "Adapter 1" tab (or the adapter you plan to use SSH with) and make sure the connection method is set to "NAT". 4.
Expand "Advanced" and click the "Port Forwarding" button. 5.
In the Port Forwarding Rules window, click the green "+" button on the right to add a new rule. 6.
- **Name**: any name (e.g., "SSH") - **Protocol**: TCP - **Host IP**: Normally left blank - **Host Port**: 2222 (or any other port you wish to use on your host) - **Guest IP**: usually left blank - **Guest Port**: 22 (default port for SSH)
- Click "OK" to save the rule and "OK" again to close the configuration.
(2) Make an ssh connection Run the following command that opens a terminal on your Mac.
ssh -p 2222 [guest machine user name]@127.0.0.1 (Note: lower case p) OR ssh -p 2222 [guest machine username]@localhost