VNC RHEL

Introduction

Virtual Network Computing (VNC) is a system for remote access to a computer desktop that uses the RFB protocol (English: Remote FrameBuffer, remote frame buffer).

Control is performed by transmitting keystrokes on the keyboard and mouse movements from one computer to another and by relaying the contents of the screen over a computer network.

The VNC system is platform-independent: a VNC client, called a VNC viewer, running on one operating system can connect to a VNC server running on any other OS.

There are client and server implementations for almost all operating systems, including Java (including the J2ME mobile platform).

Multiple clients can connect to one VNC server at the same time.

The most popular uses of VNC are remote technical support and access to a work computer from home.

Installing tigervnc-server

Run

sudo yum install tigervnc-server

Log in as the user to whom you want to give access over VNC

vncpasswd

Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used

A view-only password is a password for access with viewing rights only. Mouse and keyboard actions are restricted during such access.

sudo vi /lib/systemd/system/vncserver@.service

# The vncserver service unit file # # Quick HowTo: # 1. Copy this file to /etc/systemd/system/vncserver@.service # 2. Replace <USER> with the actual user name and edit vncserver # parameters in the wrapper script located in /usr/bin/vncserver_wrapper # 3. Run `systemctl daemon-reload` # 4. Run `systemctl enable vncserver@:<display>.service` # # DO NOT RUN THIS SERVICE if your local area network is # untrusted! For a secure way of using VNC, you should # limit connections to the local host and then tunnel from # the machine you want to view VNC on (host A) to the machine # whose VNC output you want to view (host B) # # [user@hostA ~]$ ssh -v -C -L 590N:localhost:590M hostB # # this will open a connection on port 590N of your hostA to hostB's port 590M # (in fact, it ssh-connects to hostB and then connects to localhost (on hostB). # See the ssh man page for details on port forwarding) # # You can then point a VNC client on hostA at vncdisplay N of localhost and with # the help of ssh, you end up seeing what hostB makes available on port 590M # # Use "-nolisten tcp" to prevent X connections to your VNC server via TCP. # # Use "-localhost" to prevent remote VNC clients connecting except when # doing so through a secure tunnel. See the "-via" option in the # `man vncviewer' manual page. [Unit] Description=Remote desktop service (VNC) After=syslog.target network.target [Service] Type=simple # Clean any existing files in /tmp/.X11-unix environment ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' ExecStart=/usr/bin/vncserver_wrapper <USER> %i ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' [Install] WantedBy=multi-user.target

Run the first and second steps from Quick HowTo

sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

Note the 1 - this is how the display number is specified for this version of the server. Another number could have been chosen, the main thing is to remember that choice.

sudo vi /etc/systemd/system/vncserver@\:1.service

In the line

ExecStart=/usr/bin/vncserver_wrapper <USER> %i

Replace <USER> with the name of your user. (mine is called andrei)

You can do it manually with vi or you can use sed

sudo sed -i 's/wrapper\ <USER>/wrapper\ andrei/' /etc/systemd/system/vncserver@\:1.service

If your user is called vncuser the command will look like this:

sudo sed -i 's/wrapper\ <USER>/wrapper\ vncuser/' /etc/systemd/system/vncserver@\:1.service

Starting the VNC server

To make the VNC server start when the system boots, run

systemctl enable vncserver@:1

Starting the VNC server manually

sudo systemctl daemon-reload
sudo systemctl start vncserver@:1

Checking the VNC server status

systemctl status vncserver@:1

● vncserver@:1.service - Remote desktop service (VNC) Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2021-03-16 13:49:55 EET; 3min 56s ago Process: 23138 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS) Main PID: 23145 (vncserver_wrapp) CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service ├─23145 /bin/sh /usr/bin/vncserver_wrapper andrei :1 └─24097 /bin/sh /usr/bin/vncserver_wrapper andrei :1 Dec 01 05:42:59 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Dec 01 05:42:59 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[23145]: New 'localhost.localdomain:1 (andrei)' desktop is localhost.localdomain:1 Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[23145]: Creating default startup script /home/andrei/.vnc/xstartup Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[23145]: Creating default config /home/andrei/.vnc/config Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[23145]: Starting applications specified in /home/andrei/.vnc/xstartup Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[23145]: Log file is /home/andrei/.vnc/localhost.localdomain:1.log

You can check that the VNC server is listening on port 5901 with the command

ss -tulpn| grep vnc

tcp LISTEN 0 5 *:5901 *:* users:(("Xvnc",pid=1330,fd=9)) tcp LISTEN 0 128 *:6001 *:* users:(("Xvnc",pid=1330,fd=6)) tcp LISTEN 0 5 [::]:5901 [::]:* users:(("Xvnc",pid=1330,fd=10)) tcp LISTEN 0 128 [::]:6001 [::]:* users:(("Xvnc",pid=1330,fd=5))

Opening the port in the Firewall

Now port 5901 has to be opened in the firewall

Run

sudo firewall-cmd --add-port=5901/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports

19999/tcp 5901/tcp

As you can see, port 5901 has been successfully added to the list of open ports

If you are suddenly curious what port 19999 is, it is used by Locust

Connecting to the VNC server

Now you can install a VNC client, for example Remmina

sudo yum install remmina

And connect to the VNC server by entering the required IP address and port :5901

