Connect to an Amazon EC2 Mac instance
Learn how to remotely connect to an EC2 Mac instance
About | |
---|---|
✅ AWS experience | Beginner - 100 |
⏱ Time to complete | 20 minutes |
💰 Cost to complete | $22 for 24 hours |
🧩 Prerequisites | An AWS Account |
💻 Code Sample | - none - |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? |
⏰ Last Updated | 2023-06-28 |
Note that there are some cases where the instance is already configured at start (this article shows you how to configure an instance ready to use). In those cases, there is no need to remotely connect to your instance. It is perfectly possible to start and use a pre-configured EC2 Mac instance without ever connecting to it. But for this tutorial, we assume you started the EC2 Mac instance from one of the AWS-provided Amazon Machine Image (AMI) and you have a clean macOS installation available.
- at launch time, you have specified the SSH keypair that will be used for user authentication. Note that this can only be done at launch time. If you forgot to do it launch, you may terminate your instance and start a new one, on the same dedicated host (it might take a while to scrub the machine when you terminate it, and before it becomes available again).
- the instance has been launched on a network (VPC) that has public network connectivity (a public subnet of the VPC), and your instance has a public IP address. These two parameters are the default values when launching an EC2 instance.
- at launch time, or afterwards, you have associated a Security Group with your EC2 Mac instance. The security Group is configured to authorize inbound TCP 22 (SSH) traffic from your laptop IP address or your client network IP range.
ec2-user
and you have to use the -i
option to refer to your private key, stored on your laptop.mac1.metal
instance running) - your IP address will be different than 1.0.0.0
used below:1
2
aws ec2 describe-instances \
--query "Reservations[].Instances[? InstanceType == 'mac1.metal'].NetworkInterfaces[][].Association.PublicIp"
1
2
3
4
# Response
[
1.0.0.0
]
pem
file with the -i
option and use the default username AWS created for you on macOS: ec2-user
. In this example, we connect to a Big Sur instance (please remember to replace 1.0.0.0
with your instance's IP address from the previous command).1
2
3
4
5
6
7
8
9
10
11
ssh -i ./path_to/my_private_key.pem ec2-user@1.0.0.0
Last login: Fri Jul 1 12:07:28 2022 from 52.95.4.11
┌───┬──┐ __| __|_ )
│ ╷╭╯╷ │ _| ( /
│ └╮ │ ___|\___|___|
│ ╰─┼╯ │ Amazon EC2
└───┴──┘ macOS Big Sur 11.6.6
ec2-user@ip-172-31-44-83 ~ %
ec2-user
is included in the /etc/sudoers
file and you can elevate privileges to root with the sudo
command, without a password.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# First create the trust policy file
cat << EOF > ec2-role-trust-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}
]
}
EOF
# Second, create role
aws iam create-role \
--role-name ssmAccess \
--assume-role-policy-document file://ec2-role-trust-policy.json
# Response
{
"Role": {
"Path": "/",
"RoleName": "ssmAccess",
"RoleId": "AROAXCTVZHOCU72YCLAQT",
"Arn": "arn:aws:iam::123456789012:role/ssmAccess",
"CreateDate": "2022-07-01T15:52:13+00:00",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
}
}
arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
.1
2
3
aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore \
--role-name ssmAccess-Profile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Create an instance profile
aws iam create-instance-profile \
--instance-profile-name ssmAccess-Profile
# Response
{
"InstanceProfile": {
"Path": "/",
"InstanceProfileName": "ssmAccess-Profile",
"InstanceProfileId": "AIPAXCTVZHOC4QNUPTLZT",
"Arn": "arn:aws:iam::123456789012:instance-profile/ssmAccess-Profile",
"CreateDate": "2022-07-01T16:02:52+00:00",
"Roles": []
}
}
# Attach the role to the profile
aws iam add-role-to-instance-profile \
--instance-profile-name ssmAccess-Profile \
--role-name ssmAccess
# Search for my EC2 Mac Instance Id, search by name=macOS Monterey
INSTANCE_ID=$(aws ec2 describe-instances \
--filter "Name=tag:Name,Values=macOS Monterey" \
--query "Reservations[].Instances[?State.Name == 'running'].InstanceId[]" \
--output text)
# Associate the profile to the instance
aws ec2 associate-iam-instance-profile \
--instance-id $INSTANCE_ID \
--iam-instance-profile Name=" ssmAccess-Profile"
# Response
{
"IamInstanceProfileAssociation": {
"AssociationId": "iip-assoc-07d308386ff04f72d",
"InstanceId": "i-01e833b396e0cbf02",
"IamInstanceProfile": {
"Arn": "arn:aws:iam::123456789012:instance-profile/ssmAccess-Profile",
"Id": "AIPAXCTVZHOC4QNUPTLZT"
},
"State": "associating"
}
}
1
2
3
4
5
6
7
# Search for my EC2 Mac Instance Id, search by name=macOS Monterey
INSTANCE_ID=$(aws ec2 describe-instances \
--filter "Name=tag:Name,Values=macOS Monterey" \
--query "Reservations[].Instances[?State.Name == 'running'].InstanceId[]" \
--output text)
aws ssm start-session --target $INSTANCE_ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Starting session with SessionId: sst-0a9c1047a20fdbd7c
sh-3.2$ uname -a
Darwin ip-172-31-32-67.us-east-2.compute.internal 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
sh-3.2$ diskutil list
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *121.3 GB disk0
/dev/disk1 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *214.7 GB disk1
1: EFI EFI 209.7 MB disk1s1
2: Apple_APFS Container disk2 214.5 GB disk1s2
/dev/disk2 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +214.5 GB disk2
Physical Store disk1s2
1: APFS Volume Macintosh HD - Data 48.3 GB disk2s1
2: APFS Volume Preboot 267.0 MB disk2s2
3: APFS Volume Recovery 1.1 GB disk2s3
4: APFS Volume Macintosh HD 15.2 GB disk2s4
5: APFS Snapshot com.apple.os.update-... 15.2 GB disk2s4s1
6: APFS Volume VM 20.5 KB disk2s6
sh-3.2$ id
uid=502(ssm-user) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),701(com.apple.sharepoint.group.1),100(_lpoperator)
exit
.ssm-user
(and not ec2-user
as with SSH). Both users are included in the /etc/sudoers
file and you can elevate privileges to root with the sudo
command, without using a password.ssh
to use aws ssm start-session
command as proxy command when SSH'ing to your hosts. If you're interested, or just curious, check out this documentation. ec2-user
, then start the Apple Remote Desktop server component.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# NB: These commands are when using SSH to connect, when connected with SSM, switch to ec2-user first with 'su -c ec2-user'
# set a password to ec2-user
sudo passwd ec2-user
# enable ARD
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
-activate -configure -access -on \
-restart -agent -privs -all
# Response
Starting...
Warning: macos 10.14 and later only allows control if Screen Sharing is enabled through System Preferences.
Activated Remote Management.
Stopped ARD Agent.
ec2-user: Set user remote control privileges.
ec2-user: Set user remote access.
ssm-user: Set user remote control privileges.
ssm-user: Set user remote access.
Done.
localhost:5900
).1
2
# We assume the EC2 Mac instance public IP address is 1.0.0.0
ssh -C -N -L 5900:localhost:5900 -i /path/my-key-pair.pem ec2-user@1.0.0.0
-i
, allows you to specify the private part of the keypair used for authentication-L
is the SSH tunneling option. It tells the SSH client on your machine to start to listen to incoming connections on TCP port 5900 (5900:
), and to forward all traffic received to the destination host (1.0.0.0
). Once on the destination host, to send the traffic tolocalhost:5900
which is the address of the ARD server.-C
tells SSH to compress the traffic in the tunnel-N
tells SSH to not start an interactive session on the client. The command blocks until you interrupt it withCtrl-C
localhost
. The SSH tunnel will act as a server and forward all the traffic to the actual ARD server running on your EC2 Mac instance on the other side of the tunnel.1
open vnc://localhost
Note that the URI starts withvnc://
because Apple Remote Desktop client is based on Virtual Network Computing (VNC) protocol.
ec2-user
) and the password you choose when you enabled ARD.displayplacer
, the open source command line tool developed by Jake Hilborn.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# install the displaylist command line tool
brew tap jakehilborn/jakehilborn && brew install displayplacer
# then list available resolutions
displayplacer list
Persistent screen id: 69784AF1-CD7D-B79B-E5D4-60D937407F68
Contextual screen id: 1020887298
Type: 24 inch external screen
Resolution: 1024x768
Hertz: 60
Color Depth: 8
Scaling:off
Origin: (0,0) - main display
Rotation: 0
Resolutions for rotation 0:
mode 0: res:1024x768 hz:60 color_depth:4
mode 1: res:1024x768 hz:60 color_depth:8 <-- current mode
mode 2: res:640x480 hz:60 color_depth:4
mode 3: res:640x480 hz:60 color_depth:8
mode 4: res:720x480 hz:60 color_depth:4
mode 5: res:720x480 hz:60 color_depth:8
mode 6: res:640x480 hz:60 color_depth:4
mode 7: res:640x480 hz:60 color_depth:8
mode 8: res:800x600 hz:60 color_depth:4
mode 9: res:800x600 hz:60 color_depth:8
mode 10: res:1280x720 hz:60 color_depth:4
mode 11: res:1280x720 hz:60 color_depth:8
mode 12: res:1440x900 hz:60 color_depth:4
mode 13: res:1440x900 hz:60 color_depth:8
mode 14: res:1680x1050 hz:60 color_depth:4
mode 15: res:1680x1050 hz:60 color_depth:8
mode 16: res:1920x1080 hz:60 color_depth:4
mode 17: res:1920x1080 hz:60 color_depth:8
mode 18: res:1920x1200 hz:60 color_depth:4
mode 19: res:1920x1200 hz:60 color_depth:8
mode 20: res:2560x1440 hz:60 color_depth:4
mode 21: res:2560x1440 hz:60 color_depth:8
mode 22: res:2560x1600 hz:60 color_depth:4
mode 23: res:2560x1600 hz:60 color_depth:8
mode 24: res:1024x576 hz:60 color_depth:4
mode 25: res:1024x576 hz:60 color_depth:8
Execute the command below to set your screens to the current arrangement:
displayplacer "id:69784AF1-CD7D-B79B-E5D4-60D937407F68 res:1024x768 hz:60 color_depth:8 scaling:off origin:(0,0) degree:0"
# Lastly, set the display resolution. We will choose 1440x900
displayplacer "id:69784AF1-CD7D-B79B-E5D4-60D937407F68 res:1440x900 origin:(0,0) degree:0"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# first, select the instance ID of the machine you want to connect
# the below command search for a machine named "macOS Monterey"
INSTANCE_ID=$(aws ec2 describe-instances \
--filter "Name=tag:Name,Values=macOS Monterey" \
--query "Reservations[].Instances[?State.Name == 'running'].InstanceId[]" \
--output text)
# second, start the SSM tunnel
aws ssm start-session --target $INSTANCE_ID \
--document-name AWS-StartPortForwardingSession \
--parameters '{"portNumber":["5900"],"localPortNumber":["5900"]}'
Starting session with SessionId: sst-0f3b970f24182795d
Port 5900 opened for sessionId sst-0f3b970f24182795d.
Waiting for connections...
1
open vnc://localhost
aws ssm
command with Ctrl-C
.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.