Как я могу подключиться по ssh к серверу с помощью ключа rsa с определенным именем пользователя? [закрыто]

Я хочу использовать ssh со своего домашнего компьютера (пользователь: antonio @ antonio-home ) на serveruser @ serverhost с ключом rsa.

Я сгенерировал rsa-ключ с помощью ssh-keygen -t rsa и загрузил его на сервер, но он все еще запрашивает у меня пароль.

Если я создам пользователя antonio на serverhost и скопирую свой id_rsa.pub в домашний каталог пользователя antonio , я смогу успешно выполнить ssh antonio @serverhost . Но ssh serveruser @ serverhost не работает (id_rsa.pub тоже находится внутри домашнего каталога serveruser )

Что я делаешь неправильно? может быть, я должен указать имя пользователя при генерации ключа rsa? Что-то вроде ssh-keygen -t rsa --user serveruser ?


Я использовал ssh-copy-id для отправки ключа на сервер, также пытался сделать cat ~ / .ssh / id_rsa_serveruser.pub | ssh -lserveruser <имя хоста или IP-адрес сервера> "cat >> .ssh / authorized_keys"

Права доступа к файлам на сервере: drwx ------ .ssh и -rw ------- авторизованные_ключи . serveruser является владельцем этих файлов и каталогов.

Файл authorized_keys на сервере:

ssh-rsa AAAAB3NzaC1yc2EA....AAADAQA antonio@Antonio-Home

Я вижу, что в конце этого файла все еще есть неправильное имя пользователя: antonio @ Antonio-Home . Может, в этом проблема?

Я пробовал устанавливать, а не устанавливать парольную фразу для ключа - без разницы

Antonio-Home:.ssh antonio$ cat config 
Host serveruser
Hostname <ip>
User serveruser
Identityfile2 ~/.ssh/id_rsa

ssh -vvvv serveruser
OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /Users/antonio/.ssh/config
debug1: /Users/antonio/.ssh/config line 1: Applying options for serveruser
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 20: Applying options for *
debug1: /etc/ssh/ssh_config line 102: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to <ip> [<ip>] port 22.
debug1: Connection established.
debug1: identity file /Users/antonio/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/antonio/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2 pat OpenSSH_6.6.1* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to <ip>:22 as 'serveruser'
debug3: hostkeys_foreach: reading file "/Users/antonio/.ssh/known_hosts"
debug3: record_hostkey: found key type RSA in file /Users/antonio/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from <ip>
debug3: order_hostkeyalgs: prefer hostkeyalgs: ssh-rsa-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-rsa
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-rsa,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ssh-dss-cert-v00@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,ssh-dss
debug2: kex_parse_kexinit: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-md5-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-md5-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:ceUAVoQrX7gnlD3N4j82eaYSO15RKgNDfdL66+cdTCA
debug3: hostkeys_foreach: reading file "/Users/antonio/.ssh/known_hosts"
debug3: record_hostkey: found key type RSA in file /Users/antonio/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from <ip>
debug1: Host '<ip>' is known and matches the RSA host key.
debug1: Found key in /Users/antonio/.ssh/known_hosts:1
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /Users/antonio/.ssh/id_rsa (0x7f97e1713cb0), explicit
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/antonio/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
serveruser@<ip>'s password: 
2
задан 25 October 2016 в 00:49
3 ответа

Выполните:

ssh-keygen -trsa -b2048 -f ~/.ssh/id_rsa_serveruser

Затем

cat ~/.ssh/id_rsa_serveruser.pub |ssh -lserveruser <hostname or IP of server> "cat >> .ssh/authorized_keys"

введите свой пароль, когда будет предложено.

затем В вашем ~ / .ssh / config файле:

Host <nickname for connection>
Hostname <hostname or IP address of serverhost>
User serveruser
Identityfile2 ~/.ssh/id_rsa_serveruser

и вы должен иметь возможность использовать

ssh <nickname for connection>

, и вам не нужно будет вводить пароль. если это не сработает, убедитесь, что файл ~ / .ssh / authorized_keys действительно существует с соответствующими разрешениями на сервере

РЕДАКТИРОВАТЬ

Я вижу, что в конце этого файла все еще указано неправильное имя пользователя файл: antonio @ Antonio-Home. Может быть, в этом проблема?

из справочной страницы ssh-keygen :

For RSA1 keys, there is also a comment field in the key file that is only for convenience to the user
     to help identify the key.  The comment can tell what the key is for, or whatever is useful.  The com-
     ment is initialized to ``user@host'' when the key is created, but can be changed using the -c option.

Я думаю, что «имя пользователя», которое вы видите в конце этого .pub файла, и есть тот комментарий, так что это наиболее скорее всего, не проблема.

0
ответ дан 3 December 2019 в 12:38

Вы можете настроить свой ssh-клиент для подключения к вашему серверу, используя по умолчанию другое имя пользователя. В вашем ~ / .ssh / config:

Host serverhost
User serveruser

Таким образом вы подключаетесь напрямую, без изменений для пользователей вашего сервера.

Затем убедитесь в разрешениях файлов ssh на домашней странице serveruser:

chmod 0700 .ssh; chmod 0600 .ssh / authorized_keys

0
ответ дан 3 December 2019 в 12:38

Наконец-то я нашел решение! На сервере моя папка .ssh принадлежала пользователю serveruser и группе root , когда я сменил группу на hosting (группа, где находится serveruser). Все заработало нормально. Спасибо всем за помощь!

1
ответ дан 3 December 2019 в 12:38

Теги

Похожие вопросы