はじめてAWSのEC2でUbuntuを作ってSSHとリモートデスクトップで接続した時の記録
ドキュメントどおりにやっていれば超簡単にできます。
が、無駄につまずくので記録します。
EC2インスタンスを作成してSSH接続します。
EC2インスタンスを作成します。
参考 : 仮想マシンの起動
- AWSマネジメントコンソールの[EC2]からEC2 コンソールを表示します。
- [インスタンス]でインスタンスの一覧を表示します。
- [インスタンスの作成]ボタンで作成画面を表示します。
- [次のステップ〜]ボタンで進めながら内容を入力していきます。以下以外は規定値のままとしました。
- [確認と作成]ボタンで確認画面を表示して内容を確認します。
- [起動]ボタンで鍵の作成について選択して[インスタンスの作成]ボタンでインスタンスを作成します。
- 作成にはちょっと時間がかかります。
SSH接続します。
- インスタンス作成時に取得した鍵の権限を設定します。
- インスタンスの一覧から作成したインスタンスを選択して詳細画面を表示し、インスタンスのIPv4パブリックIPを確認します。
- MacのターミナルなどでSSH接続します。
# ssh接続する方法 : ssh -i {作成した秘密鍵のファイルパス} {ユーザ名:AMIの種類で変わる}@{インスタンスのIPv4パブリックIP} $ ssh -i .ssh/key.pem ubuntu@{インスタンスのIPv4パブリックIP} The authenticity of host '{インスタンスのIPv4パブリックIP} ({インスタンスのIPv4パブリックIP})' can't be established. ECDSA key fingerprint is SHA256:Zxxx Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '{インスタンスのIPv4パブリックIP}' (ECDSA) to the list of known hosts. Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-1057-aws x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Tue Feb 25 11:15:51 UTC 2020 System load: 0.0 Processes: 87 Usage of /: 13.6% of 7.69GB Users logged in: 0 Memory usage: 14% IP address for eth0: 172.31.17.109 Swap usage: 0% 0 packages can be updated. 0 updates are security updates. The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. ubuntu@ip-1xx-xx-xx-1xx:~$
aptコマンドでmysqlコマンドをインストールしてみました。
作ったUbuntu Serverにリモートデスクトップ接続できるようにします。
Ubuntu Server側を設定します。
aptの更新とユーザー作成をします。
# 1. SSHでログインします。 # 2. aptのリポジトリ一覧を更新します(-y : 問い合わせがあった場合はすべて「y」と答える) $ sudo apt update -y Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic InRelease Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease Hit:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] #...省略... 32 packages can be upgraded. Run 'apt list --upgradable' to see them. # 3. インストール済みのパッケージを更新します。 $ sudo apt upgrade -y Reading package lists... Done Building dependency tree #...省略... Processing triggers for initramfs-tools (0.130ubuntu3.9) ... update-initramfs: Generating /boot/initrd.img-4.15.0-1060-aws # 4. リモートデスクトップ用にユーザーを作成します。 $ sudo adduser ponsuke Adding user `ponsuke' ... Adding new group `ponsuke' (1001) ... Adding new user `ponsuke' (1001) with group `ponsuke' ... Creating home directory `/home/ponsuke' ... Copying files from `/etc/skel' ... Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Changing the user information for ponsuke Enter the new value, or press ENTER for the default Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] y # 5. 作ったユーザーをsudoグループに参加させます。 $ sudo gpasswd -a ponsuke sudo Adding user ponsuke to group sudo
デスクトップ環境をインストールします。
# 1. パッケージを検索します。 $ apt search ubuntu-desktop Sorting... Done Full Text Search... Done edubuntu-desktop/bionic 15.12.9 amd64 educational desktop for Ubuntu kubuntu-desktop/bionic 1.370 amd64 Kubuntu Plasma Desktop/Netbook system lubuntu-desktop/bionic-updates 0.94.1 amd64 Lubuntu Desktop environment qtubuntu-desktop/bionic 0.64+17.10.20170707-0ubuntu7 amd64 Qt plugins for Mir support on Ubuntu (desktop) ubuntu-desktop/bionic-updates 1.417.4 amd64 The Ubuntu desktop system xubuntu-desktop/bionic 2.225 amd64 Xubuntu desktop system # 2. ubuntu-desktopをインストールします。 $ sudo apt -y install ubuntu-desktop Reading package lists... Done Building dependency tree #...省略... Processing triggers for systemd (237-3ubuntu10.39) ... Processing triggers for ureadahead (0.100.0-21) ...
xrdpをインストールします。
xrdp とは
xrdp はオープンソースで開発されている Remote Desktop Protocol のサーバソフトウェアです。
フロントページ - 日本 xrdp ユーザ会
# 1. パッケージを検索します。 $ apt search xrdp Sorting... Done Full Text Search... Done xorgxrdp/bionic 0.9.5-2 amd64 Remote Desktop Protocol (RDP) modules for X.org xorgxrdp-hwe-18.04/bionic-updates 0.9.5-2~18.04.1 amd64 Remote Desktop Protocol (RDP) modules for X.org xrdp/bionic 0.9.5-2 amd64 Remote Desktop Protocol (RDP) server xrdp-pulseaudio-installer/bionic 0.9.5-2 amd64 Remote Desktop Protocol (RDP) server - PulseAudio module installer # 2. xrdpをインストールします。 $ sudo apt install -y xrdp Reading package lists... Done Building dependency tree #...省略... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Processing triggers for ureadahead (0.100.0-21) ... # 3. バージョンを確認します。 $ xrdp -v xrdp: A Remote Desktop Protocol server. Copyright (C) Jay Sorg 2004-2014 See http://www.xrdp.org for more information. Version 0.9.5 # 4. new_cursorsを無効化します。 $ ls -la /etc/xrdp/ | grep xrdp -rw------- 1 xrdp root 2939 Mar 4 11:41 rsakeys.ini -rw-r--r-- 1 root root 4916 Feb 16 2018 xrdp.ini -rw-r--r-- 1 root root 3361 Feb 16 2018 xrdp_keyboard.ini $ sudo sed -e 's/^new_cursors=true/new_cursors=false/g' -i /etc/xrdp/xrdp.ini # 5. xrdpサービスを再起動します。 $ sudo systemctl restart xrdp # 6. xrdpサービスを有効化します。 $ sudo systemctl enable xrdp.service Synchronizing state of xrdp.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable xrdp $ sudo systemctl enable xrdp-sesman.service $ systemctl list-unit-files -t service | grep xrdp xrdp-sesman.service enabled xrdp.service enabled # 7. 作成したユーザーに切り替えます。 $ su ponsuke Password: To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. $ cd ~ $ pwd /home/ponsuke # 8. 環境変数を設定します。 $ DESKTOP=/usr/share/ubuntu:/usr/local/share:/usr/share:/var/lib/snapd/desktop # 9. .xsessionファイルを作成します。 $ cat <<EOF > ~/.xsessionrc > export GNOME_SHELL_SESSION_MODE=ubuntu > export XDG_CURRENT_DESKTOP=ubuntu:GNOME > export XDG_DATA_DIRS=${DESKTOP} > export XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg > EOF
Authentication Requiredダイアログを回避できるようにします。
Polkit(旧名PolicyKit)とは、Unix系オペレーティングシステムで、システム全体の権限を制御するためのアプリケーション開発ツールキットである。このライブラリによって、特権を持たないプロセスが、特権を持つプロセスと通信することができるようになる。
Polkit - Wikipedia
# 1. 設定します。 $ cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/xrdp-color-manager.pkla > [Netowrkmanager] > Identity=unix-user:* > Action=org.freedesktop.color-manager.create-device > ResultAny=no > ResultInactive=no > ResultActive=yes > EOF [sudo] password for ponsuke: [Netowrkmanager] Identity=unix-user:* Action=org.freedesktop.color-manager.create-device ResultAny=no ResultInactive=no ResultActive=yes # 2. サービスを再起動します。 $ sudo systemctl restart polkit
セキュリティグループに設定を追加します。
Mac側を設定します。
リモートデスクトップアプリをインストールします。
- Apple StoreからMicrosoft Remote Desktop 10をインストールします。
Ubuntuへリモートデスクトップ接続します。
- Microsoft Remote Desktop 10を起動して[Add PC]ボタンで設定画面を開きます。
- 「インスタンスのIPv4パブリックIP」と「Ubuntuで作成したユーザー」を設定して[Add]ボタンで設定を追加します。
- 注意:今回はElasticIPアドレスを使用していないため、インスタンスを起動するたびにIPアドレスが変わります。なので、起動するたびにリモートデスクトップに設定するIPアドレスを変更する必要があります。
- 追加された設定をダブルクリックします。
- 初めての場合はダイアログが表示されるので[Connect]ボタンで進めます。
- Ubuntuに接続できました。
失敗したこと
Amazon Machine Image (AMI) で何を選択したかによってSSH接続する時のユーザ名が異なります。
ドキュメントの例では、
ですが初めて作ったインスタンスでは
でした。
これに気がつくのにかなり時間がかかりました。
xn--o9j8h1c9hb5756dt0ua226amc1a.com
インスタンスタイプが「t2.micro」では小さすぎた。
インスタンスタイプを「t2.micro」で作成しました。
SSH接続して作業するには問題なかったのですが、
デスクトップ環境をインストールしてリモートデスクトップ接続したところ
画面は表示されるものの全然動かない!
なので「t2.medium」に変更しました。
無料枠から外れてしまうのですが動かないよりはいいかと・・・。