1、查看SSH客户端版本
& k2 [9 H& N0 X% ^
# _. }5 A t& ^2 W/ L有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:; `! ?" m% e* h+ B
' G4 C6 p. K" }9 k) I2 ` f2 h
$ ssh -V 5 G! ~! ?4 x4 p7 X
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
$ `# R; V) w3 Z6 w9 c7 B" Y3 h8 Y, _$ |) Z5 O/ u0 ~- G
下面的例子表明该系统正在使用SSH2: _1 Q9 [( Q% f- c0 z l
' F$ H ?. m& |8 V
$ ssh -V
/ q' O& `5 `9 nssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu$ B$ w% A* x1 r$ }/ h$ A9 j
m ^7 ~% h9 {1 B
2、用SSH登录到远程主机
0 e; K/ b( |, t! U7 e& g7 Y( C; S- {
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
0 a% N. e& a) Y4 n/ _7 r7 c
8 b+ E; s: ~# q: t1
2 z4 S6 |, u6 F* e( E. B* [2
3 K0 G& R' z9 f7 T7 T( t3
7 h! b. n: X- K( q3 ~. M' g- L4
- P9 |3 w6 Y7 M1 R- z5; E+ d* }" p$ s% t
6! r. n* m8 \- a% ?2 [+ {, f, u8 _
7/ g$ k1 E7 B2 b `
8
) N0 }* M' L7 x2 f( ?% x, ] ~localhost$ ssh -l jsmith remotehost.example.com( @1 B: C& V2 k5 Q" A6 i
Host key not found from database.0 ^0 s/ O$ \$ K# ]
Key fingerprint:( d: T; n5 \( h
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
9 `1 Z; p- `4 Y) VYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
3 X# U$ \4 y+ s4 [& fAre you sure you want to continue connecting (yes/no)? Yes
0 q1 B3 l6 k) b3 j+ @. mHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,1 r. E. H7 y, m7 J. ?2 C- N6 X5 t
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$3 H$ E) p- @& T8 t% s) B
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。; s* e3 V/ K0 B/ C
4 ^8 \, H/ m/ T5 [1; a$ O d* h6 ?. g( D( g
2: U% O* Z7 z4 o* R
3; F- X" z3 ~" s. b/ N3 {
localhost$ ssh -l jsmith remotehost.example.com( t$ v4 W0 [; j9 z2 M6 h/ e9 ]# j5 H
jsmith@remotehost.example.com password:- z, j5 w! P$ p# p
remotehost.example.com$
2 \5 K0 ~; q' Q" u7 W由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:, e8 l& ]- Y" i, o# t- T
7 t; F- U& c" I7 Y& x; s$ P2 ^4 Eo 系统管理员在远程主机上升级或者重新安装了SSH服务器
; o$ q4 ?# J% r! so 有人在进行一些恶意行为,等等。
4 G% Z A t# e5 N- P
+ U$ U' U. N+ f- i& f/ d在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。. x; D8 S0 d( o j6 ^6 p" o* J
, {8 ~7 z- m& i3 y* h$ _5 y
1
! Q7 \+ ~5 d- W; [% Q2" _. [0 V' s H, x' ~# L5 S
3
, y; p! P. L( a9 n1 _4# A! B) w1 x7 q- F
51 D( V- p& F9 l2 r0 g
67 ^0 C) O/ ~6 D9 `1 a1 k
7
# ]4 H* T1 r- P2 A& n @86 x% ?0 t/ H' T/ Y
9" D5 k8 ?# q# I
10, R' F0 \( H& \9 f
11
3 T% h6 F7 ~4 M2 L8 m2 z: ~12" u' j/ X3 ^3 f! |2 z' Z) ^
13
4 i: E d$ A( u149 ]; b. g# z7 H" c, E4 O
15
* o. [4 C- W- i16
9 t2 w4 M2 h: C+ [: z8 U [localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' o3 N' d+ v0 G6 R@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@6 `: M8 k% v- g) ^* d
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
( P; ^0 H6 L pSomeone could be eavesdropping on you right now (man-in-the- middle attack)!, U4 |1 S3 j" j3 u2 S
It is also possible that the host key has just been changed.
, M% O% Q& I$ k! g! P1 MPlease contact your system administrator.! m+ h% Q# M$ M
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.% Z9 [5 X7 o. Q" Y2 z: f
Received server key's fingerprint:# s3 l: A- T' r! E! h% q
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum/ b* b" I4 W- J! e* h; K2 {
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.8 A9 @) o1 y! V9 I9 Y2 d
Agent forwarding is disabled to avoid attacks by corrupted servers.
3 N: A6 Y" s, ^7 g6 x+ K- ?Are you sure you want to continue connecting (yes/no)? yes3 L( Y; K8 x' l @4 T
Do you want to change the host key on disk (yes/no)? yes* A' u+ a3 t+ v3 s3 x7 e
Agent forwarding re-enabled.
4 P/ X: E8 B4 }/ RHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,( [) w+ h' T7 o" h+ y
accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$! E" p9 ~: i* k& p7 ^$ R3 X
3、调试SSH客户端会话
I9 n/ b8 p. ]* {
+ T- V$ L7 X( Y- v, N% A当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。0 m1 \+ K% v, H) S/ J2 \* Y4 t
/ ?4 k/ C9 H% a' {) h/ }! w% ]没有SSH客户端调试信息的例子:& {$ Z0 Y: Z" }3 }
5 ]9 |, k" P6 I9 J
1
) e. U# ~& Q3 P% D# ^ `6 T* y6 g2
& t; t2 \* _' }5 ]localhost$ ssh -l jsmith remotehost.example.com) [8 G: a% V1 Q* R/ ]. z0 A. S. a
warning: Connecting to remotehost.example.com failed: No address associated to the name$ d9 Z* y) C+ C7 p* T* C, q% U
包含ssh调试信息的例子:2 T3 n' E v: T1 p, p) }9 d
' o$ k* N$ y, ~1
: c7 k' s+ p5 S0 z2) \" o( @! m, @% Z" A
39 `; o) B! R5 \* h( x, u
4
, W% e" P4 w: C9 b3 H" f# n5
3 u9 q0 O. K8 G7 v( a' f+ B4 L4 G6; w, P- m5 w, \$ I9 {
75 Z/ F$ v1 G- m* k
8* @0 C* @ K6 h$ B2 K
9
, m: L& W1 y& N6 a10: x1 L7 O( ]5 f, O! k
locaclhost$ ssh -v -l jsmith remotehost.example.com
7 i0 z; X% L6 V& _7 v( Pdebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
6 A3 N) g$ @/ o/ }Metaconfig parsing stopped at line 3.
8 f# l. j' l- l" Sdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
! S: G4 a' w: N' jSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
6 n; \ n" c$ N' gdebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
" s& e1 _, ~ E! l4 sdebug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')* Q# L7 t3 f4 t, y6 U1 p& s
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used) X9 P% d) ~" w6 e" H* X
warning: Connecting to remotehost.example.com failed: No address associated to the name
( o- l+ }5 Y- d* O+ d+ I[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]7 m4 a# u0 n' E" E2 h
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:4 b) F8 p* J) k* E# y
4 i) a* `9 ^" W% A& [/ ?
4、用SSH退出符切换SSH会话
: e8 ^. s, t; [6 Q1 P( X7 x
0 d: [) u) U/ J! b/ y: c8 ?, n这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。" K g1 d# I# X
& S, ]1 n: i. u o2 U: T8 R4 t当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
) J- Y# `) B% h5 P# I( c; @: [ s$ ]) }) u, U( p9 t8 a
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:1 j- `" w; g2 ]& p
& o5 [$ T2 L% ?8 f/ k) Q' h5 v9 \
1.登入远程主机:
2 ~! _& W) w3 m5 \& d
' M: @9 v) R7 ~3 d" p$ x) jlocalhost$ ssh -l jsmith remotehost
/ X; [+ I6 G: g; W" e( u& t! p0 o6 x+ h0 i3 F p" \
2.已连接远程主机:
7 s) Y3 q, v7 I+ D# ?' L3 [
6 E |3 m+ R7 ^) |+ x' `9 Jremotehost$0 ~+ t/ {- q# `$ _
0 g. S* q# T. V- C6 \3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。4 p8 _$ u# Q1 C- C( F
& f) X, b& j" k5 T$ J# H当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
' [ f, T/ P" U2 d0 G& U( T5 |7 J, Q. }3 X
remotehost$ ~^Z : s5 I. j2 F( s" Y4 M/ [
[1]+ Stopped ssh -l jsmith remotehost 6 \; j9 O4 u1 P! ~# F4 t' G4 u
localhost$; r; B, t$ W E4 l
$ T1 z. s4 g# M; v4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
8 D. o; ^0 H. n' g& u7 E2 Q/ R: K3 P5 k6 y' E0 p5 f9 ]
localhost$ jobs 1 Z: `" k3 \: f" C3 w+ ^* {
[1]+ Stopped ssh -l jsmith remotehost
2 U1 v, z! t+ a: [8 l4 q& \- k
: c) d* D8 g7 W* p6 a5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码9 ]. d5 i2 y2 j1 L8 N. \
7 |4 H6 x7 v' a5 _# llocalhost$ fg %1
5 b( V* w: z6 g& |4 h; J. r+ Bssh -l jsmith remotehost ( C( g/ A2 N1 M' r8 s- k: y
remotehost$5 s9 N9 s8 U+ Z6 h2 O
. p4 T# `' _7 N5、用SSH退出字符会话,显示信息
0 ~5 [* L& k e& f a$ U
& J ?3 w$ S2 L) S要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。: x" t, D# R. q
$ i6 c$ C- p- w: q2 h3 s( z: e \9 b登录到远程服务器6 n, L1 A7 x- V Y
9 z. J" M4 ?2 Z- z. vlocalhost$ ssh -l jsmith remotehost
$ O7 H& b+ ~) Q+ r4 p" _1 k* L0 e6 @2 o6 Z- i( N" D) y" ^' `
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息% x: D- E0 H: P4 W3 x
+ w1 H* s1 u; E! U J1; h' M, {, H# N0 S9 K3 c
2
: n7 a& K" `# L* I3 |3
1 K. P, f" D" M, [/ f3 a m+ ?4 E40 a7 F6 d: d0 b+ \
53 M2 \2 D+ d9 ]! x# R
6
7 r2 Y7 M6 c! ]/ O7
5 N( u. ?( j5 ]' y+ S8 n0 J3 _8
; k/ A7 G) _$ [& b7 E9 l! T5 W90 _' g4 V: f' F
10: |2 R5 K( K! } \. d
11
- }* Y5 C/ H( v B( |# E5 s$ f9 ]! v12* ^# K% P. m5 z" E5 i
13' v7 D& M$ H3 `
14( v' d8 N+ s2 i8 p- ]' E
15, X! p( D' k1 C% Z. }
16
% p! a) C8 P5 I3 k# b174 ~5 n, N7 L* g8 V' ?" `
182 ~+ _0 n- f9 }# o
19+ ~. X+ I" j1 H1 H6 O
20
; U- L1 W7 Y6 L+ m21
7 h% E& E' H) N F5 L22+ a. y+ s6 o3 I. \1 h' F
23
& ]" j$ z/ h/ e4 q24
/ p1 ? K/ n, C* q- H1 t. {: Gremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
! e" |: J$ k+ Vremote host: remotehost
6 I% x+ a* `# ~8 r' ulocal host: localhost$ p1 O1 Z* C0 k8 h4 w
remote version: SSH-1.99-OpenSSH_3.9p1% F1 q) N0 v& k; ?5 i
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)' c6 o+ E/ n" u' ]. E7 I- p
compressed bytes in: 1506
+ q( o7 s- J8 p. N5 d4 e# t# o* Kuncompressed bytes in: 1622- J$ r" t; R9 j3 X+ c1 u, Y! G
compressed bytes out: 4997
1 V- E/ E7 C5 q% ]5 |6 d+ L m8 Runcompressed bytes out: 5118& _% c [4 A! e: b6 g' l" B9 `( v
packets in: 154 U' n7 H, I& d7 V" [" u! D6 e
packets out: 24
$ [4 n, Q6 e: G6 c/ _rekeys: 03 x% w: n1 ?- h$ q2 V
Algorithms:
$ ~, S: D' r3 a' W W& h0 HChosen key exchange algorithm: diffie-hellman-group1-sha1& E! l) B; X! d' f7 c+ G! O
Chosen host key algorithm: ssh-dss
4 [; H i8 J& y7 A: _9 \Common host key algorithms: ssh-dss,ssh-rsa9 l6 ?1 G- |+ E4 c) ~: t5 p+ {5 X
Algorithms client to server:) X+ g8 A% ^" R
Cipher: aes128-cbc9 l$ G+ I ?1 Y$ }) i) m
MAC: hmac-sha1
1 m8 B( r% q. X. Z3 ]Compression: zlib
: B3 @; [' o5 S( f6 ^* DAlgorithms server to client:) B8 O$ m1 T5 ~" h# g3 L7 n
Cipher: aes128-cbc MAC: hmac-sha1
! T) S- R3 g6 CCompression: zlib( A) u1 v/ Q- l( y! o# {, Z- S
localhost$ F8 `. f& q* V8 |" V$ V
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |