VNC: Remote Desktop on Rocky
Introduction
Virtual Network Computing (VNC) is a system for remote access to a computer desktop using the RFB protocol (English Remote FrameBuffer, remote frame buffer).
Control is carried out by transmitting keyboard keystrokes and mouse movements from one computer to another and relaying the screen contents over a computer network.
VNC 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 you want to give VNC access to
vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used
view-only password is the password for access with view-only rights. Mouse and keyboard actions are limited 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
Carry out the first and second steps from the Quick HowTo
sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
Pay attention to 1 - this is how the display number is specified for this version of the server. You could have chosen another number, the main thing is to remember this choice.
sudo vi /etc/systemd/system/vncserver@\:1.service
In the line
ExecStart=/usr/bin/vncserver_wrapper <USER> %i
Replace <USER> with your user name. (mine is called andrei)
You can do it manually with vi
or with 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
sudo systemctl daemon-reload
sudo systemctl start vncserver@:1
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 Mar 16 13:49:45 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Mar 16 13:49:55 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Mar 16 13:50:35 localhost.localdomain vncserver_wrapper[23145]: New 'localhost.localdomain:1 (andrei)' desktop is localhost.localdomain:1 Mar 16 13:50:35 localhost.localdomain vncserver_wrapper[23145]: Creating default startup script /home/andrei/.vnc/xstartup Mar 16 13:50:35 localhost.localdomain vncserver_wrapper[23145]: Creating default config /home/andrei/.vnc/config Mar 16 13:50:35 localhost.localdomain vncserver_wrapper[23145]: Starting applications specified in /home/andrei/.vnc/xstartup Mar 16 13:50:35 localhost.localdomain vncserver_wrapper[23145]: Log file is /home/andrei/.vnc/localhost.localdomain:1.log
To make the VNC sever start at system boot, run
systemctl enable vncserver@:1
You can check that the vnc server listens 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))
Now port 5901 has to be opened in 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 ones
If you suddenly became 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
Enter the password of the user for whom you created vncpasswd
Stop 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 ===
Change 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 2021-03-17 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) Mar 17 16:55:55 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Mar 17 16:55:55 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Mar 17 16:55:56 localhost.localdomain vncserver_wrapper[8426]: Warning: localhost.localdomain:0 is taken because of /tmp/.X0-lock Mar 17 16:55:56 localhost.localdomain vncserver_wrapper[8426]: Remove this file if there is no X server localhost.localdomain:0 Mar 17 16:55:56 localhost.localdomain vncserver_wrapper[8426]: A VNC server is already running as :0 Mar 17 16:55:56 localhost.localdomain systemd[1]: vncserver@:0.service: main process exited, code=exited, status=2/INVALIDARGUMENT Mar 17 16:55:56 localhost.localdomain vncserver_wrapper[8426]: FATAL: 'runuser -l andrei' failed! Mar 17 16:55:56 localhost.localdomain systemd[1]: Unit vncserver@:0.service entered failed state. Mar 17 16:55:56 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 has to 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 2021-03-17 17:14:50 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) Mar 17 17:14:50 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Mar 17 17:14:50 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Mar 17 17:14:50 localhost.localdomain vncserver_wrapper[9358]: Warning: localhost.localdomain:0 is taken because of /tmp/.X11-unix/X0 Mar 17 17:14:50 localhost.localdomain vncserver_wrapper[9358]: Remove this file if there is no X server localhost.localdomain:0 Mar 17 17:14:50 localhost.localdomain vncserver_wrapper[9358]: A VNC server is already running as :0 Mar 17 17:14:50 localhost.localdomain systemd[1]: vncserver@:0.service: main process exited, code=exited, status=2/INVALIDARGUMENT Mar 17 17:14:50 localhost.localdomain vncserver_wrapper[9358]: FATAL: 'runuser -l andrei' failed! Mar 17 17:14:50 localhost.localdomain systemd[1]: Unit vncserver@:0.service entered failed state. Mar 17 17:14:50 localhost.localdomain systemd[1]: vncserver@:0.service failed.
Now the file has to be deleted .X11-unix/X0k has to be deleted
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 2021-03-17 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 Mar 17 17:16:39 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)... Mar 17 17:16:39 localhost.localdomain systemd[1]: Started Remote desktop service (VNC). Mar 17 17:16:42 localhost.localdomain vncserver_wrapper[9516]: WARNING: The first attempt to start Xvnc failed, possibly because the font Mar 17 17:16:42 localhost.localdomain vncserver_wrapper[9516]: catalog is not properly configured. Attempting to determine an appropriate Mar 17 17:16:42 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) Mar 18 15:50:32 localhost.localdomain vncserver_wrapper[4260]: (EE) Cannot establish Mar 18 15:50:32 localhost.localdomain vncserver_wrapper[4260]: _XSERVTransSocketUNIX Mar 18 15:50:32 localhost.localdomain vncserver_wrapper[4260]: _XSERVTransMakeAllCOT Mar 18 15:50:32 localhost.localdomain vncserver_wrapper[4260]: (EE) Mar 18 15:50:32 localhost.localdomain vncserver_wrapper[4260]: Fatal server error:
Make sure you do not have X Window System
usually it occupies display 0.
If it is running, close X
Article author: Andrei Olegovich