华强北电脑城 龙岗电子世界 龙华电脑城  凯尔电脑

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2447|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本1 |4 `$ f3 T! X4 n1 [

- }* n) c- X* D, h; e5 Z( Y9 {有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
( O) R5 G1 q! M0 ]- v) S5 [  }7 k" Y# A/ L& R# |2 u$ V, v4 T
$ ssh -V 5 E$ ^4 g0 J% G' j" Z
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
3 q" l  s3 F* j# R" C. E/ k; _( s+ r5 }: n5 t0 z! d3 q3 [
下面的例子表明该系统正在使用SSH2:, ^7 ^) U7 P( r. i0 h1 G( @

; E! T  u& ^2 J# j4 P$ ssh -V ( k7 l" ?( f& B1 ?- ]  _9 B( t
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
; i& ^0 _- {: j9 L. P/ ^2 s7 g) a# s0 C5 z: x, R+ V
2、用SSH登录到远程主机; z) t6 i4 N0 h1 K, l9 h; s
) C4 {9 x7 d5 F- x0 T; S+ A! N
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:  w9 \" k- t3 a! v+ n9 A; ~
7 \0 k. R5 W! [4 [6 U7 u2 i; G
16 E* n% g  h* [# a' F4 g! e& Z* @" X
2
+ {& c; q9 [4 o' W, k3
; S7 D3 v' W  h+ S2 p- b( U9 D42 ?" P$ e  o1 I) V4 i+ h
5# U( W* S$ a& A$ L1 |
6
( Y: f- `% }* }& ~. F0 M7
. M) i' n: q( x9 I8
  s  I2 z- S1 t: v% _localhost$ ssh -l jsmith remotehost.example.com- z7 s# Q4 Z" }3 ?7 r- `
