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.
|
|
For setting vnc password in service mode,
|
|
1.5 Authenticating to VNC Server
- Open the
/root/.vnc/config.d/vncserver-x11 config file
- Replace
Authentication=SystemAuth
withAuthentication=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,
|
|
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
(*).
|
|
Allow VNC port
|
|
Check the status again.
|
|
Then reboot Raspberry Pi
|
|
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.
|
|
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
|
|