1、查看SSH客户端版本
$ I! t0 e( z6 f' A t7 G. D5 x9 Q G
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
, y. a4 t$ I' ]; p7 W' i. ~8 w. j$ L% i
$ ssh -V 7 N% O) f* C! M* r9 B2 K
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003, E3 C) u: c& }/ ^
' o/ A$ K. P3 e3 G
下面的例子表明该系统正在使用SSH2:" [) i0 }3 Y' k2 j
$ R5 f4 Q$ O) a! F
$ ssh -V 6 k* B4 w' {* y1 U- v9 V
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
9 m5 ~- Q, [" C, }% C7 ^
: r& q- @" I4 Q% G/ Q3 o$ T2、用SSH登录到远程主机# Z3 a4 {2 J6 O4 t% k. B! o7 z
X' D$ N1 w) P2 p k4 W& T& k当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
1 b5 N: L+ q C r) [9 i3 h
1 _5 f" m) Y/ N% Z5 ^ }- h0 _6 S1' q" q6 X a. y, ]
2
7 ?2 E6 q! b7 H; R* i7 C33 y3 S/ i* K6 M4 T% K) B8 X, c6 X
4 ?4 ?+ B+ }0 z' ]# W. `( O
5
6 J, }& Z& k: h: q$ G. l6$ W6 M$ G" G) A
7
1 }& F: M- z, E- A- p6 y9 i81 w, W3 h: O0 Q1 Q4 W& G- f
localhost$ ssh -l jsmith remotehost.example.com; L, M( b0 x# C% j
Host key not found from database.
D7 S" y/ m/ {1 y8 f6 `Key fingerprint:
; D T0 F4 K3 b4 a5 Z9 dxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum/ B0 r* k) Y# p
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile." }6 o0 f4 p! j6 g1 ?* S! X
Are you sure you want to continue connecting (yes/no)? Yes; e' V% b% u6 x
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
! B9 ]0 h" V: n+ T2 S" s. maccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$* R7 h N& J7 u4 q
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
% ?# [0 [( m8 o5 p
# m" j7 T7 v( c8 v6 h( K7 [1
! W/ o1 S! {9 [! ~7 N4 P# u4 R21 u! P; C" e, M! D& V/ S
3
6 K7 u2 f* X, ^3 h* X# J( X- Clocalhost$ ssh -l jsmith remotehost.example.com/ k: M& U" X1 l& v9 o% [
jsmith@remotehost.example.com password:
' j' i. n( h2 n9 r8 }# Vremotehost.example.com$4 k2 j4 c& c' }6 ^7 Q/ |+ G+ O
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
+ z% t9 o+ K- O! L% u- o* l- {! R0 {$ E
o 系统管理员在远程主机上升级或者重新安装了SSH服务器 : O7 ~/ }: y# b2 r0 y* a* [
o 有人在进行一些恶意行为,等等。
# ~+ D4 A4 `! G [( z, R* a' Z& \7 [/ c( m) j- u/ I+ e/ b
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
3 I. I) k( s2 U k% T v
+ q6 L p; x; W# ?: P$ w9 N; n1
% f* D t- K' Z1 B8 W2
. f6 W/ `# ^% Z$ Q; _2 G3
4 A3 A7 b1 {+ ~4
# {! H! }/ b. {3 l5
7 U# o: D. M+ p$ N% I1 y6
4 n- k# G1 I# r6 V# ]3 M7
9 h3 `3 u" k# m' ~) o+ y d8 G8
4 X- A- m }) ?3 M8 B% ~8 x' j! c9 |9+ ?; j: m' n( c L8 y2 ]0 P; x
10
6 \. I% J! H0 \$ ?2 c2 s11
v7 ]# ^2 t3 q- {% S. _121 I2 `* _* t8 ^' O
13! j3 I. ~7 y! n# c# ?
14% B# h; r3 F( g- z$ v
15% [& d5 X- @- H6 |: d
16
0 r/ T+ V4 Z+ P3 u8 dlocalhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2 i& e9 |: r5 q@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+ P/ I4 M( I$ p8 F, z
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
7 A1 O! h! S, }Someone could be eavesdropping on you right now (man-in-the- middle attack)!) c5 }6 H, K: h3 `. r8 s1 ^, n8 T
It is also possible that the host key has just been changed.
) |9 P% ~& _' G3 XPlease contact your system administrator.( t! X# K9 h# v& V6 v
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message." J! \7 _, x3 b( t/ v
Received server key's fingerprint:9 \' o d& _& U; h
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum+ {* U: I' _+ b
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
& Z( N) x: f& g: @Agent forwarding is disabled to avoid attacks by corrupted servers.. |$ c9 G0 {+ ^( X" e) z
Are you sure you want to continue connecting (yes/no)? yes
7 }* }2 ^, X7 a) a1 e' `" y2 RDo you want to change the host key on disk (yes/no)? yes( a# p! H3 @: U5 R+ @; W
Agent forwarding re-enabled.& u% ~- q) f0 a/ U2 |
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
# H7 a; J# e7 ~3 K3 O; |+ V# laccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
4 e: I, k6 J$ p* B8 J; n3、调试SSH客户端会话
5 X0 }1 w' N0 Z" Y2 Y# ~
' }4 u8 h! l4 @$ X" h! Q当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。9 n. b1 f7 _, a/ N
" S* t& ^ G4 ?+ K没有SSH客户端调试信息的例子:- K, S4 [7 t; f# O" C: N5 U
- z4 v! Y+ _5 l$ @/ I1
- J* m9 M! V0 Q" {2 A1 y2! J0 R. L- t/ N3 L# [9 @; R
localhost$ ssh -l jsmith remotehost.example.com
+ x0 t: @# r: P+ Cwarning: Connecting to remotehost.example.com failed: No address associated to the name
3 Q ?$ P) B3 ]7 ~+ O! ? i4 z包含ssh调试信息的例子:
0 e; b( J( n) ^& E' w8 Q
/ A6 W3 V2 q. Q: @0 H. m1
4 ?# q) [. D; ~* z- A* v. M2
. _# d' {2 G0 ]3 L- B2 I) d% K: M" f& |0 k
4
) v; P, Q7 c' ?. f: ?9 {# M5
8 o& L) ?7 F* U3 ^/ v4 r! h3 ?6& O9 a5 m6 R5 b j
78 L' Q/ D& V; t7 ~% O- f
8
& ~1 U) d% [# R9
) {2 E0 r+ M& E4 { u10- X( y' e" O$ C! ^5 f
locaclhost$ ssh -v -l jsmith remotehost.example.com% H3 I% y- }0 c# g+ C& @
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
: c) q/ a7 F& |) XMetaconfig parsing stopped at line 3.
# M; a6 U4 I; ^& u/ }debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
2 }9 y. \5 j1 _ |5 k) I( ]5 hSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.6 r4 K) s3 c* @1 J; p+ w5 s
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.& _3 k( q( }/ |% I O" Z$ l
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
- c: ]: n6 J: Y. {3 c. Gdebug: Connecting to remotehost.example.com, port 22… (SOCKS not used)2 p: p3 s+ q% [5 |
warning: Connecting to remotehost.example.com failed: No address associated to the name! W- u# g0 I7 u, K4 t( }7 b( X* u
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
+ B: A' a" L9 H! E! ?$ A当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
& y; y! E$ M& L& |# o' Z" _1 O
7 v3 Z4 A5 d7 H5 R7 q* s/ |# y4、用SSH退出符切换SSH会话
; f9 X8 ^% X8 T1 A# l# E0 Z5 ~4 ?1 x7 ]- T9 r, x" |
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。3 V/ S* _6 U$ |7 C% g
. l% `* g( Z0 `6 P
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:) Z& e' l2 T: g. j
1 [$ m) X( X u当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
/ M! E, S8 C( |1 T$ W+ `3 B3 y
8 K2 F R% M1 j( _: y& X. _6 I! H1.登入远程主机:
6 h: W. D# q* J
0 s7 @) G; _: _+ I' Tlocalhost$ ssh -l jsmith remotehost
' Z& h8 q) Y) u9 r$ m3 s: \- A# f1 E
2 U! n5 b( v/ Y2.已连接远程主机:
5 {( r, U) _2 f! n% b
+ G# V' M% o; H$ z9 |1 G7 a5 kremotehost$
5 ~! a9 @2 F# n8 q' [0 N( k4 I4 t/ q/ W5 x: a1 l6 y
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
6 O! k( t( b, |3 ]- }
/ b& J- v# k3 E当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
* |7 |( o4 F8 h$ e6 D" ]
5 b; I+ }2 m, e* rremotehost$ ~^Z 8 e4 W$ T, z' p) f( Q6 B/ ~
[1]+ Stopped ssh -l jsmith remotehost
5 Q7 f# j6 G3 i! J& c- ]7 T+ ?- \localhost$
T% v7 F v( ?4 j8 Q8 u
% p! T e# U0 A4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
7 L% _- |* ^3 U, k; p( s! u& G* ~; d o( V
localhost$ jobs " F" r' Q: U/ {
[1]+ Stopped ssh -l jsmith remotehost
1 e! V9 b7 p. ]& t
9 G. K8 u# }3 i, x& e0 d5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
3 Z2 [: x6 Q. j4 ~3 }
, Z' a6 H0 w* f7 O: f; i( Klocalhost$ fg %1
; I8 y: a# V: z$ T3 Gssh -l jsmith remotehost : g, W6 H1 i; r: X& L
remotehost$
) C) @( I4 [. J/ o# K2 [ i l$ Z2 G
5、用SSH退出字符会话,显示信息
) m7 n- g. H' i5 s) |7 m7 a6 ?4 n9 n4 ?7 U1 P6 d/ ]0 x
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。8 x6 a8 C# _( R4 A4 p+ N
3 a* @3 J b* {登录到远程服务器
, T2 z+ @* D( a
% G: {0 K; p& ~& Q$ ?localhost$ ssh -l jsmith remotehost
9 P5 O& ]# d) G7 g" [8 t' V3 x% M7 g: \% Y9 |& R, n ~5 h+ A0 O) n. [
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
6 o/ r) F" h, L. d) k8 q) j* H1 K7 c
1: q2 x# A- ?. b4 d% I) ]- K
2
m m# V W$ U32 p- q& O/ {3 P( s8 w6 T2 }4 _% O
4( I, L/ x' V6 B
5
& Y4 m$ O- H. v# T9 W6
% J+ W. V* ~3 Q8 {4 K/ @7
$ P; _ V- h# y9 n _6 ?- O2 n88 T* N s s4 v8 H% v# u& ]
9
; V4 ?; X% d, U' M1 v+ b) ~* L10
' I& I5 x/ t+ e4 W11
$ q) s4 \9 H, R0 A/ g: `12" x5 j- C0 }3 v2 Y, Y7 L, E
13
1 a9 ~$ |9 s6 j. F6 ?14) G# l! \, c6 `2 R( F" }( S4 L
15* @8 G6 s* J, X1 @# k/ ?
16
. K4 H2 \& v6 \0 E17' @ ]; k! H6 R
183 `$ k; M$ \ k
191 Q$ x8 {+ u2 B( y
20% O5 k" O$ j: |
21
' W" ?5 R- [, W22
7 ]+ k: g+ H* \23
+ {: j( e9 r& X9 U" S% x5 C+ r* e246 r4 ^! r/ H' I9 [
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]+ I! D9 V* e/ g" y h5 ?7 e
remote host: remotehost
+ q: h0 v+ Q5 Glocal host: localhost
, S1 J' [4 s2 N* Oremote version: SSH-1.99-OpenSSH_3.9p1; c& s9 T7 ] p* j5 _9 Y
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
! V9 R" I8 e. wcompressed bytes in: 1506
( A7 ]; G( a v5 P6 ^uncompressed bytes in: 16228 S2 C( ~. @0 r% h5 z$ z- r
compressed bytes out: 4997
* a K0 |8 e, |* duncompressed bytes out: 5118
5 o0 r2 a/ V& D9 |packets in: 15; A( C/ z( l% A6 Z G+ N/ r) v7 C
packets out: 24; P1 n+ {" H1 x6 }
rekeys: 0 X7 K: X4 F; h2 J B6 `: l5 ]
Algorithms:4 Z, b+ Q, }* y
Chosen key exchange algorithm: diffie-hellman-group1-sha1/ n! |" z) Q0 @& }: m
Chosen host key algorithm: ssh-dss
% v* Z, T: c9 |- u- r5 x K7 q- |Common host key algorithms: ssh-dss,ssh-rsa6 T4 E( I* n; R$ |% D a
Algorithms client to server: H! H- M- l4 V8 ?8 ]
Cipher: aes128-cbc c! H: p% A6 G6 j
MAC: hmac-sha1
: i0 I- n/ \/ O1 U& r0 DCompression: zlib: Q! E' ^+ H P8 t0 v- t) }; g
Algorithms server to client:5 h. p( o- [1 T. ?( k! }
Cipher: aes128-cbc MAC: hmac-sha1
+ N) I9 ^( e# r' kCompression: zlib1 C( o/ {" W& Q7 I
localhost$
6 A! q0 x' Y7 ]1 R3 D( z以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |