1、查看SSH客户端版本
# K9 C" k9 ]/ g, S+ C; o; s: I+ [* ]2 e* E
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:' s2 X) d7 l3 B& @ z. \
3 y+ p6 |. p% x$ ssh -V ; S6 F; a7 Z+ m4 }1 O
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003: v" o. Z3 q: [8 O: O
2 i4 v$ n$ \% l" y! G) a1 _+ P) i
下面的例子表明该系统正在使用SSH2:8 X G/ x5 {. j, G" O$ {0 i
3 ?, M I3 F, c8 m8 ~
$ ssh -V ( F2 X- K* _) m/ O8 x
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu! K. x$ M! w) T; d% d
1 e" t. I' u7 R: ]2、用SSH登录到远程主机
" P# Z/ N9 d# P8 k( @- y! g
) Q( o+ w5 {" R$ o/ R$ Q' w当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:0 j Q3 y6 T4 A! d! v0 ?
; d5 W3 ^" \6 t# F% D: f- o$ v5 Z
1
& r/ w2 ^6 D, Y: D {% d B2
- J, x( D) c# j2 g% o( E3# h* U, n0 H k) m
46 R3 U7 F7 y7 }3 q
5
1 Y; o$ H' s4 x; R6
1 I! f/ s4 @) ]7
% o8 W D8 A5 R* \7 S8( d$ D1 L6 y& a5 z
localhost$ ssh -l jsmith remotehost.example.com2 u) B, C' y% u# E& l
Host key not found from database.( f9 d `- \; b( W. O' n6 R2 q; `
Key fingerprint:
/ N; f" S6 v0 ?7 j9 cxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
: `8 A; C+ n; jYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.# q4 b! ?4 b8 ~/ q' y: ?! ?
Are you sure you want to continue connecting (yes/no)? Yes, I1 \0 w: l. n2 U) p
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
! S3 e- u* p. P9 {accepted by jsmith Mon May 26 2008 16:06:50 -0700 [email protected] password: remotehost.example.com$
( n* ~ a. \- U1 b( g% P$ J因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
: F$ n1 J3 q" b( h7 g; P" v5 v0 \4 d Z
1
* U) L6 p' a9 f$ q; a2; l+ H, c+ b( a5 k0 H# ]; b3 C
3: N9 o: i/ T. C V3 y
localhost$ ssh -l jsmith remotehost.example.com/ i$ ?: W4 u9 U! U& |! m1 |
[email protected] password:
4 B! H- F. ^# jremotehost.example.com$
+ \% g4 H9 W) N1 ]2 s由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
% y/ ?! F9 k: R/ G7 _2 l- n7 l$ Z4 a
o 系统管理员在远程主机上升级或者重新安装了SSH服务器 ! K( X$ r, O! }' E
o 有人在进行一些恶意行为,等等。" ^! {7 M4 o# S$ u! o8 o
# Z/ z+ l5 ?9 c* f3 e
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。* [0 i$ H M* Q; E. ?- |
8 J4 M1 a/ E# z$ H
1
( ^6 r4 t) T# w, {: ]% y2
3 {+ _+ ?( X7 w) N2 J5 j* |7 @4 A3
# z5 l+ }0 ^& F0 u4# ^6 I# c1 b. S
5+ ~9 U3 d( r9 y
64 m* \9 ?# |- B. | q7 F0 J& m
7
: {3 y) g' S6 G: Z* L3 z9 K( g' w8, T& i9 G( C( ~1 p
9
% X! C2 l9 ~- {& E' D10
7 n5 c8 k* o% ]8 n, r' p3 W$ _11/ }( D) w& r+ K/ R1 J
12
/ X. `" x- p, h( ?3 o5 n8 r- x13; N8 [8 }% W! P) s7 S
14
7 f$ a3 T# u4 ] N) a$ J$ w15
8 ]3 |; d. O+ c4 X9 t; v8 S' C16
8 c% F$ f0 X# m" a5 A3 ]localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@* S5 G* i/ [4 I
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@# U) X$ O+ P9 f" v- O
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!" ]5 x; c) t) C* @2 t y
Someone could be eavesdropping on you right now (man-in-the- middle attack)!
' {' V3 L3 W% K3 s' M) a3 wIt is also possible that the host key has just been changed.; ^) Y( v2 i2 N$ H, z1 e
Please contact your system administrator.
3 g8 `- i2 f' h5 s7 oAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
! Q! }3 }; s7 y. _Received server key's fingerprint:
, T4 r* b% n& I+ G4 X" j$ ?8 kxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
6 g% v4 b5 `3 S+ ZYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
" A6 K: ^# g( yAgent forwarding is disabled to avoid attacks by corrupted servers.& P* O2 _' t% z5 s% }2 F+ L
Are you sure you want to continue connecting (yes/no)? yes
3 R B# | v+ T5 T8 K) VDo you want to change the host key on disk (yes/no)? yes
/ E- B) j; S# w% lAgent forwarding re-enabled." ]0 B7 e( H9 H, Y& N
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
) u( y3 j) s$ R8 _% F- saccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
* }9 k9 G+ f4 t. T+ ]. d2 r3、调试SSH客户端会话" n8 L0 L8 {% q# |& H6 P
! b# S1 y+ t. z# Z) u8 k当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
3 |: V9 @( x8 O5 ^
9 x3 V) b6 x2 ]" @) ~没有SSH客户端调试信息的例子:" s4 M0 J# j0 N' q- b
" M/ Z9 C3 e/ Z. ]% B, P/ A1
# W" O# O( H% l Z22 a+ ?3 n5 I- F% |! d
localhost$ ssh -l jsmith remotehost.example.com
7 Q2 q3 q4 s) P$ Nwarning: Connecting to remotehost.example.com failed: No address associated to the name
: u/ H# Q. V3 E1 _4 \包含ssh调试信息的例子:
& m% w& P# s4 r. x- N+ U
& _2 n, S! n" e6 C; A1/ m& S. U8 _3 \* _ g* |9 ]
22 L& }( n0 W/ ?# }' U
3
4 ~2 Z1 m" C; n& G% ^" O; \4
5 k1 `/ l/ P, M5# ]+ i/ i8 o6 v( ?
6' N# v& l" E* x3 Q; D {) k- \6 w
7
/ t+ Y) }2 j8 p+ x8
( I2 |- ]* i; s, G9( r5 K0 L% h% z4 l, l
10, N: \! c5 u m, X8 {
locaclhost$ ssh -v -l jsmith remotehost.example.com
% ^3 I) f5 C7 }" L/ |/ ldebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
: W. |' V+ a0 C3 L0 ^9 _! R9 xMetaconfig parsing stopped at line 3./ \* V/ s9 f& M' \: }2 N
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
2 ~* N# ~4 W1 t% o+ M- I2 K$ wSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
& K! X4 `! c0 Z+ @ E4 _debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.- ~) f. r1 }. B" x! h- E
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
! F* h& U* f+ D* C* X: W* Edebug: Connecting to remotehost.example.com, port 22… (SOCKS not used)) ]& x( t- l5 {& r" Z% w; k
warning: Connecting to remotehost.example.com failed: No address associated to the name5 d: c/ a7 i( t, g& G- @4 x! @2 z* r
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]/ l7 `' e0 {. k% L6 ^
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
# j1 W0 u6 W# P& q2 P! X' H* \2 d" F, u: o9 { X. W& d; q: w5 M2 {
4、用SSH退出符切换SSH会话
+ x8 \# C' L ^' h
! q/ }6 g' D# {4 p) K; w/ _这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。/ S3 }, u. l& h
* Z& ]* h/ v; y当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
% e y _( a, @: W
h6 s9 G! |: ? f; Y1 a当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:% L2 I: `, G( v3 B
% I; e/ f9 X7 ]- M! M# A1.登入远程主机:/ p8 J" S9 h+ n
1 ?1 F* \1 j+ ~localhost$ ssh -l jsmith remotehost. O3 _5 O( z0 H, b& _# e
# F. A, w" A( a# z1 I( |% i
2.已连接远程主机:
; N! w6 D- W/ i, e: ~: a
. H% L- z7 [; O$ c6 kremotehost$' o% j) T9 ?3 h. s: }& y5 W" Q
8 k7 _3 D: a% P) |/ s
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
$ H% ?1 X" H3 o5 J0 }+ y& }3 t! {5 ^6 G) ~) M4 t
当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”+ t R9 V8 g2 r$ M" ^! Q
; d E6 o X/ z
remotehost$ ~^Z : W- S. q8 O) h8 G9 E4 }
[1]+ Stopped ssh -l jsmith remotehost
; P {) d4 c* y# \/ slocalhost$
}/ R/ o2 Z* J6 u2 O# u; Q+ L8 `) ~2 F% H! I3 D1 j5 Z9 W
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:8 A! W5 L/ t* l
7 W, q* b. n* y" Clocalhost$ jobs
/ }' }3 V w; |6 I3 b" n2 \[1]+ Stopped ssh -l jsmith remotehost
0 G$ o& J% D; X, w8 o( G1 Q3 A
$ @1 [$ H y$ v1 l. Y. l9 j5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码. {9 b9 Z5 A' e; y O p( {
; H5 o( L W4 d* g0 ~2 `5 alocalhost$ fg %1
( {$ M9 ^5 {$ issh -l jsmith remotehost 8 _0 L9 S; j/ J& Q, r) n, l3 K3 |
remotehost$! ?. }3 ?9 ?% h8 g; m, j
: u7 E; ]8 w5 ?" w; z5、用SSH退出字符会话,显示信息
9 Z) E5 H* B; Z! @9 |9 h+ J4 I& i# G) U" E
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
1 E/ T- K/ [" Y' L, H9 n) t/ d, J, F Z, V1 N G
登录到远程服务器4 I' |8 r, ?# Y: w; @
9 ^* V: ^1 n. l/ \4 l) F/ M" f Llocalhost$ ssh -l jsmith remotehost
2 m g5 u T* x4 ^; z; x! _- S' J7 r7 R9 e
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
6 M$ j/ P* W w3 v% ]
+ Z( h6 v" {3 N' a# j" @1
3 {/ Z, r, g: o. v+ u' @2+ T1 B# s& m) }3 ^! [
31 R9 m3 b3 Z- N6 m" ?
4
* b4 q' e' D5 _; @- j5$ o/ S* F# p- C0 S0 o9 K; N
6
+ l5 s6 V9 Q) Y3 p7- p. A& t4 j1 J$ j4 j! @
8
! e" z" o9 K; n; ]5 k; o9- |/ |5 f: l. s) j) _% ~" r) r
107 S! x1 V0 m: e
11
$ b, c! g; ]# \/ T( R7 y12
- i/ ?0 ^% g# u( o% ^$ Q! ^13
: c+ f2 U% D+ z _6 h" Y14
( u% d+ @! |( n& B( T15$ J: q, F; }/ y' v4 d
16 ]6 d( Q2 Y9 C6 [+ [
17
% v+ p0 w a! B! K7 p18! f- w. X% B0 S
19
. l: O- h- }; s7 }4 p/ z208 Y# x7 P }* D, v4 N9 x, I/ }
21% Q* M$ N9 `5 m9 i% T
227 j. z* U' T" d, r
23 J [/ D8 Y9 V& `
24
v: b, x0 K. xremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
2 o2 r2 O; g- R9 Xremote host: remotehost
% `% T1 g& h6 K3 h- mlocal host: localhost1 P. U5 i/ O, L7 a: t9 N' M' I. J @% s
remote version: SSH-1.99-OpenSSH_3.9p1
' H3 |, C- Q) i: @2 O0 A' S; elocal version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)9 @4 l; w" Y; @* D) d K
compressed bytes in: 1506* K& W1 F- m, Z. y. b5 i* i
uncompressed bytes in: 1622
& ~" [/ n5 h! q! w, x `: a0 ucompressed bytes out: 4997 n$ q- ~/ Q; r5 ?0 O" O# o
uncompressed bytes out: 5118- B2 i( p3 E* L6 Q4 l
packets in: 15" j% H; q& O; @& f$ J
packets out: 24
' ? U" `' h2 g9 M# C0 n* D/ x: krekeys: 0# q% d! w8 u- k" g
Algorithms:
* U! C, B7 \& Z' xChosen key exchange algorithm: diffie-hellman-group1-sha1/ i e( X+ W: b8 m8 K% _
Chosen host key algorithm: ssh-dss
9 t4 X4 {2 c X" @Common host key algorithms: ssh-dss,ssh-rsa
h! |+ |; Y- a$ S7 ^Algorithms client to server:
8 R9 G/ @+ k' ?; ?& ?' NCipher: aes128-cbc, v S4 g$ C5 r
MAC: hmac-sha1! r. [/ M" W( L7 Z% V$ o# F8 Z
Compression: zlib
7 e4 m% A; r8 O) f* l3 d! ^Algorithms server to client:
7 C( d0 g# K: @, z3 dCipher: aes128-cbc MAC: hmac-sha1' x- m) f: C2 n5 b- V
Compression: zlib# J8 J9 K D' c# v
localhost$
. J6 V9 h8 m: Y" d' f8 U以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |