Намного удобнее, быстрее и безопаснее подключиться на удаленный сервер по SSH, без ввода пароля.
Речь идет об аутентификации по ключам.
C помощью набора утилит из OpenSSH ее можно реализовать в три простых шага:
- Сгенерировать ключи аутентификации на локальной машине;
- Скопировать открытый ключ на удаленный сервер;
- Добавить секретный ключ в агент аутентификации.
Шаг 1: Генерируем пару ключей на локальной машине
Сгенерируем ключи аутентификации для SSH с помощью ssh-keygen
.
$ ssh-keygen
Вывод:
Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): Created directory '/home/user/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is: 8c:2a:ed:82:98:6d:12:0a:3a:ba:b2:1c:c0:25:be:5b
Шаг 2: Устанавливаем открытый ключ на удаленном сервере
С помощью ssh-copy-id
устанавливаем открытый ключ (id_rsa.pub) на удаленный сервер. Он будет скопирован в файл authorized_keys
.
$ ssh-copy-id -i ~/.ssh/id_rsa.pub UserName@RemoteServer
Вывод:
UserName@RemoteServer's password: ******** Now try logging into the machine, with "ssh 'username@remoteserver'", and check in: ~/.ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
Шаг 3: Добавляем секретный ключ в агент аутентификации на локальной машине
С помощью ssh-add
добавим секретный ключ в агент аутентификации (ssh-agent).
$ ssh-add
Вывод:
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)
Теперь Вы сможете подключаться к удаленному серверу по протоколу SSH без ввода пароля.
Класс, самая понятная инструкция из всех, что нашёл. Спасибо!
user_a@Server_A>ssh-keygen -t rsa -b 4096 -N «» -f ~/.ssh/id_rsa
user_a@Server_A> ssh-copy-id -i ~/.ssh/id_rsa.pub user_b@Server_B
ssh-add
Will recommend checking this article if you have an issue and particularly if you get — bad ownership or modes for directory /home/dave/.ssh.
https://www.daveperrett.com/articles/2010/09/14/ssh-authentication-refused/
____
SSH doesn’t like it if your home or ~/.ssh directories have group write permissions. Your home directory should be writable only by you, ~/.ssh should be 700, and authorized_keys should be 600 :
Excelente guia.
Muchas gracias por el aporte.
ssh-add not working
«$ ssh-add
Could not open a connection to your authentication agent.»
If you have:
Could not open a connection to your authentication agent
when trying ssh-add, do this instead:
$ ssh-agent /bin/sh
$ ssh-add