日曜ITエンジニア劇場

日曜に勉強した情報の整理と、挑戦する新たな技術(私にとって)のつまづきポイントを綴っていきます。

SSH公開鍵の登録(ssh-copy-idコマンド)


参考資料

blog.apar.jp


ありがとうございます!
シンプルで分かりやすい記事だったので
すんなり作業が完了しました🙇🏻‍♀️


作業環境

接続元

$ sw_vers  
ProductName:    Mac OS X  
ProductVersion: 10.15.4  

接続先

$ cat /etc/redhat-release   
CentOS Linux release 8.1.1911 (Core)   

手順

1.Homebrewでssh-copy-idをインストールする。

brew install ssh-copy-id  

2. SSH鍵を生成する。

$ ssh-keygen -t rsa -b 4096  
Generating public/private rsa key pair.  
Enter file in which to save the key (/Users/[user_name]/.ssh/id_rsa):   
Enter passphrase (empty for no passphrase):   
Enter same passphrase again:   
Your identification has been saved in /Users/[user_name]/.ssh/id_rsa.  
Your public key has been saved in /Users/[user_name]/.ssh/id_rsa.pub.  
The key fingerprint is:  
SHA256:u+eDgIQBy51mfyEJU6YlP0Yz2v/l1EykfwotqvX+wkQ [user_name]@[user_name]noMacBook-Air.local  
The key's randomart image is:  
+---[RSA 4096]----+  
|.. +.B       .   |  
|..o & +     o    |  
|.. X B .   . .   |  
|  + + + .  E*    |  
|   . o oS .= = . |  
|    . o ..=.o o  |  
|       ..=o. .   |  
|        +.+o     |  
|       ..oo+o.   |  
+----[SHA256]-----+  
  

3.SSH鍵を任意のディレクトリに移動する。

※単なる整理整頓なので、この手順は飛ばしても構わない。

$ mkdir ~/.ssh/virtual_box  
$ mv ~/.ssh/id_rsa ~/.ssh/virtual_box/  
$ mv ~/.ssh/id_rsa.pub ~/.ssh/virtual_box/  
$ ls -la ~/.ssh/virtual_box/  
total 16  
drwxr-xr-x   4 [user_name]  staff   128  6  7 13:15 .  
drwx------  11 [user_name]  staff   352  6  7 13:15 ..  
-rw-------   1 [user_name]  staff  3414  6  7 13:13 id_rsa  
-rw-r--r--   1 [user_name]  staff   768  6  7 13:13 id_rsa.pub  

4.SSH公開鍵を接続先に登録する。

$ ssh-copy-id -p [ポート番号] -i ~/.ssh/virtual_box/id_rsa.pub [user_name]@[接続先のIPアドレスまたはホスト名]  
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/naitoukazue/.ssh/virtual_box/id_rsa.pub"  
/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  
[user_name]@[接続先のIPアドレスまたはホスト名]'s password:   
  
Number of key(s) added:        1  
  
Now try logging into the machine, with:   "ssh -p '[ポート番号]' '[user_name]@[接続先のIPアドレスまたはホスト名]'"  
and check to make sure that only the key(s) you wanted were added.  

5.接続を確認してみる。

$ ssh -p [ポート番号] [user_name]@[接続先のIPアドレスまたはホスト名]  
[user_name]@[接続先のIPアドレスまたはホスト名]'s password:   
Permission denied, please try again.  
[user_name]@[接続先のIPアドレスまたはホスト名]'s password:   
Activate the web console with: systemctl enable --now cockpit.socket  
  
Last failed login: Sun Jun  7 14:11:51 JST 2020 from 192.168.0.2 on ssh:notty  
There was 1 failed login attempt since the last successful login.  
Last login: Sun Jun  7 12:45:06 2020 from 192.168.0.2  
[[user_name]@localhost ~]$   

できました😊