TigerVNC

提供: ArchWiki
2023年2月22日 (水) 10:39時点におけるKusanaginoturugi (トーク | 投稿記録)による版 (→‎物理ディスプレイで vncserver を実行: 記事を削除)
ナビゲーションに移動 検索に移動

関連記事

TigerVNCVNC プロトコルの実装です。この記事ではサーバー機能に焦点をあてています。

インストール

tigervnc パッケージをインストールしてください。

ノート: パッケージには vncserver, x0vncserver, vncviewer が含まれています。

Vncserver には2つのリモート制御機能が存在します:

  1. 標準的な X サーバーと同じような仮想 (ヘッドレス) サーバー。物理的な画面ではなく仮想スクリーンを使用する。仮想サーバーは物理的な X サーバーと並行して動作します。
  2. 物理モニターを使用しているローカルの X セッションの直接制御。

vncserver をバーチャル(ヘッドレス)セッションで起動する

初回セットアップ

ノート: Linux システムでは物理メモリが許すかぎりいくらでも VNC サーバーを使うことができます。それぞれは互いに並行して動きます。

クイックスタートについては、以下のステップを参照してください。 設定オプションの完全なリストについては vncserver(8) を読むことをお勧めします。

  1. vncpasswd を使用してパスワードを作成して、ハッシュ化したパスワードを ~/.vnc/passwd に保存します。
  2. /etc/tigervnc/vncserver.users を編集して、ユーザマッピングを定義します。このファイルで定義された各ユーザは、そのセッションが実行される対応するポートを持っています。ファイル内の数字は、TCP ポートに対応します。デフォルトでは、:1 が TCP ポート 5901 (5900+1) になっています。別の並列サーバが必要な場合、2 番目のインスタンスは次に高い空きポート、つまり 5902 (5900+2) で実行することができます。
  3. ~/.vnc/config を作成し、最低限 session=foo のような行で、実行するデスクトップ環境を foo に対応させたセッションの種類を定義してください。どのデスクトップ環境がシステムで利用できるかは、/usr/share/xsessions/ 内の .desktop ファイルで確認できます。例えば:
~/.vnc/config
session=lxqt
geometry=1920x1080
localhost
alwaysshared
ノート: 前のステップを完全に動作させるには、追加のセキュリティ設定が必要です。設定を完了するには、#SSHトンネル経由での vncserver へのアクセスを参照してください。テスト目的では、この手順を省略して localhost 以外からの安全でない接続を許可します。

tigervnc の開始と終了

vncserver@.service テンプレートのインスタンスを 起動 します。オプションで起動時やシャットダウン時に動くよう有効化します。この場合のインスタンス識別子はディスプレイ番号であることに注意してください(例:ディスプレイ番号 :1 のインスタンス vncserver@:1.service など)。

ノート: /usr/bin/vncserver への直接の呼び出しは、適切なセッションスコープを確立しないため、サポートされていません。systemd サービスは TigerVNC を使うために唯一サポートされている方法です。Issue #1096 を参照してください。

Expose the local display directly

Tigervnc comes with libvnc.so which can be directly loaded during X initialization which provides better performance. Create a following file and restart X:

/etc/X11/xorg.conf.d/10-vnc.conf
Section "Module"
Load "vnc"
EndSection

Section "Screen"
Identifier "Screen0"
Option "UserPasswdVerifier" "VncAuth"
Option "PasswordFile" "/root/.vnc/passwd"
EndSection

Running x0vncserver to directly control the local display

tigervnc also provides x0vncserver(1) which allows direct control over a physical X session. After defining a session password using the vncpasswd tool, invoke the server like so:

$ x0vncserver -rfbauth ~/.vnc/passwd
ノート:
  • x11vnc is an alternative VNC server which can also provide direct control of the current X session.
  • x0vncserver does not currently support clipboard sharing between the client and the server (even with the help of autocutsel). See: Issue #529.

With xprofile

A simple way to start x0vncserver is adding a line in one of the xprofile files such as:

~/.xprofile
...
x0vncserver -rfbauth ~/.vnc/passwd &

With systemd

With a system service

This option will allow the users to access the current display, including the login screen provided by your display manager.

The service will be relaunched automatically every time an user logs off of their session.

LightDM is used for the example below, but it should be possible to adapt it to other display managers by modifying the XAUTHORITY variable.

/etc/systemd/system/x0vncserver.service
[Unit]
Description=Remote desktop service (VNC) for :0 display
Requires=display-manager.service
After=network-online.target
After=display-manager.service

