はじめに
Google Compute Engineのインスタンスを作成して、SSH接続をできるようにしました。
インタネットで調べると、古い情報が多く見つかり、設定に時間がかかったため、その過程をこちらに残しておくことにしました。
なお、GCPの画面は2024/02/18時点のものです。
この記事でやっていること
- GCEのインスタンスの作成
- 公開鍵の作成・GCEへ登録
インスタンスの作成
まずはインスタンスを作成します。
ナビゲーションメニューから、「Compute Engine」-「VM インスタンス」を選択します。
「インスタンスを作成」をクリックします。
インスタンスの名前を設定します。
ここの例では、デフォルトのままにしています。
ブートディスクの「変更」をクリックして設定します。
OSとバージョンは好みで変更します。
ブートディスクの種類は、「標準 永続ディスク」を選択します。
サイズは「30」GBに設定します。
「選択」をクリックします。
Webサーバ構築予定があるなら、「HTTPSトラフィックを許可する」にチェックを入れます。
「詳細オプション」と「ネットワーキング」の隣の三角マークをクリックして展開します。
「ネットワーク インターフェース」の隣の三角マークをクリックして展開します。
外部IPv4アドレスの「静的外部IPアドレスを予約」をクリックします。
ページの一番下にある「作成」をクリックします。
インスタンスの作成には少し時間がかかります。
外部IPを控えておきます。
これでインスタンスができました。
SSH証明書の作成・設定
SSH証明書の作成・設定を行います。
なおクライアント側は、Windows 11にOpenSSLをインストールしています。
PowerShellで実行します。
ssh-keygenコマンドのCオプションには、インスタンスにログインするアカウントの名前を入力します。
このアカウント名で、インスタンスにアカウントが作成されます。
ここの例では、testuserというアカウントがインスタンスに作成されます。
> mkdir ~/.ssh
> cd ~/.ssh
> ssh-keygen -t ed25519 -f gce-ssh-key -C testuser
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase): <-- パスフレーズを入力します
Enter same passphrase again: <-- 確認のためのパスフレーズを入力します
Your identification has been saved in gce-ssh-key
Your public key has been saved in gce-ssh-key.pub
The key fingerprint is:
SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX testuser
The key's randomart image is:
+--[ED25519 256]--+
| =|
| . . =+|
| E =.=|
| .+ ++|
| S. ...oo*|
| oo. =oB+|
| .= +.X.=|
| ..ooB.Bo|
| .ooo=o=|
+----[SHA256]-----+
❯ cat gce-ssh-key.pub
ssh-ed25519 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX testuser
最後に表示した公開鍵は、後で使用するので控えておきます。
configファイルに追記します。
HostNameには、控えておいた外部IPを入力します。
Host instance-1
HostName XXX.XXX.XXX.XXX
User testuser
IdentityFile "C:\Users\ユーザ名\.ssh\gce-ssh-key"
PasswordAuthentication no
IdentitiesOnly yes
ブラウザのVMインスタンス画面から操作します。
インスタンスの名前をクリックします。
SSH認証鍵の「項目を追加」をクリックします。
控えておいた公開鍵をコピーします。
ページの一番下にある「保存」をクリックします。
このとき、インスタンスではtestuserアカウントが作成されています。
SSH接続します。
❯ ssh testuser@instance-1
The authenticity of host 'XXX.XXX.XXX.XXX (XXX.XXX.XXX.XXX)' can't be established.
ED25519 key fingerprint is SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes <--「yes」を入力します
Warning: Permanently added 'XXX.XXX.XXX.XXX' (ED25519) to the list of known hosts.
Enter passphrase for key 'C:\Users\XXXXXXX\.ssh\gce-ssh-key': <-- パスフレーズを入力します
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 6.5.0-1013-gcp x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
Expanded Security Maintenance for Applications is not enabled.
0 updates can be applied immediately.
Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status
The list of available updates is more than a week old.
To check for new updates run: sudo apt update
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.
testuser@instance-1:~$
SSHでログインできました。
安全のために、SSHのポート番号を変更します。
ここの例では、ポート番号 9999に変更します。
SSH接続したPowerShellをそのまま使います。
testuser@instance-1:~$ echo "port 9999" | sudo tee /etc/ssh/sshd_config.d/10-port.conf
port 9999
testuser@instance-1:~$ cat /etc/ssh/sshd_config.d/10-port.conf
port 9999
testuser@instance-1:~$ sudo systemctl restart sshd
ナビゲーションメニューから、「VPCネットワーク」-「VPCネットワーク」を選択します。
「default」をクリックします。
vpc-firewall-rulesの隣の三角をクリックして、展開します。
プロトコルとポートの「ポート」を変更します。
ここの例では、ポートを「9999」にしています。
ページの一番下にある「保存」をクリックします。
変更したポート番号でSSH接続します。
❯ ssh testuser@instance-1 -p 9999
Enter passphrase for key 'C:\Users\XXXXXX\.ssh\gce-ssh-key':
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 6.5.0-1013-gcp x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
To restore this content, you can run the 'unminimize' command.
Expanded Security Maintenance for Applications is not enabled.
0 updates can be applied immediately.
Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status
The list of available updates is more than a week old.
To check for new updates run: sudo apt update
Last login: Sun Feb 18 06:11:47 2024 from XXX.XXX.XXX.XXX
testuser@instance-1:~$
おまけ
この後、SSHの設定に下記の内容を追加しました。
- 待ち受けをIPv4に限定
- rootのログインの禁止
- 公開鍵認証の許可
- パスワードログインの禁止
Port 9999
AddressFamily inet
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
最後に
この記事が皆さんにとって有益であれば嬉しいです。
私の経験が、皆さんの時間節約に役立つことを願っています。