ユーザー名とパスワードの認証を使用する代わりに、キーベースの認証を使用するようにSSHを構成できます。鍵認証方式を使用するには、鍵のペア(公開鍵と秘密鍵—)を生成する必要があります。秘密鍵操作「パスワード」はユーザー側に保存され、公開鍵はユーザーが接続したいリモートホストに配置されます。ローカルコンピュータは秘密鍵を使用してリモートホストの公開鍵を検証し、リモートホストの「正当性」を判断します。
環境紹介:
ワークステーションとserveraの2つの仮想マシンがあります。ワークステーションはクライアントで、serveraはサーバーです。次に、sshを使用して、キー認証を介してワークステーション仮想マシンからserveraに接続します。
ssh-keygenを使用してワークステーションでキーペアを生成し、キーのパスワードを指定します(パスワードが指定されていない場合は、パスワードが使用されていないことを意味します。キーを使用してログインする場合、パスワードの入力を求められることはありません。 。)
[it@workstation ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/it/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/it/.ssh/id_rsa.
Your public key has been saved in /home/it/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bUXox2I6qVu4frpCPSgXhFo8b48tc8qyzsv851UGaoc [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| . . .. |
| = . .. |
| o + .. .. |
|. + o o+.o |
| . BE S+=o |
| . B.*o++ |
| = =.oo. |
| +. + o+. |
| .B=.=B= |
+----[SHA256]-----+
[it@workstation ~]$ ls .ssh/
id_rsa id_rsa.pub
[it@workstation ~]$
id_rsaは秘密鍵、id_rsa.pubは公開鍵です(ここではシステムのデフォルト名を使用します)
使用するためにはssh-コピー-IDサーバaに公開鍵をアップロードします
[it@workstation ~]$ ssh-copy-id -i .ssh/id_rsa it@servera
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host 'servera (10.10.10.25)' can't be established.
ECDSA key fingerprint is SHA256:/Xj4qZo8BWSfrzHt3OpZ4sLuasIFYxd6Wf/ucFN2UL8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
it@servera's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'it@servera'"
and check to make sure that only the key(s) you wanted were added.
sshを介してserveraに接続し、ポップアップウィンドウに秘密鍵のパスワードを入力します(私のワークステーションはグラフィカルインターフェイスであるため、ポップアップウィンドウに鍵のパスワードを入力するように求められます)。
[it@workstation ~]$ ssh it@servera
Last login: Thu Oct 29 14:41:13 2020 from 10.10.10.20
[it@servera ~]$
パスワードを繰り返し入力するのが面倒な方もいらっしゃるかもしれませんが、パスワードを入力しない方法はありますか?ssh-agentを使用して、bashプロセスで秘密鍵のパスワードをキャッシュできます。
[it@workstation ~]$ eval $(ssh-agent)
Agent pid 3191
[it@workstation ~]$ ssh-add .ssh/id_rsa
Enter passphrase for .ssh/id_rsa:
Identity added: .ssh/id_rsa ([email protected])
[it@workstation ~]$ ssh it@servera
Last login: Thu Oct 29 14:42:51 2020 from 10.10.10.20
[it@servera ~]$
パスワードは現在のbashにキャッシュされているため、新しいbashに切り替えるときは、秘密鍵のパスワードを入力する必要があります。
同様に、キーをserverbに転送します。