Contents

Enable VNC on Raspberry Pi by command line

For accessing raspberry pi from remote computer over GUI, I needed to activate VNC on rpi by command line as I do not have physical access for display and input devices on my rpi. Below are a list of configuration for activating VNC.

Although some web pages say that Real VNC Server needs to be configured on GUI, I did not need to enter GUI on Raspberry pi for configuring it.

1. Enable VNC Server on Raspberry Pi

1.1. Change Desktop mode logon

After logon to Raspberry pi over SSH and running configuration tool by sudo raspi-config

  • 1 System Options
  • S5 Boot / Auto Login
  • B3 Desktop

1.2. Set resolution

Set screen resolution by sudo raspi-config

  • 7 Advanced Options
  • A5 Resolution
  • DMT 1024×768

On Linux rpi4 6.1.21-v8+,

  • 2 Display Option
  • D5 VNC Resolution
  • 1024x768

1.3. Enable VNC

Enable VNC server by sudo raspi-config

  • 5 Interfacing Options
  • P3 VNC
  • Would you like the VNC Server to be enabled? : Yes

On Linux rpi4 6.1.21-v8+,

  • 3 Interface Options
  • I3 VNC
  • Would you like the VNC Server to be enabled? : Yes

1.4. Set vncpasswd

Set vnc password by below command.

1
vncpasswd

For setting vnc password in service mode,

1
sudo vncpasswd -service

1.5 Authenticating to VNC Server

  • Open the /root/.vnc/config.d/vncserver-x11 config file
  • Replace Authentication=SystemAuth with Authentication=VncAuth and save the file..
  • In the command line, run sudo vncpasswd -service. This will prompt you to set a password, and will insert it for you in the right config file for VNC Server running in Service Mode.

1.5 Run VNC server

If needed,

1
vncserver

You should see something like VNC desktop number like output strings like pizero01:1

You can kill it by vncserver -kill:1

2. Firewall setting on Raspberry Pi

Check firewall status if you installed ufw (*).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
4444                       ALLOW       Anywhere                  
22                         ALLOW       Anywhere                  
23                         ALLOW       Anywhere                  
4444 (v6)                  ALLOW       Anywhere (v6)             
22 (v6)                    ALLOW       Anywhere (v6)             
23 (v6)                    ALLOW       Anywhere (v6)             

Allow VNC port

1
sudo ufw allow 5900

Check the status again.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
4444                       ALLOW       Anywhere                  
22                         ALLOW       Anywhere                  
23                         ALLOW       Anywhere                  
5900                       ALLOW       Anywhere                  
4444 (v6)                  ALLOW       Anywhere (v6)             
22 (v6)                    ALLOW       Anywhere (v6)             
23 (v6)                    ALLOW       Anywhere (v6)             
5900 (v6)                  ALLOW       Anywhere (v6)             

Then reboot Raspberry Pi

1
sudo reboot

3. Check networking configuration on router

Checking home router and see if the port mapping is enabled on the router. In my case, Atem router has a configuration.

4. Access Raspi from VNC client

In my case, I installed VNC Viewer by RealVNC and access it by IP address:display number like 192.168.0.xxx:1.

Appendix

Upgrade apt and refresh the key

You may encounter issue on Certificate. In that case, it would be recommended that upgrading apt.

1
sudo apt-get update && sudo apt-get upgrade

And delete the old key (or rename it) to force vncserver to re-generate one rm /root/.vnc/private.key and finally start the server sudo vncserver.

Firewall setup by ufw on linux

1
2
3
4
sudo apt install ufw
sudo apt ufw default deny
sudo ufw allow 22 # SSH
sudo ufw allow 23 # Telnet

Reference