PowerShellでのSSHの使い方とは?
今回は、PowerShellでのSSHの使い方について説明します。ここでは、PowerShellターミナルからPaizaCloudで作成した無料サーバーへSSH接続する方法を紹介します。
PowerShellでのSSHの使い方に興味のある人はぜひご覧ください。
事前準備
PowerShellにSSHコマンドが使えない場合はインストールしてください。インストールされた状態では、SSHコマンドを実行すると以下のようにusageが表示されます。
1
2
3
4
5
6
7
8
9
|
PS C:\Users\(ユーザー名)> SSH
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
[-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
[-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
[-i identity_file] [-J [user@]host[:port]] [-L address]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-Q query_option] [-R address] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] destination [command]
PS C:\Users\(ユーザー名)>
|
PowerShellのSSHのバージョンは以下のように確認できます。
1
2
3
|
PS C:\Users\(ユーザー名)> SSH -V
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
PS C:\Users\(ユーザー名)>
|
公開鍵と秘密鍵の作成
SSH接続するために、ssh-keygenコマンドで公開鍵と秘密鍵を作成します。ここでは「-t rsa」オプションを付与してRSA暗号方式で作成します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
PS C:\Users\(ユーザー名)> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\(ユーザー名)/.ssh/id_rsa): # そのままEnterを押下
Created directory 'C:\Users\(ユーザー名)/.ssh'.
Enter passphrase (empty for no passphrase): # passphraseを入力
Enter same passphrase again: # 再度、passphraseを入力
Your identification has been saved in C:\Users\(ユーザー名)/.ssh/id_rsa.
Your public key has been saved in C:\Users\(ユーザー名)/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:glwgc4NFGjxtfcGyRAYenHR8BzkH9I2p4Dl1reGIGEw (ユーザー名)@(PCの機種名)
The key's randomart image is:
+---[RSA 2048]----+
| .+BE*+o== |
| +X=*= *.o= |
| .o+.o=.+* o |
| . *.= = o |
| + * S o |
| o |
| |
| |
| |
+----[SHA256]-----+
PS C:\Users\(ユーザー名)>
|
ssh-keygenコマンドが成功すると、.sshフォルダが生成されます。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
PS C:\Users\(ユーザー名)\.ssh> ls
ディレクトリ: C:\Users\(ユーザー名)\.ssh
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/12/29 8:24 1766 id_rsa
-a---- 2020/12/29 8:24 403 id_rsa.pub
PS C:\Users\(ユーザー名)\.ssh>
|
id_rsaが秘密鍵、id_rsa.pubが公開鍵です。
id_rsa.pubの内容をクリップボードにコピーしておきます。
1
2
3
|
PS C:\Users\(ユーザー名)\.ssh> cat .\id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQClQ1ZSy0j****************************************************************************************************************************************************************************************************** (ユーザー名)@(PCの機種名)
PS C:\Users\(ユーザー名)\.ssh>
|
SSHの設定
PaizaCloudにログインしてサーバーを作成します。ユーザー登録していない場合は、無料でユーザー登録できます。また、サーバーも無料で作成できます。
https://paiza.cloud/ja/
サーバー作成時には、「SSH利用」にチェックを付けて、「Public keys」にid_rsa.pubの内容を張り付けて「新規サーバ作成」をクリックしてください。
サーバーが起動したら、画面左上のプルダウンメニューから「SSH設定」を選択してください。
「SSH Command」をクリップボードにコピーしておいてください。「Public keys」にはサーバー生成時に指定したid_rsa.pubの内容が表示されています。
SSH接続
PowerShellからPaizaCloudのサーバーにSSH接続してみます。SSH CommandをそのままPowerShellのターミナルに貼り付けます。
初回接続時は接続が確立されていませんので、接続するか否か聞かれますので、yesと入力してください。passphraseは作成時に指定したものを入力してください。
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
|
PS C:\Users\(ユーザー名)\.ssh> SSH -p (ポート番号) ubuntu@(PaizaCloudのサーバー名).paiza-user-free.cloud
The authenticity of host '[(PaizaCloudのサーバー名).paiza-user-free.cloud]:52799 ([150.95.201.9]:52799)' can't be established.
ECDSA key fingerprint is SHA256:1lA7s1E1sdGNBrJvb3htZKvB4FtxXbepVYcP8ek+rgQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[(PaizaCloudのサーバー名).paiza-user-free.cloud]:52799,[150.95.201.9]:52799' (ECDSA) to the list of known hosts.
Enter passphrase for key 'C:\Users\(ユーザー名)/.ssh/id_rsa':
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-76-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
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.
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-76-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
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.
ubuntu@(PaizaCloudのサーバー名):~$
|
コマンドプロンプトがubuntu@(PaizaCloudのサーバー名)となっていますので、正常にSSH接続できています。
何かコマンドを入力してみます。PaizaCloudのサーバーにSSH接続できています。
1
2
3
4
5
|
ubuntu@(PaizaCloudのサーバー名):~$ pwd
/home/ubuntu
ubuntu@(PaizaCloudのサーバー名):~$ uname -a
Linux (PaizaCloudのサーバー名) 4.15.0-76-generic #86-Ubuntu SMP Fri Jan 17 17:24:28 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
ubuntu@(PaizaCloudのサーバー名):~$
|
SSH接続を終了する場合は「exit」コマンドを入力してください。コマンドプロンプトがPowerShellに戻ります。
1
2
3
4
|
ubuntu@(PaizaCloudのサーバー名):~$ exit
logout
Connection to (PaizaCloudのサーバー名).paiza-user-free.cloud closed.
PS C:\Users\(ユーザー名)\.ssh>
|
まとめ
いかがでしたでしょうか。PowerShellターミナルからPaizaCloudで作成した無料サーバーへSSH接続してみました。ここでは紹介していませんが、鍵の作成方法やSSHの設定など、細やかな設定が可能です。
ぜひご自身でPowerShellのコマンドを書いて、理解を深めてください。
インフラエンジニア専門の転職サイト「FEnetインフラ」
FEnetインフラはサービス開始から10年以上『エンジニアの生涯価値の向上』をミッションに掲げ、多くのエンジニアの就業を支援してきました。
転職をお考えの方は気軽にご登録・ご相談ください。