账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    ssh公钥登陆root失败
    38
    0

    搜了很多资料也没能解决,只好把所有的背景信息都带上来提问。
    在目标机器(CentOS 7.0)上以root用户登录,通过ssh-keygen生成了公钥并写入authorized_keys,权限如下:

    ls -alZ .ssh
    drwx------. root root unconfined_u:object_r:ssh_home_t:s0 .
    drwx------. root root system_u:object_r:admin_home_t:s0 ..
    -rw-------. root root unconfined_u:object_r:ssh_home_t:s0 authorized_keys
    -rw-------. root root unconfined_u:object_r:ssh_home_t:s0 id_rsa
    -rw-r--r--. root root unconfined_u:object_r:ssh_home_t:s0 id_rsa.pub

    关闭了SElinux:

    /etc/selinux/config
    SELINUX=disabled

    sshd_config中的部分其配置如下:

    AuthorizedKeysFile .ssh/authorized_keys
    PasswordAuthentication yes
    RSAAuthentication yes
    KerberosAuthentication no
    PubkeyAuthentication yes
    GSSAPIAuthentication yes
    PermitRootLogin yes

    在gitlab-CI中,使用如下方式远程执行命令:

    ssh -vvv -t -i key_file -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "root@$host" "xxx"

    得到debug信息如下:

    OpenSSH_7.4p1 Debian-10+deb9u1, OpenSSL 1.0.2l  25 May 2017
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    Pseudo-terminal will not be allocated because stdin is not a terminal.
    debug2: resolving "10.1.17.171" port 22
    debug2: ssh_connect_direct: needpriv 0
    debug1: Connecting to 10.1.17.171 [10.1.17.171] port 22.
    debug1: Connection established.
    debug1: permanently_set_uid: 0/0
    debug1: SELinux support disabled
    debug1: identity file key_file type 1
    debug1: key_load_public: No such file or directory
    debug1: identity file key_file-cert type -1
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_7.4p1 Debian-10+deb9u1
    debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
    debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
    debug2: fd 3 setting O_NONBLOCK
    debug1: Authenticating to 10.1.17.171:22 as 'root'
    debug3: hostkeys_foreach: reading file "/dev/null"
    debug3: send packet: type 20
    debug1: SSH2_MSG_KEXINIT sent
    debug3: receive packet: type 20
    debug1: SSH2_MSG_KEXINIT received
    debug2: local client KEXINIT proposal
    debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
    debug2: host key algorithms: 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-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
    debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc
    debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc
    debug2: MACs ctos: 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
    debug2: MACs stoc: 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
    debug2: compression ctos: none,zlib@openssh.com,zlib
    debug2: compression stoc: none,zlib@openssh.com,zlib
    debug2: languages ctos: 
    debug2: languages stoc: 
    debug2: first_kex_follows 0 
    debug2: reserved 0 
    debug2: peer server KEXINIT proposal
    debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
    debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
    debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
    debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
    debug2: MACs ctos: 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
    debug2: MACs stoc: 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
    debug2: compression ctos: none,zlib@openssh.com
    debug2: compression stoc: none,zlib@openssh.com
    debug2: languages ctos: 
    debug2: languages stoc: 
    debug2: first_kex_follows 0 
    debug2: reserved 0 
    debug1: kex: algorithm: curve25519-sha256
    debug1: kex: host key algorithm: ecdsa-sha2-nistp256
    debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
    debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
    debug3: send packet: type 30
    debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    debug3: receive packet: type 31
    debug1: Server host key: ecdsa-sha2-nistp256 SHA256://dKwq7yklZ+aHdPqYQxzPdRrilYsY+JHiSS8CbRSag
    debug3: hostkeys_foreach: reading file "/dev/null"
    Warning: Permanently added '10.1.17.171' (ECDSA) to the list of known hosts.
    debug3: send packet: type 21
    debug2: set_newkeys: mode 1
    debug1: rekey after 134217728 blocks
    debug1: SSH2_MSG_NEWKEYS sent
    debug1: expecting SSH2_MSG_NEWKEYS
    debug3: receive packet: type 21
    debug1: SSH2_MSG_NEWKEYS received
    debug2: set_newkeys: mode 0
    debug1: rekey after 134217728 blocks
    debug2: key: key_file (0x7f304d3a0bd0), explicit
    debug3: send packet: type 5
    debug3: receive packet: type 7
    debug1: SSH2_MSG_EXT_INFO received
    debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
    debug3: receive packet: type 6
    debug2: service_accept: ssh-userauth
    debug1: SSH2_MSG_SERVICE_ACCEPT received
    debug3: send packet: type 50
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive
    debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive
    debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
    debug3: authmethod_lookup gssapi-keyex
    debug3: remaining preferred: gssapi-with-mic,publickey,keyboard-interactive,password
    debug3: authmethod_is_enabled gssapi-keyex
    debug1: Next authentication method: gssapi-keyex
    debug1: No valid Key exchange context
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup gssapi-with-mic
    debug3: remaining preferred: publickey,keyboard-interactive,password
    debug3: authmethod_is_enabled gssapi-with-mic
    debug1: Next authentication method: gssapi-with-mic
    debug1: Unspecified GSS failure.  Minor code may provide more information
    No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_0)
    
    debug1: Unspecified GSS failure.  Minor code may provide more information
    No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_0)
    
    debug2: we did not send a packet, disable method
    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: key_file
    debug3: send_pubkey_test
    debug3: send packet: type 50
    debug2: we sent a publickey packet, wait for reply
    debug3: receive packet: type 60
    debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
    debug2: input_userauth_pk_ok: fp SHA256:K/re5/UEgCwLhxFl7BDgBBhWV3J8adbENMX+1ohNRzk
    debug3: sign_and_send_pubkey: RSA SHA256:K/re5/UEgCwLhxFl7BDgBBhWV3J8adbENMX+1ohNRzk
    debug1: read_passphrase: can't open /dev/tty: No such device or address
    debug2: no passphrase given, try next key
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup keyboard-interactive
    debug3: remaining preferred: password
    debug3: authmethod_is_enabled keyboard-interactive
    debug1: Next authentication method: keyboard-interactive
    debug2: userauth_kbdint
    debug3: send packet: type 50
    debug2: we sent a keyboard-interactive packet, wait for reply
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive
    debug3: userauth_kbdint: disable: no info_req_seen
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup password
    debug3: remaining preferred: 
    debug3: authmethod_is_enabled password
    debug1: Next authentication method: password
    debug1: read_passphrase: can't open /dev/tty: No such device or address
    debug3: send packet: type 50
    debug2: we sent a password packet, wait for reply
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive
    Permission denied, please try again.
    debug1: read_passphrase: can't open /dev/tty: No such device or address
    debug3: send packet: type 50
    debug2: we sent a password packet, wait for reply
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive
    Permission denied, please try again.
    debug1: read_passphrase: can't open /dev/tty: No such device or address
    debug3: send packet: type 50
    debug2: we sent a password packet, wait for reply
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive
    debug2: we did not send a packet, disable method
    debug1: No more authentication methods to try.
    Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive).

    留意到这行:debug1: key_load_public: No such file or directory,不知道是不是问题产生的原因。

    目标机器上的ssh日志:

    Jul 26 14:25:55 sshd[26010]: Failed password for root from 10.1.5.227 port 41539 ssh2
    Jul 26 14:25:55 sshd[26010]: Failed password for root from 10.1.5.227 port 41539 ssh2
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 藍了天白赴美 普通会员 1楼

      如果你尝试ssh公钥登陆root,但失败了,可能有以下几个原因:

      1. 公钥文件不存在:首先,你需要确保你的公钥文件存在于正确的位置。你可以使用ssh-keygen命令来创建一个新的公钥文件。例如,如果你的公钥文件在~/.ssh/id_rsa.pub,你可以使用以下命令来创建一个新的文件:

      ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

      这将创建一个名为id_rsa.pub的文件,其中包含了你的公钥信息。

      1. 管理员没有权限:如果你尝试的用户没有权限登录到root,你将无法ssh到root。你可以尝试使用chown命令来更改权限:

      chown root:root /path/to/your/file

      这将把文件的所有权从你改为root用户。

      1. 括号不匹配:在ssh命令中,-i选项用于指定公钥文件的路径。如果你的文件路径和命令中提供的路径不匹配,你将无法ssh到root。你可以尝试调整路径:

      ssh-keygen -i ~/.ssh/id_rsa.pub

      这将把文件路径改为~/.ssh/id_rsa.pub

      1. ssh命令版本问题:如果你使用的ssh命令版本过低,你将无法ssh到root。你可以尝试升级ssh命令:

      sudo apt-get update sudo apt-get install openssh-client

      如果问题仍然存在,你可以尝试使用新的ssh版本,例如sshv3

      1. 公钥问题:如果你的公钥不是有效的,你将无法ssh到root。你可以尝试使用ssh-keyscan命令来检查你的公钥:

      ssh-keyscan example.com >> /dev/null 2>&1

      如果你收到一条关于你公钥的错误消息,你可能需要检查你的公钥文件或检查你的网络连接。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部