1、查看SSH客户端版本
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh-V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH:下面的例子即表明该系统正在使用OpenSSH:
$ssh-V
OpenSSH_3.9p1,OpenSSL0.9.7aFeb192003
下面的例子表明该系统正在使用SSH2:
$ssh-V
ssh:SSHSecureShell3.2.9.1(non-commercialversion)oni686-pc-linux-gnu
2、用SSH登录到远程主机
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的.ssh/hostkeys下,这样你就可以继续操作了。示例如下:
localhost$ssh-ljsmithremotehost.example.com
Hostkeynotfoundfromdatabase.
Keyfingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
Youcangetapublickey‘sfingerprintbyrunning%ssh-keygen-Fpublickey.pubonthekeyfile.
Areyousureyouwanttocontinueconnecting(yes/no)?Yes
Hostkeysavedto/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pubhostkeyforremotehost.example.com,
acceptedbyjsmithMonMay26200816:06:50-0700jsmith@remotehost.example.compassword:remotehost.example.com$
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
localhost$ssh-ljsmithremotehost.example.com
jsmith@remotehost.example.compassword:
remotehost.example.com$
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
o系统管理员在远程主机上升级或者重新安装了SSH服务器
o有人在进行一些恶意行为,等等。
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
localhost$ssh-ljsmithremotehost.example.com@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@WARNING :HOSTIDENTIFICATIONHASCHANGED!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ITISPOSSIBLETHATSOMEONEISDOINGSOMETHINGNASTY!
Someonecouldbeeavesdroppingonyourightnow(man-in-the-middleattack)!
Itisalsopossiblethatthehostkeyhasjustbeenchanged.
Pleasecontactyoursystemadministrator.
Addcorrecthostkeyto─/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub∥togetridofthismessage.
Receivedserverkey'sfingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
Youcangetapublickey'sfingerprintbyrunning%ssh-keygen-Fpublickey.pubonthekeyfile.
Agentforwardingisdisabledtoavoidattacksbycorruptedservers.
Areyousureyouwanttocontinueconnecting(yes/no)?yes
Doyouwanttochangethehostkeyondisk(yes/no)?yes
Agentforwardingre-enabled.
Hostkeysavedto/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pubhostkeyforremotehost.example.com,
acceptedbyjsmithMonMay26200816:17:31-0700jsmith@remotehost.example.com'spassword:remotehost$
3、调试SSH客户端会话
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
没有SSH客户端调试信息的例子:
localhost$ssh-ljsmithremotehost.example.com
warning:Connectingtoremotehost.example.comfailed:Noaddressassociatedtothename
包含ssh调试信息的例子:
locaclhost$ssh-v-ljsmithremotehost.example.com
debug:SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
Metaconfigparsingstoppedatline3.
debug:SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
Settingvariable嘠攀爀恋漀猀攀Mode'toFALSE'.
debug:SshConfig/sshconfig.c:3130/ssh_config_read_file_ext:Read17paramsfromconfigfile.
debug:Ssh2/ssh2.c:1707/main:Userconfigfilenotfound,usingdefaults.(Lookedfor/home/jsmith/.ssh2/ssh2_config')
debug:Connectingtoremotehost.example.com,port22…(SOCKSnotused)
warning:Connectingtoremotehost.example.comfailed:Noaddressassociatedtothename
[注:很多命令中,v选项对应的英文是verbose,也就是详细的信息的意思。]
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
4、用SSH退出符切换SSH会话
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
1.登入远程主机:
localhost$ssh-ljsmithremotehost
2.已连接远程主机:
remotehost$
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
当你输入“~”你不会立即在屏幕上看到,当你按下并且按回车之后才一起显示。如下,在远程主机中以此输入“~”
remotehost$~^Z
[1]+Stoppedssh-ljsmithremotehost
localhost$
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
localhost$jobs
[1]+Stoppedssh-ljsmithremotehost
5.你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
localhost$fg%1
ssh-ljsmithremotehost
remotehost$
5、用SSH退出字符会话,显示信息
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH2客户端上使用。
登录到远程服务器
localhost$ssh-ljsmithremotehost
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
remotehost$[注:当你在命令行上输入~s时,它是不可见的.]
remotehost:remotehost
localhost:localhost
remoteversion:SSH-1.99-OpenSSH_3.9p1
localversion:SSH-2.0-3.2.9.1SSHSecureShell(non-commercial)
compressedbytesin:1506
uncompressedbytesin:1622
compressedbytesout:4997
uncompressedbytesout:5118
packetsin:15
packetsout:24
rekeys:0
Algorithms:
Chosenkeyexchangealgorithm:diffie-hellman-group1-sha1
Chosenhostkeyalgorithm:ssh-dss
Commonhostkeyalgorithms:ssh-dss,ssh-rsa
Algorithmsclienttoserver:
Cipher:aes128-cbc
MAC:hmac-sha1
Compression:zlib
Algorithmsservertoclient:
Cipher:aes128-cbcMAC:hmac-sha1
Compression:zlib
localhost$
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助。想必各位看了这篇文章之后一定会有所收货,若想了解更多相关知识请继续锁定希赛系统学院!