TeamViewer "FAILED TO establish the default connection to the WindowServer" I need TeamViewer on my Mac to work remotely. I installed it via ssh using the command line

brew cask install teamviewer


which worked fine.

However, I seem to be unable to configure TeamViwer from the command line. Due to Corona, I cannot personally access the Mac at the moment, which means I have to do everything via ssh.

I tried

$ /Applications/TeamViewer.app/Contents/MacOS/TeamViewer


which fails with


  _RegisterApplication(), FAILED TO establish the default connection to the WindowServer, _CGSDefaultConnection() is NULL. 
  Could not find domain for 
  Could not find domain for 
  2020-03-23 18:53:18.935 TeamViewer[1695:9920] +[NSXPCSharedListener endpointForReply:withListenerName:]: an error occurred while attempting to obtain endpoint for listener 'ClientCallsAuxiliary': Connection interrupted


and then stops until I exit with Ctrl+C.

I also tried this answer, which gave me

$ open /Applications/TeamViewer.app



  LSOpenURLsWithRole() failed with error -610 for the file /Applications/TeamViewer.app.


and 

$ sudo open /Applications/TeamViewer.app



  LSOpenURLsWithRole() failed with error -10810 for the file /Applications/TeamViewer.app.


Furthermore, the path Library/Preferences/com.teamviewer.* does not exist.

What do I have to do next? What else can I try?
This should work (sudo privs required!)


Login with ssh on the remote Mac
Enter:

sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart  -activate -configure -access -on -privs -all -restart -agent 


to enable remote management 
open Screen Sharing locally (/System/Library/CoreServices/Applications/Screen Sharing.app)
Enter the IP of the remote Mac
Enter your remote user creds
Log in with your remote user creds at the login screen
Launch and configure TeamViewer


Depending on the security settings of the remote network (firewall etc.) you have to use the same tunnel/channel as the ssh connection.

The steps above only work for macOS up to High Sierra (10.13.6).



In macOS Mojave and later kickstart only works with managed devices and special profiles, if screen sharing wasn't enabled previously:

Use the kickstart command-line utility in macOS Mojave 10.14 and later

Eventually, you have to access the remote Mac personally and set up screen sharing in System Prefs > Sharing (to access it via VNC) or launch and configure TeamViewer.