Configure VNC Server on CentOS 7 / RHEL 7

ADVERTISEMENT

VNC Server on CentOS 7
VNC Server on CentOS 7

The following simple guide helps you to setup the VNC server on RHEL 7/CentOS 7 machines, VNC help to share the desktop with the other machines which have a client installed. VNC server and client are not installed by default, you need to setup the vnc server manually.

Before installing the VNC server, let us install Gnome desktop.

ADVERTISEMENT
Article will continue after the ad
[root@server ~]# yum groupinstall "GNOME Desktop" "Graphical Administration Tools"

Install the tigervnc server and X11 fonts.

[root@server ~]# yum install tigervnc-server xorg-x11-fonts-Type1

Copy the VNC server configuration file to /etc/systemd/system/ for configuring the system service. While copying, you can mention which port it should listen. By default VNC server listens on 5900, but if you setup port-offset for VNC; we can run a service on a sub-port of the default port 5900. For example, if we mention port 5, it will be accessible on 5905.  You can connect the VNC server by specifying ipaddress:sub-port (192.168.2.109:5 or 192.168.2.109:5905)

[root@server ~]# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:5.service

Edit the copied file.

[root@server ~]# vi /etc/systemd/system/vncserver@:5.service

At the end of the file, you will find the lines like below.

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[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 <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target

Replace <USER> with your real user, in my case, i replaced with a user called “raj” .

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[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 raj -c "/usr/bin/vncserver %i"
PIDFile=/home/raj/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target

Add firewall rules to allow the VNC connection, the following rule is set as per the port-offset mentioned in the previous step.

[root@server ~]# firewall-cmd --permanent --zone=public --add-port=5905/tcp
[root@server ~]# firewall-cmd --reload

Start VNC server after switching to raj.

[raj@server ~]$ vncserver

Set the password.

You will require a password to access your desktops.

Password:
Verify:
xauth:  file /home/raj/.Xauthority does not exist

New 'localhost.localdomain:1 (raj)' desktop is server.itzgeek.com:1

Creating default startup script /home/raj/.vnc/xstartup
Starting applications specified in /home/raj/.vnc/xstartup
Log file is /home/raj/.vnc/server.itzgeek.com:1.log

Reload the systemctl daemon as root

systemctl daemon-reload

Start the VNC service as root.

systemctl start vncserver@:5.service

Enable it on system startup as root.

systemctl enable vncserver@:5.service

Take remote desktop.

ipaddress:5905 or ipaddress:5
CentOS 7 - VNC Connection
CentOS 7 – VNC Connection
You will be asked to enter the password, enter the password that you have created earlier.
CentOS 7 - VNC Authendication
CentOS 7 – VNC Authentication
Now you will get the vnc desktop session.
CentOS 7 - VNC Desktop
CentOS 7 – VNC Desktop
That’s all you have successfully configured VNC server on CentOS/RHEL 7.
ADVERTISEMENT

POSTS YOU MAY LIKE -:)

Share This Post

  • Phil

    Have you figured out a way to set the geometry (screen size) ?

    • Raj

      As per the manual, geometry value can be specified in the service file it self, just have to append the value in the line ExecStart=/sbin/runuser -l raj -c “/usr/bin/vncserver %i”. It did not work for me, so i manually changed the disaplay after taking the VNC.

  • ahmed

    hi Raj,

    i tried using your instructions, but wasnt able to get it connected. this is what i get when i run journalctl -xn. any help would be appreciated.

    Unit vncserver@:5.service has begun starting up.
    Aug 26 20:56:32 localhost.localdomain runuser[14370]: runuser: user does not exist
    Aug 26 20:56:32 localhost.localdomain systemd[1]: vncserver@:5.service: control process exited, code=exited status=1
    Aug 26 20:56:32 localhost.localdomain systemd[1]: Failed to start Remote desktop service (VNC).
    — Subject: Unit vncserver@:5.service has failed
    — Defined-By: systemd
    — Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

    • Raj

      Hi Ahmed,

      Please find the following line in “vncserver@:5.service” file,

      ExecStart=/sbin/runuser -l raj -c “/usr/bin/vncserver %i -geometry 1280×1024”

      replace “raj” with your “real user”

      • Ahmed

        Thanks heaps Raj,

        that worked,

  • Johnny

    Hi

    Thanks for that guide, it’s pretty usefull ! But i wonder if we can also install or pass with RDP on 7 ? I know XRDP can be install on Centos6.5, but on 7 was it too..? There’s the rpm Epel that needed , but don’t know if it’s already over the dvd iso of 7…
    Or maybe it’s for a futur install guide..

    thanks again

    • Diego

      Because of the corporate Firewall rules we have in place; I can not use any group of ports on the 5900 range. so I wonder if you can provide me information on how to configure my redhat server in order to SSH tunneling and use tigervnc with a localhost port instead ?. You help will be highly appreciate it.

      Thank you very much.

    • Raj

      Hi Johnny,

      New how to made for xrdp on CentOS 7. Use the following link.

      http://www.itzgeek.com/how-tos/linux/centos-how-tos/install-xrdp-on-centos-7-rhel-7.html

  • Andreas

    Many thanks for great tutorial!

    When I connect to VNC server, I get black screen (of seemingly correct resolution) with a small grey rectangle in top left corner, saying
    “Accept clipboard from viewers”
    “Send clipboard to viewers”
    “Send primary selection to viewers”
    And I cannot do anything, i. e. no matter where I click, there is no reaction.
    (I am using vncviewer from UltraVNC package on Windows.)

    • Andreas

      Now it is working.. after a forced reboot.
      I had another problem that gnome-shell was taking 2-6GB per process with my system running into swap.
      Maybe there was another error.. anyway, it is working now.

      I also followed the xrdp tutorial. Thanks for that too. 🙂

      I am a bit surprised though that unlike the windows usage (vnc: current session, rdp: new session) on linux, both create new (parallel) sessions. And those are then reused for each following connection through same channel. a bit different handling.

      Also, unlike Windows, VNC is faster than RDP (using tight compression it is like working on a local machine).

      But then, I have not used a GUI for Linux for the past years, only doing a little administration through the shell here and there. 🙂

  • Johnny

    Hi

    Almost related, but will it be possible to put a vnc tutorial for Mint 17 also ? Tigervnc might be working fine on..

    But with Mint, the vino-preference doesn’t work, supposed to be included, but…

  • Brent

    We have always configured remote access to VNC through xinetd. Doing so allows for more security controls on who can remotely access the system. With CentOS 6 and prior the xinetd configuration provides the “gdm greeter” login screen and you can have any number of concurrent instances of the gnome desktop subject to xinetd configuration. That’s to say you don’t create a separate config for each user. So all around it is a better configuration (more secure access, Linux login controls and multiple sessions with one config) albeit a little more complex to set up.

    So that preamble all leads up to a question. Do you know of anyone that has posted the xinetd setup for a CentOS 7 remote vnc desktop? I’m trying to work through it, but I can see it’s going to be a challenge without some tips.

    Thanks!

  • James

    Follow the guide but at the last few steps (Starting the vnc service as root I get command not found…

    • Raj

      Start VNC from normal user

  • Phil

    Raj, How did you manually change the display size??

  • Walter

    Hello Ray

    Trying to do the last step (starting the service) but I get this error

    [root@server mike]# systemctl start vncserver@:1.service
    Job for vncserver@:1.service failed. See ‘systemctl status vncserver@:1.service’ and ‘journalctl -xn’ for details.

    [root@server mike]# systemctl -l status vncserver@:1.service
    vncserver@:1.service – Remote desktop service (VNC)
    Loaded: loaded (/etc/systemd/system/vncserver@:1.service; disabled)
    Active: failed (Result: exit-code) since Thu 2015-02-26 17:16:14 CET; 2min 54s ago
    Process: 10481 ExecStart=/sbin/runuser -l mike -c /usr/bin/vncserver %i -geometry 1280×1024 (code=exited, status=98)
    Process: 10477 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)

    Feb 26 17:16:14 server.localdomain systemd[1]: Starting Remote desktop service (VNC)…
    Feb 26 17:16:14 server.localdomain systemd[1]: vncserver@:1.service: control process exited, code=exited status=98
    Feb 26 17:16:14 server.localdomain systemd[1]: Failed to start Remote desktop service (VNC).
    Feb 26 17:16:14 server.localdomain systemd[1]: Unit vncserver@:1.service entered failed state

    What can I do about this?
    Thank your very much.

    • Raj

      Try to use vncserver@5

  • vinay

    how to remove all the packages to redo the setup

  • Ashish

    Hi Raj,

    Once logged in using VNC client, the session is different from the actual system. Is there a way to get the same session ?

  • John O’Shaughnessy

    The instructions worked well. Thanks!

  • Ranger ForHire

    this does not work

    • Tutorial has been updated, it has been checked

  • Alberto Santos

    localhost.internal systemd[1]: Failed to start Remote desktop service (VNC).

    — Subject: Unit vncserver@:1.service has failed

    Remove this file if there is no X server
    : A VNC server is already running as :1

    .internal:1 is taken because of /tmp/.X11-unix/X1

    pam_unix(runuser-l:session): session opened for user root by (uid=0)

    hi, i failed to start the vnc server any ideas?

  • vncserver@:5.service – Remote desktop service (VNC)

    Loaded: error (Reason: Invalid argument)

    Active: inactive (dead)

    Dec 09 12:11:43 localhost.localdomain systemd[1]: vncserver@:5.service has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.

    Dec 09 12:11:51 localhost.localdomain systemd[1]: vncserver@:5.service has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.

    Dec 09 12:11:53 localhost.localdomain systemd[1]: vncserver@:5.service has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.

    Dec 09 12:11:59 localhost.localdomain systemd[1]: vncserver@:5.service has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.

    [root@localhost ~]# chkconfig vncserver on

    error reading information on service vncserver: No such file or directory

  • I am trying to setup remote desktop on CentOS 7, its work for me… Cheerful
    [How To Install and Configure VNC Remote Access for the GNOME Desktop on CentOS 7](https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-remote-access-for-the-gnome-desktop-on-centos-7?comment=35734)

  • Rashid

    thanks for sharing, i configure its working but the issue is i can view user desktop i access desktop what user is doing i cant see, it work like Remote login not as VNC like redhat 6 and below. can you help me how i get full access of user desktop and view what he is doing

Shares