How to setup Ubuntu Desktop on Headless Server
I have an issue when trying to setup a build server for Unity Project on Ubuntu Server because Unity, although it can be build to command line interface, has to be installed through GUI (Unity Hub). The only way I can achieve my goal is to get GUI running on the Ubuntu server, then I can remote desktop to setup Unity Hub.
Headless server is a server machine that doesn’t connect to any monitor
You need 2 machines:
- Client - this machine needs a remote desktop app
- Server - which is headless and no GUI is installed
On Server Machine
- Install ubuntu-desktop for a GUI software that’s needed to run desktop
sudo apt install ubuntu-desktop
- Install tightvncserver to be able to serve remote desktop
sudo apt install tightvncserver
- Run vncserver for the first time using command
vncserver
- Set the password and ‘Enter’ untill setup is completed
- To kill the server, type
vncserver -kill :1
Number 1 can be modified to match the XSERVER number that you what to kill. (if you type vnc server again without killing the previous ones, more XSERVER will be generated) - The server No.1 is available at port 5901
- Kill x-server
- Run vncserver for the first time using command
- Setup vncserver by running editing file
nano ~/.vnc/xstart
Replace the original content with following setting.[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources xsetroot -solid grey vncconfig -iconic & x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME" export XDG_MENU_PREFIX="gnome-flashback-" gnome-session --session=gnome-flashback-metacity --disable-acceleration-check &
Save and exit nano.
- Create .XResources file at home directory
touch ~/.Xresources
- Start the server by running
vncserver -geometry 1920x1080
Desktop resolution is specified by argument after -geometry. - To show the log, type
tail -f ~/.vnc/{yourServerName}:1.log
- Now your Vncserver (the graphic server that render your desktop) is serving at port 5901. However, we don’t want to have our server exposing all ports. Later on we will forward 5901 connection on server to ssh tunnel
On Client Machine
Assuming you have Ubuntu desktop running in this machine.
- Install Vinagre (Most of the time you already have it installed)
sudo apt install vinagre
Once the installation is completed, the program is called “Remote Desktop Viewer” in the app menu.
- On open terminal and type this command
ssh -L 5901:127.0.0.1:5901 -C -N -l {vncPasswrod} {serverAddress}
The above command bind 5901 on server to 5901 on this client via ssh connection.
- Open remote desktop app (Vinagre we installed eariler)
- Click Connect
- Protocol choose “VNC”
- Under Host type “127.0.0.1:5901”
- Check “Scaling” and “Keep aspect ratio”
- Click Connect
You should be able to use the desktop. On Vinagre, make sure you have “View > Keyboard shortcuts” checked, otherwise you can’t get out of full screen mode (F11).
On Windows client, I use TightVNC as remote desktop client.