パスワードを入力しないで ssh でログインするには?

ショコラ
ショコラ

パスワードを入力しないで ssh でログインするには?

Ubuntu 22.04 は「ssh-keygen -t ed25519」で秘密鍵を作ってキメマス。

もっさん先輩
もっさん先輩

手順

パスワードを入力しないで ssh でログインする手順。

  1. ユーザーのホームに .sshディレクトリを作成します。
mkdir ~/.ssh
chmod 0700 ~/.ssh
cd ~/.ssh
  1. 秘密鍵と公開鍵を作成します。
ssh-keygen -t ed25519

エンターを3回押します。

$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_ed25519):(エンター)
Enter passphrase (empty for no passphrase):(エンター)
Enter same passphrase again:(エンター)
Your identification has been saved in /home/ubuntu/.ssh/id_ed25519
Your public key has been saved in /home/ubuntu/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:ZIUigt6AUbiNrOeHbMa8COA2GKngsTvP3EMNn1Q7CrA ubuntu@ubuntu-ThinkCentre-M90p
The key's randomart image is:
+--[ED25519 256]--+
|o=.      ..      |
|+.... . .o       |
|o+o.o. .+ .      |
|o+.E o + o       |
|*.    * S .      |
|O.+  . =         |
|+% ..            |
|oo& o.           |
|.++* ..          |
+----[SHA256]-----+
  1. 公開鍵を authrized_keys に追加します。
cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
  1. PubkeyAuthentication を有効にします。
sudo sed -i -e 's/#PubkeyAuthentication/PubkeyAuthentication/' /etc/ssh/sshd_config
  1. sshd を再起動します。
sudo systemctl restart sshd
  1. ログインできるか確認します。
ssh -i ~/.ssh/id_ed25519 ubuntu@localhost

あとは、作成した秘密鍵(~/.ssh/id_ed25519)を自分の PC にコピーして、それを TeraTerm の「Private key file」に設定してログインできるか確認してください。

因みに、パスワードを入力して ssh でログインするには PasswordAuthentication を yes に設定します。

PasswordAuthentication yes

以上

シチュエーション

Ubuntu 22.04 に パスワードを入力しないでログインしたい。

RSAでの鍵の作成

RSA で 4096bit の 秘密鍵と公開鍵を作成します。

ssh-keygen -t rsa -b 4096

エンターを3回押します。

$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kasai/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/kasai/.ssh/id_rsa
Your public key has been saved in /home/kasai/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:6oUeyJnGLAg4CbtC1JYfFjUlAHi1JqSEL+MJueTwGpc kasai@SAAS
The key's randomart image is:
+---[RSA 4096]----+
|...oo+o+..       |
|o.+.. o o        |
|.=.= =           |
|X+o = .          |
|%*.. .  S        |
|=BE+ + o         |
|++. O + .        |
|o  o o o         |
|      o          |
+----[SHA256]-----+
Scroll to Top