Ubuntu 16.04 – Configure your system to have x11vnc running at startup

x11Vnc_u16_logo

Hello World;

Today, we continue our journey into the next to come release of Ubuntu (i.e. Ubuntu 16.04) and remote desktop connections. In the previous posts, we have updated the procedure to perform a standard xrdp installation and the procedure to perform a custom xrdp installation. 

Another post that has been quite popular is the one about having the x11vnc service running at startup.  This post will provide an update of the existing post that target Ubuntu 15.04 (see Ubuntu 15.04 – Configure your system to have x11vnc running at startup). The installation process has not changed since Ubuntu 15.x.  The previous post ( Ubuntu 15.04 – Configure your system to have x11vnc running at startup) was explaining how to perform the installation step by step.

In this post, instead of repeating the same things, we have put together a quick and dirty script that can help you configuring your system with X11VNC service running at startup.

Why would you need to have the x11vnc service at startup ? Simply because we would like to be able to login from a vnc client to a remote system where no users are currently logged on or to be able to access the system remotely even if multiple reboot occurs.

So, let’s see how we can achieve this

The Script

Content of the Script

Important Note :

If you copy/paste the script, you might encounter some issues because font formatting might be not maintained during the copy/paste operation. Please ensure that format is  accurate before launching the script. At the end of the post, you can download the script…

Disclaimer : As usual, use this at your own risk !! 

# ##################################################################
# Script Name : vnc-startup.sh
# Description : Perform an automated install of X11Vnc
# Configure it to run at startup of the machine 
# Date : Feb 2016
# Written by : Griffon 
# Web Site :http://www.c-nergy.be - http://www.c-nergy.be/blog
# Version : 1.0
#
# Disclaimer : Script provided AS IS. Use it at your own risk....
#
# #################################################################
# Step 1 - Install X11VNC 
# ################################################################# 
sudo apt-get install x11vnc -y
# Step 2 - Specify Password to be used for VNC Connection 
# #################################################################
sudo x11vnc -storepasswd /etc/x11vnc.pass
# Step 3 - Create the Service Unit File
# #################################################################
cat > /lib/systemd/system/x11vnc.service << EOF
[Unit]
Description=Start x11vnc at startup.
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -auth guess -forever -loop -noxdamage -repeat -rfbauth /etc/x11vnc.pass -rfbport 5900 -shared
[Install]
WantedBy=multi-user.target
EOF
# Step 4 -Configure the Service 
# ################################################################
echo "Configure Services"
sudo systemctl enable x11vnc.service
sudo systemctl daemon-reload
sleep 10
# Step 5 - Restart System 
# ################################################################
sudo shutdown -r now

As you can see, the script is really not to difficult.  We first install the x11vnc package. Then, we configure a vnc password to protect the access to the remote machine.  The remaining steps are creating and configuring the x11vnc service to run at startup through the systemd implementation.  At the end of the script, we reboot the machine and it’s time to check if the configuration has been performed accordingly or not.

Execute the script

To execute the script, you will need to have your system connected to internet.  This is needed because you will download some additional packages from Ubuntu Repository.

To execute this script, you will need to either copy/paste the script content into a text file on your Ubuntu machine (and please check formatting) or you can download the script from here to your Ubuntu machine.  You will need to mark the script as executable before you will be able to run it.

We will use the command line approach to perform all the necessary actions before running the script.  So, you will open a Terminal console and you type the following command

chmod +x  <%path_of_File%>/vnc-startup.sh   (adapt the path accordingly to match your system configuration)

You are ready to proceed with the automated installation of x11vnc and have it configured to run at startup.  As mentioned before, we assume that you have internet connection and you will be able to download all the necessary packages needed (x11vnc) to perform this configuration.

When you are ready, go to the folder containing the vnc-startup.sh script and you can execute the script by issuing the following command in the command prompt.

sudo ./vnc-startup.sh   

We assume that you have downloaded or created the file in the your home folder under the Download folders.  If this is not the case, browse to the location and execute the script from there.

Wait for completion of the script.  The machine will reboot automatically when done.

After the reboot, you can test your configuration by trying to connect to your ubuntu machine via the vnc client while nobody is logged into the system.

Screencast – Script Demo

We are providing hereafter also a short video demonstrating how the script works and what should be the final results. So, sit back and relax 🙂

 

 

Download Script

 

For your convenience, we are providing a downloadable version of the script. You can find it here

 

Final Notes