Host key not found from database.
9 m6 m" i8 m; qKey fingerprint:& T+ y6 A6 G- _8 _+ g' N4 x) n8 k
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum+ S! d6 I, e* j9 `
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.6 N: G5 @+ N& e5 y
Are you sure you want to continue connecting (yes/no)? Yes
2 F" q/ q" |# \& {$ ~* vHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
) A: S% ?& {* g2 Yaccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
. _2 x+ I; U( C/ C2 [  O( |因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
2 C& ]& B1 G8 V$ U
5 h6 j: M& n( b: v8 N. D' h  v# X1$ |/ M2 |1 d8 |( C
2& e0 |- i  k8 G  h# c
3+ I) _7 K" Y2 l: A4 t/ L) ~
localhost$ ssh -l jsmith remotehost.example.com
1 S7 n- [; Z) w' k! s8 kjsmith@remotehost.example.com password:
) v8 n( m  ?+ |1 dremotehost.example.com$, N7 Q7 j; y  C4 p# p5 |$ m! J
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
# o9 }6 l4 A9 _$ l: o  E2 \/ q1 k% D1 [: z/ M- ?
o 系统管理员在远程主机上升级或者重新安装了SSH服务器
! }4 \& d% C0 u' }& @o 有人在进行一些恶意行为,等等。
9 l+ @6 P" \" X/ m& d
' i: {6 m( y& n- J在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。- T+ J7 O% ^2 `& H( F, n9 s
' b. w0 w- g6 A0 s0 J  S1 X+ ?0 g7 Y
1
. |& p" Y  B* a) L2 `27 x$ z1 P9 O6 p( h  Z) Q
3
  P# P' s6 N' t. M  O1 p4
* z2 ~- q' p9 F& n  r  A/ Z5  {& u( X* {% S8 K# F
6- `2 M* j$ _- B0 \* U+ H) r! B
7
" i* B" T3 O( s/ T% B* x8 O8( S9 D4 u8 l! g4 ^5 ^
9
5 Q  J5 w& e+ v' t10
( d# y8 k" L- p2 a1 I11, b0 c. \# P0 L/ |3 [$ A, T# E
12
0 J$ m. H9 `! r9 B+ P2 w4 }, H6 e13
* ~: [  F/ x( {8 w6 c! Y9 B  V' B# K14
& Z" ~& O; n& J4 p: j# r15- E9 g$ F# E) C6 h5 P1 Q
16
) M* _9 }( W& Z* p" |9 p7 dlocalhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
& |" V8 u' Y( K* L2 }. V@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@6 {6 w$ A) w/ C' G# z
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!" ~: |( h4 W1 e2 q
Someone could be eavesdropping on you right now (man-in-the- middle attack)!
. k; ?  H: C9 T$ P5 ^% g& l: HIt is also possible that the host key has just been changed.
. }# G7 V* D$ z3 _5 E* RPlease contact your system administrator.
0 v% F  _" W1 g  p: Y  A$ mAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.1 ?% w* F, K2 J( q8 V
Received server key's fingerprint:
/ |6 {. F, w: \9 [. Ixabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum. H, v6 O/ }3 a) D
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.+ m- r& m, {* `$ Z: `  K6 t
Agent forwarding is disabled to avoid attacks by corrupted servers.
' Q: b6 }+ g0 v0 _Are you sure you want to continue connecting (yes/no)? yes
, U) w6 U6 j* JDo you want to change the host key on disk (yes/no)? yes% w1 l( J! a7 i/ M+ Z8 [$ h' F, {" i
Agent forwarding re-enabled.
+ m( d! K  X; y& [! bHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
$ q2 }+ `  `$ J1 ], W7 baccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
* e2 a# a6 x4 N! ^: P# U( S+ ]3、调试SSH客户端会话  `5 S% W% s" ?/ X* o, ]

  g7 b- a* K( d' l5 Q0 n当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
1 \6 G8 R$ V5 M8 T. v: u5 @$ w8 J, U0 A% R8 ^1 V0 m4 y  }
没有SSH客户端调试信息的例子:
4 T, ^1 c  V- b/ v& r% F. W6 c% p. O8 z" |
1
  V; x+ Y5 V) d3 C( I* j4 ~2  N5 K3 X! d: J& b1 L: K% b
localhost$ ssh -l jsmith remotehost.example.com  a$ }8 v2 g2 O9 X+ z0 n$ H" t6 G% y2 r
warning: Connecting to remotehost.example.com failed: No address associated to the name3 E. `8 Y$ X; h" Q
包含ssh调试信息的例子:
; _( N  V; u: }
3 ~& y% F0 b7 f9 h3 L: @1 O" s- r3 \" S1) X4 ~) o6 z% O: z
2
8 p2 h/ {6 D* d3
0 C' I) l& Y6 H" i9 H$ @42 q4 K; ^- \* n- ~0 s$ O" L: [4 Z9 K
5
9 Q# l4 u# J1 n! q6
2 _# ^8 M* s: {: |! N. O4 C7" @+ K/ G( U5 M/ w0 s' m/ N0 E* n0 G6 T
8
0 H" R: g  w# A9
$ X+ j. ]* w# B' J& U10
6 A3 B( f4 A6 q( E0 q8 z( \locaclhost$ ssh -v -l jsmith remotehost.example.com
- L0 C' E# `3 N4 _/ vdebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:+ r* [9 a( {; Z: y- I0 }
Metaconfig parsing stopped at line 3.$ C3 M4 I2 M+ v# b
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:- T5 g. u& u; q( a
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
5 o+ p2 @  y4 i* pdebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.3 v+ F3 ^, V) o" g* a2 ~
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')! o2 X3 n$ S! H0 l- G
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used); Z, m* {' m3 |
warning: Connecting to remotehost.example.com failed: No address associated to the name
: ]( k* G% i6 i2 V! E[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
5 h8 @& v2 t# f. ?! a3 P当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
( {9 S& w2 B2 X, C7 V; H5 @/ n$ q
4、用SSH退出符切换SSH会话
& k/ K1 K, m7 ^* X& x& M6 X' \  @# c0 |
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
( M+ z7 O7 i7 U4 o: T; J2 w. L! d  e. j+ J3 \  z- Z" \
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:6 V& h6 _/ M& {1 m. y7 I

% Q) X9 m" g2 T) O$ \" r当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:+ W& X# w$ E1 F0 F1 W% M) D! r9 k

3 H3 J4 C! |. _. J  ]1.登入远程主机:' w2 X! g* Q% O0 [% k6 I! O
5 `- z9 D( @, H/ O2 _
localhost$ ssh -l jsmith remotehost5 j7 x9 N8 t- w& @7 |. e4 C

" p" ]0 X5 Q  v: s. Y2.已连接远程主机:6 x8 p$ O) Y5 k, F

6 ?! p* m" w2 b( }/ lremotehost$- e% [% @3 S. X' J- S
0 F& ]8 T2 e! G
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。) L) [$ X  X0 W7 v

! |, O5 z3 U4 J1 X当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”+ H3 Z. O! h: U4 \$ f# a( }
% \& t' B1 \8 z+ j, S5 u
remotehost$ ~^Z
$ D% P0 G& S4 h[1]+ Stopped ssh -l jsmith remotehost
# j5 M4 u! c% @+ Z3 s- L7 y$ ^% Mlocalhost$
9 J' E* u2 A0 r% X1 D2 e3 l3 i' }
* j/ q% H3 z" x4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:( l5 v) c1 Q) w' A( s: [

! R8 J' ~$ f) k7 R% b: ~; }localhost$ jobs
7 }6 k! y$ ?4 P7 g5 ][1]+ Stopped ssh -l jsmith remotehost
) Q8 G7 E# v4 K4 [% t) \; X# {8 O9 K7 |/ I: A4 D4 t1 |$ |
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
( H( r9 |) q8 e; U8 s! R& [0 l; i* j% h& W4 L3 h- \
localhost$ fg %1
. G" U( X: P& Pssh -l jsmith remotehost
$ b# `* \- i7 a- ?2 lremotehost$+ u) p9 L% t0 F3 f% O1 K1 U/ P) L

8 f" X1 r4 c0 d* h+ x) E5、用SSH退出字符会话,显示信息
/ S. y# @9 D% ~6 U/ b
2 o% e! U1 ~6 V! o要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。* T* [7 O# B( C2 h
- x3 D1 ?/ w$ u  h& R6 ~1 ]9 c; |$ I
登录到远程服务器
4 C) ]) Q9 z; r6 ?2 \
; _9 ]# v9 S( T5 P! _* elocalhost$ ssh -l jsmith remotehost
) B$ e% ]! m: m: {
; R7 B( x: @8 H' p, f如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息# p: e9 g( @) z6 L( I
  u* _! j% e6 _& \# N8 s; Y
1
( V9 T9 S& I! `8 o8 v: P2 E( Z" `2
+ o; B2 W1 Y, V% l2 @- @3' I. L6 v+ {; n5 f
4& F7 J: m) _- G% @6 ~  H8 ?
56 h4 \1 b0 }; Z4 g2 ^( R0 c8 A( |
6
4 g+ }' P9 d0 ^3 I7; y. [! b+ r" q* f: O9 f
8# t* l/ o. m3 k7 U( D4 C, Z; j
9
, Y# q2 V" w  ~2 k: B10
, ]0 a* q) U5 }' S4 s+ \/ V119 o9 W; ~" o7 q
12
# W8 n, b) N# D( N. J13
# c& B5 H0 ?; M9 L0 I, _# c' A14
" D0 |8 V& J% b" X5 e  b150 \9 K1 d! ^, @2 F& u& A
16
3 A3 l  i1 }% q7 Q! ^  Y1 T17
2 f* x: C0 g$ V( f18' Z2 T/ p+ ]  ^
19( q$ ?; C$ @1 l7 K5 Y
205 H  l# s6 X7 e* }0 w4 I+ C
21" H! U- m/ D( s' O1 S
226 `( A+ V  B% A, f1 P/ p
23& H4 ]: I6 e% J& H, E# w9 ^
24
  F2 _4 B' Q# v/ c( ]remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
1 r/ O+ }" b. `remote host: remotehost2 |6 A  F( z. a. |  |
local host: localhost
, K5 D+ X8 ?2 N8 g/ N9 k% premote version: SSH-1.99-OpenSSH_3.9p1# r+ P9 M( E5 ?; D& y! ^
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
  p; A0 H3 R: ]2 h# \- scompressed bytes in: 15066 o! ~. I  R1 `5 v; U+ u( _0 G
uncompressed bytes in: 1622
0 e: d5 Y8 }+ A- h6 Jcompressed bytes out: 4997
3 ]/ U0 |% `# f% H+ x8 Tuncompressed bytes out: 5118- @% N* v. ]: ~9 b' U+ j
packets in: 157 t& ^9 F0 _7 @: @! @0 \; s
packets out: 24+ x& m5 n' ~8 {% T- @2 N
rekeys: 0
2 E" r5 v, D$ t& K0 \Algorithms:
, K8 r  j. J7 Y- s2 \Chosen key exchange algorithm: diffie-hellman-group1-sha1
* R/ q* ^1 B; Y3 m; {8 l7 [Chosen host key algorithm: ssh-dss
- i- _3 }4 Z3 K, U4 ^2 B7 RCommon host key algorithms: ssh-dss,ssh-rsa5 S& U2 A: a& ~
Algorithms client to server:  S6 u" J1 n  w# A
Cipher: aes128-cbc
6 H. g5 L/ b3 ?8 WMAC: hmac-sha16 G5 `, L6 `4 B" F
Compression: zlib
) |6 d$ j  A2 C* @Algorithms server to client:- @- P# o) T# Y
Cipher: aes128-cbc MAC: hmac-sha1
% s1 d5 G% D1 Q" y/ V0 p+ iCompression: zlib7 r- v; q( u" p5 A4 k4 g
localhost$
. P$ E- C9 U: R  N2 U5 |  y) Q以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|华强北 电脑城 龙岗电子世界 龙华电脑城 pc4g.com ( 粤ICP备16039863号 )

GMT+8, 2025-6-17 21:28 , Processed in 0.187527 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表