[Service]
Type=simple
Environment=HOME=/root
Environment=XAUTHORITY=/var/run/lightdm/root/:0
ExecStart=x0vncserver -display :0 -rfbauth ~/.vnc/passwd
Restart=on-failure
RestartSec=500ms

[Install]
WantedBy=multi-user.target

As this is a system unit, -rfbauth ~/.vnc/passwd refers to /root/.vnc/passwd

Start/enable x0vncserver.service.

With a user service

In order to have a VNC Server running x0vncserver, which is the easiest way for most users to quickly have remote access to the current desktop, create a systemd unit as follows replacing the user and the options with the desired ones:

~/.config/systemd/user/x0vncserver.service
[Unit]
Description=Remote desktop service (VNC)

[Service]
Type=simple
ExecStartPre=/bin/sh -c 'while ! pgrep -U "$USER" Xorg; do sleep 2; done'
ExecStart=/usr/bin/x0vncserver -rfbauth %h/.vnc/passwd

[Install]
WantedBy=default.target

The ExecStartPre line waits for Xorg to be started by ${USER}.

To login with your username and password, replace ExecStart by /usr/bin/x0vncserver -PAMService=login -PlainUsers=${USER} -SecurityTypes=TLSPlain.

Start/enable the x0vncserver.service user unit.

Running Xvnc with XDMCP for on demand sessions

One can use systemd socket activation in combination with XDMCP to automatically spawn VNC servers for each user who attempts to login, so there is no need to set up one server/port per user. This setup uses the display manager to authenticate users and login, so there is no need for VNC passwords. The downside is that users cannot leave a session running on the server and reconnect to it later.

To get this running, first set up XDMCP and make sure the display manager is running. Then create:

/etc/systemd/system/xvnc.socket
[Unit]
Description=XVNC Server

[Socket]
ListenStream=5900
Accept=yes

[Install]
WantedBy=sockets.target
/etc/systemd/system/xvnc@.service
[Unit]
Description=XVNC Per-Connection Daemon

[Service]
ExecStart=-/usr/bin/Xvnc -inetd -query localhost -geometry 1920x1080 -once -SecurityTypes=None
User=nobody
StandardInput=socket
StandardError=syslog

Start/enable xvnc.socket. Now, any number of users can get unique desktops by connecting to port 5900.

If the VNC server is exposed to the internet, add the -localhost option to Xvnc in xvnc@.service (note that -query localhost and -localhost are different switches) and follow #Accessing vncserver via SSH tunnels. Since we only select a user after connecting, the VNC server runs as user nobody and uses Xvnc directly instead of the vncserver script, so any options in ~/.vnc are ignored. Optionally, autostart vncconfig so that the clipboard works (vncconfig exits immediately in non-VNC sessions). One way is to create:

/etc/X11/xinit/xinitrc.d/99-vncconfig.sh
#!/bin/sh
vncconfig -nowin &

vncserver に接続する

警告: LAN の外でセキュアな対策を取ることなく vncserver に接続することは推奨されません。LAN の外側で接続する必要がある場合は下の説明を読むことを推奨します。SecurityTypes をセキュアでないオプションに設定しないかぎり TigerVNC はデフォルトで暗号化されますが、通信相手の確認はしないため接続時の MITM 攻撃を防ぐことはできません。セキュアな接続をするときは X509Vnc を使うことを推奨します。

vncserver に接続できるクライアントは多数存在します。下は 10.1.10.2 のポート 5901 (:1) で動作している vncserver に接続するコマンド例です:

$ vncviewer 10.1.10.2:1

パスワードなしで認証

-passwd スイッチを使うことでサーバーの ~/.vnc/passwd ファイルの場所を定義することができます。サーバー上のこのファイルには SSH か物理的なアクセスによってユーザーがアクセスすることが期待されています。どちらの場合でも、クライアントのファイルシステム上の安全な場所、つまり特定のユーザーだけが読み込みアクセスできる場所にファイルを置いて下さい。

$ vncviewer -passwd /path/to/server-passwd-file

GUI ベースのクライアント

TigerVNC の vncviewer にはシンプルが GUI が存在し、何もパラメータを付けずに実行します:

$ vncviewer

SSH トンネル経由で vncserver にアクセス

SSH 接続を提供するサーバの利点は、VNC トラフィックが SSH ポートを介してトンネリングされるため、既に開いている SSH ポート以外のポートを外部に開く必要がないことです。