We have confirmed and validated that with Ubuntu 16.04, we can still have the x11vnc service running at startup which allows us to connect remotely even if no user are logged on or if reboots occur.  This configuration would also allow you to connect to the Unity desktop through xrdp (kind of). Check this post for more information.

This is it for today. Time to move on and blog about new things…

till next time

See ya

85 thoughts on “Ubuntu 16.04 – Configure your system to have x11vnc running at startup

  1. I’ve tried the above to the letter, and I still can’t get this to work at all.

    I’ve got 16.04 Ubuntu Mate installed.

    The script ran as expected.

    I have the correct ports opened and forwarded in my router, to the correct IP address.

    When my server is set to headless, I can’t VNC in at all.

    All I get is “Cannot establish connection. Are you sure you have entered the correct network address, and port number if necessary?”

    All IP’s are correct on the network.

    Really at a loss at the moment to fix this.

  2. hello Chris;

    We just retested the script and the information provided in the post. You are mentionning a router. can you check that the configuration is working when you are trying from your local network. The message seems to provide information about the fact that either ip or port is not correct. When connecting via the vnc viewer; do you specifyport 5900 ?

    till next time
    See ya

  3. Hi,

    Thanks for the reply.

    I had a system running Linux Mint, that I’d set up to run x11vnc using port 5920.

    This was working fine over VNC.

    I decided to update my OS to 16.04 Ubuntu, which meant a clean install.

    Since then I’ve had no joy at all getting x11vnc to work.

    I’ve taken some screen shots of various things I’ve done below.

    I can’t access VNC via the LAN or WAN

    However, I can access SSH via LAN and WAN fine.

    I’ve used port 5920 as port 5900 is already in use by my Mac for VNC, but port 5920 worked fine when I had Mint installed.

    http://imgur.com/L0bAJOO Router Port Forward

    http://imgur.com/CtkT28w Server IP

    http://imgur.com/DpCL3pt x11vnc script

    http://imgur.com/922E1V1 x11vnc Config /lib/systemd

    http://imgur.com/6CEoo3f x11vnc Config /etc/init

    The only reason I have the config in the init file is that is left over from an earlier attempt, which also didn’t work.

  4. Hello Chris,
    we have tested the config with different ports and again, we have it working
    get rid of the /etc/init.d/x11vnc.conf
    check that the service x11vnc is present and running on port 5920 (something like ps aux | grep x11vnc and then sudo systemctl status x11vnc)
    This should be working man (at least from your LAN)

    in the command line when starting the x11vnc, you can add a switch to obtain some logging as well to see what could be happening

    Hope this help
    Till next time

  5. @Chris

    So based on your screenshot, something is wrong in your configuration or your system (could be a simple typo)
    Can you try to execute the command on a terminal session and see if there is an error that’s displayed or if the service can start.
    If the service starts, so re-check the service file and ensure that the command is provided correctly and that there is no typo or any other formatting issues
    As mentioned in previous comment, enable the logging for the x11vnc (you simply need to pass the swithc -log in the command line – google for more info)

    Hope this help
    Till next time
    See ya

  6. I’ve double and triple checked the config, and the script.

    I’ve checked the format of it, and even pasted it into notepad and re done it.

    Still getting the same issue, where x11vnc isn’t starting on boot up

    When I execute the command x11vnc from terminal the below is what I get

    http://imgur.com/8boIxCG

    Tried to enable logging in the startup script but it doesn’t seem to generate a log file at all.

    When I manually start the service I get the below

    http://imgur.com/oxBrM8t

    Getting an xauth issue which I don’t understand.

  7. Hello Chris,
    Do you have monitor attached to the server you are trying to access to ?
    If you have no monitor attached to it, you will not be able to use the script as is. You actually need to install additional software to emulate the missing monitor
    To emulate your missing screen, you would need a package like xfvb and configure it. Then you could execute the command to start x11vnc specifying the display identity.

    This approach used to work in the past….you can give it try

    Hope this help
    Till next time
    See ya

  8. Hi, I’ve plugged my server into a monitors part of my trouble shooting process.

    This was working it now it that isn’t even working.

    Getting that error message stating the port on the server can’t be contacted

  9. Hello Chris,

    If I understand correctly, You have it working when you plug the screen on your system
    You might need to change x11vnc server package with a alternative vncserver solution. The x11vnc might not the best option for headless configuration

    Hope this help
    Till next time
    See ya

  10. So after doing some testing with the server connected to a monitor I’ve had the following results.

    Initially unable to connect via VNC at all.

    When I loaded the x11vnc server GUI for settings, I was able to change the port and display, and this now works.

    However it doesn’t appear to be persistant and resets after a reboot, so need to figure out a way to change this in the config files.

    Screen shot shows the settings now

    http://imgur.com/tNAGOa3

    So it’s listening on server:1 and display:80 which relates to my chosen port.

  11. Just wanted to give an update.

    I tried everything I could think of on my 16.04 install, and couldn’t get x11vnc to run at all.

    I did a new install of 15.10 over it, and x11vnc worked straight away, with out a hitch, so I’m absolutely stumped as to why it didn’t work on 16.04.

    I’ll have to set it up in a VM and have a play.

  12. Hello Chris,

    thank for the feedback. On my side, I have checked if it was possible to have xrdp on Ubuntu 16.04 working. We had great success and we will publish a quick how to guide
    the second part of the issue is about the headless part… IF we have some time, we will try and test this setup and publish the results of it if the outcome is positive

    Keep in touch
    Till next time
    See ya

  13. I created the x11vnc.service file using a “sudo gedit” command, and while it works great, there’s one difference. I had to create the file in /etc/systemd/system, not /lib/systemd/system. The latter earned me an error message when I ran the “sudo systemctl enable x11vnc.service” command. Other than that, works great. Thanks!

  14. Quick update – I’ve tried out your x11vnc.service code on a fresh install of Antergos, and it’s working flawlessly. This really opens things up for me. Previously if I wanted to connect to a non-*buntu distro I’d have to resort to installing either the gdm3 or mdm display manager and then insert a nohup script into the /etc/gmd3|mdm/Init/Default file. This simplifies things a lot and potentially opens up a lot of other distros for me. Thanks for all the hard work and saving me my own hard work!

  15. You lose me at step 5 all the others are commands to enter in terminal. Can you please explain step 3 I tried it in terminal and nothing happens.

  16. @cmcaulty

    Step 3 simply create file on your system and populate it with the neccessary instructions in order to have the X11VNC starting at bootup
    If you have executed step 3, you should have the file x11vnc.service created in the following directory /lib/systemd/system/
    The x11vnc.service should contain a bunch of lines (the one we are passing via terminal) The file content should look like
    [Unit]
    Description=Start x11vnc at startup.
    After=multi-user.target
    (….)

    Till next time
    See ya

  17. sorry I am doing this for multiple machines at a library and have to be sure I am doing it right. Do I copy all 3 boxes of step 3 into a file? and what does EOF mean?

    OK I really lose it with nano. I tried doing it with gedit and no luck but I used your 15.04 directions and it worked easy. The trouble with nano is I am doing all this over a WAN and it won’t accept my keyboard combinations remotely. Thank you very much. I have a suggestion for dummies like me, why not put the easy 15.04 instructions in the 16.04 page along with the more complicated version. This has soaked up a week of time until I found your instructions. As all the x11vnc configs I knew before don’t work in systemd. Thanks again.

  18. Seemed to all work well, once I remembered to open the port in the firewall. Very slow screen updates, so I added -wireframe to the x11vnc script.

    Only one real issue, which almost drove me nuts. If I reboot the system, the server doesn’t come up, not right away. From systemctl status, it is loaded and enabled, but not running. Try to manually start it and it seems to hang, waiting on something. If I simply boot and wait, in about 4-1/2 minutes the x11vnc server will start, and all works normally.

    This seems to be only at boot. If I then daemon-reload and restart, it restarts just fine. I guess I’m heading towards a long read of log files, as I suspect this is some kind of resource wait, but I have no idea what for.

  19. Could you please help me get this to work for Linux Mint Debian Edition 2 “Cinnamon”? I want to switch to a debian-based but I was discouraged immediately because I install this first and got errors. What could I change in the script to get it to work? Thank you!

  20. @Neal,

    We do not work with Mint. We will need to have a look to into it but we are quite busy lately….
    Would it be an option for you to replace the vnc capability by a XRDP solution and still connect to your Cinnamon desktop ?
    Would you like to test a procedure for Linux Mint and provide feedback

    Hope this help

    Till next time
    See ya

  21. The Script worked brilliantly on my Odroid Xu3 Lite running Ubuntu Mate 16.04.

    As always Griffon, thanks for this extremely great guide, and in this case Script, it saved so much typing, thanks ! 🙂

  22. Hello Griffon,
    first I want to thank you for this instuction and all the other helpful tips and step-by-step how-to’s.
    I’m running Lubuntu in a VMware enviroment and installed x11vnc with your script. First everythinks worked fine… until the Lubuntu desktop were locked automatically after 5 min.
    While the screen is locked, I can still connect to the vncserver, but I don’t get a login screen, I see the normal desktop and can’t make any input. No mouse or keyboard is working until I log in again with the VSphere client console window…
    Could you imagine why?
    Thanks a lot…

    Joerg

  23. @Joerg,

    Honestly, I have no clue about that…I would need to test this on my test lab and see if the same behavior happens. Which vnc client are you using to connect ?
    Till next time
    See ya

  24. I manually ran the commands provided above on a 16.04.1 x64 Desktop clean install and everything executed properly. I then rebooted the system, opened an SSH tunnel with port 5900 with PuTTY from my Windows desktop, opened TightVNC and connected to 127.0.0.1::5900, and the login screen appeared. The problem is that once I type my password and hit enter my VNC session gets disconnected immediately. I walked over to the physical console and noticed that I’m not logged in there – doesn’t even show that I entered the password.

    Is x11vnc somehow creating a new login “session” that only applies to VNC and killing it once it switches from login to desktop? Is there an easy way to fix this?

  25. @Matt,

    Maybe you should review the following post http://c-nergy.be/blog/?p=6020 which might provide some insight on how to configure xrdp/vnc with SSH….
    I think that’s what you are trying to achieve….
    If you really want to start ssh tunnel first and then start tightvnc, I wonder if you can have all the traffic going through the same port….you do ssh on port 5900 and then you do vnc connection to port 5900 ? does not sound rignt to me…

    Hope this help
    Till next time
    See ya

  26. Thanks so much, someone posted this link in reference to me asking how to do exactly this procedure on AskUbuntu, and it worked flawlessly. Somehow I never found this page while googling for it.

    Thanks again!

  27. @Bryan,

    Thank you for sharing your positive experience with us. We are always pleased to see that the information provided here can be of any help to others…
    We are maybe not easy to find but at the end you find the info you were looking for

    Till next time
    See ya

  28. @Griffon,

    I had a weird experience, and since it was your tutorial which got me up and running to begin with, I thought I might ask.

    Thinking that everything seemed to be running great, I unhooked my monitor from my Linux box (over HDMI), and rebooted to really test it out. As expected, VNCing in from my iMac worked great at the login screen. Everything was fast – mouse movement, clicks, pulldown menus and typing. As soon as I logged in though, nothing would happen for about the first minute (programs wouldn’t appear to launch if clicked, tooltips didn’t even show up.)

    After that first minute, there would be about a 20-30 second lag for anything I do, except actually moving the mouse, which was still fine. So I rebooted and hooked up the monitor, thinking I’d need to make some edits on the x11vnc.

    I tried vnc’ing back in, and suddenly it was fast again. Unhooked the monitor from the Linux box and it was *still* fast, so I thought it may have been a glitch, and tried rebooting one more time. Now unhooked from monitor again, it was still slow *after* login over vnc.

    This time, while it was slow, I decided to try hooking up the screen back to the Linux box. Suddenly like magic, the iMac’s screen refreshed and the vnc connection was flying again.

    For some reason, the x11vnc really likes that I have the Linux box plugged into a monitor, but I don’t have room on my desk for 2 27″ screens (and if I did, I wouldn’t need to use VNC to begin with.)

    Is there some option I can set that will make the server run normal speed *after logging in*, when the Linux box is headless? I followed your initial tutorial pretty much to the letter, and the server DOES work, but is slow after login with no monitor attached…

    Thanks for any help you might be able to provide 🙂
    (I’m currently in Linux through my VNC connection, with my second monitor sitting next to me qhilw typing this.)

    -Bryan

  29. @Bryan,

    Using X11vnc as vnc server generally requires to have a monitor connected to the machine (out of the box). If you want to use a headless config, you need additional configuration and you might need to create a Dummy Screen so x11vnc does not complain…

    Your options would be to change x11vnc software with another vnc server (like TigerVNC – check maybe https://wiki.archlinux.org/index.php/TigerVNC#Running_vncserver_for_virtual_.28headless.29_sessions) or you could try to install xRDP Software.

    This software allows you to perform remote desktop session to your machine. To obtain more info, you can have a look at the following post (http://c-nergy.be/blog/?p=8952 or http://c-nergy.be/blog/?p=9962 or some teasers at http://c-nergy.be/blog/?p=10011) or you could browse in the XRDP library in this blog (See http://c-nergy.be/blog/?cat=79) and check which option would best fits your needs

    Hope this Help
    Till next time
    See ya

  30. Oh wow, ok, I didn’t know that x11vnc pretty much requires having a monitor…I guess I’ll look at one of the other options you mentioned. I know RDP is kind of the Microsoft version, but it doesn’t matter much to me – just being able to run it headless is the ultimate goal 🙂

    Thanks for the info, I’ll check out Tiger and XRDP, thanks!!

    -Bryan

  31. IMO – You should add :

    ufw allow 5900

    To the x11vnc.service file – it will open the port ,

    Other than that – it worked perfectly .
    Thank you .

  32. @Fred,

    Thank you for the feedback and positive comments.
    For port 5900, we never had to open the port to have the solution working. However, It might be a good idea to add this in order to ensure firewall would not block the solution

    Till next time
    See ya

  33. @Griffin
    I had originally enabled the fw via the CLI when setting up the initial setup manually before adding the startup service.

  34. Very valuable post!

    1.
    The option -o /var/log/x11vnc.log creates a log file which may help in debugging.

    2.
    Creating the unit file in /etc/systemd/system/x11vnc.service would indicated that it is user based. The files under /lib/systemd/system/ came with the distribution an may change in the future. They files in /etc/… always override the ones in /lib/… on startup.

    3.

    The commands:
    sudo systemctl daemon-reload
    sudo systemctl restart x11vnc
    sudo systemctl status x11vnc
    should immediately start the server and give it’s status back. This may shorten the time for debugging. The result may be:

    user@remotehost:~$ sudo systemctl status x11vnc
    ● x11vnc.service – Start x11vnc at startup..
    Loaded: loaded (/etc/systemd/system/x11vnc.service; disabled; vendor preset:
    Active: active (running) since Mo 2016-12-26 22:51:06 CET; 50s ago
    Main PID: 10890 (x11vnc)
    CGroup: /system.slice/x11vnc.service
    ├─10890 /usr/bin/x11vnc -auth guess -forever -loop -noxdamage -o /var
    └─10893 /usr/bin/x11vnc -auth guess -forever -loop -noxdamage -o /var

    Dez 26 22:51:06 remotehost systemd[1]: Started Start x11vnc at startup..
    Dez 26 22:51:06 remotehost x11vnc[10890]: — x11vnc loop: 1 —
    Dez 26 22:51:07 remotehost x11vnc[10890]: — x11vnc loop: waiting for: 10893
    Dez 26 22:51:11 remotehost x11vnc[10890]: PORT=5900

  35. @Bernd,

    Thank you for the feedback and useful inform provided through your comments
    Any improvement is welcome….:-)

    Till next time
    See ya

  36. Hello. Thank you for the excellent posting.

    I’m new to all this so I’m looking for clarification: I am not sure if I should be using x11VNC as you described here or another solution such as xRDP or tight/tigerVNC. I have a server running Ubuntu Mate 16.04 with a monitor, keyboard, etc. that I’d like to access from another Linux machine on the same network. But I’d like to get all my files, desktop, etc. exactly as if I’m standing at the machine. Which of these is the correct solution for me please?

    Thank you very much

  37. Hello,
    Has anyone tried this on ubuntu 17.04?
    Should it work?
    Thanks in advance.

  38. Hi!
    How can I make this VNC server accept only local connections like as vinos “gsettings set org.gnome.Vino network-interface lo” command?

  39. @Radeon2k8,

    To have the x11vnc to accept only local connections, you have to include in your command line starting the vnc the -localhost option….
    When you install x11vnc software, you have also a GUI interface available where you can tick an option called “Listen on localhost

    Hope this help
    Till next time
    See ya

  40. @Glenn,

    Hello there, no we have not tested yet this procedure on Ubuntu 17.04… It should be working but as the os is bringing each time new features and changes, the process might be broken and adjustment might be needed…..
    We will try to check that and provide feedback

    Till next time
    See ya

  41. @Joseph,

    Because you are using MAte-desktop, in theory you could be using any vnc server you mentioned. If you do not want to use any vncserver, it also possible. xRDP version 0.9.xx ship with a backend server components that actually replace the vncserver part…. The only thing here is that you would need to perform a custom xrdp installation…. You can have a look at the following post…. (http://c-nergy.be/blog/?p=10513) and adapt it to your desktop environment… (i.e update the .xsession file to reflect your desktop env.)

    Hope this help
    Till next time
    See ya

Leave a Reply