「TigerVNC」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(→‎推奨セキュリティ設定: SSH トンネルを使った vncserver へのアクセスを追加)
213行目: 213行目:
   
 
x509 証明書の作成方法はこの記事では説明しません。[[Let’s Encrypt]] を使用したり、[[OpenSSL]] を使って手動で証明書を発行してサーバーとクライアントで鍵を共有できます。
 
x509 証明書の作成方法はこの記事では説明しません。[[Let’s Encrypt]] を使用したり、[[OpenSSL]] を使って手動で証明書を発行してサーバーとクライアントで鍵を共有できます。
  +
  +
== SSH トンネルを使った vncserver へのアクセス ==
  +
  +
For servers offering SSH connection, an advantage of this method is that it is not necessary to open any other port than the already opened SSH port to the outside, since the VNC traffic is tunneled through the SSH port.
  +
  +
=== On the server ===
  +
  +
On the server side, ''vncserver'' or ''x0vncserver'' must be run.
  +
  +
When running either one of these, it is recommended to use the {{ic|localhost}} option in {{ic|~/.vnc/config}} or the {{ic|-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:
  +
  +
{{hc|~/.vnc/config|2=
  +
session=lxqt
  +
geometry=1920x1080
  +
localhost
  +
alwaysshared
  +
}}
  +
  +
Make sure to [[Start]] or [[Restart]] the {{ic|vncserver@.service}}, for example (see also [[#Initial setup]]):
  +
  +
# systemctl start vncserver@:1
  +
  +
or for ''x0vncserver'':
  +
  +
$ x0vncserver '''-localhost''' -SecurityTypes none
  +
  +
=== On the client ===
  +
  +
The VNC server has been setup on the remote machine to only accept local connections.
  +
Now, the client must open a secure shell with the remote machine (10.1.10.2 in this example) and create a tunnel from the client port, for instance 9901, to the remote server 5901 port. For more details on this feature, see [[OpenSSH#Forwarding other ports]] and {{man|1|ssh}}.
  +
  +
$ ssh 10.1.10.2 -L 9901:localhost:5901
  +
  +
Once connected via SSH, leave this shell window open since it is acting as the secured tunnel with the server. Alternatively, directly run SSH in the background using the {{ic|-f}} option. On the client side, to connect via this encrypted tunnel, point the ''vncviewer'' to the forwarded client port on the localhost.
  +
  +
$ vncviewer localhost:9901
  +
  +
What happens in practice is that the vncviewer connects locally to port 9901 which is tunneled to the server's localhost port 5901. The connection is established to the right port within the secure shell.
  +
  +
{{Tip|It is possible, with a one-liner, to keep the port forwarding active during the connection and close it right after:
  +
{{bc|$ ssh -fL 9901:localhost:5901 10.1.10.2 sleep 10; vncviewer localhost:9901}}
  +
What it does is that the {{ic|-f}} switch will make ssh go in the background; it will still be alive executing {{ic|sleep 10}}. vncviewer is then executed and ssh remains open in the background as long as vncviewer makes use of the tunnel. ssh will close once the tunnel is dropped which is the wanted behavior.
  +
  +
Alternatively, vncviewer's {{ic|-via}} switch provides a shortcut for the above command:
  +
{{bc|$ vncviewer -via 10.1.10.2 localhost::5901}}
  +
(Notice the double colon – vncviewer's syntax is {{ic|[host]:[display#]}} or {{ic|[host]::[port]}}.)
  +
}}
  +
  +
=== Connecting to a vncserver from Android devices over SSH ===
  +
  +
To connect to a VNC server over SSH using an Android device as a client, consider having the following setup:
  +
  +
# SSH running on the server
  +
# vncserver running on server (with {{ic|-localhost}} flag for security)
  +
# SSH client on the Android device: ''ConnectBot'' is a popular choice and will be used in this guide as an example
  +
# VNC client on the Android device: ''androidVNC'' used here
  +
  +
In ''ConnectBot'', connect to the desired machine. Tap the options key, select ''Port Forwards'' and add a port:
  +
  +
Type: Local
  +
Source port: 5901
  +
Destination: 127.0.0.1:5901
  +
  +
In ''androidVNC'', connect to the VNC port; this is the local address following the SSH connection:
  +
  +
Password: the vncserver password
  +
Address: 127.0.0.1
  +
Port: 5901
   
 
=== フルスクリーンの切り替え ===
 
=== フルスクリーンの切り替え ===

2023年2月21日 (火) 18:25時点における版

関連記事

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 を参照してください。

物理ディスプレイで vncserver を実行

TigerVNC の x0vncserver を使う

tigervnc には物理的な X セッションを直接操作できる x0vncserver バイナリが入っています。以下のようにして実行してください:

$ x0vncserver -display :0 -passwordfile ~/.vnc/passwd

詳しくは x0vncserver の man ページを参照。

systemd で x0vncserver を起動・停止

警告: ローカルネットワークが信頼できない場合は以下のサービスを使ってはいけません。

VNC サーバーで x0vncserver を実行するには、systemd ユニットを作成します。現在のデスクトップにリモートからアクセスする一番簡単な方法です。

ノート: 以下のユニットはユーザーによって X セッションが実行されている場合にのみ使用することができます。

/etc/systemd/system/x0vncserver.service を作成してサーバーを実行するユーザーやオプションを指定してください:

/etc/systemd/system/x0vncserver.service

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking
User=foo
ExecStart=/usr/bin/sh -c '/usr/bin/x0vncserver -display :0 -rfbport 5900 -passwordfile /home/foo/.vnc/passwd &'

[Install]
WantedBy=multi-user.target

x11vnc を使う

x11vnc には利点と欠点が存在しアクセスするのに root を必要とします。詳しくは X11vnc を見てください。

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 トンネリングの利点は他のポートを開く必要がなく、文字通り通信がトンネル化されるということです。ユーザーは WAN に対して既に開いている SSH ポートを使用することができます。以下の方法で vncserver を実行する際は -localhost スイッチを使用してローカルホストからの接続だけを許可するようにすることを強く推奨します。物理的にマシンに ssh することができるユーザーだけが vncserver を使えるようになります。

ノート: TigerVNC は SecurityTypes パラメータを変更したいかぎりデフォルトで TLSVnc 暗号を使用します。認証や通信は暗号化されますが、通信相手の確認はしません。サーバーの同一性を確認することができる X509Vnc などの暗号化も TigerVNC によってサポートされています。サーバー側の SecurityTypes でセキュアでないオプション (None, VncAuth, Plain, TLSNone, TLSPlain, X509None, X509Plain) が優先されるように設定されている場合、暗号化を使用することはできません。その場合、SSH で VNC をトンネル化できます。vncviewer を動作させるときは、SecurityTypes を明示的に設定して暗号化していない通信を許可しないようにすると良いでしょう。

サーバー側

以下は -localhost フラグを使って vncserver を起動するコマンド例です:

$ vncserver -geometry 1440x900 -alwaysshared -dpi 96 -localhost :1

もしくは、~/.vnc/config に "localhost" オプションを追加してください。以下は上記コマンドと同じ設定例です:

~/.vnc/config

## Supported server options to pass to vncserver upon invocation can be listed
## in this file. See the following manpages for more: vncserver(1) Xvnc(1).
## Several common ones are shown below. Uncomment and modify to your liking.
##
geometry=1200x700
alwaysshared
dpi=96
localhost

クライアント側

サーバーはローカルホストからの接続だけを許可しているので、-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 を使って手動で証明書を発行してサーバーとクライアントで鍵を共有できます。

SSH トンネルを使った vncserver へのアクセス

For servers offering SSH connection, an advantage of this method is that it is not necessary to open any other port than the already opened SSH port to the outside, since the VNC traffic is tunneled through the SSH port.

On the server

On the server side, vncserver or x0vncserver must be run.

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):

# systemctl start vncserver@:1

or for x0vncserver:

$ x0vncserver -localhost -SecurityTypes none

On the client

The VNC server has been setup on the remote machine to only accept local connections. Now, the client must open a secure shell with the remote machine (10.1.10.2 in this example) and create a tunnel from the client port, for instance 9901, to the remote server 5901 port. For more details on this feature, see OpenSSH#Forwarding other ports and ssh(1).

$ ssh 10.1.10.2 -L 9901:localhost:5901

Once connected via SSH, leave this shell window open since it is acting as the secured tunnel with the server. Alternatively, directly run SSH in the background using the -f option. On the client side, to connect via this encrypted tunnel, point the vncviewer to the forwarded client port on the localhost.

$ vncviewer localhost:9901

What happens in practice is that the vncviewer connects locally to port 9901 which is tunneled to the server's localhost port 5901. The connection is established to the right port within the secure shell.

ヒント: It is possible, with a one-liner, to keep the port forwarding active during the connection and close it right after:
$ ssh -fL 9901:localhost:5901 10.1.10.2 sleep 10; vncviewer localhost:9901

What it does is that the -f switch will make ssh go in the background; it will still be alive executing sleep 10. vncviewer is then executed and ssh remains open in the background as long as vncviewer makes use of the tunnel. ssh will close once the tunnel is dropped which is the wanted behavior.

Alternatively, vncviewer's -via switch provides a shortcut for the above command:

$ vncviewer -via 10.1.10.2 localhost::5901

(Notice the double colon – vncviewer's syntax is [host]:[display#] or [host]::[port].)

Connecting to a vncserver from Android devices over SSH

To connect to a VNC server over SSH using an Android device as a client, consider having the following setup:

  1. SSH running on the server
  2. vncserver running on server (with -localhost flag for security)
  3. SSH client on the Android device: ConnectBot is a popular choice and will be used in this guide as an example
  4. VNC client on the Android device: androidVNC used here

In ConnectBot, connect to the desired machine. Tap the options key, select Port Forwards and add a port:

Type: Local
Source port: 5901
Destination: 127.0.0.1:5901

In androidVNC, connect to the VNC port; this is the local address following the SSH connection:

Password: the vncserver password
Address: 127.0.0.1
Port: 5901

フルスクリーンの切り替え

vncclient のメニューから切り替えることができます。デフォルトでは vncclient のメニューは F8 で呼び出せます。

小なり記号が入力できない (<)

リモートクライアントで < と入力すると > が出力される場合、キーのマッピングを変更してみてください [2]:

$ x0vncserver -RemapKeys="0x3c->0x2c"