
在ssh命令中可以通过-v选项直接开启调试模式,在连接的过程中将根据设定的调试等级输出对应的信息,这篇文章以确认ssh首次连接的动作为例,说明调试模式的使用方式。
调试模式
ssh命令中,调试模式有三种,具体说明如下所示:
类别 |
设定方法 |
使用示例 |
使用说明 |
调试模式1 |
-v |
ssh -v 具体命令 |
显示debug1的调试信息 |
调试模式2 |
-vv |
ssh -v 具体命令 |
显示debug1和debug2的调试信息 |
调试模式3 |
-vvv |
ssh -v 具体命令 |
显示debug1、debug2和debug3的调试信息 |
首次连接
比如在192.168.163.121这台机器上, 使用-v来确认连接的过程:
输入命令ssh -v 192.168.163.121
执行之前确认一下.ssh的密钥信息,可以看到有rsa的密钥对
[root@host121 ssh]# ls $HOME/.ssh
id_rsa id_rsa.pub
[root@host121 ssh]#
执行信息如下所示:
[root@host121 ssh]# ssh -v 192.168.163.121
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.163.121 [192.168.163.121] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.163.121:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
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
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:y9ir2Jbc7kNZPhP9h/O9juUZbTmGDo6NZi2IZnLwg0s
The authenticity of host '192.168.163.121 (192.168.163.121)' can't be established.
ECDSA key fingerprint is SHA256:y9ir2Jbc7kNZPhP9h/O9juUZbTmGDo6NZi2IZnLwg0s.
ECDSA key fingerprint is MD5:d5:e7:dc:b3:21:06:5a:e5:c0:f7:a4:60:f7:59:d4:48.
Are you sure you want to continue connecting (yes/no)?
可以看到开始登录时会确认 /root/.ssh/id_rsa的信息,并返回了存在的信息,在输入known_hosts之前,确认了服务器的密钥指纹信息,并提示用户是否继续
输入yes继续确认
输入yes之后,可以看到试图使用密钥方式进行登录的过程,ssh支持密钥方式登录和用户名/密码登录等两种方式,数据传输的安全性都是可以得到保证的,不同在于是否需要输入密码。
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.163.121' (ECDSA) to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
root@192.168.163.121's password:
此时在别的终端已经可以确认此时已经生成了know_hosts的信息,这是上一步骤中yes输入之后的结果。
[root@host121 ~]# ls ~/.ssh
id_rsa id_rsa.pub known_hosts
[root@host121 ~]# cat ~/.ssh/known_hosts
192.168.163.121 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEy3w5004yyzNA8dXo1vo67E63tZybV88SiDupDfnHzHCeZ4tHjNqdabc9Fa+Jwwe5uIjcUFb+Ag4HaBjK4982U=
[root@host121 ~]#
输入密码完成登录
输入密码之后即可完成登录
root@192.168.163.121's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.163.121 ([192.168.163.121]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_CTYPE = en_US.UTF-8
Last login: Sat Dec 7 18:48:33 2019 from gateway
[root@host121 ~]#
首次之后的连接
首次之后的连接,不再需要确认know_hosts的信息,使用-v确认结果如下所示
[root@host121 ~]# ssh -v 192.168.163.121
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.163.121 [192.168.163.121] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.163.121:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
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
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:y9ir2Jbc7kNZPhP9h/O9juUZbTmGDo6NZi2IZnLwg0s
debug1: Host '192.168.163.121' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
root@192.168.163.121's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.163.121 ([192.168.163.121]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_CTYPE = en_US.UTF-8
Last login: Sat Dec 7 19:46:25 2019 from host121
[root@host121 ~]#
密钥登录方式
ssh连接设定
SSH在使用中,更多的使用则是使用密钥方式登录,首先使用ssh-copy-id设定登录,本示例中是本机连接本机,但是方式都是相同的。
[root@host121 ~]# ls ~/.ssh
id_rsa id_rsa.pub known_hosts
[root@host121 ~]# ssh-copy-id -i 192.168.163.121
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.163.121's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.163.121'"
and check to make sure that only the key(s) you wanted were added.
[root@host121 ~]#
[root@host121 ~]# ls ~/.ssh
authorized_keys id_rsa id_rsa.pub known_hosts
[root@host121 ~]# cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCj7f9fLXrbmXCXvdjlKwgJhDZ7VNr8eT++j3elmJq2SmVAPKT2Ows8Xjtrvsik9p7dTCwX6B88VUTTBdAQJCoADTxUIcn847XCkLSz5803d4spR3fVaZl6U+VEvW6fDnTAPUzuWpb2E/lBIVr9x8p1hayZFHtODrKPzyDp1li3fu6F+NR1gVsbiuYIyKH8QJIL1P2QYiyGn0/+qfaJAJ9QvbqmCCnJLhbrfBeFb4e/K+66guctaHc7qPXaeA98kJhKZg5ZqlHY+jC7F4T+/zp/bwophcnk67HakVju++R44zJdGVercZNkHZdMNWfMiwF4Xkt7uT/SEMaLUvWd+3UT root@host121
[root@host121 ~]# ls -l ~/.ssh
total 16
-rw-------. 1 root root 394 Dec 7 19:51 authorized_keys
-rw-------. 1 root root 1679 Dec 7 16:46 id_rsa
-rw-r--r--. 1 root root 394 Dec 7 16:46 id_rsa.pub
-rw-r--r--. 1 root root 177 Dec 7 19:36 known_hosts
[root@host121 ~]#
当然手动设定也是一样,主要在于authorized_keys文件以及其600的权限需要注意。此文件需要设定在目标机器上。
连接过程确认
[root@host121 ~]# ssh -v 192.168.163.121
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.163.121 [192.168.163.121] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.163.121:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
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
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:y9ir2Jbc7kNZPhP9h/O9juUZbTmGDo6NZi2IZnLwg0s
debug1: Host '192.168.163.121' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.163.121 ([192.168.163.121]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_CTYPE = en_US.UTF-8
Last login: Sat Dec 7 19:48:37 2019 from host121
[root@host121 ~]#
从上述信息中可以看到,由于通过ssh-copy-id进行了ssh密钥设定,所以在证书认证阶段就已经成果,所以没有再有后面的Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password通过密码方式登录的提示信息了流程了。