Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Start VNCServer on Mac1 EC2 Instance
# YouTube (english) : https://www.youtube.com/watch?v=FtU2_bBfSgM
# YouTube (french) : https://www.youtube.com/watch?v=VjnaVBnERDU
#
# On your laptop, connect to the Mac instance with SSH (similar to Linux instances)
#
ssh -i <your private key.pem> ec2-user@<your public ip address>
#
# On the Mac
#
# Set a password for ec2-user
sudo passwd ec2-user
# Enable VNC Server (thanks arnib@amazon.com for the feedback and tests)
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
-activate -configure -access -on \
-configure -allowAccessFor -specifiedUsers \
-configure -users ec2-user \
-configure -restart -agent -privs -all
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
-configure -access -on -privs -all -users ec2-user
exit
#
# On your laptop
# Create a SSH tunnel to VNC and connect from a vnc client using user ec2-user and the password you defined.
#
ssh -L 5900:localhost:5900 -C -N -i <your private key.pem> ec2-user@<your public ip address>
# open another terminal
open vnc://localhost
#
# On the mac EC2 instance, resize the APFS container to match EBS volume size
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-mac-instances.html#mac-instance-increase-volume
#
PDISK=$(diskutil list physical external | head -n1 | cut -d" " -f1)
APFSCONT=$(diskutil list physical external | grep "Apple_APFS" | tr -s " " | cut -d" " -f8)
sudo diskutil repairDisk $PDISK
# Accept the prompt with "y", then paste this command
sudo diskutil apfs resizeContainer $APFSCONT 0
#
# (optional) On the mac EC2 Instance, set screen resolution
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-mac-instances.html#mac-screen-resolution
#
brew tap jakehilborn/jakehilborn && brew install displayplacer
displayplacer list
displayplacer "id:<screenID> res:<width>x<height> origin:(0,0) degree:0"
RES="2560x1600"
displayplacer "id:69784AF1-CD7D-B79B-E5D4-60D937407F68 res:${RES} scaling:off origin:(0,0) degree:0"
@jbwasp
Copy link

jbwasp commented Dec 9, 2020

So far it worked for me at least for 200+ MAC users. Generates random vnc password when end user launch the app and terminates vnc service when session ended

@aidenmitchell
Copy link

aidenmitchell commented Dec 14, 2020

Is there a way to change the VNC resolution?

@sebsto
Copy link
Author

sebsto commented Dec 15, 2020

@aidenmitchell Currently you cannot increase the resolution. This was a deliberate choice as the mac1 instances are explicitly not designed as virtual workspaces, based on the macOS Software License Agreement

See Section 3 on page 6 "Leasing for Permitted Developer Services." "...Permitted Developer Services means continuous integration services, including but not limited to software development, building software from source, automated testing during software development, and running necessary developer tools to support such activities...."

@Sridhar-245
Copy link

Sridhar-245 commented Dec 18, 2020

I am running into an issue with Mac Catolina instance that I created. I created a Mac OS Catolina instance with root EBS volume with 75GB. But when I login to the machine and run "df -h", I dont see the EBS Volume (/dev/sda1) in the list and the EBS root volume is not mounted to root. The disk got partitioned to different volumes. Here is the result of df -h:

Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk2s5 30Gi 10Gi 2.4Gi 82% 488252 312036148 0% /
devfs 230Ki 230Ki 0Bi 100% 796 0 100% /dev
/dev/disk2s1 30Gi 15Gi 2.4Gi 86% 205594 312318806 0% /System/Volumes/Data
/dev/disk2s4 30Gi 2.0Gi 2.4Gi 46% 1 312524399 0% /private/var/vm
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /System/Volumes/Data/home
/dev/disk3s1 113Gi 7.6Gi 105Gi 7% 110 1181818330 0% /Volumes/SSD

When I try to install software like XCode, it says there is no enough space. Although I created the EBS volume with 75GB space, I dont find 75 GB space available in the above result. Is there any way we can mount the EBS volume to root and make the 75GB space available in the root. Please help.

@Sridhar-245
Copy link

Sridhar-245 commented Dec 18, 2020

Please ignore the Bold text. Its unintentional.

@sebsto
Copy link
Author

sebsto commented Dec 18, 2020

