1、查看SSH客户端版本
3 H+ ]" U5 M6 H2 b1 u2 e
: w6 u1 Y/ K, z# J* m有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:6 h7 H, X T* S6 ~3 n9 x2 U9 n
: C9 c4 p9 S6 l$ ssh -V 0 m- A+ }7 c. o5 o) N7 d8 ]
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003. @3 i/ _ h E* B
" T" C4 z- E, ]2 d- c5 \4 a下面的例子表明该系统正在使用SSH2:
0 u/ o5 H0 U9 L$ x5 [7 V+ T4 v( `3 g3 m- o% v* _2 p
$ ssh -V
! x; ~6 P9 x D% ~% T% sssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
% n# T, G _9 h `* G; k% k2 P6 n2 x$ i" a9 I9 q2 q6 L& I; f0 u& O
2、用SSH登录到远程主机
/ }6 v, H$ K4 P+ L; G! B2 _' C" z& }- [8 p3 w& R1 Q, ]% r
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:2 I$ c1 ~: q+ U: s$ X. m
" ]/ V& _$ |4 {' T
1
3 |# W9 r' L0 ~: Z25 E* `$ C% n( ~2 N- [8 O6 ^
3' E) i- J$ b) K( \; {
4- w4 t5 Z' f) W: V0 s2 `) A% ]. u
58 l/ L6 O6 u, q! r9 ]
6: ]7 r6 L! ]; m& M3 O9 t
7( D: V' U0 }% Z3 O
89 d x# j6 P, R9 N$ t H- w
localhost$ ssh -l jsmith remotehost.example.com
9 I7 o% M. k6 M+ E: m# qHost key not found from database.* P! _% O- @5 [8 D' F3 u: V
Key fingerprint:
- h* |& @9 f$ Z6 p C2 oxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum6 [% o. I1 `7 U3 ?, z" D4 V
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.' n3 n t" f/ b; v! E% y
Are you sure you want to continue connecting (yes/no)? Yes
" B# T( b R( g5 [3 e7 R v" H5 mHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
& `. u+ c% Z; @0 @! Vaccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$' M* _1 K/ N- t0 V
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。$ u. Q+ @$ b, U) ]7 ^
/ k+ }, x" ^& c7 p% P14 r9 k& Y S; k+ {9 B" C! y
2$ O; U' r4 {; [- K1 X
3
$ J, L9 h7 h. D8 N: dlocalhost$ ssh -l jsmith remotehost.example.com
9 h1 _4 O4 l9 H. g) n! Rjsmith@remotehost.example.com password:
: A9 K8 v ?5 rremotehost.example.com$
* N I( _9 Y! s% C+ M+ k+ B0 N由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
. b2 L: A( S3 L/ T) g) y7 I6 q/ O* M, `
0 p) ~( ~% V9 @" F R% b1 c: Wo 系统管理员在远程主机上升级或者重新安装了SSH服务器 ) H6 L3 H* r! \* |
o 有人在进行一些恶意行为,等等。
7 _6 J( i7 o3 h( \# @7 |2 e" d$ G: x' u
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
/ ], I5 x) G7 W" d) P
! ?0 f- y9 v5 e/ t& V1
" K" M7 M3 u8 l* b- M21 g3 X& J$ a" ]) Q/ A
38 P9 I7 H% G. c* u% F1 @
4- ^ i' y# O2 I: }- A& U
5/ L* y# y/ M5 {2 H- X
6
* l" ]$ M8 i/ n# o7; u/ a3 e" f) M& X
8$ Z- I( f& @" x
9
8 k4 }; U* f$ m10
# u- z" P' D$ q2 |11
$ s( C/ [1 g# F+ B* i, U6 `: X% X12# w m1 K$ T: ^7 h" o
13
! b2 S/ P0 ~3 _14
8 [# l R, j% m9 j) x. g15
; F( b$ W. h$ F; D' b3 a16
- C8 ]* E, {9 ]localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@$ p9 X& H2 }2 H' n) L
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8 M; L) _# F2 \, e, q7 r8 a& p
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
9 m9 g4 | v$ z: gSomeone could be eavesdropping on you right now (man-in-the- middle attack)!
. O2 ^ N, {* AIt is also possible that the host key has just been changed.
* n4 `# A& m) e, T; w% mPlease contact your system administrator.
& h5 Z. }0 u+ @2 x$ ^2 GAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
3 b# i1 @9 Y0 Q3 TReceived server key's fingerprint:
; g+ u" y: Q" N7 j \* J6 Axabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum' f0 Q, z$ E8 g$ _/ y
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.: L1 _' \. |' _; S( d3 U7 x; [
Agent forwarding is disabled to avoid attacks by corrupted servers.
+ ?7 q) c# L; G/ L2 i* QAre you sure you want to continue connecting (yes/no)? yes$ e/ d/ b" L- {
Do you want to change the host key on disk (yes/no)? yes
% D! z9 E3 l- C5 H3 c- EAgent forwarding re-enabled.
5 l! S" T- b1 q" T. dHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
- q# J$ Y" F" B$ |$ S! Faccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$7 o x6 H7 I; T- i
3、调试SSH客户端会话
- P0 V8 F* i2 U( J
5 S0 c! X% z5 g2 U7 o当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。 N! ~3 C/ k' B1 x! R" ]% C; w& d
m& {* t( D4 k( s1 {" ^/ o. z0 h
没有SSH客户端调试信息的例子:
& _( O+ b/ d9 d0 G. a) p/ ]) u+ r( z% I, n2 W% P
1
y: M% h) q* L1 Y& y* Q2. m% \3 C* ], G8 \; N
localhost$ ssh -l jsmith remotehost.example.com
0 o6 T1 L5 {+ j- }; M, o, Lwarning: Connecting to remotehost.example.com failed: No address associated to the name$ ~& g. j$ c2 S: D- }. `+ p
包含ssh调试信息的例子:
z# e5 i, h# _$ y) e& m. i6 J% b/ J, b. n0 R# x
1
( V7 U1 E2 o) ~. N% o2
. y( e: D6 G& F3
9 x! K; \# {; J" T4( O3 Q9 b1 A) j* {
5* {) ~7 O% r' o. u
6+ i- o4 k3 T* B) w# L* g
7
) r2 [7 o+ Z, Z9 m6 \( J, Q! ~8
2 }& Q H7 m3 {9
7 S0 a- W0 |% m/ @- z10
2 p6 S* s' Y) X2 y3 K) N1 klocaclhost$ ssh -v -l jsmith remotehost.example.com
! V1 Z6 n3 B7 ~7 `debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:2 G2 a. q$ G' g" |2 r7 _
Metaconfig parsing stopped at line 3.( I9 y9 w" Q( T5 _) a
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:0 [ [9 ^& r: A! _* d7 K! x
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
3 B" \2 L1 \( K; B1 F4 s% N5 `! B* P4 udebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.# O8 |% B3 n8 l1 Y7 N- I m6 V7 t
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')( Z" Z) ?3 @+ W3 d" r5 |4 X
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)0 e8 c/ C1 z- F& T' W8 h& ^
warning: Connecting to remotehost.example.com failed: No address associated to the name
# ?' R# w5 _+ ?* d[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]/ F- V0 X/ b7 c3 ?- s
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
+ F% w% ^* |6 T; w
6 {1 J0 X4 C9 R; @$ @4、用SSH退出符切换SSH会话
) @' M \7 i: k4 [: T! s- |2 ^% U2 W! k' V2 m: R
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。2 O( Q3 y( B2 B' i( @
2 g, f+ d( f0 e0 b1 z2 f: x
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
: G4 U0 u# e& h5 S0 W1 R5 b+ d- H+ D' p% E
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:! S0 A, @: h2 d7 L, c
+ N; h5 y; K7 q7 Q0 E- T3 H
1.登入远程主机:: B6 }; ^7 W& U2 t6 \
- \! j$ f4 w1 h) @) X
localhost$ ssh -l jsmith remotehost* m* C, g2 b. Z* J: l! \" U
6 ~8 `% h0 ^' A6 D. y
2.已连接远程主机:
/ v3 p# k' L5 M& k
& O2 ]! k! R H0 D" K8 ^1 p, xremotehost$
. W( L4 U3 W2 x- q' K+ D2 ^* K2 y2 S3 k
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。8 e+ s, q, K8 x$ ]2 n' {* L
" ~6 A* l2 `4 [2 Y* {5 [
当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”' V7 m+ K. k5 L B9 }
7 l: n% x% \! W, a1 o1 c% kremotehost$ ~^Z . S8 {8 v; x9 m+ N; Q/ I# w2 U
[1]+ Stopped ssh -l jsmith remotehost % A7 B0 o8 B2 {1 r" Y
localhost$% P0 l( l* e Q: g) v3 ~( f
- f( C; M3 S& G$ [0 I: ^! r4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:6 D" U9 N8 v) h
) x7 T6 ]7 O i
localhost$ jobs 0 e! x7 A& o4 `! i
[1]+ Stopped ssh -l jsmith remotehost1 s/ J3 y' O# m. `9 t; Y2 _
) e3 ~4 o& ^8 W& S
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
. b5 I, F1 x, I1 r( a. G
1 w; N9 Y6 r! g# ~/ J" b* k3 f% Xlocalhost$ fg %1
. |' o+ q1 E# H9 X& A. ^, `ssh -l jsmith remotehost 3 T) o- B1 `( O6 X
remotehost$
2 c, S- W4 I! `( `) a' v7 j, Y" [+ g+ g& v ^# X
5、用SSH退出字符会话,显示信息
/ j& d. }) H x& X( c
6 f, G4 ^3 G5 Z9 ^7 ^要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
* s: z9 F+ b" } o2 g& A& V/ z4 Q9 d" A% \
登录到远程服务器. l0 i( X0 z7 o; s# I
W! q2 j! f9 z$ P- V" v5 `' qlocalhost$ ssh -l jsmith remotehost9 j" ?) M8 F; `
9 v6 N6 f+ v1 E如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
1 d5 w& g$ Z9 ?% D6 {
* i& }$ _8 i2 i+ W ~15 w# M0 ~% v/ K2 l* ^/ i! N( M
2
' K+ h3 s; _2 `0 ^& {5 d+ p. g3
8 ?- n3 F- H5 ]" k5 n7 ~4
, |3 e" z4 N; a" x+ b5
7 d/ t* o1 ^3 |2 z2 a6
( A1 Z, w, f# R. t9 z' b70 {( s3 Y. K4 @: m
8
( k/ q! Z$ C8 d; d4 ]3 y5 A- \90 L7 ^5 t( P" Y# s3 e! R. H
10
# j" O; b7 O* i1 t9 v$ e/ ?11
; N7 o8 j' z& ~. ], {2 j1 X12
4 d j( z6 y/ Y) N5 P/ V3 ]13
7 }; {$ I9 C0 b$ O" G14
4 }& [: l/ E; _15
" K9 ^: _0 z9 z) A16( n& F& ^' {( e' \ p
173 X5 Q' w& q" ]7 l7 _) Q
18
/ F7 ^* T* t+ T4 N196 `8 o0 [) ^/ \+ `) E
206 }& J+ S- U( P$ b
21
: m. \9 |$ a, p" T. u9 X- H22
, t7 Z* C3 q& ?# ], L23# _0 p2 O9 N- R# I1 u2 C1 N
24& C! T0 L. _ ]
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]' B% _2 c' }: o
remote host: remotehost1 g( q0 J& ]3 n1 x
local host: localhost/ v; I3 R+ ]9 `& c
remote version: SSH-1.99-OpenSSH_3.9p16 d: U$ P) O2 R9 _) d2 h. m
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
4 v+ G K: Y* t/ Qcompressed bytes in: 1506% v; ]9 T3 h6 i8 c/ t
uncompressed bytes in: 1622
9 A4 s9 V8 o8 bcompressed bytes out: 4997
1 n8 w/ V9 u2 m9 ^7 K. zuncompressed bytes out: 5118
% ^8 h2 S8 B/ p, i3 Ypackets in: 15" I. @; J/ E$ S8 [; U6 _/ o0 }
packets out: 24- n* R- M+ E# g" F
rekeys: 0
7 b% d# e, r9 h$ NAlgorithms:
' d* ? G" s9 J6 U6 j9 O) `/ TChosen key exchange algorithm: diffie-hellman-group1-sha1
* w- S1 D! B' ~6 n+ l: F. ?) PChosen host key algorithm: ssh-dss
# R* }& ^) e5 b0 ~2 D+ HCommon host key algorithms: ssh-dss,ssh-rsa; Q) q& R3 V( K$ }7 L
Algorithms client to server:
- H: Q/ J5 I1 P- D8 I9 p7 {" FCipher: aes128-cbc! A& R$ h; s# M* D5 M+ W
MAC: hmac-sha19 ^( A1 P7 l: H% {) g0 F
Compression: zlib
3 s0 X& _/ _4 H" t R! ~Algorithms server to client:
# ~4 f% k# {% O( |Cipher: aes128-cbc MAC: hmac-sha1
- {+ `& k' C. O6 c' N' ?: A# O9 `Compression: zlib3 H" y/ Y2 s8 v
localhost$
5 t- m8 E- a2 F% E6 V! d以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |