いつも忘れるので。
前提
- 以下で読み替えてください。
| 項目 | 値 |
|---|---|
| ユーザ名 | kanno |
| サーバ名 | messiah |
手順
-
ローカルマシン上で、公開鍵と秘密鍵を作る。
kanno@HORIZON:~$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/kanno/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/kanno/.ssh/id_rsa Your public key has been saved in /home/kanno/.ssh/id_rsa.pub The key fingerprint is: SHA256:tTP9FC9XKhhUA+tOOfzqk8r6Q6Bz3cBtFiruaLkS0pk kanno@HORIZON The key's randomart image is: +---[RSA 3072]----+ | ooo | | ... . | | . o+. . .| | o +++* +.| | . o o +S=@ o + o| |. E o o oo.= + o | | . . * . ... . | | . + ... o. | | o...o++o. | +----[SHA256]-----+ kanno@HORIZON:~$ -
公開鍵と秘密鍵が作られていることを確認する。
kanno@HORIZON:~$ ls ~/.ssh 合計 28 drwx------ 2 kanno kanno 4096 5月 20 17:23 . drwxr-x--- 11 kanno kanno 4096 2月 18 2023 .. -rw------- 1 kanno kanno 2602 5月 21 16:10 id_rsa -rw------- 1 kanno kanno 567 5月 21 16:10 id_rsa.pub -rw------- 1 kanno kanno 4196 5月 20 17:53 known_hosts kanno@HORIZON:~$ -
公開鍵をサーバへ登録する。
kanno@HORIZON:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub kanno@messiah /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/kanno/.ssh/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 kanno@messiah's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'kanno@messiah'" and check to make sure that only the key(s) you wanted were added. kanno@HORIZON:~$ -
秘密鍵を使って、パスワードなしでSSHする接続する。
kanno@HORIZON:~$ ssh -i ./.ssh/id_rsa kanno@messiah Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-31-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro Expanded Security Maintenance for Applicationsが無効化されています。 0のアップデートはすぐに適用されます。 ESM Appsを有効化すると、さらに追加のセキュリティアップデートを適用できます。 https://ubuntu.com/esm を参照するか、次のコマンドを実行してください: sudo pro status Last login: Tue May 21 16:06:58 2024 from 192.168.100.20 kanno@messiah:~$
以上。