Hello @sridhar-25 Glad to see you managed to connect to the instance.

The last part of the gist has instructions to resize the APFS container and enjoy the full size of your EBS volume.

Repeated here for convenience :

PDISK=$(diskutil list physical external | head -n1 | cut -d" " -f1)
APFSCONT=$(diskutil list physical external | grep "Apple_APFS" | tr -s " " | cut -d" " -f8)
sudo diskutil repairDisk $PDISK
# Accept the prompt with "y", then paste this command
sudo diskutil apfs resizeContainer $APFSCONT 0

@Sridhar-245
Copy link

Sridhar-245 commented Dec 18, 2020

Hi @sebsto,

Thank you very much for providing the instructions to resize the APFS container. After running the above commands, I could see the full size of EBS volume mounted on root. I really appreciate the quick response and providing the right solution.....

Regards,
Sridhar.

@diogocsc
Copy link

diogocsc commented Feb 20, 2021

Thank you @sebsto for sharing this.
I'm running the terminal on a Windows 10 OS.
After I create the tunnel on port 5900 I get the message "bind [127.0.0.1]:5900: Permission denied" .
Should I worry about this ? If so, what can I do to get it right?

Then I open another terminal on Windows and try to run "open vnc://localhost" I get:
'open' is not recognized as an internal or external command,
operable program or batch file.

Where should I be running the open command ?

@sebsto
Copy link
Author

sebsto commented Feb 20, 2021

@diogocsc the first error seems to indicate the VNC server is not started on macOS. Please check that one before proceeding.
On Windows, open vnc:// ... will not work. You can use any graphical VNC client instead. Check RealVNC or TightVNC for example.

@Sridhar-245
Copy link

Sridhar-245 commented Feb 26, 2021

Hi @sebsto, I need your assistance. Here is the situation. I am currently using AWS Mac-mini instance for which the EBS Volume is about 90Gi. We have decided to buy a Mac-mini desktop computer for cost reasons. Is there any way I can reuse the content in EBS volume for new mac-mini by cloning it instead of configuring the new mac-mini machine from the scratch ? Thanks in advance!

@sebsto
Copy link
Author

sebsto commented Feb 26, 2021

Hello @Sridhar-245,
I don't know any tool able to do that.
It is possible to write such a tool using the EBS block level API https://aws.amazon.com/blogs/aws/new-programmatic-access-to-ebs-snapshot-content/ but would require some programming effort

The best practice I am trying to follow is to script all my mac1 instance installation (incl XCode and the development tools), so that I can reproduce my setup on any machine, in the cloud or on my desktop.

@Sridhar-245
Copy link

Sridhar-245 commented Feb 26, 2021

Thank you @sebsto. I will try to go through the API mentioned above will try to find out if there is a way to recreate the configuration.

@chrisgalousis
Copy link

chrisgalousis commented Mar 18, 2021

vnc is too slow. is the another way to access the mac instance from a windows machine?

@sebsto
Copy link
Author

sebsto commented Mar 18, 2021

@chrisgalousis : ssh is always my preferred way. Have a look at https://github.com/sebsto/amplify-ios-getting-started/tree/main/code for full instructions how to install Xcode, build and sign your applications, just using the command line

@chrisgalousis
Copy link

chrisgalousis commented Mar 18, 2021

Thank you @sebsto for your prompt reply. What I am trying to do is testing Microsoft Office files in Mac iOS. When I am in the gui environment thru VNC viewer, everything is too slow... Just to drag a window from place to place it needs 5-6 seconds!!... Is there another remote way?

@sebsto
Copy link
Author

sebsto commented Mar 18, 2021

Not that I am aware of. VNC is natively integrated into macOS. You might try to start the EC2 instance in a region closer from where you are to reduce latency. (I personally have no such lag between Europe and us-east-2) or explore proprietary remote connection applications that allow to take control of a mac remotely, like the ones listed here https://machow2.com/rdp-for-mac/

@chrisgalousis
Copy link

chrisgalousis commented Mar 18, 2021

@sebsto perfect! So, I can access the GUI with another remote software, nice!
Also, I have another question. I haven't executed the last part:

