Enable SSH and VNC Server on Ubuntu 18.04

SSH and VNC Server setup on Ubuntu 18.04 LTS

Install SSH

SSH by default is not installed after system setup. Follow this link to install it:

https://linuxconfig.org/enable-ssh-on-ubuntu-18-04-bionic-beaver-linux.

Here I put my steps from that post:

  1. In order to enable ssh on Ubuntu Linux, we first need to perform an SSH package installation. Open up terminal and enter command:
    $ sudo apt install ssh
  2. At this stage you should be able to login from this host to any other SSH enabled Ubuntu server. For example, to log into a server with host name ubuntu-server as user linuxconfig, just enter:
    $ ssh linuxconfig@ubuntu-server
  3. Confirm that SSH server is up and running by executing the bellow command. Look for keyword Active: active (running). Press q if you need to get your command line prompt back:
    $ service ssh status

Enable/Disable SSH on Ubuntu

  1. In an event that you need to temporarily disable SSH on your Ubuntu host execute:
    $ sudo service ssh stop
  2. To start ssh again, run:
    $ sudo service ssh start
  3. In order to completely disable SSH after reboots execute:
    $ sudo systemctl disable ssh
  4. To enable SSH again on your Ubuntu host use command:
    $ sudo systemctl enable ssh

Enable VNC server on Ubuntu

Follow this link to install and enable VNC server. I found some difference from my machine and have marked it below.

https://linuxconfig.org/vnc-server-on-ubuntu-18-04-bionic-beaver-linux

Ubuntu VNC server setup

Let's start by the installation of the VNC server and the Xfce desktop manager core files:

$ sudo apt install vnc4server xfce4 xfce4-goodies

Once the VNC server is installed we can begin the configuration by setting up a user password to be used by a VNC client when creating a remote connection:

$ vncpasswd

It will ask you set up a password for desktop access. Remember this as later you need it when you try to connect to this server from your vnc viewer. This will also create a folder ~/.vnc and put a file 'passwd' inside it.

Next, create the ~/.vnc/xstartup file to start the Xfce4 desktop:

$ mkdir ~/.vnc  (vncpasswd command already create this folder in my case)
$ nano ~/.vnc/xstartup

Insert the the following content and save:

#!/bin/bash
startxfce4 &

Lastly, make the ~/.vnc/xstartup file executable:

$ chmod +x ~/.vnc/xstartup

At this stage we are ready to start the VNC server. For this simply run the vncserver command from your home directory:

$ vnc4server

New 'ubuntu:1 (linuxconfig)' desktop is ubuntu:1

Starting applications specified in /home/linuxconfig/.vnc/xstartup
Log file is /home/linuxconfig/.vnc/ubuntu:1.log

The VNC server will open a new port for every new VNC desktop you create. Your Ubuntu system should now be listening on the port 5901 for incoming VNC connections:

$ ss -ltn
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128    0.0.0.0:22                 0.0.0.0:* 
LISTEN      0      128    0.0.0.0:6001               0.0.0.0:* 
LISTEN      0      128       [::]:22                    [::]:*  
LISTEN      0      5            *:5901                     *:* 

In case you have the UFW firewall enabled, open the port 5901 for incoming connections or see below how to tunnel the VNC connections via the SSH protocol:

$ sudo ufw allow from any to any port 5901 proto tcp
Rule added
Rule added (v6)

If you wish to make your firewall rules more strict or allow range of ports for multiple VNC sessions visit our How to Open/Allow incoming firewall port guide for more information.

 

Note:

In my test cases, I have direct operation on the Ubuntu machine (logged into the system and opened firefox with sever web pages. Then when I used TightVNCViewer from my windows 10 pc to connect to it, I could not launch firefox anymore. It says "Firefox is running but not responding. ... You must first close existing firefox process or restart system". Looks like it has conflict and need to run killall firefox cmd first. BTW, pidof xx returns the pid of process xx.

Leave a Reply

Your email address will not be published.