How telecommuting or remote flexible working is becoming more and more popular in the tech world. One technology behind this trend is remote desktop. Your desktop environment is in the cloud, and you can access your remote desktop anywhere you go, or at home or at work.
This tutorial describes how to set up a CentOS-based remote desktop in a VPS. Now, we will first show the basic environment of CentOS.
We assume you have created a CentOS 7 VPS instance (eg, using DigitalOcean or Amazon EC2). Please make sure your VPS instance has at least 1GB of RAM. Otherwise, CentOS will crash when you access the remote desktop.
Step 1: Install CentOS Desktop
If the CentOS version you are currently installing is a minimal version without a desktop, you will need to install a desktop on your VPS first (eg GNOME). For example, DigitalOcean's image is a minimal version, which requires a desktop GUI to be installed as follows
code show as below:
Restart the VPS after the installation is complete.
Step 2: Install and configure the VNC server
The next step is to install and configure the VNC server. We are using TigerVNC, an open source VNC service implementation.
code show as below:
Now create a user account (eg: xmodulo) to access the remote desktop.
code show as below:
# passwd xmodulo
When a user tries to access a remote desktop using VNC, the VNC daemon is started to handle the request. This means you need to create a separate VNC profile for each user.
CentOS relies on systemd to manage and configure system services. So we will use systemd to configure the VNC server for user xmodulo.
First let's check the status of the VNC server using any of the following commands.
code show as below:
By default, the newly installed VNC service is not activated (disabled).
Now copy a generic VNC service file to create a VNC service configuration for user xmodulo.
code show as below:
Open the configuration file with the text editor and replace under [Service] with the actual username (eg: xmodulo). same. Append the "-geometry" parameter to ExecStart. Finally, modify the lines "ExecStart" and "PIDFile" below.
code show as below:
<p> [Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c ‘/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :’
ExecStart=/sbin/runuser -l xmodulo -c “/usr/bin/vncserver %i -geometry 1024×768”
PIDFile=/home/xmodulo/.vnc/%H%i.pid
ExecStop=/bin/sh -c ‘/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :’
Now set a password for user xmodulo (optional). First switch to that user and run the vncserver command.
code show as below:
# vncserver
You will be prompted for the user's VNC password. After the password is set, you will need to use this password to access your remote desktop next time.
Finally, reload the service to make the new VNC configuration take effect:
code show as below:
Automatically start the VNC service at boot:
code show as below:
Check the port the vnc service is listening on:
# netstat -tulpn | grep vnc
Port 5901 is the default port used by VNC clients to connect to the VNC server.
Step 3: Connect to the Remote Desktop via SSH
The Remote Frame Buffer (RFB) used by VNC is not a secure protocol by design, so it is not a good idea to connect directly to the VNC server on the VNC client. Any sensitive information such as passwords can be easily leaked in VNC traffic. Therefore, I strongly recommend using an SSH tunnel to encrypt your VNC traffic.
On the local machine where you want to run the VNC client, use the following command to create an SSH tunnel to the remote VPS. When asked to enter an SSH password, enter the user's password.
code show as below:
Replace "xmodulo" with your own VNC username and fill in your own VPS IP address.
Once the SSH tunnel is established, remote VNC traffic is routed through the ssh tunnel and sent to 127.0.0.1:5901.
Now start your favorite VNC client (eg: vinagre) and connect to 127.0.0.1:5901.
You will be asked to enter the VNC password. When you enter the VNC password, you can securely connect to the CentOS remote desktop.
Then you will see the display as shown in the title image.