LoginSignup
3
4

More than 1 year has passed since last update.

[Ubuntu Server 22.04]  Ubuntu Desktop RDP セットアップ

Last updated at Posted at 2023-01-21

注意事項

リンク先に記載

[Ubuntu Server 22.04] 個人的初期設定 覚書

概要

「目次」

1.Tailscale インストール
2.WeTTY インストール
3.Ubuntu Desktop(GDM3 ubuntu カスタム) インストール、自動ログイン設定
4.Ubuntu Desktop RDP セットアップ
  概要
  コンストラクション
   RDP セットアップ
     0.GNOME の設定から RDP を有効にする
     1.openSSL 証明書と鍵ファイルの作成
     2.RDP を有効にし、View-only を解除する
     3.GNOMEキーリング を解錠する
       Unlock Keyring (CLI)
     4.RDP 用ユーザ認証情報を設定する
   チェック
     1.諸々の status 確認
     2.設定を修正する
     3.RDP service 再起動
   実際に RDP で接続する
     1.gnome-remote-desktop をサービスに登録
     2.GNOME desktop 起動、キーリング解除
     3.任意のクライアントから接続
      ※RDP クライアントの画面を拡張ディスプレイにしたいとき
      ※headless について
   トラブルシューティング
  引用元、参照元、参考先
5.PowerTOP TLP インストール
6.自動再起動設定(Crontab, rtcwake)

「説明」

GNOME RDP は、Ubuntu 22.04 から標準搭載されるようになった RDP ホストです。

「目標」

・ディスプレイを接続しないサーバー用途のPCで、デスクトップ環境を使うこと

「必要なもの」

・仮想ディスプレイアダプタ(ダミープラグ)
headless で接続する場合は必要ありません)

「確認事項」

・Ubuntu 22.04 LTS は GNOME 42 がインストールされます。
・先に「[Ubuntu Server 22.04] Ubuntu-Desktop インストール、自動ログイン設定」を行ってください。
・ubuntu 22.04 における GNOME RDP では、音声が出力されない可能性があります。

コンストラクション

RDP セットアップ

0.GNOME の設定から RDP を有効にする

gsettings set org.gnome.desktop.remote-desktop.rdp enable true

1.openSSL 証明書と鍵ファイルの作成

grdctl statusを実行し、"TLS certificate" と "TLS key" に何も設定されていなければ(以下のEx.1のとおり)、bash:1を実行してください。
※すでに設定されている場合にはこの操作は必要ありません。「2.RDP を有効にし、View-only を解除する」に進んでください。

Ex.1
USER@servername:~$ grdctl status
RDP:
        Status: disabled
        TLS certificate: 
        TLS key: 
        View-only: yes
        Username: (empty)
        Password: (empty)
bash:1
# ディレクトリの作成
mkdir -p /home/$(whoami)/.local/share/gnome-remote-desktop/
cd /home/$(whoami)/.local/share/gnome-remote-desktop/
# 証明書と鍵の作成
openssl genrsa -out tls.key 4096
openssl req -new -key tls.key -out tls.csr
openssl x509 -req -days 730 -signkey tls.key -in tls.csr -out tls.crt
# 名前を gnome-remote-desktop 標準に合わせる
mv tls.crt rdp-tls.crt
mv tls.key rdp-tls.key
# 作成した証明書と鍵をセットする
grdctl rdp set-tls-cert /home/$(whoami)/.local/share/gnome-remote-desktop/rdp-tls.crt
grdctl rdp set-tls-key  /home/$(whoami)/.local/share/gnome-remote-desktop/rdp-tls.key
# 確認
grdctl status

自己証明書"openssl req -new -key tls.key -out tls.csr"の例
・オレンジの箇所が入力内容です。
ServerName は好きに設定してください。
 (Ubuntu 標準は GNOME です)
・赤い文字のところには、何も入力しないでください。
※何も入力しなくても Enter を押せば次の項目に進めます。
USER@servername:~$ openssl req -new -key tls.key -out tls.csr

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Shinjuku-ku
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:ServerName
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request

