1、查看SSH客户端版本
$ \* O( j+ a+ F0 n% ^/ e9 S$ j. R% l q! y, q0 v2 B6 N
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:& Y/ R* s* A% Y s; H% u, Q$ p
C! o) X5 z! o. p C$ ssh -V # G7 y6 S) L$ ^+ c
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003/ _7 Y3 F; ]) n. M. y
% T9 j. B$ k3 x5 X; m4 j) q5 L
下面的例子表明该系统正在使用SSH2:% {+ ^3 g. Q$ V6 ^5 M G
0 y* W8 V5 q, U! g7 n y$ ssh -V
3 |# ^7 b& ^ R+ l; ?ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
8 f4 p3 V2 S. b! ^* e5 l, y' ], l& y7 b
2、用SSH登录到远程主机+ X$ L" Y8 \6 Z4 K
- ^" C' I+ r$ b) a2 l! Z9 P T/ t
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
% O6 l" K1 J; @2 M; h
/ S, I9 s/ O9 |6 b' b# z1: N3 @: t4 ]2 Y1 p+ S% o
2
& i+ W3 u. A4 L, K2 E3
0 @ a/ a% H& q. C5 A9 f42 r; c S6 [& Q2 S4 ~( i5 Y
59 q3 [/ c# }+ p; T
65 F& |( J; J5 G
7
% w7 `5 W' [6 q9 ^5 W8
- v# ^, }1 O# X. slocalhost$ ssh -l jsmith remotehost.example.com$ c* `# Z7 x! U3 {/ @# u1 S6 L
Host key not found from database.
& p& @$ K* ~* Y4 @3 b, F4 ^. z+ ZKey fingerprint:) g* \ l6 `3 R, j# I1 D
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
* K* X7 ]8 }- d9 |/ rYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
6 z- P) e( e [4 g: u( \Are you sure you want to continue connecting (yes/no)? Yes
1 _! c* ]( p; x4 m( t8 UHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,5 s: B' q1 _: K6 C
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$, z* V, R y) [$ q8 ], ]
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
) e: ~1 V: u, m" ?0 z. ]( H9 \2 G7 Q8 T C v$ N- E
1" P4 f# \( ^- m8 B( w' K3 a
2( \8 M5 [5 K( U" M
3
' r7 F3 [. M; z- flocalhost$ ssh -l jsmith remotehost.example.com9 o% X6 t- S) A" w
jsmith@remotehost.example.com password:, p$ o0 v8 z; L( j! o* L
remotehost.example.com$
- g5 ?3 {" h8 k$ w, p由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
! Z/ a8 g) B' q3 O0 x5 K
. Z+ l+ w, f) ~# \9 ~" So 系统管理员在远程主机上升级或者重新安装了SSH服务器 % J5 ]% f$ h1 @% C! r
o 有人在进行一些恶意行为,等等。
+ V' G3 {( ]# @' r( ~/ N$ c( p9 l8 l% j) _/ \" }) k
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
9 z' \' E3 y% G$ O, k7 [3 s" O% p2 I! J, ~, Q7 _
1
* c, {3 o9 \! P' n( {1 D2
* y, u, e, M0 Z4 L' T3 m$ ?1 b3
( d8 _' L& E# T2 G$ \7 a41 S) v4 _* X# h. G4 y
5
8 } ~6 D: o, ~- b; r64 E2 V+ |# R, S, f( ]0 P n" E
7( ^% w: \' h" i8 h
8 I; L4 g0 z# \3 E3 d3 @
9
. W( E- t; ^4 h. U10( k5 a* R8 y4 g3 Z5 j8 X9 |" v
11+ s8 b, ?) i6 T
122 O5 h; c3 s. `3 V
13
& M) P1 n# |2 W' W8 {( ]8 o149 R, [! }; A, D2 z, U
15! B$ f3 [ r7 s7 Q5 k7 V
16& _8 V4 N/ R) L# Z1 E7 I9 C$ t; K
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1 w, R6 B9 d# V) l
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+ i; t3 m8 ~! }3 y8 Z
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!3 ?+ w+ O! L: e" K
Someone could be eavesdropping on you right now (man-in-the- middle attack)!5 @+ ~' b' S p3 i t. u. p
It is also possible that the host key has just been changed.1 z3 M9 r1 G! i5 b0 L9 }( M- N
Please contact your system administrator.: l+ A, W/ k) w, P+ D% H' U/ ^) l
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
3 P; R7 `6 P) O1 n# K: cReceived server key's fingerprint:+ i/ {( j5 [# G" P$ p, F
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum9 q& q# F" U/ P4 d3 [. z
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.9 Q# M) b& f2 D! d9 ]. G, @! V
Agent forwarding is disabled to avoid attacks by corrupted servers.
( m! {3 a3 h3 t) U b) \* Z; M5 F" `Are you sure you want to continue connecting (yes/no)? yes
* D& U8 [8 [5 |- o( a+ z; w% j. IDo you want to change the host key on disk (yes/no)? yes% ?1 D. X f) S7 Y6 e8 P! r
Agent forwarding re-enabled.
+ b' F, D) W- x4 y0 A% H) HHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com," N, K3 l' r [( L
accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
% ?' {3 }% q2 Q4 d6 e: ^) ~3、调试SSH客户端会话 p2 Q1 d8 s2 W& V+ E ]5 W
! H7 I. b1 {: Z* I- l2 i+ a% _当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
. }4 m* J9 _0 J# u/ R7 \. f# R& x! n( n" D# z
没有SSH客户端调试信息的例子:" k C4 j( h8 @' A7 C2 N
- g* X$ Y# f% {, x9 L1$ U6 c, `$ C1 C0 B8 G4 U% w L
25 k7 D& h) a( f
localhost$ ssh -l jsmith remotehost.example.com0 r% Z! \( C- r$ O
warning: Connecting to remotehost.example.com failed: No address associated to the name/ R& \) K+ [4 @; A) F" @
包含ssh调试信息的例子:7 [8 H% y# Z3 f6 ~5 v$ i7 w
/ ~4 l6 k- }4 Z1 O9 k/ {" y1! }+ t# O F2 t4 B7 \! A& Q
25 |0 z( u" ]5 N' L/ N2 l
3
+ g+ l6 [( ~3 }; w4
( q0 Y9 s- w: ~; G$ Z5 H5
% Y; t3 H V! o, @' Z" w7 b6# y2 C0 M! T* v) Y4 \
7) `) K3 k8 [' M9 u; R( W
8* B, Q% m* P; U/ b9 l
9
, ~; y; e) U3 a10: t9 O) K# q/ j. D
locaclhost$ ssh -v -l jsmith remotehost.example.com; g( m4 t. x; f) b/ ?$ t/ w
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
4 U, B* h- S: \' y; EMetaconfig parsing stopped at line 3.
. N$ c' K! t7 z$ n6 E8 t- X, Sdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:' b' W; G8 ] k( V
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.; L( y. V; C$ J9 n
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.6 j% M1 {3 j9 w3 N: ^: @1 ?
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config'). ?- l: {8 f6 ]' T
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
1 F G E% x1 A6 O' R0 E) Wwarning: Connecting to remotehost.example.com failed: No address associated to the name, |, e" E8 D8 w4 B5 N6 e# H; y& v$ v# L
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]: W( n3 f- V# _
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
+ Z! G* X5 o7 p% K6 {7 p4 a
5 P* ]7 Q& d: g8 l n1 ^% ^1 M4、用SSH退出符切换SSH会话 \9 u& t" I5 X. R9 T4 Q, m8 U
9 a8 ]6 |' z7 n! O这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
! ~& `8 n4 Y( ~5 l6 i4 Q, |) x3 f- @. A B4 S! E: @: y
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:) Q5 ?$ y, M( L& _5 o# F* k [8 ?
) n3 b) W. I1 X# E
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:) Z; a% ~$ c7 r/ Q, G( z" e
; j; _) f$ C% \6 `( r1.登入远程主机:
3 w; Q& [9 i0 }7 \0 ]% Q1 r2 n) ~% ^3 I1 D
localhost$ ssh -l jsmith remotehost Y9 |6 d* E/ |
: m3 W8 Q6 T9 J
2.已连接远程主机:
U* }( X, E) p: _6 P( ~* i& x
. L( j3 G3 Q' W8 {! aremotehost$% C6 k& r8 Z) }' B
! }. U- x& s' h
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。, x# f: ^) G: X0 a* E
7 x: e1 P7 R2 Y当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
( v+ \* E" C1 {1 W; @
6 w" f. c; ^( E2 S$ Nremotehost$ ~^Z
# C; O/ X6 D4 b3 J! u[1]+ Stopped ssh -l jsmith remotehost
4 z' K2 L. a: l9 e1 jlocalhost$
3 A3 `2 d' s0 z* M. X/ e' w5 N9 z$ {* R5 d
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:5 H! r( a9 j3 \ p: h
/ H4 Z5 w9 U1 H( j% u* Y p4 s5 zlocalhost$ jobs
& T! z9 O, b% ?: y' J) m[1]+ Stopped ssh -l jsmith remotehost
4 x; a$ u" S8 J2 R/ P- _% }9 H" Y: E( }, Y: v8 x
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
/ Z0 V. ]; k4 ~4 v6 @& V5 L o$ K6 k$ c
localhost$ fg %1 5 o4 K( j+ ]# ~3 l
ssh -l jsmith remotehost
# F! Z: A2 s+ `3 r: Lremotehost$
4 \8 g# P+ q0 ]" F* ]0 l- A% X1 W/ f
5、用SSH退出字符会话,显示信息* T" p# f! K* u" V5 @
8 J6 K$ N$ y- `$ i: u; {& Z
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
6 q' G1 b( [5 a- X& O
8 ]/ [( @& w- g# O8 P0 ?; I: g6 M! c登录到远程服务器; W; j2 p% d; p* W5 e1 m; F6 d; C
6 y/ e0 q! z" G- u# elocalhost$ ssh -l jsmith remotehost
! q, V% B" Q5 @$ G9 f9 U" u4 d0 }2 }! m3 r8 o: `
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
% ]3 x' C9 O! h9 ~ ]$ ]7 E7 j0 ^, y
1
9 s5 C) n. E: i: M7 ]6 z22 D, t1 |; a" K$ Z# C! }; L
3
6 k+ h; i; u7 B7 r& [ M4- @/ o6 U/ \8 X' R
5$ W2 ], t% h2 W: H
6
% p" Q+ m. t; l2 \- r' h, s7" [4 j9 t* R0 M6 U6 n+ w& b
89 E& X. D/ B; M7 ]% P& N7 I6 C, ~+ [
9) e, @8 m1 L/ i7 b- W
10
6 q* B; p# p+ r! h7 D11, U+ o2 d' E( Y3 L% W2 T( v, r" ~
128 a6 N8 Z6 m7 f$ a9 l
13
- O5 H4 |0 p: L14
' t. Y# L' ~3 u$ k+ H } _9 D15
9 S, L' E7 D4 a' g8 ~: U& f9 a* @3 V) e160 K5 Q# _' F! E
17
! N! W" s) j+ }( A& [# i1 |' T2 F18
4 g* `! x" u; O `( f/ d4 x# m) k0 k19* ?9 o8 H3 ~) `6 p5 z$ K8 |
20; A5 c ~. t! j
214 u, ? J4 u9 E9 v7 P+ g3 f
22
$ \# [) ? w& ?* ?! H23
v% S/ `8 c9 V) \4 i p# I24" W* T2 v, M: {6 _, m, s# o
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
: \$ e; H7 ~8 _2 B1 }" Cremote host: remotehost
% Z" m, V# Q0 olocal host: localhost
* u$ \) {$ Y$ C: X3 Premote version: SSH-1.99-OpenSSH_3.9p1
2 W1 [; H4 A0 t$ v5 Rlocal version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)0 o0 T: v! [+ |' J2 ]
compressed bytes in: 1506, f" i" b S1 u; E- z
uncompressed bytes in: 1622
$ a8 o* R( o# q- r1 \compressed bytes out: 4997
5 z5 \9 U7 S. e _0 Huncompressed bytes out: 5118
" `0 [" O0 l- K' Q7 Cpackets in: 15
! Z* v! f" g! h5 q9 t2 d' hpackets out: 243 g- r$ P& D. G$ h( k
rekeys: 0% @7 O9 J2 {) L2 p" t3 r r
Algorithms:' i. [/ j3 _( m8 {% m, p
Chosen key exchange algorithm: diffie-hellman-group1-sha1* z" I/ y. j. A6 W) G
Chosen host key algorithm: ssh-dss) a& @1 u4 K2 `* F2 X! k6 f( P/ S
Common host key algorithms: ssh-dss,ssh-rsa
5 O: t( Q( n, h+ @" V* T. L$ }Algorithms client to server:
# v: k" Q/ M7 }1 H3 F5 A6 sCipher: aes128-cbc
& B7 l/ q, I& U1 O) L: E2 B4 gMAC: hmac-sha12 ` f0 t; w* G
Compression: zlib
& l; `# G' h' _$ qAlgorithms server to client: ?9 V) W" P: r+ z6 q
Cipher: aes128-cbc MAC: hmac-sha1
4 t& T5 ~ v9 z( p6 HCompression: zlib
; y* O0 x( s9 S" y2 Q$ Nlocalhost$( z8 O- Y7 L* a4 q2 _
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |