5 个基本的 Linux SSH 客户端命令
【摘要】 让我们了解一下 ssh 客户端的以下 5 个基本命令行用法。
让我们了解一下 ssh 客户端的以下 5 个基本命令行用法。
- 识别 SSH 客户端版本
- 登录远程主机
- 向/从远程主机传输文件
- 调试 SSH 客户端连接
- SSH 转义字符用法:(切换 SSH 会话、SSH 会话统计等)
1. SSH 客户端版本:
有时可能需要识别您当前运行的 SSH 客户端及其对应的版本号,可以如下所示进行识别。请注意 Linux 自带 OpenSSH。
$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2014
$ ssh -V
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
2.登录远程主机:
- 第一次从本地主机登录到远程主机时,它会显示找不到主机密钥的消息,您可以选择“是”继续。远程主机的host key会被添加到你家目录的.ssh2/hostkeys目录下,如下图。
localhost$ ssh -l tiamo remotehost.example.com
Host key not found from database.
Key fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
You can get a public key’s fingerprint by running
% ssh-keygen -F publickey.pub
on the keyfile.
Are you sure you want to continue connecting (yes/no)? yes
Host key saved to /home/tiamo/.ssh2/hostkeys/key_22_example.com.pub
host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:06:50 -0700
tiamo@example.com password:
remotehost.example.com$
- 第二次当您从本地主机登录到远程主机时,它只会提示输入密码,因为远程主机密钥已经添加到 ssh 客户端的已知主机列表中。
localhost$ ssh -l tiamo example.com
tiamo@example.com password:
example.com$
- 由于某种原因,如果第一次登录后远程主机的主机密钥被更改,您可能会收到如下所示的警告消息。这可能是由于各种原因,例如 1) 系统管理员升级/重新安装远程主机上的 SSH 服务器 2) 有人在进行恶意活动等,在对下面的消息说“是”之前采取的最佳措施是致电您的系统管理员并确定您收到主机密钥更改消息的原因并验证它是否是正确的主机密钥。
localhost$ ssh -l tiamo example.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the host key has just been changed.
Please contact your system administrator.
Add correct host key to "/home/tiamo/.ssh2/hostkeys/key_22_example.com.pub"
to get rid of this message.
Received server key's fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
You can get a public key's fingerprint by running
% ssh-keygen -F publickey.pub
on the keyfile.
Agent forwarding is disabled to avoid attacks by corrupted servers.
Are you sure you want to continue connecting (yes/no)? yes
Do you want to change the host key on disk (yes/no)? yes
Agent forwarding re-enabled.
Host key saved to /home/tiamo/.ssh2/hostkeys/key_22_example.com.pub
host key for example.com, accepted by tiamo Mon May 26 2014 16:17:31 -0700
tiamo @example.com's password:
remotehost$
3. 文件传输到/从远程主机:
ssh 客户端的另一个常见用途是使用 scp 从/向远程主机复制文件。
- 将文件从远程主机复制到本地主机:
localhost$ scp jsmith@remotehost.example.com:/home/tiamo/remotehostfile.txt remotehostfile.txt
- 将文件从本地主机复制到远程主机:
localhost$ scp localhostfile.txt jsmith@remotehost.example.com:/home/tiamo/localhostfile.txt
4.调试SSH客户端:
有时需要查看调试消息以解决任何 SSH 连接问题。为此,将 -v(小写 v)选项传递给 ssh,如下所示。
- 没有调试消息的示例:
localhost$ ssh -l tiamo remotehost.example.com
warning: Connecting to remotehost.example.com failed: No address associated to the name
localhost$
- 带有调试消息的示例:
locaclhost$ ssh -v -l tiamo remotehost.example.com
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext: Metaconfig parsing stopped at line 3.
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose: Setting variable 'VerboseMode' to 'FALSE'.
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for '/home/tiamo/.ssh2/ssh2_config')
debug: Connecting to remotehost.example.com, port 22... (SOCKS not used)
warning: Connecting to remotehost.example.com failed: No address associated to the name
5.转义符:(切换SSH会话、SSH会话统计等)
转义字符 ~ 引起 SSH 客户端的注意, ~ 后面的字符决定了转义命令。
切换 SSH 会话:当您从本地主机使用 ssh 登录到远程主机时,您可能希望返回本地主机执行一些活动并再次返回远程主机。在这种情况下,您不需要断开与远程主机的 ssh 会话。而是按照以下步骤操作。
- 从本地主机登录到远程主机: localhost$ ssh -l jsmith remotehost
- 现在您已连接到远程主机: remotehost$
- 要暂时返回本地主机,请键入转义字符 ~ 和 Control-Z。当您输入 ~ 时,您不会立即在屏幕上看到它,直到您按 <Control-Z> 并按 Enter。因此,在新行中的远程主机上输入以下按键以使以下内容起作用:~<Control-Z>
remotehost$ ~^Z
[1]+ Stopped ssh -l tiamo remotehost
localhost$
- 现在您返回到 localhost 并且 ssh remotehost 客户端会话作为典型的 unix 后台作业运行,您可以按如下所示进行检查:
localhost$ jobs
[1]+ Stopped ssh -l tiamo remotehost
- 通过将后台 ssh 远程主机会话作业置于 localhost 的前台,您无需再次输入密码即可返回远程主机 ssh
localhost$ fg %1
ssh -l tiamo remotehost
remotehost$
SSH 会话统计:要获取有关当前 ssh 会话的一些有用统计信息,请执行以下操作。这仅适用于 SSH2 客户端。
- 从本地主机登录到远程主机: localhost$ ssh -l tiamo remotehost
- 在远程主机上,键入 ssh 转义字符 ~ 后跟 s,如下所示。这将显示有关当前 SSH 连接的许多有用的统计信息。
remotehost$ [Note: The ~s is not visible on the command line when you type.]
remote host: remotehost
local host: localhost
remote version: SSH-1.99-OpenSSH_3.9p1
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
compressed bytes in: 1506
uncompressed bytes in: 1622
compressed bytes out: 4997
uncompressed bytes out: 5118
packets in: 15
packets out: 24
rekeys: 0
Algorithms:
Chosen key exchange algorithm: diffie-hellman-group1-sha1
Chosen host key algorithm: ssh-dss
Common host key algorithms: ssh-dss,ssh-rsa
Algorithms client to server:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib
Algorithms server to client:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib
localhost$
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)