背景・目的
以前、下記の記事で踏み台サーバへの多段接続、リモート接続を個別に試してみました。
今回は、踏み台サーバ+リモートデスクトップを試してみたいと思います。
実践
下記の構成を構築します。
前提
VPC
以前、下記で作成したVPCを再利用します。下記が用意されています。
- Public Subnet * 1
- Private Subnet * 1
- NAT
- IGW
EC2インスタンスの起動
- 下記の要領で作成します
項目 | 踏み台サーバ | プライベートマシン |
---|---|---|
用途 | 踏み台 | リモートデスクトップ |
サブネットワーク | パブリック | プライベート |
セキュリティグループ | ・ポート:22 ・ソース:自分のPC |
・ポート:3389、22 ・ソース:踏み台サーバのSG |
OS | Amazon Linux | Ubuntu |
IPアドレス | Public Private |
Privateのみ |
SSH Configの設定とProxy Jump
-
.ssh/config
を下記の修正しますHost bastion # 踏み台のEC2 HostName <Public IP> User ubuntu IdentityFile ~/.ssh/bastion-key.pem Host rdp # プライベートサブネットのEC2 HostName <Private IP> User ubuntu IdentityFile ~/.ssh/private-key.pem ProxyJump bastion
-
ssh rdp
でProxy Jumpを使用してログインします。できました~ % ssh rdp Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.8.0-1024-aws x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro System information as of Sun Apr 20 05:16:25 UTC 2025 System load: 0.01 Processes: 106 Usage of /: 34.0% of 6.71GB Users logged in: 0 Memory usage: 27% IPv4 address for enX0: XX.XX.XX.XX Swap usage: 0% * Ubuntu Pro delivers the most comprehensive open source security and compliance features. https://ubuntu.com/aws/pro Expanded Security Maintenance for Applications is not enabled. 40 updates can be applied immediately. To see these additional updates run: apt list --upgradable Enable ESM Apps to receive additional future security updates. See https://ubuntu.com/esm or run: sudo pro status *** System restart required *** Last login: Sun Apr 20 05:14:00 2025 from XX.XX.XX.XX To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. ubuntu@ip-XX-XX-XXX-XXX:~$
EC2のセットアップ
UbuntuにGUI(xfce4)とxrdpをインストール
下記と同様にインストールします。
-
apt update & upgrade
しますsudo apt update && sudo apt upgrade -y
-
xfce4 xfce4-goodies xrdp
をインストールしますsudo apt install xfce4 xfce4-goodies xrdp -y
-
セッションをxfce4に設定します
~$ echo xfce4-session > ~/.xsession ~$ cat ~/.xsession xfce4-session ~$
-
再起動します
sudo systemctl restart xrdp
パスワードを設定(初回ログイン用)
- パスワードを入力します
~$ sudo passwd ubuntu New password: Retype new password: passwd: password updated successfully ~$
クライアントPCから接続
SSHトンネルを作成
-
~/.ssh/config
を設定しますHost bastion # 踏み台のEC2 HostName <Public IP> User ubuntu IdentityFile ~/.ssh/bastion-key.pem Host rdp-tunnel HostName <Private IP> # RDPマシンのプライベートIP User ubuntu ProxyJump bastion # bastion を経由 IdentityFile ~/.ssh/private-key.pem LocalForward 13389 127.0.0.1:3389 ExitOnForwardFailure yes ServerAliveInterval 60
- 下記で接続します
ssh rdp-tunnel
Windows Appからの接続
以前、下記でセットアップしたMicrosoft Remote Desktopを使用します。
-
Terminalを起動してみます。設定したマシンのIPアドレスとユーザが確認できました
ubuntu@ip-XXXX:~$ `id > > ^C ubuntu@ip-XXXX:~$ ubuntu@ip-XXXX:~$ id uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),105(lxd) ubuntu@ip-XXXX:~$ uname -a Linux ip-XXXX 6.8.0-1024-aws #26-Ubuntu SMP Tue Feb 18 17:22:37 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
考察
今回、踏み台(Bastion)を経由してプライベートサブネット上のUbuntuサーバーに対し、SSHトンネルを使ってRDP接続を行う構成を構築・検証しました。結果として、リモートデスクトップ接続できました。
参考