以前、 Ubuntu 仮想マシンへの リモートアクセス に VNC を使いましたが、それはローカルとは異なるセッションを利用するものでした。対して、TeamViewerやAnyDeskのように現セッションへ リモートアクセス する仕組みを、今回は x11vnc を使って構築してみます。
現在の構成を確認
今回リモートアクセスしたいと思っているのが、こちらのUbuntu 18.04デスクトップ機。
1 2 3 4 5 6 7 8 9 |
$ uname -a Linux WS-0300u 4.15.0-52-generic #56-Ubuntu SMP Tue Jun 4 22:49:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.6 LTS Release: 18.04 Codename: bionic |
現在使用中のディスプレイマネージャは、gdm3です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$ cat /etc/X11/default-display-manager /usr/sbin/gdm3 $ systemctl status display-manager ● gdm.service - GNOME Display Manager Loaded: loaded (/lib/systemd/system/gdm.service; static; vendor preset: enabled) Drop-In: /lib/systemd/system/display-manager.service.d └─xdiagnose.conf Active: active (running) since Wed 2023-05-17 08:04:13 HKT; 2h 1min ago Process: 1586 ExecStartPre=/usr/share/gdm/generate-config (code=exited, status=0/SUCCESS) Main PID: 1617 (gdm3) Tasks: 3 (limit: 4915) CGroup: /system.slice/gdm.service └─1617 /usr/sbin/gdm3 $ systemctl status gdm3 ● gdm.service - GNOME Display Manager Loaded: loaded (/lib/systemd/system/gdm.service; static; vendor preset: enabled) Drop-In: /lib/systemd/system/display-manager.service.d └─xdiagnose.conf Active: active (running) since Wed 2023-05-17 08:04:13 HKT; 2h 1min ago Process: 1586 ExecStartPre=/usr/share/gdm/generate-config (code=exited, status=0/SUCCESS) Main PID: 1617 (gdm3) Tasks: 3 (limit: 4915) CGroup: /system.slice/gdm.service └─1617 /usr/sbin/gdm3 |
x11vncのインストール
現在、ローカルディスプレイへ表示しているセッションをリモートアクセスするために、x11vncをインストールします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ sudo apt install x11vnc パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下の追加パッケージがインストールされます: libvncserver1 x11vnc-data 以下のパッケージが新たにインストールされます: libvncserver1 x11vnc x11vnc-data アップグレード: 0 個、新規インストール: 3 個、削除: 0 個、保留: 2 個。 1,306 kB のアーカイブを取得する必要があります。 この操作後に追加で 3,118 kB のディスク容量が消費されます。 続行しますか? [Y/n] y $ whereis x11vnc x11vnc: /usr/bin/x11vnc /usr/share/x11vnc /usr/share/man/man1/x11vnc.1.gz $ ls -l /usr/bin/x11* -rwxr-xr-x 1 root root 193696 May 31 2016 /usr/bin/x11perf -rwxr-xr-x 1 root root 2807 May 31 2016 /usr/bin/x11perfcomp -rwxr-xr-x 1 root root 1690640 Mar 29 2018 /usr/bin/x11vnc |
VNCパスワードの生成
x11vncのインストールを終えたら、リモートアクセス許可に必要なパスワードを生成します。
1 2 3 4 5 6 7 8 9 10 11 |
$ x11vnc -storepasswd Enter VNC password: ********** Verify password: ********** Write password to /home/user/.vnc/passwd? [y]/n y Password written to: /home/user/.vnc/passwd $ ls -l ~/.vnc/ 合計 4 -rw------- 1 user user 8 May 15 11:10 passwd |
デフォルトでは、ホームディレクトリ下の隠しフォルダ内にパスワードファイルが生成されますが、複数ユーザ時など普遍的な場所へ配置したい場合は、 /etc/ 下に配している例もネットで見ることができました。
ディスプレイ番号を確認してx11vncを試行
x11vncを早速試す前に、現在の画面のディスプレイ番号を収めた環境変数 DISPLAY をチェック。
1 2 |
$ echo $DISPLAY :1 |
確認のため、簡単なオプションのみで動かしてみます。
1 2 3 4 5 6 7 8 |
$ x11vnc -usepw -display :1 15/05/2023 11:36:05 -usepw: found /home/user/.vnc/passwd 15/05/2023 11:36:05 x11vnc version: 0.9.13 lastmod: 2011-08-10 pid: 5653 15/05/2023 11:36:05 Using X display :1 15/05/2023 11:36:05 rootwin: 0x4e3 reswin: 0x4400001 dpy: 0x62960e70 -略- The VNC desktop is: WS-0300u:0 PORT=5900 |
ネットワーク上の別PCから、5900番へVNC接続できました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
15/05/2023 11:36:18 Got connection from client 10.96.28.68 15/05/2023 11:36:18 other clients: 15/05/2023 11:36:18 Normal socket connection 15/05/2023 11:36:18 Disabled X server key autorepeat. 15/05/2023 11:36:18 to force back on run: 'xset r on' (3 times) 15/05/2023 11:36:18 incr accepted_client=1 for 10.96.28.68:33456 sock=10 15/05/2023 11:36:18 Client Protocol Version 3.8 15/05/2023 11:36:18 Protocol version sent 3.8, using 3.8 15/05/2023 11:36:18 rfbProcessClientSecurityType: executing handler for type 2 15/05/2023 11:36:18 Pixel format for client 10.96.28.68: 15/05/2023 11:36:18 32 bpp, depth 24, little endian 15/05/2023 11:36:18 true colour: max r 255 g 255 b 255, shift r 16 g 8 b 0 15/05/2023 11:36:18 no translation needed 15/05/2023 11:36:18 rfbProcessClientNormalMessage: ignoring unsupported encoding type ultraZip 15/05/2023 11:36:18 Using compression level 2 for client 10.96.28.68 15/05/2023 11:36:18 Using image quality level 7 for client 10.96.28.68 15/05/2023 11:36:18 Using JPEG subsampling 0, Q86 for client 10.96.28.68 15/05/2023 11:36:18 Enabling X-style cursor updates for client 10.96.28.68 15/05/2023 11:36:18 Enabling full-color cursor updates for client 10.96.28.68 15/05/2023 11:36:18 Enabling cursor position updates for client 10.96.28.68 15/05/2023 11:36:18 Enabling KeyboardLedState protocol extension for client 10.96.28.68 15/05/2023 11:36:18 Enabling NewFBSize protocol extension for client 10.96.28.68 15/05/2023 11:36:18 Enabling LastRect protocol extension for client 10.96.28.68 15/05/2023 11:36:18 Enabling SupportedMessages protocol extension for client 10.96.28.68 15/05/2023 11:36:18 Enabling SupportedEncodings protocol extension for client 10.96.28.68 15/05/2023 11:36:18 Enabling ServerIdentity protocol extension for client 10.96.28.68 15/05/2023 11:36:18 Using tight encoding for client 10.96.28.6815/05/2023 11:36:18 copy_tiles: allocating first_line at size 121 15/05/2023 11:36:18 client 1 network rate 9469.6 KB/sec (74215.5 eff KB/sec) 15/05/2023 11:36:18 client 1 latency: 1.3 ms 15/05/2023 11:36:18 dt1: 0.0147, dt2: 0.0146 dt3: 0.0013 bytes: 272104 15/05/2023 11:36:18 link_rate: LR_LAN - 1 ms, 9469 KB/s 15/05/2023 11:36:18 client useCopyRect: 10.96.28.68 -1 15/05/2023 11:36:19 client_set_net: 10.96.28.68 0.0766 15/05/2023 11:36:19 created xdamage object: 0x440007c 15/05/2023 11:36:27 created selwin: 0x440007d 15/05/2023 11:36:27 called initialize_xfixes() 15/05/2023 11:36:27 client_count: 0 15/05/2023 11:36:27 Restored X server key autorepeat to: 1 15/05/2023 11:36:27 viewer exited. 15/05/2023 11:36:27 deleted 120 tile_row polling images. $ |
クライアント側で切断すると、x11vncプロセスも終了します。引き続き待ち受けたいのであれば、 -forever オプションを使います。
サービスへ登録
常時待ち受け状態にするために、次の内容のサービスファイルを作成しました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=x11vnc Remote Local Session After=network-online.target [Service] Type=simple ExecStart=/usr/bin/x11vnc -auth guess -display :1 -rfbauth /home/user/.vnc/passwd -rfbport 5900 -forever -loop -xkb -noxdamage -repeat -shared ExecStop=/bin/kill -TERM $MAINPID ExecReload=/bin/kill -HUP $MAINPID KillMode=control-group Restart=on-failure [Install] WantedBy=graphical.target |
サービスを登録、開始すると…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$ sudo systemctl daemon-reload $ sudo systemctl enable x11vnc.service Created symlink /etc/systemd/system/graphical.target.wants/x11vnc.service → /etc/systemd/system/x11vnc.service. $ sudo systemctl start x11vnc.service $ sudo systemctl status x11vnc ● x11vnc.service - x11vnc Remote Local Session Loaded: loaded (/etc/systemd/system/x11vnc.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2023-05-15 11:49:06 HKT; 8s ago Main PID: 18980 (x11vnc) Tasks: 12 (limit: 4915) CGroup: /system.slice/x11vnc.service └─18980 /usr/bin/x11vnc -auth guess -display :1 -rfbauth /home/user/.vnc/passwd -rfbport 5900 -forever -loop -xkb -noxdamage May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 passing arg to libvncserver: -rfbauth May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 passing arg to libvncserver: /home/user/.vnc/passwd May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 passing arg to libvncserver: -rfbport May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 passing arg to libvncserver: 5900 May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 x11vnc version: 0.9.13 lastmod: 2011-08-10 pid: 19436 May 15 11:49:15 WS-0300u x11vnc[18980]: xauth: unable to generate an authority file name May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 -auth guess: failed for display=':1' May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 -auth guess: since we are root, retrying with FD_XDM=1 May 15 11:49:15 WS-0300u x11vnc[18980]: 15/05/2023 11:49:15 -auth guess: failed for display=':1' May 15 11:49:15 WS-0300u x11vnc[18980]: --- x11vnc loop: sleeping 2000 ms --- |
以下、起動にコケるループが延々と続き、その原因は xauth: unable to generate an authority file name とある通り、 -auth guess がguessできていないことにあるのだそう。
Xauthorityファイルの場所を明示的に付与する必要があるので、その場所を確認します。
1 |
/run/user/1000/gdm/Xauthority |
1 2 3 4 5 6 7 8 |
$ sudo tree /run/user/ /run/user/ ├── 1000 │ └── gdm │ └── Xauthority └── 124 └── gdm └── Xauthority |
サービスファイルを修正します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=x11vnc Remote Local Session After=network-online.target [Service] Type=simple ExecStart=/usr/bin/x11vnc -auth /run/user/1000/gdm/Xauthority -display :1 -rfbauth /home/user/.vnc/passwd -rfbport 5900 -forever -loop -xkb -noxdamage -repeat -shared -ncache 10 -speeds modem ExecStop=/bin/kill -TERM $MAINPID ExecReload=/bin/kill -HUP $MAINPID KillMode=control-group Restart=on-failure [Install] WantedBy=graphical.target |
今度はうまく立ち上がりました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ sudo systemctl start x11vnc $ sudo systemctl status x11vnc ● x11vnc.service - x11vnc Remote Local Session Loaded: loaded (/etc/systemd/system/x11vnc.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2023-05-15 12:30:49 HKT; 4s ago Main PID: 28887 (x11vnc) Tasks: 2 (limit: 4915) CGroup: /system.slice/x11vnc.service ├─28887 /usr/bin/x11vnc -auth /run/user/1000/gdm/Xauthority -display :1 -rfbauth /home/user/.vnc/passwd -rfbport 5900 -foreve └─28896 /usr/bin/x11vnc -auth /run/user/1000/gdm/Xauthority -display :1 -rfbauth /home/user/.vnc/passwd -rfbport 5900 -foreve May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 Xinerama: Use -noxwarppointer to force XTEST. May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 Xinerama: sub-screen[0] 1920x1200+0+0 May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 Xinerama: sub-screen[1] 1920x1200+1920+0 May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 Xinerama: no blackouts needed (screen fills rectangle) May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 The X server says there are 10 mouse buttons. May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 screen setup finished. May 15 12:30:50 WS-0300u x11vnc[28887]: 15/05/2023 12:30:50 May 15 12:30:50 WS-0300u x11vnc[28887]: The VNC desktop is: WS-0300u:0 May 15 12:30:50 WS-0300u x11vnc[28887]: PORT=5900 lines 1-19/19 (END) |
ログイン画面へのリモートアクセスは
gdmの場合、このサービス設定でリモートアクセス可能なのは、ログイン後のデスクトップ画面に限られます。
つまりPCの再起動後は、ローカルでユーザがログインしない限り、リモートアクセスできないことに。
これはログイン前後でディスプレイ番号やXauthorityファイルの場所が異なるためで、いくつかの対処法のうち今回は少し泥臭いものの比較的理解しやすい、
ログイン画面へリモートアクセスするためだけのx11vncサービスをもう1つ立ち上げる
を試してみます。
先ほどXauthorityファイルを探していたときに、ログイン画面用のファイルは見つけていました。
1 |
/run/user/124/gdm/Xauthority |
ログイン画面用に作成したサービスファイルは、次の通りです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=x11vnc Remote Local LOGIN Session After=network-online.target [Service] Type=simple ExecStart=/usr/bin/x11vnc -auth /run/user/124/gdm/Xauthority -display :0 -rfbauth /home/user/.vnc/passwd -rfbport 5901 -forever -loop -xkb -noxdamage -repeat -shared -ncache 10 -speeds modem ExecStop=/bin/kill -TERM $MAINPID ExecReload=/bin/kill -HUP $MAINPID KillMode=control-group Restart=on-failure [Install] WantedBy=graphical.target |
2つのx11vncサービスの違いと用途をまとめてみました。
1 2 3 |
-display -auth -rfbport :0 /run/user/124/gdm/Xauthority 5901 ログイン :1 /run/user/1000/gdm/Xauthority 5900 デスクトップ |
実際に再起動して、ログイン前の状態にあるPCの5901番ポートへアクセスしてみます。
ログインパスワードを入力後、ログイン画面は真っ黒に。
慌てず5900番ポートへアクセスすると、デスクトップ画面が開いていました。
この仕様を嫌ってか、ディスプレイマネージャを軽量なlightdmに乗り換えてしまう人も多いのですが、あまり現行のシステムを大きく変えることなく、リモートアクセスできるようにしたかったので、一段落です。