Tuesday, May 10, 2016

How to Setup VNCServer on CentOS 7 for Remote Access

VNCServer helps connect to a host remotely, the clients see a graphical interface (typically the desktop manager like GNOME or XFCE or KDE). The following steps can be followed to configure VNCServer on CentOS 7 (and also RHEL7.x).

  • Install the X Window system (if not already there):

    First make sure that yum is configured. You may run the following commands (as root) view and install the X Windows System (GNOME).

    ~]# yum group list
    Installed Environment Groups:
        Server with GUI
    Available Environment Groups:
        Minimal Install
        Infrastructure Server
        File and Print Server
        Basic Web Server
        Virtualization Host
    Available Groups:
        Compatibility Libraries
        Console Internet Tools
        Development Tools
        Graphical Administration Tools
        Legacy UNIX Compatibility
        Scientific Support
        Security Tools
        Smart Card Support
        System Administration Tools
        System Management
    Done

    ~]# yum groupinstall "Server with GUI"

  • Install VNCServer:

    Install the tigervnc-server package, which provides the required VNCServer functionality.

    ~]# yum install tigervnc-server*

  • Setup VNCServer user and Firewall:

    The VNCServer runs at ports starting from 5900 and port identifies a display number. For instance port 5901 indicates VNCServer display :1 which can typically be accessed via hostname/ipaddress:1. If needed these ports should be added to the firewall as well.

    Create a user test which will be used to login from the VNC client.

    ~]# useradd testuser
    ~]# passwd testuser

    Login as testuser and set a password for VNCServer.

    ~]# su - testuser
    ~]$ vncpasswd
    ~]$ logout
    ~]#

    Let us assume that we will be hosting VNCServer at display :1 (port 5901). Add this port to the firewall.

    ~]# firewall-cmd --permanent --zone=public --add-port=5901/tcp
    ~]# firewall-cmd --reload

  • Configure VNCServer at display :1 (port 5901):

    Copy the template service file that came with the VNCServer package to /etc/systemd/system/. Rename the file to indicate display :1.

    ~]# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

    Edit the file with an editor (vim or emacs) and change the <USER> to the testuser we have created above.

    [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=/usr/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

    Save the changes made to the file. Now enable the service to run at boot and start the service.

    ~]# systemctl daemon-reload
    ~]# systemctl enable vncserver@:1.service
    ~]# systemctl start vncserver@:1.service

Now use a VNC Client software to connect to the VNCServer running on the host at <hostname>:1.

That's it!

0 comments:

Post a Comment