Sunday, April 12, 2020

VNC On Ubuntu Linux

How to set up VNC (virtual network computing) on Ubuntu Linux and Microsoft Windows 10 to transport an X display over a network. This will allow you to connect "X" graphical displays across machines so you can run a program on Linux and view the program graphical display on Microsoft Windows PC. This example sets up a direct VNC connection, which is not secure. Once these examples are in place and working, you can configure the VNC connection to go through an SSH tunnel. Let's go one step at a time, and start with a plain VNC connection.

This example uses TightVNC from www.tightvnc.com/download.php and sourceforge.net/projects/vnc-tight.

In these examples, the lines that begin with a $ (dollar sign) indicate you should type what is after the dollar sign in to your Linux command prompt. Do not type the leading dollar sign.


### For first time setup on the Linux machine, have your system adminstrator install the packages.
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic


$ uname -a
Linux dell990 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux


# System administrator should add this line to end of /etc/apt/sources.list "deb http://mirrors.kernel.org/ubuntu bionic main universe"
$ grep mirrors /etc/apt/sources.list
deb http://mirrors.kernel.org/ubuntu bionic main universe


# System administrator should install the VNC package. Also install xterm.
$ sudo apt update
$ sudo apt install tightvncserver
$ sudo apt install xterm



### As the user of VNC on the Linux machine, do the following.
# Log in to Linux. In your Linux home directory, create the password file (~/.vnc/passwd) by running the vncpasswd program.
# From Windows, press the Start button, type cmd, and open a command prompt. In the Windows PC command prompt type "ssh testuser@ip_address" and log in to your Linux account.
# Press the Start button, type "cmd", and open a command prompt.
# In the command prompt window, using your user name, type: ssh testuser@ip_address
# For the first ssh connection, accept the ssh key fingerprint if you are prompted. Your system administrator will tell you the account name (testuser in this example) and the IP address or host name (192.168.1.9 in this example).



















# Once you are logged in to the Linux machine, set up your VNC password. You only need to do this one time.
$ vncpasswd
Using password file /home/testuser/.vnc/passwd
VNC directory /home/testuser/.vnc does not exist, creating.
Password:
Warning: password truncated to the length of 8.
Verify:
Would you like to enter a view-only password (y/n)? n


# Start the VNC server on the Linux machine.
$ vncserver
xauth:  file /home/testuser/.Xauthority does not exist
New 'X' desktop is dell990:1
Creating default startup script /home/testuser/.vnc/xstartup
Starting applications specified in /home/testuser/.vnc/xstartup
Log file is /home/testuser/.vnc/dell990:1.log

# Note the display number is "1".
# Remember in this example, the IP address is 192.168.1.9. Your IP or hostname is likely different than this example.


# The VNC server is running as a process with your username and is listening for an incoming connection.
$ ps -ef | grep vnc
testuser  6312     1  0 20:24 pts/1    00:00:00 Xtightvnc :1 -desktop X -auth /home/testuser/.Xauthority -geometry 1024x768 -depth 24 -rfbwait 120000 -rfbauth /home/testuser/.vnc/passwd -rfbport 5901 -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb




# On your PC, download the VNC viewer from www.tightvnc.com/download.php.  If your Windows PC has 4GB of RAM or less, you probably will choose the 32-bit version.
# You only need the viewer. Choose the CUSTOM setup and install the viewer.


















# On the Windows PC, run the TightVNC viewer app. Make the remote host the Linux machine with two colons and the VNC display number. In this example, it is 192.168.1.9:11.

















# Press "Connect" and it will open a TightVNC Viewer window on the Windows PC. At this point, the VNC Viewer window is like an empty display container.


















# If you try to run a graphical program on the Linux machine, it likely will not work. For example:
$ xterm
PuTTY X11 proxy: unable to connect to forwarded X server: Network error: Connection refused
xterm: Xt error: Can't open display: localhost:10.0
$ echo $DISPLAY
localhost:10.0
$ echo $TERM
xterm


# It likely didn't work because the Linux machine needs to know where to send the display output. This is set in your Linux shell environment variable DISPLAY. Type the following in the Linux shell. The number "1" is used in this example because that was the number from the earlier "vncserver" command.
$ export DISPLAY=localhost:1
$ echo $DISPLAY
localhost:1


# If the Linux shell environment variable TERM is not set to "xterm" or similar, do that now.
$ export TERM=xterm

# Test the clock program to display the output in the VNC Viewer on your Windows PC by running the clock program in the Linux shell. In the Linux shell type "xclock".
$ xclock


















# Switch back to your Linux shell. You should notice that you can't type anything; it is not accepting input. Press the ENTER key a few times to test this.

# In the Linux shell, press CTRL-c to end the clock program. The clock in the VNC Viewer on your PC should close.

# Try to run the clock again. This time, put an & (ampersand) after the command. The clock should display, the shell will show the process ID (PID) of the clock process, and you will also be able to type in the Linux shell.
$ xclock &
[1] 10904


# Open a new shell window and display it in the VNC Viewer app on your Windows PC.
$ xterm &
[2] 10907


# In the new shell window which is displayed on the PC, type a quick command. Ensure your Windows PC mouse is over the window where you want to type.
$ uname -a
Linux dell990 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux


# In the window on your PC, type "exit". It should disappear. Go back to your first Linux connection and press ENTER. The shell should tell you that process has ended.
$
[2]+  Done                    xterm

# End the clock program. In Linux use the "kill" command with the process ID (PID) that it displayed when you started the clock program. The press ENTER again and the Linux shell will tell you the process has terminated.
$ kill -term 10904
testuser@dell990:~$
[1]+  Terminated              xclock


# Start a web browser on the Linux machine and it will display in the VNC Viewer on your Windows PC.
$ firefox &
[1] 10959

# In the web browser that should have appeared in the VNC viewer, go to a website which shows the IP address. The IP address will be the public facing IP address of the Linux machine, not the IP address of your Windows PC.



















# When using an xterm window, try pressing CTRL+leftMouseButton to show an options menu.

# You should be able to close the Linux terminal session and still have the programs displayed in VNC on the PC continue to run.

# To completely stop the VNC server, on the Linux machine type:
$ vncserver -kill :1
Killing Xtightvnc process ID 503



# Customize the window settings by creating file .Xresources in your home directory. Then kill and restart the vncserver.
$ cat .Xresources
! Type "man xterm" for all settings.

! Set window size.
XTerm.vt100.geometry: 80x32
XTerm.vt100.reverseVideo: true

! Type "fc-list :scalable=true: family | sort | more" to show available fonts.
! Set a font and size.
XTerm.vt100.faceName: Ubuntu Mono:size=12:antialias=false
XTerm.vt100.faceSize: 12

! Double-click selects whole word.
XTerm.VT100.charClass: 33:48,35-37:48,43:48,45-47:48,64:48,126:48

! Scroll bar on right side. Use left or right mouse button on top of scroll bar to move it.
XTerm.vt100.scrollBar: true
XTerm.vt100.rightScrollBar: true
XTerm.vt100.scrollbar.width: 8
XTerm.vt100.saveLines: 2048

! Allow window resize.
XTerm.vt100.allowWindowOps: true


No comments:

Post a Comment