2016年5月5日 星期四

樹莓派的遠端桌面控制 : 使用 VNC

4/18 去黑部立山之前將快到期的 "Raspberry Pi 最佳入門與實戰應用 (碁峰, 柯博文)" 拿去還給市圖, 回來後想再借卻發現這本書很熱門(可見樹莓派受歡迎程度),要再借回來可能要到六月後,這實在太久了.我 4/29 日跑去明儀發現,其實這本書已經出第二版,內容有增補,與其久等圖書館的舊版,還不如買新版算了,反正也不貴,定價450,打75折才338元,畢竟時間比較寶貴,好的書也值得買起來.

金石堂書店

今天繼續看 "Raspberry Pi 最佳入門與實戰應用(第二版)" 的 3-5 節 "VNC 遠端控制程式架設", 類似 PC 上常用的遠端桌面程式 Teamviewer 的功能, 可以從別台電腦遠端連線到樹苺派的桌面 (SSH 是遠端連線到命令模式介面), 這樣只要 Pi 能夠連上網路, 我們便可以從 PC 遠端存取樹苺派的圖形介面, 不需要另外準備鍵盤滑鼠組來操作其視窗介面. 其實我去年買第一塊 Raspberry Pi B開始玩樹苺派時就用過了, 今年改灌新版 Rasbian, 重新再跑一遍, 參考 :

# 樹苺派的 VNC 遠端連線

VNC 的運作原理是必須在目的機器安裝 VNC Server, 在來源機器安裝 VNC Client, 然後就可以從 Client 以密碼登入方式存取遠端機器的桌面.

至於 TeamViewer, 因為它還沒有 ARM 版本, 所以沒辦法直接安裝使用, 但是可以利用 ExtraGear Desktop 這個軟體在 ARM 機器上跑 X86 的軟體, 參考 :

Run TeamViewer on Raspberry Pi

以下是我在新的 Rasbian 上重新安裝 VNC Server 的紀錄, 參考 :

Raspberry Pi:VNC伺服器之TightVNC
# [基礎] 以 VNC 和 Raspberry Pi 連線

login as: pi
pi@192.168.2.105's password: (預設 raspberry)

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Mar 29 21:01:26 2016 from 192.168.2.100
pi@raspberrypi:~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:97:b6:52
          inet addr:192.168.2.104  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::873a:49c1:5be0:24db/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5115 errors:0 dropped:29 overruns:0 frame:0
          TX packets:1427 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:637465 (622.5 KiB)  TX bytes:199421 (194.7 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 08:57:00:16:88:0c
          inet addr:192.168.2.105  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a57:ff:fe16:880c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6947 errors:0 dropped:324 overruns:0 frame:0
          TX packets:1123 errors:0 dropped:1 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1272940 (1.2 MiB)  TX bytes:103549 (101.1 KiB)

pi@raspberrypi:~ $ ./svnc.sh
-bash: ./svnc.sh: No such file or directory
pi@raspberrypi:~ $ sudo apt-get update     (更新軟體套件清單)
Get:1 http://mirrordirector.raspbian.org jessie InRelease [15.0 kB]
Get:2 http://archive.raspberrypi.org jessie InRelease [13.2 kB]
Get:3 http://mirrordirector.raspbian.org jessie/main armhf Packages [8963 kB]
Get:4 http://archive.raspberrypi.org jessie/main armhf Packages [145 kB]
Hit http://archive.raspberrypi.org jessie/ui armhf Packages
Ign http://archive.raspberrypi.org jessie/main Translation-lzh_TW
Ign http://archive.raspberrypi.org jessie/main Translation-lzh
Ign http://archive.raspberrypi.org jessie/main Translation-en
Ign http://archive.raspberrypi.org jessie/ui Translation-lzh_TW
Ign http://archive.raspberrypi.org jessie/ui Translation-lzh
Ign http://archive.raspberrypi.org jessie/ui Translation-en
Get:5 http://mirrordirector.raspbian.org jessie/contrib armhf Packages [37.5 kB]
Get:6 http://mirrordirector.raspbian.org jessie/non-free armhf Packages [70.3 kB]
Get:7 http://mirrordirector.raspbian.org jessie/rpi armhf Packages [1356 B]
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-lzh_TW
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-lzh
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
Ign http://mirrordirector.raspbian.org jessie/main Translation-lzh_TW
Ign http://mirrordirector.raspbian.org jessie/main Translation-lzh
Ign http://mirrordirector.raspbian.org jessie/main Translation-en
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-lzh_TW
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-lzh
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-lzh_TW
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-lzh
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
Fetched 9245 kB in 1min 16s (121 kB/s)
Reading package lists... Done
pi@raspberrypi:~ $ sudo apt-get install tightvncserver   (安裝 VNC 伺服器)
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  xfonts-base
Suggested packages:
  tightvnc-java
The following NEW packages will be installed:
  tightvncserver xfonts-base
0 upgraded, 2 newly installed, 0 to remove and 12 not upgraded.
Need to get 6736 kB of archives.
After this operation, 9895 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main tightvncserver armhf 1.3.9-6.5 [555 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main xfonts-base all 1:1.0.3 [6181 kB]
Fetched 6736 kB in 6s (988 kB/s)
Selecting previously unselected package tightvncserver.
(Reading database ... 126384 files and directories currently installed.)
Preparing to unpack .../tightvncserver_1.3.9-6.5_armhf.deb ...
Unpacking tightvncserver (1.3.9-6.5) ...
Selecting previously unselected package xfonts-base.
Preparing to unpack .../xfonts-base_1%3a1.0.3_all.deb ...
Unpacking xfonts-base (1:1.0.3) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for fontconfig (2.11.0-6.3) ...
Setting up tightvncserver (1.3.9-6.5) ...
update-alternatives: using /usr/bin/tightvncserver to provide /usr/bin/vncserver (vncserver) in auto mode
update-alternatives: using /usr/bin/Xtightvnc to provide /usr/bin/Xvnc (Xvnc) in auto mode
update-alternatives: using /usr/bin/tightvncpasswd to provide /usr/bin/vncpasswd (vncpasswd) in auto mode
Setting up xfonts-base (1:1.0.3) ...
pi@raspberrypi:~ $ sudo tightvncserver  (啟動 VNC 伺服器)

You will require a password to access your desktops.

Password:      (設定 VNC 密碼)
Warning: password truncated to the length of 8.
Verify:
Would you like to enter a view-only password (y/n)? n   (完全存取)

New 'X' desktop is raspberrypi:1

Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/raspberrypi:1.log

pi@raspberrypi:~ $ vncserver :1  (設定 VNC Server 連線位置 1, 若 1 不行就用 2 或其他號碼)

You will require a password to access your desktops.

Password:
Warning: password truncated to the length of 8.
Verify:
Would you like to enter a view-only password (y/n)? n
A VNC server is already running as :1
pi@raspberrypi:~ $ sudo bash
root@raspberrypi:/home/pi# sudo nano /etc/init.d/vncboot (編輯開機啟動檔)

樹莓派關機後, VNC 伺服器連線位置也會關閉, 每次開機都必須執行 vncserver 指令來重新設定, 如果要開機自動啟動 VNC Server, 可以到 /etc/init.d 目錄下用 sudo nano vncboot 編輯自動執行檔案如下 :

#! /bin/sh
# /etc/init.d/vncboot
USER=root HOME=/root
export USER HOME
echo "Starting VNC Server"
/usr/bin/vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565
exit 0

按 Ctrl+O 儲存後按 Ctrl+X 跳出即可. 然後更改此 vncboot 檔為 755 模式即可 :

root@raspberrypi:/home/pi# sudo chmod 755 /etc/init.d/vncboot
root@raspberrypi:/home/pi# sudo update-rc.d vncboot defaults
insserv: warning: script 'K01vncboot' missing LSB tags and overrides
insserv: warning: script 'vncboot' missing LSB tags and overrides


另外在碁峰曾吉弘譯的 "Raspberry Pi 機器人自造專案" 這本書的 1-21 頁提到, 開機自動開啟 VNC server 有安全性風險, 而且多一個軟體在執行也占用記憶體, 不如要用時再用 putty 連線手動開啟 VNC 伺服器.


開機啟動設定也可參考 :

# Ubuntu Server: How to run VNC on startup
# Raspberry pi 開機自動開啟VNC(tightvncserver)

以上樹莓派的部分就 OK 了, 接下來是遠端連線軟體, 可用 RealVNC 的 VNC Viewer :

https://www.realvnc.com/download/viewer/

點選 VNC Viewer 中的 Windows 方塊 :


下載適當版本 (我的是 64-bits) 的 exe 檔 :


填寫姓名與 email 後即可下載 :


VNC Viewer 不須安裝, 直接執行即可, 在登入視窗中輸入樹莓派中所安裝的 VNC Server 網址與伺服器編號, 按 Connect :


輸入上面安裝 VNC 伺服器時設定的密碼, 我設成樹莓派預設之 raspberry :


按 OK 就會顯示樹莓派的 GUI 視窗了 :


關閉 VNC 伺服器使用下列指令 :

pi@raspberrypi:~ $ vncserver -kill :1
Killing Xtightvnc process ID 10051

更改 VNC server 的密碼使用 vncpasswd 指令 :

pi@raspberrypi:~ $ vncpasswd
Using password file /home/pi/.vnc/passwd
Password:   (輸入 8 字元新密碼)
Warning: password truncated to the length of 8.
Verify:  (再輸入一次)
Would you like to enter a view-only password (y/n)? n  (不是唯讀輸入 n)
pi@raspberrypi:~ $

不過這個 VNC Viewer 視窗竟然沒辦法用 Print Screen 鍵或 Picpick 抓圖軟體抓下來, 參見 :

How To Take Screenshots On The Raspberry Pi
Print screen doesn't work 
# Raspberry Pi Screenshots

我是利用 Apowersoft 的免費線上螢幕錄影軟體 進行錄製, 然後在錄製過程中按暫停, 再用Print Screen 鍵或 Picpick 抓圖軟體抓下來的. 參考 :

關於螢幕錄影軟體

其他參考 :

# insserv: warning: script 'service' missing LSB tags and overrides
# Remote Control Using VNC and No-IP

2017-02-02 補充 :

如果要全彩, 則啟動伺服器時要用 -depth 32 與 rgb888, 但全彩速度較慢 :

$ vncserver -geometry 1280x768 -depth 32 -pixelformat rgb888

New 'X' desktop is raspberrypi:2

Starting applications specified in /home/pi/.vnc/xstartup
Log file is /home/pi/.vnc/raspberrypi:2.log

pi@raspberrypi:~ $


點選上方隱藏式選單最右邊的 "i" 按鈕會顯示螢幕設定 :



沒有留言 :