This process can be applied when you want to establish a TeamViewer access to a headless Ubuntu server without the need to install a Gnome desktop.
Step 1: Install TeamViewer and Google Chrome(optional) after the initial server setup:
sudo apt-get update && sudo apt-get upgrade
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
wget https://download.teamviewer.com/download/linux/teamviewer_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb
sudo apt install ./teamviewer_amd64.deb
Step 2: Set up TeamViewer and assign the device to your account. It will prompt you to login using your account and password:
sudo teamviewer setup
At this point, TeamViewer should be able to load and you should be able to log in. But the remote control screen is still the same command line screen since there is no desktop installed.
Step 3: Install a minimal dummy screen for TeamViewer to access to:
sudo apt install xserver-xorg-video-dummy
Step 4: Write 2 scripts to set up server based on how you want to access the server, either through connected monitor for onsite access or through TeamViewer for remote access:
# monitor.sh
sudo rm /usr/share/X11/xorg.conf.d/xorg.conf
# nomonitor.sh
sudo cp xorg.conf /usr/share/X11/xorg.conf.d/
The content of the actual configuration file:
# xorg.conf
Section "Device"
Identifier "Configured Video Device"
Driver "dummy"
EndSection
Section "Monitor"
Identifier "Configured Monitor"
HorizSync 31.5-48.5
VertRefresh 50-70
EndSection
Section "Screen"
Identifier "Default Screen"
Monitor "Configured Monitor"
Device "Configured Video Device"
DefaultDepth 24
SubSection "Display"
Depth 24
Modes "1366x768"
EndSubSection
EndSection
Step 5: Run one of these two scripts based on how you want to access the server (monitor connected or disconnected) and reboot the server.
Done!
P.S.: If you encountered the situation that the server will sleep after certain time (especially for those built on laptop motherboards where BIOS may have enabled S3 sleep mode), you can disable all the system suspension or sleep via:
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target