1、查看SSH客户端版本( T2 p! d1 {' F# f( m( g
" r2 y5 V; b$ z
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:! y8 Z8 |" R$ v9 n. L
7 F% ?0 w# A* m7 c$ ssh -V 6 y( U) Y& a' b. h
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
" U2 X# f5 n7 b4 K: D
, W0 Q' U2 z( B, S$ V6 [2 {下面的例子表明该系统正在使用SSH2:
- _5 @7 z7 G/ q9 s- ]) D" }! _0 Z0 v/ I% f( m. a& R
$ ssh -V ; C7 g* r8 v# e9 `$ w& V0 A1 p7 S
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu: r% T( R* a( w. j) H1 M/ A
* [* p2 n% w; h/ t& p1 z5 r8 Q2、用SSH登录到远程主机7 Q3 ?, h$ H% ~# z) t" ~ l
0 V( D# I \5 h6 i% h% G
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
E8 o" G! S) x: l3 ^" Z2 p
9 P3 _" w; R. [5 d# l1
$ t+ B! @5 i% i$ N7 N2! C* z# |: P1 y6 w
3! D( c4 ?% B( C9 [ S" S6 a; Q
4
R( ^5 s9 j6 @/ l5# o% d. n. c( L8 m" f% ~& U
6- Y8 j: R$ Y+ @& P% X: _
78 I7 T: _, l" g
82 |5 l6 P' \) V# a
localhost$ ssh -l jsmith remotehost.example.com# ~% k i5 z) D$ h5 R& l" m: q
Host key not found from database.
8 l# N% O! t% a$ A' T' Q9 S/ j! JKey fingerprint:! y. o2 p7 ?$ [' t# k
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
4 y2 _1 h2 l2 h m+ |# ^, F; A6 nYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
2 L' h. j2 m) g# M3 F4 T# VAre you sure you want to continue connecting (yes/no)? Yes
& a" K: K; R, p/ C4 [, FHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,& t4 [6 I0 S/ V: U
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$+ @& C: J: o! Z
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
; D2 W' y) c) ]: ?, q( F0 R$ m$ A) q2 g9 N
1
, _2 e% ]% O* `* u( e; v7 Y21 Z/ i; O6 e7 n9 t2 K* S
30 ]' e5 w; U9 A* L! r6 l
localhost$ ssh -l jsmith remotehost.example.com! g3 E2 V0 d0 O% w# }& T7 y
jsmith@remotehost.example.com password:
- O$ I6 o; o+ E+ E1 M6 Xremotehost.example.com$
, Z: A1 \" m' z( J9 |) [) [% S) l0 Y由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:4 N3 s" X# c1 M7 J* w0 |& u. G7 A
' T' C5 X* O, X/ }5 d) jo 系统管理员在远程主机上升级或者重新安装了SSH服务器 " w. p+ ~% R1 c8 a# f8 a$ I
o 有人在进行一些恶意行为,等等。' Z$ I! h) c" R
3 w% ], ^4 w$ N5 z在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
$ f l$ t2 h) w/ D1 M- Z
3 l/ d# @1 L9 D1# w5 F' g9 ~4 I
2
+ ^3 ^- w$ c/ s: W3& S. m# X3 V! @* H
48 ~- `( D2 O8 ~3 A( K
5
" }5 {' S1 E9 q7 h/ a6) I/ |% R% _, `. |1 Z
7, O1 y4 M7 T- B4 f
8/ z; B4 w* W& F4 e% ]
9
( _# S7 C! o4 p3 W. C6 O( c101 B" \. p- }8 C; l- Y
11
9 ^* U. t; p5 }6 |2 A6 O" q2 E2 Q# o1 e12
) I) Z8 B% e3 P5 b2 p5 B13
& g# e& p7 h7 ^" F; [- U: W" d14
6 d* L) {$ E: }4 h4 M9 l15
) o1 k, `9 L0 N7 P, v16
, q- ]; e* Q2 [7 Q7 [localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/ C2 N" f( E& E+ s
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@! ^. a! a Q- |/ N$ J6 A7 B
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!4 F$ ?4 S3 m, e* m! w, `( `1 u, p
Someone could be eavesdropping on you right now (man-in-the- middle attack)!
4 S. w8 k/ v! J( d$ qIt is also possible that the host key has just been changed.- G5 X& b% y- t; y: w& P7 v ?
Please contact your system administrator.
. ]* d) `3 D3 Q4 q/ b7 g. ^Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
5 m. h2 g5 q2 [. F2 w/ I3 uReceived server key's fingerprint:% G& {, q$ o2 ]; F
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
: U2 ]& N$ g% l8 r/ O- WYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
* j f C/ d/ W5 a4 bAgent forwarding is disabled to avoid attacks by corrupted servers.1 y, G% ~7 U7 N4 q% {+ F
Are you sure you want to continue connecting (yes/no)? yes/ q _& g- {' N# L8 t1 K
Do you want to change the host key on disk (yes/no)? yes# ~, i: k+ }% ~. M) [! h6 U6 i
Agent forwarding re-enabled.% D2 e2 C2 y/ T
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
$ s0 `. y8 S1 X- H7 v, Oaccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$+ y0 i' h3 u1 `- _
3、调试SSH客户端会话. ]* [ o, {- O2 E9 {
2 c- J0 P _3 O' c }- o当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。* [ t! |+ j! h- U( l0 }
% J M$ a" q% k. g7 v没有SSH客户端调试信息的例子:, s4 P9 N* `2 X% E7 N. u6 A8 ]+ u8 r
% t" }# @: C5 L
15 F4 n) n3 r' R+ H7 `
24 l. Z5 R4 B7 k% O# X/ e9 S' S/ b
localhost$ ssh -l jsmith remotehost.example.com- N' f5 E9 a( W* I5 U
warning: Connecting to remotehost.example.com failed: No address associated to the name: C( K2 h3 f/ a( C; ?4 @: ^& x
包含ssh调试信息的例子:
* ]; z% L" `4 B5 s7 l) U4 O4 k- ] {: ~; c
1
5 _; R( p' c0 H4 X4 E( V2! ^5 Y: ^$ {$ r" x) t* r; K9 M
3
" n Q5 r0 Q8 U. B9 t+ T- v4/ M a( o+ W# |1 G1 ~0 d5 [# ~( z
5
/ W& k8 h5 `( ]. x7 z6# L. J" d4 C' M( ?& X: {# h
7
% L6 F' a; v7 p/ r* U5 G/ g8
; f) @. ^1 }) O0 J0 k. V9
R2 D% j0 u& ], U. J: f" K109 R' \4 y+ B* k$ d) E* Y+ X
locaclhost$ ssh -v -l jsmith remotehost.example.com
5 G! ^. c" ]8 P. Fdebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:& |! p% i2 g6 K3 c
Metaconfig parsing stopped at line 3.# @. z0 o; }0 n* k" h
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:9 h( Y$ J* F! C a) G
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.* e( B/ K. o$ y. X
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
% }. l; v( d% @debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
1 S( ]3 v) C3 L/ L6 c( N6 tdebug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
4 T( w y$ |$ [. awarning: Connecting to remotehost.example.com failed: No address associated to the name) {8 k& c3 K* S
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]5 u& h, f K) Q9 p$ i
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
! |$ |2 z2 `, ]. G. A L/ Y
# j2 k* c% V( y6 m7 _4、用SSH退出符切换SSH会话
" T% m8 L: `1 T6 w7 F2 D _% a$ C8 W) L }3 K0 K( I
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
8 o4 n- k4 R# {5 ?, | `
) J2 ?# |/ x# @+ q; z# S* w r当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:- n3 w, w h N
: p0 v7 V6 }2 m8 A9 n) q r
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:* ]* F" p+ \, _! p- N; b! K: ?
6 `( A* k. U. z& L( S+ f5 ?( A
1.登入远程主机:
2 m6 Y5 g( e1 R8 h# i- Y. Z' ^! t( m; L, [4 i
localhost$ ssh -l jsmith remotehost* a4 g8 c$ J6 h( z. |
, n/ Q+ k ]4 B8 d2 Q- x2.已连接远程主机:
5 R( v4 u7 A8 Y x$ e$ K/ j$ p
remotehost$3 z( K( G, r# L; K
0 l3 a$ c. v$ Z8 G4 ~2 b0 y3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。2 t$ T# R9 Z3 c
% V; U3 e7 l) D1 D4 [
当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”# F& T) L% q" y u @
* w, N8 m# ^! A Sremotehost$ ~^Z 4 H2 Z9 d! Y1 ^$ N2 e9 C3 _
[1]+ Stopped ssh -l jsmith remotehost
+ |& X: z4 p& N4 c! qlocalhost$" t3 c1 t7 x. i( _
7 K$ P) C' B' H; _% |+ n
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
$ R3 x$ n! t7 Y3 o+ `* i+ ~- ?# \/ Y$ {4 T) b: K
localhost$ jobs ) C4 H# i6 Z9 l) Y
[1]+ Stopped ssh -l jsmith remotehost
9 V; c7 Z- G2 Z/ c" b' A' c% Z# {8 G9 k$ K
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
6 L2 q2 S/ t# K0 ~8 M/ c
) t$ y% F0 h; Q" y. alocalhost$ fg %1 ! [# G$ f* l+ O' ~# i% q
ssh -l jsmith remotehost
% g. l" m3 P" c2 u, H" C- ?remotehost$# i8 D, y2 t- f* s( N4 `; N& t1 l* x
( r X! P6 x! g$ K: ^
5、用SSH退出字符会话,显示信息
5 @& x# f7 k d& _5 N5 ^0 V, @0 b& R- V
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
! y- Q7 b4 p4 @$ A; m! G/ s- n, Q" j. R) a$ U/ X/ E* }- R
登录到远程服务器
2 {9 s8 i/ L# `4 i
m' z y# s0 a) ?. @, A: {+ O2 dlocalhost$ ssh -l jsmith remotehost% `! R) S. o2 x7 p3 u
) n# b" S9 ~) N/ D( X3 r. |
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
$ X! s0 g1 \0 `( f" K8 D7 S7 x% E l' K& f$ I8 c) T
1
& F! Z6 h) n; ]. Q) R, E% N1 T, K2
3 Y: h5 K3 q5 X: R( K/ I3; d7 h0 O0 U& c2 b* P8 R* p
4
1 J% v9 f% N$ i& J! U5
$ Y% ?" ]. p s( n, B6/ L2 J7 J: v' V6 d
7
( L2 _/ g! c0 a. m) ]8
' n" f6 ^/ L5 L9% |# v8 K( m* j( ?0 a/ P! ]6 Z
10( O' [8 G3 }" [) i9 N
111 O" }( f3 A& Z# y* t
12
+ _7 k1 `# ^; H3 Y& \13
! J' d4 _- \4 q7 n1 j14
4 z u1 O: j7 ~0 y: Z8 C15) d( m" ~, A/ B) [. x
16
6 s9 N8 w) C3 p2 T I6 l8 A/ f17
8 @; b' Z7 M6 Q& n2 @( g18( e: e* v- Z Y& B6 \! E0 ^
19
. |1 O$ Y7 T3 C20) ~4 v) [# o" W' K( |
21
" V$ L' C8 ?# C6 |3 \22
9 @- D. {8 ?$ x8 c5 | V* ^236 G4 ]7 r& |8 U. |8 x
245 s/ w% X9 q3 w) O- O
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
" V, I; y! `- F7 E5 Wremote host: remotehost
0 M' d0 S8 A, B* B9 O5 Wlocal host: localhost% B6 y$ T5 w: A/ {
remote version: SSH-1.99-OpenSSH_3.9p1, i3 E" _- W" z8 W
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
* B5 r9 e6 T( V" x, Kcompressed bytes in: 1506' o$ Z: \4 k$ G
uncompressed bytes in: 1622% [/ V* A" H8 R0 ?: `1 ~/ ?
compressed bytes out: 4997; B/ @& ^ u+ a/ j# ?2 u
uncompressed bytes out: 5118
& {0 Z! n+ t1 Jpackets in: 15
1 J C1 b. c9 Y3 q! r3 q. bpackets out: 24
) W* i# a2 x, A# g6 L6 b z7 A2 V, Srekeys: 0
4 b, \; C. e" M/ a! R2 Q6 k2 H2 nAlgorithms:
" K; y% {' Z6 m; U5 S7 }Chosen key exchange algorithm: diffie-hellman-group1-sha1
. n3 Q$ w0 J6 ^Chosen host key algorithm: ssh-dss
+ d6 k8 C; q9 ?, u1 M( w, |Common host key algorithms: ssh-dss,ssh-rsa4 p: @8 _' o9 K" D1 O3 q9 w1 `- _6 z
Algorithms client to server:
0 d/ {7 x% ^5 c( d$ I" ECipher: aes128-cbc
- c& @8 o- ^0 T1 K2 ]MAC: hmac-sha1
4 W- b6 a% `+ tCompression: zlib
5 K; I3 H+ p2 m0 Z) h, x, a) GAlgorithms server to client:
9 ^$ b$ {% y5 G: iCipher: aes128-cbc MAC: hmac-sha1
, x, h6 V. O# w' dCompression: zlib4 n7 g: S# q9 c! O& T' h
localhost$+ ^) L0 m" O( v" v+ O1 |0 k3 w
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |