Advertisement
Guest User

Untitled

a guest
May 11th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.61 KB | None | 0 0
  1.  
  2. Sign UpLog In
  3.  
  4. Search
  5. Tutorials
  6. Questions
  7. Projects
  8. Meetups
  9. Main Site
  10. Community
  11. TutorialsQuestionsProjectsMeetupsSign UpLog In
  12.  
  13. Search
  14. jbarnett
  15. Josh Barnett
  16.  
  17. 54 77 529.3k Share Contents
  18. How to Install and Configure VNC on Ubuntu 14.04
  19. Oct 23, 2014 Miscellaneous Ubuntu
  20. Introduction
  21.  
  22. VNC, or "Virtual Network Computing", is a connection system that allows you to use your keyboard and mouse to interact with a graphical desktop environment on a remote server. VNC makes managing files, software, and settings on a remote server easier for users who are not yet comfortable with working with the command line.
  23.  
  24. In this guide, we will be setting up VNC on an Ubuntu 14.04 server and connecting to it securely through an SSH tunnel. The VNC server we will be using is TightVNC, a fast and lightweight remote control package. This choice will ensure that our VNC connection will be smooth and stable even on slower Internet connections.
  25.  
  26. Prerequisites
  27. Before you begin with this guide, there are a few steps that need to be completed first.
  28.  
  29. You will need an Ubuntu 14.04 server installed and configured with a non-root user that has sudo privileges. If you haven't done this yet, you can run through steps 1-4 in the Ubuntu 14.04 initial server setup guide to create this account.
  30.  
  31. Once you have your non-root user, you can use it to SSH into your Ubuntu server and continue with the installation of your VNC server.
  32.  
  33. Step One — Install Desktop Environment and VNC Server
  34. By default, most Linux server installations will not come with a graphical desktop environment. If this is the case, we'll need to begin by installing one that we can work with. In this example, we will install XFCE4, which is very lightweight while still being familiar to most users.
  35.  
  36. We can get the XFCE packages, along with the package for TightVNC, directly from Ubuntu's software repositories using apt:
  37.  
  38. sudo apt-get update
  39. sudo apt-get install xfce4 xfce4-goodies tightvncserver
  40. To complete the VNC server's initial configuration, use the vncserver command to set up a secure password:
  41.  
  42. vncserver
  43. (After you set up your access password, you will be asked if you would like to enter a view-only password. Users who log in with the view-only password will not be able to control the VNC instance with their mouse or keyboard. This is a helpful option if you want to demonstrate something to other people using your VNC server.)
  44.  
  45. vncserver completes the installation of VNC by creating default configuration files and connection information for our server to use. With these packages installed, you are ready to configure your VNC server and graphical desktop.
  46.  
  47. Step Two — Configure VNC Server
  48. First, we need to tell our VNC server what commands to perform when it starts up. These commands are located in a configuration file called xstartup. Our VNC server has an xstartup file preloaded already, but we need to use some different commands for our XFCE desktop.
  49.  
  50. When VNC is first set up, it launches a default server instance on port 5901. This port is called a display port, and is referred to by VNC as :1. VNC can launch multiple instances on other display ports, like :2, :3, etc. When working with VNC servers, remember that :X is a display port that refers to 5900+X.
  51.  
  52. Since we are going to be changing how our VNC servers are configured, we'll need to first stop the VNC server instance that is running on port 5901:
  53.  
  54. vncserver -kill :1
  55. Before we begin configuring our new xstartup file, let's back up the original in case we need it later:
  56.  
  57. mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
  58. Now we can open a new xstartup file with nano:
  59.  
  60. nano ~/.vnc/xstartup
  61. Insert these commands into the file so that they are performed automatically whenever you start or restart your VNC server:
  62.  
  63. #!/bin/bash
  64. xrdb $HOME/.Xresources
  65. startxfce4 &
  66. The first command in the file, xrdb $HOME/.Xresources, tells VNC's GUI framework to read the server user's .Xresources file. .Xresources is where a user can make changes to certain settings of the graphical desktop, like terminal colors, cursor themes, and font rendering.
  67.  
  68. The second command simply tells the server to launch XFCE, which is where you will find all of the graphical software that you need to comfortably manage your server.
  69.  
  70. To ensure that the VNC server will be able to use this new startup file properly, we'll need to grant executable privileges to it:
  71.  
  72. sudo chmod +x ~/.vnc/xstartup
  73. Step Three — Create a VNC Service File
  74. To easily control our new VNC server, we should set it up as an Ubuntu service. This will allow us to start, stop, and restart our VNC server as needed.
  75.  
  76. First, open a new service file in /etc/init.d with nano:
  77.  
  78. sudo nano /etc/init.d/vncserver
  79. The first block of data will be where we declare some common settings that VNC will be referring to a lot, like our username and the display resolution.
  80.  
  81. #!/bin/bash
  82. PATH="$PATH:/usr/bin/"
  83. export USER="user"
  84. DISPLAY="1"
  85. DEPTH="16"
  86. GEOMETRY="1024x768"
  87. OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY} -localhost"
  88. . /lib/lsb/init-functions
  89. Be sure to replace user with the non-root user that you have set up, and change 1024x768 if you want to use another screen resolution for your virtual display.
  90.  
  91. Next, we can start inserting the command instructions that will allow us to manage the new service. The following block binds the command needed to start a VNC server, and feedback that it is being started, to the command keyword start.
  92.  
  93. case "$1" in
  94. start)
  95. log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
  96. su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
  97. ;;
  98. The next block creates the command keyword stop, which will immediately kill an existing VNC server instance.
  99.  
  100. stop)
  101. log_action_begin_msg "Stopping vncserver for user '${USER}' on localhost:${DISPLAY}"
  102. su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
  103. ;;
  104. The final block is for the command keyword restart, which is simply the two previous commands (stop and start) combined into one command.
  105.  
  106. restart)
  107. $0 stop
  108. $0 start
  109. ;;
  110. esac
  111. exit 0
  112. Once all of those blocks are in your service script, you can save and close that file. Make this service script executable, so that you can use the commands that you just set up:
  113.  
  114. sudo chmod +x /etc/init.d/vncserver
  115. Now try using the service and command to start a new VNC server instance:
  116.  
  117. sudo service vncserver start
  118. Step Four — Connect to Your VNC Desktop
  119. To test your VNC server, you'll need to use a client that supports VNC connections over SSH tunnels. If you are using Windows, you could use TightVNC, RealVNC, or UltraVNC. Mac OS X users can use the built-in Screen Sharing, or can use a cross-platform app like RealVNC.
  120.  
  121. First, we need to create an SSH connection on your local computer that securely forwards to the localhost connection for VNC. You can do this via the terminal on Linux or OS X via the following command:
  122.  
  123. (Remember to replace user and server_ip_address with the username and IP you used to connect to your server via SSH.)
  124.  
  125. ssh -L 5901:127.0.0.1:5901 -N -f -l user server_ip_address
  126. If you are using a graphical SSH client, like PuTTY, use server_ip_address as the connection IP, and set localhost:5901 as a new forwarded port in the program's SSH tunnel settings.
  127.  
  128. Next, you can use your VNC viewer to connect to the VNC server at localhost:5901. Make sure you don't forget that :5901 at the end, as that is the only port that the VNC instance is accessible from.
  129.  
  130. Once you are connected, you should see the default XFCE desktop ready for configuration and use! It should look something like this:
  131.  
  132. First VNC<br>
  133. connection
  134.  
  135. Once you have verified that the VNC connection is working, add your VNC service to the default services, so that it will automatically start whenever you boot your server:
  136.  
  137. sudo update-rc.d vncserver defaults
  138. Conclusion
  139. You should now have a secured VNC server up and running on your Ubuntu 14.04 server. Now you'll be able to manage your server's files, software, and settings with an easy-to-use graphical interface.
  140.  
  141.  
  142. Heart
  143. 54 Subscribe
  144. Share
  145. jbarnett
  146. Author:
  147. Josh Barnett
  148. Spin up an SSD cloud server in under a minute.
  149. Simple setup. Full root access. Straightforward pricing.
  150. DEPLOY SERVER
  151. Related Tutorials
  152. How To Find Broken Links on Your Website Using Wget on Debian 7
  153. How To Set Up SETI@home on Ubuntu 14.04 or Debian 7
  154. How To Set Up an OSRM Server on Ubuntu 14.04
  155. How To Install and Configure Sphinx on Ubuntu 14.04
  156. How To Use Pageant to Streamline SSH Key Authentication with PuTTY
  157. 77 Comments
  158. BIULOLLinkCodeHighlightTable
  159.  
  160. Leave a comment...
  161.  
  162. Logged in as:
  163. Comment Notify me of replies to my comment
  164. ikatalan
  165. ikatalan November 4, 2014
  166. If you get any trouble connecting with VNC client I had to remove the -localhost flag from /etc/init.d/vncserver OPTIONS var
  167. Than restart the vncserver
  168. and reconnect with client.
  169.  
  170. 5
  171. jrp780586
  172. jrp780586 November 15, 2014
  173. ikatalan's suggestion should be followed. Tut flowed really nicely, with appropriate level of explanation in my view. But the localhost flag did need removal.
  174.  
  175.  
  176. Snowman8
  177. Snowman8 May 9, 2015
  178. Where and how to remove flag. Can you provide us with code for PUTTY ?
  179.  
  180.  
  181. doraemon
  182. doraemon November 9, 2014
  183. I notice that tab key is'nt working as expected after following this guide. So i found the workaround on : the following website
  184. Pasted here:
  185.  
  186. edit
  187. ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml
  188.  
  189. find the line
  190.  
  191. <property name="&lt;Super&gt;Tab" type="string" value="switch_window_key"/>
  192.  
  193. and change it to
  194.  
  195. <property name="&lt;Super&gt;Tab" type="empty"/>
  196.  
  197. reboot or whatever and then tab will work properly!
  198.  
  199. I have no idea why but when using vnc this file seems to override tab's normal behaviour and makes it into a switch window key.
  200.  
  201.  
  202. 2
  203. TheVukasLt
  204. TheVukasLt November 24, 2014
  205. Hi,
  206. thanks for tutorial, it's very good. But how I can enable clipboard between my computer and server?
  207. Also can enable file transfer?
  208.  
  209.  
  210. FlxVctr
  211. FlxVctr April 25, 2015
  212. You need an SSH client (like Cyberduck on Mac OS) for file transfer (or use scp in a Shell).
  213.  
  214.  
  215. Shirleyhandzus
  216. Shirleyhandzus December 9, 2014
  217. I'm very new to Ubuntu but I was able to understand and get thur most of the steps however I'm having problems with step 4 setting up the SSH connection. I'm trying to connect to my Ubuntu server from a Windows 7 computer. When I enter the ssh command on my Ubuntu server I get an error message "address already in use". My command looks like this
  218.  
  219. ssh -L 5901:127.0.0.1:5901 -N -f -l shandzus 172.16.10.29
  220.  
  221. 2
  222. Doxos
  223. Doxos December 14, 2014
  224. HI at all, thank Josh for this tutorial.
  225.  
  226. I have a problem in step 3
  227.  
  228. when I try using the service, I start the new VNC server writitng in shell
  229.  
  230. *********************************************SHELL**************************************
  231. doxos@doxos-DeskTop:~$ sudo service vncserver start
  232. [sudo] password for doxos:
  233.  
  234. À partir vncserver pour l'utilisateur'doxos' sur le localhost:1...
  235. Couldn't start Xtightvnc; trying default font path. Please set correct fontPath in the vncserver script. Couldn't start Xtightvnc process.
  236. Unrecognized option: -:1
  237. use: X [:<display>] [option]
  238.  
  239. have you any suggestions to resolve this issue???
  240.  
  241. thanks a lot
  242.  
  243.  
  244. peterzwart
  245. peterzwart December 17, 2014
  246. Thank you very much for this awesome tutorial. Very easy to understand, clear and every step well explained.
  247.  
  248.  
  249. benporter
  250. benporter January 3, 2015
  251. Does Step 4, specifically the "ssh -L 5901:127.0.0.1:5901 -N -f -l user serveripaddress" code need to be run on the remote machine that vncserver is being installed on, or the local machine that we're connecting from?
  252.  
  253.  
  254. benporter
  255. benporter January 4, 2015
  256. I answered my own question, "ssh -L 5901:127.0.0.1:5901 -N -f -l user serveripaddress" is run on the local machine.
  257.  
  258. Best tutorial I could find on the subject.
  259.  
  260.  
  261. ohad3027
  262. ohad3027 January 4, 2015
  263. Hi,
  264. Thanks for the tutorial, it is really amazing!
  265. I have only one problem though-when I am trying to access as a client to the computer I installed the vnc on, I get a grey screen. Does anyone know what the problem might be?
  266.  
  267. Thanks
  268.  
  269. 1
  270. jggoodyear
  271. jggoodyear March 12, 2015
  272. Run: chmod +x xstartup
  273. and restart the vncserver
  274.  
  275.  
  276. fernandoestrada
  277. fernandoestrada April 2, 2015
  278. I am getting the grey screen as well. I ran the chmod +x xstartup command but i get an error that xstartup directory does not exist. i have killed the vncserver service and start it up agin and get a message A vnc server is already running as :1 i connect with tight vnc and still receive the gray screen. Any help would be gladly appreciate it. Thank you!
  279.  
  280.  
  281. xwang71
  282. xwang71 June 26, 2015
  283. check spelling of this file ~/.vnc/xstartup
  284.  
  285.  
  286. tej.nri
  287. tej.nri January 10, 2015
  288. I am also getting a blank screen after logging into vnc
  289.  
  290.  
  291. Drop
  292. Drop February 5, 2015
  293. Step Four using PuTTy and TightVNC Viewer on Windows:
  294.  
  295. Download the installer for TightVNC
  296. Choose custom setup type, only install TightVNC Viewer (optional).
  297. In PuTTy, go to Session, select and load the settings you use to connect to your server, then go to Connection > SSH > Tunnels. Type 5901 in Source port and localhost:5901 in Destination. Click Add. Go back to Session, enter a new name for these settings and save them for later use. Now click Open.
  298. Fire up TightVNC Viewer, enter localhost::5901 as remote host and click connect. You will be prompted for your password. Done !
  299.  
  300. 5
  301. PaulSchnau
  302. PaulSchnau March 5, 2015
  303. Thanks for this. I would add that you need to actually login through putty first before connecting with TightVNC.
  304.  
  305. 3
  306. RinkuY
  307. RinkuY March 23, 2015
  308. This should be in last section of the article. Thank you very much.
  309.  
  310. 1
  311. detoxic71
  312. detoxic71 March 26, 2015
  313. I cannot connect, it says "This server does not have a valid password enabled. Until a password is set, incoming connections cannot be accepted. Any solutions ?
  314.  
  315.  
  316. tomasale
  317. tomasale May 14, 2015
  318. Thanks it worked on VNC viewer as well, but then i connect to my ubuntu server i get gray-screen, any solutions?
  319. http://postimg.org/image/jlktl8w1z/
  320.  
  321.  
  322. allomiraj
  323. allomiraj December 8, 2015
  324. Thanks it's worked :D
  325.  
  326.  
  327. Mizagorn
  328. Mizagorn April 6, 2016
  329. Thanks a bunch for the very clear writeup on this part. I had gotten as far as the Tunnels in PuTTy but couldn't quite make the rest work.
  330.  
  331. Cheers! Very much appreciated.
  332.  
  333.  
  334. fvaisman
  335. fvaisman February 10, 2015
  336. Hi Josh, thanks for posting this tutorial - very clear - excellent job. As an Ubuntu 14.04 newbie, this is very useful. Unfortunately, I ran into a snag at the very last part of actually getting a remote viewing session up and running. I tried accessing natively from Mac OS X 10.10.2 (CMD+K, vnc://localhost:5901) - I get a Screen Sharing prompt for pwd, but local nor remote passwords work. Using Real VNC from a Win 7 Pro PC, produces "The connection was refused by the host computer". Using TightVNC (tightvnc-jviewer.jar) on the Mac, accepts the remote Ubuntu computer's password, but then gets stuck with "Handshaking with remote host" and then it just sits there. Any thoughts?
  337.  
  338.  
  339. PaulSchnau
  340. PaulSchnau March 5, 2015
  341. The password is for the User you set at the beginning of Step 3.
  342.  
  343.  
  344. arifaydogmus
  345. arifaydogmus March 5, 2016
  346. [deleted]
  347.  
  348.  
  349. mehmetyatki
  350. mehmetyatki March 5, 2016
  351. Make sure you have run "vncserver" command.
  352.  
  353.  
  354. kozzy
  355. kozzy February 11, 2015
  356. Fantastic tutorial!
  357.  
  358. However, I have seen the last snapshot in this tutorial, and I cannot click and type anything.
  359. Then, screensaver is opened...I just cannot do anything.
  360.  
  361. Anyone has a solution?
  362.  
  363.  
  364. FlxVctr
  365. FlxVctr April 25, 2015
  366. I suspect you are using the read-only password.
  367.  
  368.  
  369. leiva.steven
  370. leiva.steven February 26, 2015
  371. Thanks for this great tutorial. Exactly what I needed, and all in one place. Works flawlessly.
  372.  
  373.  
  374. suyalynx
  375. suyalynx March 5, 2015
  376. Successfully connected to my server, but the icons is not there (menu icons, file manager folder icons) how do I fix it?
  377.  
  378. Update:
  379. Got it,
  380. I have to update the appearance settings
  381.  
  382.  
  383. christophedivoux
  384. christophedivoux March 8, 2015
  385. I'm having issues with TightVNC: Every time I'm trying to connect to hostname::5901 or hostname:5901 It leaves the message: "Connection has been gracefully closed". Do you know where I could've failed ?
  386.  
  387.  
  388. christophedivoux
  389. christophedivoux March 8, 2015
  390. Well maybe I've found where it comes from: This is what happen when trying to create an SSH connection:
  391.  
  392. necrioss@danBoy:~$ ssh -L 5901:127.0.0.1:5901 -N -f -l necrioss 178.62.42.44
  393. necrioss@178.62.42.44's password:
  394. bind: Address already in use
  395. channel_setup_fwd_listener: cannot listen to port: 5901
  396. Could not request local forwarding.
  397.  
  398.  
  399. timbog80
  400. timbog80 March 10, 2015
  401. love it
  402.  
  403.  
  404. opi045
  405. opi045 March 12, 2015
  406. Thank you very much... This post work for me... I am trying to see if it also work well with X11 forwarding too... Thank you again!!
  407.  
  408.  
  409. FlxVctr
  410. FlxVctr April 25, 2015
  411. See my comment below. X11 forwarding works well with XQuartz on Mac OS X.
  412.  
  413.  
  414. orbin50
  415. orbin50 March 13, 2015
  416. How can I use a desktop environment like in RedHat or Ubuntu?
  417.  
  418.  
  419. bs10five
  420. bs10five March 13, 2015
  421. when I try to connect with putty I get the error: network error: connection refused
  422.  
  423.  
  424. arifaydogmus
  425. arifaydogmus March 5, 2016
  426. [deleted]
  427.  
  428.  
  429. mehmetyatki
  430. mehmetyatki March 5, 2016
  431. Make sure you have run "vncserver" command.
  432.  
  433.  
  434. Load More Comments
  435. Creative Commons License
  436. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
  437. Copyright © 2016 DigitalOcean™ Inc.
  438. Community Tutorials Questions Projects Tags RSS
  439. Distros & One-Click Apps Terms, Privacy, & Copyright Security Report a Bug Get Paid to Write
  440.  
  441. SCROLL TO TOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement