Amazon EC2 Mac Instance上のmacOSのデスクトップにVNCでログインしてみた #reinvent

2020.12.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

re:Invent 2020のAWS Late Night Week 1でMac Instanceが発表と同時にGAになりました。

SSHアクセスについてはもこさんが速攻で以下のブログをアップされたので、

【速報】EC2がMac対応! Amazon EC2 Mac Instancesがリリースされたので触ってみた #reinvent

VNCアクセスを試してみます。

Macインスタンスの起動

まずは、もこさんのブログの手順に沿って、Macインスタンスを起動して、SSHでログインします。

【速報】EC2がMac対応! Amazon EC2 Mac Instancesがリリースされたので触ってみた #reinvent

VNCサーバーの有効化

MacインスタンスでVNCサーバーを有効化します。以下の通りコマンドを順番に実行します。こちらの記事を参考にしました。3ステップ目の-vncpw XXXXXXXXXXXの部分にはVNCログイン時のパスワードを設定します。

ec2-user@ip-172-31-7-248 ~ % sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -allowAccessFor -allUsers -privs -all
Starting...
Setting allow all users to YES.
Setting all users privileges to 1073742079.
Done.
ec2-user@ip-172-31-7-248 ~ % sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -clientopts -setvnclegacy -vnclegacy yes

Starting...
Set the client options.
Done.
ec2-user@ip-172-31-7-248 ~ % sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -clientopts -setvncpw -vncpw XXXXXX

Starting...
Set the client options.
Done.
ec2-user@ip-172-31-7-248 ~ % sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -restart -agent -console

Starting...
Done.
ec2-user@ip-172-31-7-248 ~ % sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate
Starting...
Warning: macos 10.14 and later only allows control if Screen Sharing is enabled through System Preferences.
Activated Remote Management.
Done.

最後のコマンド実行で以下のような警告が出たので、

Warning: macos 10.14 and later only allows control if Screen Sharing is enabled through System Preferences.

こちらの記事を参考にしてScreen Sharingを有効化しました。

ec2-user@ip-172-31-7-248 ~ % sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false
ec2-user@ip-172-31-7-248 ~ % sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
/System/Library/LaunchDaemons/com.apple.screensharing.plist: service already loaded

ec2-userパスワードの変更

SSHでログインしたec2-userのmacOSのデフォルトパスワードがわからなかったので、こちらの記事を参考にコマンドでリセットしてしまいました。

sudo /usr/bin/dscl . -passwd /Users/ec2-user XXXXXX

VNCログイン

MacBookProのFinderアプリのVNCクライアントでログインしてみました。

SSHポートフォワーディングによるVNC接続

SSHトンネル越しにVNC接続する場合は、SSH接続を行う際に以下のようにLオプションでローカルポートフォワーディングを指定します。macOS側のデフォルトのVNCポート番号は5900でした。

ssh -L 5901:localhost:5900 -i PEM_FILE ec2-user@INSTANCE_IP

まとめ

Amazon EC2 Mac Instance 上のmacOSデスクトップにVNCでログインしてみました。クラウド上でもmacOSを使える日がついにやって来ました!

参考