_PDISK=$(diskutil list physical external | head -n1 | cut -d" " -f1)
APFSCONT=$(diskutil list physical external | grep "Apple_APFS" | tr -s " " | cut -d" " -f8)
sudo diskutil repairDisk $PDISK

sudo diskutil apfs resizeContainer $APFSCONT 0_
Is that necessary? I run a Win10 laptop.
Update: I have tried the AnyDesk app and I couldn't do anything. I couldn't click anywhere just move the mouse cursor around. Is there anything to do with the vnc? Like a conflict?...

@sebsto
Copy link
Author

sebsto commented Mar 19, 2021

@chrisgalousis : these commands resize the file system of the mac to match the EBS volume size. They need to be executed on the mac EC2 instance. It doesn't matter if you are using Windows, Mac or Linux on your laptop.

I don't know anything about non VNC based remote desktop app, I would suggest to reach out the user community or software editor to get some support about these tools

@chrisgalousis
Copy link

chrisgalousis commented Mar 19, 2021

@sebsto where should I run those commands? (the last part) on my laptop or in my Mac instance?

@sebsto
Copy link
Author

sebsto commented Mar 19, 2021

As explained in the message above

They need to be executed on the mac EC2 instance

And on the initial gist on the top of this page, its says On the mac

#
# On the mac, resize the APFS container to match EBS volume size
#

PDISK=$(diskutil list physical external | head -n1 | cut -d" " -f1)
APFSCONT=$(diskutil list physical external | grep "Apple_APFS" | tr -s " " | cut -d" " -f8)
sudo diskutil repairDisk $PDISK
# Accept the prompt with "y", then paste this command
sudo diskutil apfs resizeContainer $APFSCONT 0

@chrisgalousis
Copy link

chrisgalousis commented Mar 19, 2021

thank you @sebsto
Update: I have tried Anydesk as a remote app. I can see now a better response.

@glebsonik
Copy link

glebsonik commented Mar 22, 2021

@sebsto after I followed these steps and trying to create a tunnel by command below

ssh -L 5900:localhost:5900 -C -N -i ec2-user@

then I am facing an error that

bind [127.0.0.1]:5900: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 5900
Could not request local forwarding

@sebsto
Copy link
Author

sebsto commented Mar 22, 2021

@glebsonik : this is because port 5900 on your local machine is already used. Maybe you have Screen Sharing enabled ?
You can just use a different port :

ssh -L 5555:localhost:5900 -C -N -i ec2-user@IP_ADDRESS

Then

open vnc://localhost:5555

@glebsonik
Copy link

glebsonik commented Mar 22, 2021

Thanks a lot @sebsto !!! It works!!!
Cnn this access, that I provided for vnc, be disabled somehow to avoid some insecurity issues?

@sebsto
Copy link
Author

sebsto commented Mar 22, 2021

@glebsonik. Yes you can disable screen sharing by going to System Preferences => Sharing
https://support.apple.com/en-gb/guide/mac-help/mh11848/mac

or using the same command in the terminal with the option « access off »

@b3kN
Copy link

b3kN commented Mar 31, 2021

This is fantastic and was very informative for me.

I was becoming very frustrated with not being able to get Xcode installed on AWS mac1.metal AMI for build processes and this helped to cure many headaches.

Understanding that the SSH command opens a tunnel to localhost for my RealVNC was very helpful, as well as the commands to sync up the available disk space after increasing the storage volume.

Thanks for this Gist @sebsto

@scottmalkie
Copy link

scottmalkie commented May 3, 2021

You can change the screen resolution with EC2 Mac Instances now: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-mac-instances.html#mac-screen-resolution

@sebsto
Copy link
Author

sebsto commented May 4, 2021

Thank you @scottmalkie for the update

@damusix
Copy link

damusix commented Jun 7, 2022

This was absolutely vital and the missing part of documentation for Github runners. Big thanks to everyone who contributed to these efforts.

@HarryWeppner
Copy link

HarryWeppner commented Jun 10, 2022

@scottmalkie thanks for the great info. However, it appears displayplacer list only shows a connection after a VNC connection is established. Are you aware of an approach that provides native display capabilities (as a macOS equivalent to Xfvb) without requiring a connection to be opened? Note: we're exploring using Macs in a CI/CD context.

On a related note, I saw a comment that M1 Macs may support that out of the box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment