[メモ] Raspberry Pi : VNCサーバ設定(自動起動)

- Raspbian 2016-09-23より、RealVNCが使える <br>=> 
[[メモ] Raspberry Pi : VNCサーバ設定(raspbian 2016-09-23～)](http://qiita.com/mt08/items/bcefc691d89c7981fad9)

## 概要
- 元記事: [VNC server in Jessie (using a systemd service)](https://www.raspberrypi.org/forums/viewtopic.php?t=123457)
のまま
- lightdmでの方法を追加
![RPi_vncviewer4.png](https://qiita-image-store.s3.amazonaws.com/0/75594/4c5f3f01-2381-cdc8-92fd-8495f90d6020.png)

![RPi_vncviewer3.png](https://qiita-image-store.s3.amazonaws.com/0/75594/91414013-8506-f12e-5463-5399a0e4ecf2.png)



## ためした環境
- Raspbeey Pi 2 / 3
- `2016-05-27-raspbian-jessie.img` (X使うので、liteじゃないよ)

## 手順
### その１: `systemd`
1. いつもの(microSDにimgやいて、`sudo apt-get update; sudo apt-get upgrade -y; sudo reboot`..)
2. インストール: <br>`sudo apt-get install -y tightvncserver`
3. 手動で一回立ち上げて、パスワード設定: <br>`vncserver :1`
4. systemdのファイル編集:<br>`sudo vi /etc/systemd/system/vncserver@.service`

    ```txt:/etc/systemd/system/vncserver@.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=pi
PAMName=login
PIDFile=/home/pi/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target
```
5. サービス登録:<br>`sudo systemctl daemon-reload && sudo systemctl enable vncserver@1.service`
6. 再起動:<br>`sudo reboot`
7. `vncviewer.exe`(Windows クライアント)をダウンロードインストール
    - [RealVNC](https://www.realvnc.com/download/viewer/windows/) 名前とEmailの登録が必要
      <br>![RPi_vncviewer.png](https://qiita-image-store.s3.amazonaws.com/0/75594/4f18afcb-e3fb-4ea6-2647-e96eedc5b93c.png)
    - [TigerVNC](https://github.com/TigerVNC/tigervnc/releases) => [Binaries are available from bintray:](https://bintray.com/tigervnc/stable/tigervnc/1.6.0)
   　　　<br>![RPi_vncviewer2.png](https://qiita-image-store.s3.amazonaws.com/0/75594/2819e920-ed1b-b736-647c-20fea3c22139.png)

8. `VNC Server:`へ、"ラズパイのIPアドレス:5901"を入れて `[Connect]`、パスワードいれてログイン
![RPi_vncviewer3.png](https://qiita-image-store.s3.amazonaws.com/0/75594/91414013-8506-f12e-5463-5399a0e4ecf2.png)

9. もうテレビでGUI loginしなくてもいいなら、Textログインでいいよね??
 - `sudo raspi-config` で、`3 Boot Options` > `B1 Console` を選択、再起動。

### その2: `lighdm`
1. いつもの(microSDにimgやいて、`sudo apt-get update; sudo apt-get upgrade -y; sudo reboot`..)
2. インストール: <br>`sudo apt-get install -y tightvncserver`
3. 手動で一回立ち上げて、パスワード設定: <br>`vncserver :1`
<br>やらないと、パスなしになる
4. `lightdm`設定編集:<br>`sudo vi /etc/lightdm/lightdm.conf`

    ```txt:/etc/lightdm/lightdm.conf
...
... 最後方。コメントはずして編集
[VNCServer]
enabled=true
command=Xvnc
port=5900
width=1280
height=800
depth=24
```
5. `sudo raspi-config` の`3 Boot Options` 
    - 自動ログイン(デフォルト): `B4 Desktop Autologin` を選択
    - ログインスクリーン: `B3 Desktop` を選択
    <br>![RPi_vncviewer4.png](https://qiita-image-store.s3.amazonaws.com/0/75594/4c5f3f01-2381-cdc8-92fd-8495f90d6020.png)
    - !!! コンソールを選ぶと`lightdm`が自動起動しない
6. `sudo reboot`
7. `vncviewer.exe`(Windows クライアント)をダウンロードインストール、
8. `VNC Server:`へ、"ラズパイのIPアドレス<font color='red'>**:5900**</font>"を入れて `[Connect]`、パスワードいれてログイン




## その他
- 画面のサイズをかえたい
<br>設定ファイル/etc/systemd/system/`vncserver@.service`の1280x800を変更。サービス再起動。
<br>`sudo systemctl daemon-reload && sudo systemctl restart vncserver@1.service`
- セッションを増やす?
<br>`sudo systemctl enable vncserver@2.service` で、`IP_address:5902`でログインできるが、、設定ファイルが`User=pi`...




### 設定ログ
```shell-session
pi@raspberrypi:~ $ sudo apt-get install -y tightvncserver 
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 33 not upgraded.
Need to get 6,736 kB of archives.
After this operation, 9,895 kB of additional disk space will be used.
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 [6,181 kB]
Fetched 6,736 kB in 3s (2,013 kB/s)      
Selecting previously unselected package tightvncserver.
(Reading database ... 118080 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:~ $ vncserver :1 

You will require a password to access your desktops.

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

New 'X' desktop is raspberrypi:1

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

pi@raspberrypi:~ $ sudo vi /etc/systemd/system/vncserver@.service
pi@raspberrypi:~ $ sudo systemctl daemon-reload && sudo systemctl enable vncserver@1.service
Created symlink from /etc/systemd/system/multi-user.target.wants/vncserver@1.service to /etc/systemd/system/vncserver@.service.
pi@raspberrypi:~ $ sudo reboot

```