I created a public and private key using PuTTYgen and copied the public key to .ssh/authorized_keys
under my user account.
Then I try to specify the private key when trying to log in, but apparently it doesn't pick it up and keep asking for the username/password I originally had. I'm using WinSCP to connect and specify the private key in Advanced/Authentication section. Am I missing any steps in WinSCP?
The content of authorized_keys
looks like as below
ssh-rsa AAAAB3NzaC1yc2EAAAABpEVSiiRXi7tOHpkOyFa9w2OLpBep31k9lePCK7RQxsdfs9u11+rdu0XCidRKOY5j4anD1eDaNBj87wqZbsreRe5cFcsakyGUAYXAvqgGApvsep31k9lePCK7RQxlOY5j4anD1eDaNBj8LJO++K3SkUN8E0srRBO8YyMT6Y03/F7+AAAAB3NzaC1yc2Q4h2RLGtr12CDKSBVAnFEc+JucuF4uF0WY4Sh66MSFI63mCQFu9iYNYwWyT6lUo6sks4WypEVSiiRXi7tOHpkOyFa9w2OLpBzAlTA/VSQwdNTFYUI1vquaufZ9ORzTa6dkbBRo/mLVdevYSRMSDw1BUcinYz/ogdxRvw==
I changes the permission to .ssh
to 700 and authorized_keys
to 600.
Although I go to authentication section in WinSCP and specify the private key (as shown in the screenshot) it looks like it still need username and password and doesn't pick it up.
When I use PuTTY and specify the private key, after entering the login username it says
Server refused our key
Here is the log from PuTTY
2018-04-28 17:43:05 Connecting to 158.85.98.202 port 22
2018-04-28 17:43:05 We claim version: SSH-2.0-PuTTY_Release_0.70
2018-04-28 17:43:05 Server version: SSH-2.0-OpenSSH_7.4
2018-04-28 17:43:05 Using SSH protocol version 2
2018-04-28 17:43:05 Doing ECDH key exchange with curve Curve25519 and hash SHA-256
2018-04-28 17:43:05 Server also has ecdsa-sha2-nistp256 host key, but we don't know it
2018-04-28 17:43:05 Host key fingerprint is:
2018-04-28 17:43:05 ssh-ed25519 256 6b:0d:e2:f6:c5:9e:15:84:0c:1b:2c:19:62:cd:5b:ef
2018-04-28 17:43:05 Initialised AES-256 SDCTR client->server encryption
2018-04-28 17:43:05 Initialised HMAC-SHA-256 client->server MAC algorithm
2018-04-28 17:43:05 Initialised AES-256 SDCTR server->client encryption
2018-04-28 17:43:05 Initialised HMAC-SHA-256 server->client MAC algorithm
2018-04-28 17:43:05 Reading key file "C:\Users\\Desktop\private_key.ppk"
2018-04-28 17:43:09 Offered public key
2018-04-28 17:43:09 Server refused our key
2018-04-28 17:43:09 Using SSPI from SECUR32.DLL
2018-04-28 17:43:09 Attempting GSSAPI authentication
2018-04-28 17:43:09 GSSAPI authentication request refused
Чтобы избежать распространенных ошибок при настройке для аутентификации с открытым ключом используйте команду / сценарий ssh-copy-id
. Так как у вас возникнут проблемы с запуском в Windows, вы можете запустить его на самом сервере.
.pub
. .pub
на сервер. На сервере введите:
ssh-copy-id -i mykey. pub имя пользователя @ localhost
Если вы не хотите делать это вручную, вы можете использовать WinSCP 5.15. Он может настроить для вас аутентификацию с открытым ключом.
Используйте кнопку Инструменты> Установить открытый ключ на сервер на странице SSH> Аутентификация диалогового окна дополнительных настроек сайта WinSCP .
(я являюсь автором WinSCP)
Еще один альтернативой является скрипт ssh-copy-id
. В Windows он поставляется с Git для Windows. Так что вы можете использовать это локально, если оно у вас есть.
Если вы используете Windows 10 с PowerShell, вы можете сделать это:
Во-первых, в Windows сгенерируйте ключ RSA. По умолчанию ключ будет сохранен в $env:USERPROFILE\.ssh\id_rsa.pub
:
## Generate RSA Key
ssh-keygen -t rsa -b 4096
. Вы можете использовать опцию -f
, чтобы добавить собственное имя файла и местоположение
## Generate RSA Key
ssh-keygen -t rsa -b 4096 -f <custom-path>
## Copy to the remote server
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh <user>@<host> -p <port> "cat >>.ssh/authorized_keys"
С помощью пользовательского порта и пути к файлу :
## Copy to the remote server
type <custom-path> | ssh <user>@<host> -p <port> "cat >>.ssh/authorized_keys"
. ] Замените <user>
, <host>
, <port>
и <custom-path>
своей информацией.
Вы найдете два основных файла.:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 11/23/2021 3:20 PM 199 config
-a---- 11/23/2021 7:09 PM 3381 id_rsa
-a---- 11/23/2021 7:09 PM 746 id_rsa.pub
-a---- 11/23/2021 3:20 PM 742 known_hosts
В этом примере id_rsa.pub
— это ключ, а id_rsa
— ваш личный. Добавьте закрытый ключ в Putty или MobaXterm или любое приложение, которое вы хотите использовать.