A challenge password []:
An optional company name []:

2.RDP を有効にし、View-only を解除する
bash:2
grdctl rdp enable
grdctl rdp disable-view-only
3.GNOMEキーリング を解錠する

Ubuntu Desktop 付属の RDP は、接続に必要なユーザー名とパスワードを GNOMEキーリング に保存します。そのため RDP 接続するには次のどれかで gnome-remote-desktop が GNOMEキーリング を読み取れるようにする必要があります。

  1. 一度 GUI にコンソールサインインする(自動ログインならログアウト➝ログインする)
  2. コンソールから GNOMEキーリング を開いて、アンロック
  3. GNOMEキーリング のパスワードを空に設定する
  4. CLI から GNOMEキーリング をアンロックする

ここでは、4 の方法で行います。
※注意※
 1度も GNOME Desktop を起動したことがない場合、ここで password="" に入力した値が GNOMEキーリング のパスワードに設定されます。

bash:3
password="ここに現在ログイン中のユーザーのパスワード"
killall -q -u "$(whoami)" gnome-keyring-daemon
export $( echo -n "$password" | gnome-keyring-daemon --daemonize --login ) >/dev/null 2>&1
# 確認 1  //接続ユーザー名を "test_user", パスワードを "test_password" に初期化
grdctl rdp set-credentials "test_user" "test_password"
# 確認 2
grdctl status --show-credentials

解錠が成功したかどうかは

  1. grdctl rdp set-credentials "" ""が "Cannot create an item in a locked collection" を返さなければOK。

↓のようになっていなければOK

Ex.2
USER@servername:~$ grdctl rdp set-credentials "" ""
Cannot create an item in a locked collection

さらに

  1. grdctl status --show-credentialsの実行結果のうち、 "Username:" と "Password:" が (null) になっていなければ成功 です。

↓のようになっていなければOK

Ex.3
USER@servername:~$ grdctl status --show-credentials
RDP:
        Status: enabled
        TLS certificate: /home/$(whoami)/.local/share/gnome-remote-desktop/rdp-tls.crt
        TLS key: /home/$(whoami)/.local/share/gnome-remote-desktop/rdp-tls.key
        View-only: no
        Username: (null)
        Password: (null)

簡単に実行できるようにスクリプトを作成しておきます。

ファイル名 gdm3-key でスクリプトを作成
  1. cd $HOME
  2. touch gdm3-key
  3. vim gdm3-key
gdm3-key
#!/bin/bash
printf '[Unlock Keyring] password for '"$(whoami)"': ' && read -s password && echo --completed--
killall -q -u "$(whoami)" gnome-keyring-daemon
export $( echo -n "$password" | gnome-keyring-daemon --daemonize --login ) >/dev/null 2>&1
grdctl status --show-credentials
unset password
exit

念のために(改行コードが LF であることを担保)
4. sed -i -e 's/\r//g' gdm3-key
実行は
5. bash ~/gdm3-key

4.RDP 用ユーザ認証情報を設定する

 3.GNOMEキーリング を解錠する を先に行ってください。
 ユーザー名、パスワードは自由に決めてもらって構いません。ここではユーザー名はシェルにログイン中のユーザーで設定しています。

bash:4
# grdctl rdp set-credentials "ユーザー名" "パスワード"
grdctl rdp set-credentials "$(whoami)" "RDP_Password"
# 確認
grdctl status --show-credentials

チェック

1.諸々の status 確認
bash:5
grdctl status
systemctl --user status gnome-remote-desktop.service
# gsettings list-keys org.gnome.desktop.remote-desktop.rdp
gsettings get org.gnome.desktop.remote-desktop.rdp enable
2.設定を修正する
bash:6 (:Ex.4)
‘’ 1.次の場合
‘’ USER@servername:~$ gsettings get org.gnome.desktop.remote-desktop.rdp enable
‘’ false
gsettings set org.gnome.desktop.remote-desktop.rdp enable true