Connecting to Centos over VNC, image from the website www.aredel.com
Connecting to Centos over VNC with Remmina
www.aredel.com

Enter the password of the user for whom you created vncpasswd

Connecting to Centos over VNC, image from the website www.aredel.com
Connecting to Centos over VNC with Remmina
www.aredel.com
Connecting to Centos over VNC, image from the website www.aredel.com
Connecting to Centos over VNC with Remmina
www.aredel.com

Stopping the VNC server

with the command

systemctl stop vncserver@:1

==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to manage system services or units. Authenticating as: andrei Password: ==== AUTHENTICATION COMPLETE ===

Changing the port and display

If you copy the existing file /etc/systemd/system/vncserver@:1.service

cp /etc/systemd/system/vncserver@:1.service /etc/systemd/system/vncserver@:0.service
systemctl daemon-reload

==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon === Authentication is required to reload the systemd state. Authenticating as: andrei Password: ==== AUTHENTICATION COMPLETE === [andrei@localhost system]$ systemctl start vncserver@:0 ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to manage system services or units. Authenticating as: andrei Password: ==== AUTHENTICATION COMPLETE === [andrei@localhost system]$ systemctl status vncserver@:0 ● vncserver@:0.service - Remote desktop service (VNC) Loaded: loaded (/etc/systemd/system/vncserver@:0.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Wed 2026-12-01 16:55:56 EET; 11s ago Process: 8426 ExecStart=/usr/bin/vncserver_wrapper andrei %i (code=exited, status=2) Process: 8424 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS) Main PID: 8426 (code=exited, status=2) Dec 01 05:42:59 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Dec 01 05:42:59 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[8426]: Warning: localhost.localdomain:0 is taken because of /tmp/.X0-lock Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[8426]: Remove this file if there is no X server localhost.localdomain:0 Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[8426]: A VNC server is already running as :0 Dec 01 05:42:59 localhost.localdomain systemd[1]: vncserver@:0.service: main process exited, code=exited, status=2/INVALIDARGUMENT Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[8426]: FATAL: 'runuser -l andrei' failed! Dec 01 05:42:59 localhost.localdomain systemd[1]: Unit vncserver@:0.service entered failed state. Dec 01 05:42:59 localhost.localdomain systemd[1]: vncserver@:0.service failed.

Note the warning Warning: localhost.localdomain:0 is taken because of /tmp/.X0-lock

The file .X0-lock must be deleted

rm /tmp/.X0-lock
systemctl start vncserver@:0
systemctl status vncserver@:0

● vncserver@:0.service - Remote desktop service (VNC) Loaded: loaded (/etc/systemd/system/vncserver@:0.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Wed 2026-12-01 00:01:00 EET; 13s ago Process: 9358 ExecStart=/usr/bin/vncserver_wrapper andrei %i (code=exited, status=2) Process: 9356 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS) Main PID: 9358 (code=exited, status=2) Dec 01 05:42:59 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Dec 01 05:42:59 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[9358]: Warning: localhost.localdomain:0 is taken because of /tmp/.X11-unix/X0 Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[9358]: Remove this file if there is no X server localhost.localdomain:0 Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[9358]: A VNC server is already running as :0 Dec 01 05:42:59 localhost.localdomain systemd[1]: vncserver@:0.service: main process exited, code=exited, status=2/INVALIDARGUMENT Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[9358]: FATAL: 'runuser -l andrei' failed! Dec 01 05:42:59 localhost.localdomain systemd[1]: Unit vncserver@:0.service entered failed state. Dec 01 05:42:59 localhost.localdomain systemd[1]: vncserver@:0.service failed.

Now delete the file .X11-unix/X0k as well

rm /tmp/.X11-unix/X0k
systemctl daemon-reload
systemctl start vncserver@:0
systemctl status vncserver@:0

● vncserver@:0.service - Remote desktop service (VNC) Loaded: loaded (/etc/systemd/system/vncserver@:0.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2026-12-01 17:16:39 EET; 4s ago Process: 9513 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS) Main PID: 9516 (vncserver_wrapp) CGroup: /system.slice/system-vncserver.slice/vncserver@:0.service └─9516 /bin/sh /usr/bin/vncserver_wrapper andrei :0 Dec 01 05:42:59 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Dec 01 05:42:59 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[9516]: WARNING: The first attempt to start Xvnc failed, possibly because the font Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[9516]: catalog is not properly configured. Attempting to determine an appropriate Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[9516]: font path for this system and restart Xvnc using that font path ...

Display 0

If you try to start the server on display 0 and get the error

● vncserver@:0.service - Remote desktop service (VNC) Loaded: loaded (/etc/systemd/system/vncserver@:0.service; enabled; vendor preset: Active: failed (Result: exit-code) since Thu 2021-03-18 15:50:32 EET; 2min 8s ago Process: 4260 ExecStart=/usr/bin/vncserver_wrapper andrei %i (code=exited, status= Process: 4255 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 Main PID: 4260 (code=exited, status=2) Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[4260]: (EE) Cannot establish Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[4260]: _XSERVTransSocketUNIX Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[4260]: _XSERVTransMakeAllCOT Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[4260]: (EE) Dec 01 05:42:59 localhost.localdomain vncserver_wrapper[4260]: Fatal server error:

Make sure that you do not have a running X Window System it usually occupies display 0.

If it is running - close X

Article author: Andrei Olegovich