サーバー側

サーバ側では、vncserver または、 x0vncserver を実行する必要があります。

When running either one of these, it is recommended to use the localhost option in ~/.vnc/config or the -localhost switch (for x0vncserver) since it allows connections from the localhost only and by analogy, only from users ssh'ed and authenticated on the box. For example:

~/.vnc/config
session=lxqt
geometry=1920x1080
localhost
alwaysshared

Make sure to Start or Restart the vncserver@.service, for example (see also #Initial setup):

クライアント側

サーバーはローカルホストからの接続だけを許可しているので、-L スイッチを使ってトンネルを有効にして ssh で接続します。例えば:

$ ssh 10.1.10.2 -L 5901:localhost:5901

上記はサーバーのポート 5901 をクライアントのポート 5901 に転送します。サーバーとクライアントでポート番号を必ずしも一致させる必要はありません。例:

$ ssh 10.1.10.2 -L 8900:localhost:5901

上記のコマンドはサーバーのポート 5901 をクライアントマシンのポート 8900 に転送します。

SSH で接続したら、サーバーと接続するための暗号化トンネルとして xterm やシェルのウィンドウは開いたままにしてください。暗号化トンネルを使って接続するには、vncviewer でローカルホストのクライアントポートを指定します。

サーバーとクライアントで同じポートを使用する場合:

$ vncviewer localhost:1

サーバーとクライアントで違うポートを使用する場合:

$ vncviewer localhost:8900

SSH で Android デバイスから VNC サーバーに接続する

Android デバイスを使って SSH で VNC サーバーに接続するには:

1. 接続するマシン上で SSH サーバーを動作させる。
2. 接続するマシン上で VNC サーバーを動作させる (上述のように -localhost フラグでサーバーを起動する)。
3. Android デバイスで SSH クライアントを使う (ConnectBot が人気があります、この外では例としてこれを使います)。
4. Android デバイスで VNC クライアントを使う (androidVNC)。

固定 IP アドレスを持たないマシンではダイナミック DNS サービスを使うことを考慮してください。

ConnectBot で、IP を入力してマシンに接続してください。オプションキーをタップして、Port Forwards を選択し新しいポートを追加してください:

Nickname: vnc
Type: Local
Source port: 5901
Destination: 127.0.0.1:5901 (it did not work for me when I typed in 192.168.x.xxx here, I had to use 127.0.0.1)

保存してください。

androidVNC で:

Nickname: nickname
Password: the password used to set up the VNC server
Address: 127.0.0.1 (we are in local after connecting through SSH)
Port: 5901

接続してください。

ヒントとテクニック

macOS に接続する

https://help.ubuntu.com/community/AppleRemoteDesktop を見てください。Remmina でテスト済みです。

リモートマシンからローカルにクリップボードの内容をコピーする

リモートマシンからローカルマシンへのコピーが動作しない場合、サーバー上で以下のように autocutsel を起動して下さい [1]:

$ autocutsel -fork

F8 を押して VNC メニューのポップアップを表示し、Clipboard: local -> remote オプションを選択してください。

上記のコマンドを ~/.vnc/xstartup に記述することで vncserver の起動時に自動で動作するようにすることができます。

マウスカーソルが表示されない問題

x0vncserver の使用時にマウスカーソルが表示されない場合、以下のように vncviewer を起動して下さい:

$ vncviewer DotWhenNoCursor=1 <server>

もしくは tigervnc の設定ファイルに DotWhenNoCursor=1 を記述してください。デフォルトでは ~/.vnc/default.tigervnc に存在します。

推奨セキュリティ設定

ノート: SSH トンネルを使用する場合、sshd によって暗号化が行われるため X509Vnc は不要です。

SecurityTypes は使用するセキュリティアルゴリズムを制御します。現在のバージョン 1.5.0 でのデフォルトは "X509Plain,TLSPlain,X509Vnc,TLSVnc,X509None,TLSNone,VncAuth,None" です。"X509Vnc,TLSVnc" とすることで暗号化しないデータのやりとりを無効化できます。

TLSVnc では同一性確認がないため、X509Vnc を使うことを推奨します:

$ vncserver -x509key /path/to/key.pem -x509cert /path/to/cerm.pem -SecurityTypes X509Vnc :1

x509 証明書の作成方法はこの記事では説明しません。Let’s Encrypt を使用したり、OpenSSL を使って手動で証明書を発行してサーバーとクライアントで鍵を共有できます。