‘’ 2.次の場合
‘’ USER@servername:~$ grdctl status | head -n 5 | grep Status:
‘’         Status: disabled
grdctl rdp enable
3.RDP service 再起動

systemctl --user restart gnome-remote-desktop.service
sudo shutdown -r now

実際に RDP で接続する

1.gnome-remote-desktop をサービスに登録

 gnome-session.target 起動時に gnome-remote-desktop も起動するようにさせます。
 systemctl --user enable --now gnome-remote-desktop.service
※↓登録した中身

Ex.5
USER@servername:~$ cat /home/$(whoami)/.config/systemd/user/gnome-session.target.wants/gnome-remote-desktop.service
[Unit]
Description=GNOME Remote Desktop

[Service]
Type=dbus
BusName=org.gnome.RemoteDesktop
ExecStart=/usr/libexec/gnome-remote-desktop-daemon
Restart=on-failure

[Install]
WantedBy=gnome-session.target
2.GNOME desktop 起動、キーリング解除
bash:7
sudo systemctl start gdm3
bash ~/gdm3-key
3.任意のクライアントから接続

 ここでは Windows からの接続を説明します。

  1. [Win + R]
  2. [mstsc /v:Ubuntu Server の IP アドレス] 例)mstsc /v:192.168.xxx.xxx:3389
  3. その他➝別のアカウントを使用する
  4. "4.RDP 用ユーザ認証情報を設定する" で設定したユーザー名とパスワードで接続

例)↓の画面から Enter を押して "資格情報を入力してください" と出ればOK
スクリーンショット (369).png

 ※RDP クライアントの画面を拡張ディスプレイにしたいとき

  RDP クライアントの拡張ディスプレイ化

 ※headless について

  Ubuntu Desktop ではなくて GNOME Shell になら、
  headless に RDP で接続できます。headless

トラブルシューティング

 ユーザーとパスワードで認証してもすぐに落ちる場合には、

bash:8
gsettings set org.gnome.desktop.remote-desktop.rdp enable false
gsettings set org.gnome.desktop.remote-desktop.rdp enable true
systemctl --user restart gnome-remote-desktop.service

 それでもうまくいかない場合は、GUI にコンソールログインして、
↓画像の◯で囲まれた部分を、ON OFF する。
スクリーンショット (371)ver1.png

 接続しても少しおくと切断される場合は、
↓画像の◯で囲まれた部分を、すべて OFF にする。
スクリーンショット (370)ver1.png

 再接続できないときは、GDM3 を再起動する。

bash:9
sudo systemctl restart gdm3
bash ~/gdm3-key

 
 ホストの解像度がクライアントより高いとうまく表示できない可能性があります。↓の記事を参考にして解像度を変更してください。
CLI からデスクトップの解像度を変更する

引用元、参照元、参考先

Ubuntu 22.04 起動時のパスワードとキーリングのロック解除
 ・https://arakoki70.com/?p=7040#
Unlock Gnome Keyring Daemon from command line
 ・https://unix.stackexchange.com/questions/602313/unlock-gnome-keyring-daemon-from-command-line/602935
Ubuntu 22.04 remote desktop headless?
 ・https://askubuntu.com/questions/1407444/ubuntu-22-04-remote-desktop-headless
[未完] Ubuntu Server 22.04 on LXDにデスクトップ環境を入れ、gnome-remote-desktopを使いたい [🍊]
 ・https://qiita.com/daikichi298/items/a0157954411f6e827411
Ubuntu Manpage [gsettings - GSettings configuration tool]
 ・https://manpages.ubuntu.com/manpages/trusty/man1/gsettings.1.html
Shell Scriptを書くときのテクニック10選
 ・https://keens.github.io/blog/2015/06/17/shell_scriptwokakutokinikiwotsuketaikoto/
OpenSSL で SSL 自己証明書を発行する手順
 ・https://weblabo.oscasierra.net/openssl-gencert-1/
10.2.4.1 クライアント証明書の作成
 ・http://www2.matsue-ct.ac.jp/home/kanayama/text/nginx/node99.html

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4