1、查看SSH客户端版本
7 i6 H( i7 q4 k4 F# r6 l$ D, C, D$ b6 i( ^
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
9 P# c( @. a& ^, E. E7 s
' f2 h& ?; a- r/ v' V$ ssh -V
8 d$ N8 }" L( ?5 tOpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003+ w0 v; M) o7 X. D' W& S/ K
4 e: Z- C ^* ?, h2 D, o
下面的例子表明该系统正在使用SSH2:
2 K* l' [# I" i: |2 F% b
. X/ f9 B' g" J2 Q1 a) P$ ssh -V 7 C! I' _. j. ?2 B; O) e T
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
- [# P B. ]4 P. d+ J4 f, ~- d0 A- M( Z. j' Z
2、用SSH登录到远程主机
$ [/ n+ C) H: o2 S" \0 M/ E) U8 j
. r! V8 V9 I* a( g当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:* [$ |8 I$ h' {& _) t. E) i
9 L/ N$ t1 {' s8 {% Z5 N9 k11 q8 L! `) P1 A3 g! E' C
2
6 y0 ?$ M. l# E3 H4 r2 P3
6 t$ A+ q- I; e4
- ~# n9 h0 w- _# O2 ~. @- c+ ^. t5
N, p" p M- g3 j- c# ~6
% d% K9 s9 ~& ~7 l) @ H" p7& P- `& [& V0 K. C( z. k/ q
8
+ T' @+ a0 o3 {6 \localhost$ ssh -l jsmith remotehost.example.com: r4 S, _, Q+ q7 a# O
Host key not found from database.- e0 m+ V j" ?( y" T
Key fingerprint:6 E3 l ^5 l+ q% \# x* U
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum+ A, s! v' m- j. H' J7 o( X2 u8 z
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.' K% }- f$ \. y* |6 |
Are you sure you want to continue connecting (yes/no)? Yes
% t; V4 a* \! E' {Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
4 m& d, {3 K5 U Vaccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$- I( j: G! @9 j
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
6 a" o' u6 o& b' `: p$ z v5 ?/ U1 o$ i9 c$ E, ]1 ~6 h
1! n, G; I8 ^$ @
2
/ N- l% m) y/ Z2 m+ T8 s32 |$ @, i! d) i7 ]# U
localhost$ ssh -l jsmith remotehost.example.com+ V0 Y* ~- K6 {. C1 I& X% E
jsmith@remotehost.example.com password:( j! y6 ^7 J$ ?' x- N3 S' }/ `. o- C
remotehost.example.com$
8 k& `$ f7 J2 j/ G3 w F( D, v由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
4 v3 p/ m" g J3 f( \/ X2 z3 c5 @6 d4 S
o 系统管理员在远程主机上升级或者重新安装了SSH服务器 + ~/ N; h$ [6 q$ O3 s5 n
o 有人在进行一些恶意行为,等等。
% o- |3 g3 ]3 l# R5 M( ^, ]: g$ J; _3 }# } V9 H$ G- {
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。 X& v/ p6 }) n1 C. k
# \& e% H6 P4 C$ g17 }, F/ s. ?+ b2 ~
2
8 g+ G9 w F7 {, r7 l- r! H' H2 v3! J9 v" s& h$ v% A. h
40 k- m* F0 o' R& y, O+ }! F
5
6 G7 R6 t$ q0 q# l6
+ }3 M j" m& Y8 V( Z, \8 q74 P$ P2 o3 e2 ?8 {& e
8
W- P" Z5 T; v4 C. g95 b$ j2 F- R# U& A7 E
10
* s% j- \1 C7 I- T% n11
$ ]0 o0 k/ T5 v6 r. n+ e12$ k O( W% c6 A$ X5 u
13
! ^0 l% f# q; w14; Z, |) J6 w6 l* c; {
15
0 m( c0 @) E/ y16
2 t0 {- z4 j1 A& Olocalhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' ^2 l0 f, F6 }. o$ ?9 m$ B
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@9 g* V: }! K( l
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
& ?, c$ D6 [2 |7 @ nSomeone could be eavesdropping on you right now (man-in-the- middle attack)!1 y6 a5 T/ O/ u5 O# ?6 e
It is also possible that the host key has just been changed.! v/ H5 {& S& {
Please contact your system administrator.
: `- o# i! T& `5 |! ^Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.# R/ i: t. Z8 T% A3 W# y
Received server key's fingerprint:
& a4 o' O( A" G; G4 i, kxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum3 x8 ?' a$ Z6 Z
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
6 j! G1 y$ k# ^* kAgent forwarding is disabled to avoid attacks by corrupted servers.* B( f; j9 J4 x- }
Are you sure you want to continue connecting (yes/no)? yes
! Q4 V( G+ r" O0 y7 ~3 jDo you want to change the host key on disk (yes/no)? yes
1 \; D* ^) e" c/ [- s8 mAgent forwarding re-enabled.0 o+ ^0 ]0 x2 C
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,9 @1 W5 A+ k3 t) C% y. Q& ~' w% u
accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
" F1 c; N/ h9 l2 b. u, G! e3、调试SSH客户端会话
7 m2 |5 q2 E K$ N3 {0 k( `: F! b7 P' c) U7 H$ |
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
- E% d( ?/ Z, B0 g B, f2 Z2 \# ~6 @! a6 e
没有SSH客户端调试信息的例子:
/ D7 Z w6 v' t+ S; }
) _2 A0 M" r3 K1
( k' @. p' @( }8 j/ h" S8 x21 s3 ^6 H3 x+ z8 g0 n; B
localhost$ ssh -l jsmith remotehost.example.com
# h; V* u( l4 \warning: Connecting to remotehost.example.com failed: No address associated to the name
- N) E- o+ w8 h+ H" D$ I" B6 g, H% |; m7 y包含ssh调试信息的例子:; T3 w2 k. p" `# w8 h( G1 u: T9 Y
t; V8 y( _/ ^5 ]2 H2 C1, H! H! G* J; g! R1 u
2: G5 N' o5 U1 J! V, R+ {9 v. d1 i
31 C9 Z: { s0 U. L: i! i; l4 Y" V
4
/ A# T, v7 S) m* A) M% b5
6 _4 s8 \$ d2 Y5 r0 v6! } |- m* C: w& A6 q4 a
7
( T) o( {: ^# w% T5 m. I6 u, I8/ ~1 I/ T# v) t9 w
9# V; T, V9 f( J. M& \+ s# q i
10
5 c5 j; o P6 x7 plocaclhost$ ssh -v -l jsmith remotehost.example.com' T+ V* V1 e$ ^4 l* c$ S9 Q, U
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
" {* x" ~ r8 r8 }5 O4 `5 V& tMetaconfig parsing stopped at line 3.) W, A! q3 a- ]5 p
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
7 Z: Y T+ T4 mSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
2 i* E. V: j5 q! J _4 ^debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
% x' T$ x) j6 A4 a: Rdebug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')6 K2 o) y: ^4 B) D" i R$ a
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)0 |. V' a* d1 o" ^' v. M3 l
warning: Connecting to remotehost.example.com failed: No address associated to the name4 z, C Q$ S j; N4 J5 z7 ~
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]# y8 b: G& i L
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
& o7 R5 w# t: g% {
" v# X' S5 B1 x3 p/ g4、用SSH退出符切换SSH会话
2 T; u6 v. z; A7 D; S; e( C
/ B! P) Z& r' l7 I0 t% O. f这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。4 ^$ Y& Y6 S5 k% H' Q# n1 N8 j
`# i5 @3 [, k" Y8 O5 z7 J当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:9 V' \) \8 C5 ~) w) `. I
8 p% C, ?* X' O
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
( |$ K0 Z. z8 A' H
$ ~7 n3 p2 \: r/ N* m1.登入远程主机:; Y6 i( T4 y% x* X
: ?3 A' i5 P- S. ^# K1 W2 B) [localhost$ ssh -l jsmith remotehost W$ R- c$ L# h5 T8 v$ A/ D
7 F8 N; n& W# c! d$ j3 H
2.已连接远程主机:
2 \" ]& _5 U( O9 Y+ G1 C9 y; Z3 f9 j7 u3 c' Y# |3 l
remotehost$. h _: W s @9 p4 A) @8 O. t
) K0 J6 b" k C% c `3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。# _& O1 V) I# d$ B5 Q, @1 C1 t
& H8 }( [9 S) a0 U
当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”# l% F( J# H g# [! w
7 v# x( P* z' ?7 z" P( ^# {remotehost$ ~^Z
4 n2 b- O& o$ f+ _# ^) M[1]+ Stopped ssh -l jsmith remotehost
5 Z+ u# R( ]0 D6 q7 J8 w- [+ Slocalhost$
$ B9 W( f6 q- a1 n3 U) A4 e& r8 g1 W& s7 R; ?
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
8 A$ T" b6 z7 w; h6 S3 a! T l8 r6 b! ^+ Q2 p2 p
localhost$ jobs
9 T' N c; q/ s7 B* U! ?! n[1]+ Stopped ssh -l jsmith remotehost9 G0 N: ~* \- j. {* I+ G" q
6 O! w& T: Z5 V+ s7 P8 @5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码& H( {/ O1 u* V4 M6 E) x1 P( ]
! v6 _" N7 _3 @4 O+ Z$ Clocalhost$ fg %1 ' b1 }; G% N( a+ [% @1 s
ssh -l jsmith remotehost : O% M& L0 I5 V# B( r- o6 l
remotehost$' g. N# M$ y9 ]/ g9 u6 Z( y# z
3 O' F: d) Y' M3 x* [5、用SSH退出字符会话,显示信息9 A0 n% R( @! o# A
* C. [) X1 N1 K: A1 h, k要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
; {9 x; u% M( d3 g2 B7 d+ u9 g6 Q7 K$ m3 N' V4 G( @4 ~: u
登录到远程服务器
3 ]2 M9 e- T! ]1 G, s. _1 A8 ^: \8 B2 M/ [$ T
localhost$ ssh -l jsmith remotehost
1 M( H# K& z$ E& M K$ v! X' n' O
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息- a$ F5 O- Z8 j) B. a m
" ~$ D4 m- }+ ~0 W$ o* B1: _' P9 x- V% h: o- @$ ^
21 l0 |! h8 A/ u/ e/ Y% r
3% h1 c/ _- g9 i2 E8 X, x$ h: K! G
4 q6 P! \8 f' r7 v
5- B' w% r& b% r3 w. I
6
5 f9 |& r. R4 [5 [, h7 h75 p* d+ d. p; e! ^) ?4 Z1 V
85 g: N& o3 ~4 {( [/ k. V& |& N
9: f( m( j( f0 F
10& c- j" Q2 P! Y7 S( p1 A1 H
11
/ Y& _& ^' K9 s3 d12- s3 G1 Y; ]! x8 |$ G, a
136 V5 l5 z w: k c% e
14
% K5 \" c2 F% T3 P' E2 ?15" ~4 {! p$ i0 e* ^0 y
16
" t+ q5 ^# h. f* R" i7 p9 b0 O% H17
! K1 W; K5 O* u7 m* F& Y9 U0 W184 A F9 q* U: d6 }3 L$ h6 k
191 o- z6 S; f- n, L
20( C7 \( `* u* s! C1 y
214 b' @9 F7 S4 o+ ?1 q0 E2 v3 C, d
22
m+ b1 t3 q9 `23( S. D6 z7 w+ w, L2 g
24
: W3 F2 @- U; u% wremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]# D1 z4 F9 v9 `( e8 q# ?+ m4 S
remote host: remotehost6 P3 g, y8 Q. [7 N; R
local host: localhost6 {) l# s! a# r M4 y! B
remote version: SSH-1.99-OpenSSH_3.9p13 U. }8 Z5 @$ y- U9 ^8 @. X) V1 i
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
/ ^ Q4 m+ f+ D6 |* @. K; d$ D) Ucompressed bytes in: 1506
6 Q; i; k T0 ~uncompressed bytes in: 1622
6 a! Q0 F T9 M; a$ J+ dcompressed bytes out: 4997
$ E5 r. K( ]& f3 A' |uncompressed bytes out: 5118
, ~( |: z# | z8 @6 k) e/ jpackets in: 15
. y0 N2 R$ A, ipackets out: 24
0 G$ {! Q% {7 ~& I, Mrekeys: 0
3 ^) e5 S" v) T8 _6 ~Algorithms:
% p* K) t, ~- A- J- [0 GChosen key exchange algorithm: diffie-hellman-group1-sha18 Q, W/ w P; ^
Chosen host key algorithm: ssh-dss, U* `: H/ S8 _
Common host key algorithms: ssh-dss,ssh-rsa
$ i, [$ J/ o% V+ y4 s9 A0 wAlgorithms client to server:
1 T6 E" i) M. f& nCipher: aes128-cbc; T+ y# u0 W: \- w4 b- ]! `
MAC: hmac-sha1
+ I, O3 O7 C- q7 F# B/ MCompression: zlib9 i/ V+ ~+ y3 y* h \
Algorithms server to client:
" n: e: n% W% D9 X4 z5 v0 k% ZCipher: aes128-cbc MAC: hmac-sha1
/ ^# s5 p5 a/ \/ E. eCompression: zlib8 \1 }5 Q& A: I7 e& {/ e
localhost$1 ^1 y2 ?" Y/ w( F
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |