找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2678|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本
  m; C/ F" N( J4 l, b7 Q) v9 @$ a3 W" _! e7 B& Q
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
. y6 g4 ?- L- f$ [4 R' P# u% p) `* t4 v9 K
$ ssh -V 1 F4 N5 ~: ]% e
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003+ H2 S/ W* p  B8 F0 E. |+ i& n

1 ^9 ^  Y4 |: Z4 ^下面的例子表明该系统正在使用SSH2:& J$ t/ S) b3 w: B8 h9 ^$ z+ @4 m

+ A7 v1 \/ b" J  c0 H/ v8 ^' G$ ssh -V ) g9 v( V) j( d7 B4 \
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
4 ^' T! B$ M7 Q7 Z. A$ d( Y* Q1 P) g
2、用SSH登录到远程主机
5 j" p' Y7 F# y4 F
4 A# G6 V9 [7 K6 ~! u当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:, e: N) K! P) f5 k& g2 S
5 T+ b) i" m/ Q5 e6 ]: T; F2 m
1- m/ M. f- z9 l+ ]: `+ {# t
2; J4 f6 D2 E4 z) {
3' N" [' a! J5 b! A; e: ~
40 `; A- d7 V) G. {' ]* q, `9 J
5
' n4 V; U0 |8 m1 E* L! }6
) Z/ K. @3 W! K# [7 C7- [) h% [9 k: g: G
8  Y' H5 y& {. j- o, `/ _% H& ]
localhost$ ssh -l jsmith remotehost.example.com$ X3 w; D) F" T3 F  q6 y
Host key not found from database.
4 v& a5 s& ?% C  R( j, O2 mKey fingerprint:
, k4 ^$ P, t3 Y2 r( v& kxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum% \+ F0 S4 \) F( C4 G( c' L! Q
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.$ l: V( x9 h5 I
Are you sure you want to continue connecting (yes/no)? Yes
1 G7 |4 v, @' h/ |9 [Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,: P1 }5 [% p; N# N/ u4 a5 |* R
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
2 J9 o! v4 Z7 p/ E) l% p因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。9 X# U4 e$ _8 R- @

) q9 D  P2 a8 h4 c. M  Z1
7 T' i5 ]- s6 r4 R2, E! \& I" O( ]3 t! k& q
3
! q" E4 J% F# T& N! \" plocalhost$ ssh -l jsmith remotehost.example.com. |% C& a  W5 F  r
jsmith@remotehost.example.com password:7 y2 N& v8 h9 i9 |/ h/ ~
remotehost.example.com$
/ j1 J. n9 W- v( G由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
5 B" w" h+ ]- s- R" B7 s- O
, C) D8 l9 {* h' _7 z8 X( Uo 系统管理员在远程主机上升级或者重新安装了SSH服务器
6 J' f2 W8 q, ]8 ^o 有人在进行一些恶意行为,等等。! r7 ~1 ~% i) f' y5 z6 ~
7 e) V! u# E! Y7 A: @
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
  ^/ j$ Q+ j. H; |) g. L
/ G* p' l- Q2 i1 {0 Y5 L6 I1 H4 R1( a$ t$ s/ N5 l* {* L- ]6 J  \5 S
23 F, Q7 H  ]+ _4 p9 N# O" w
3
$ v# Y' R/ M; J4 S4
/ [' q# X7 Q; F5/ ?' r! ?  @; L+ _2 {& f
65 t: O- W% v$ I! q5 H
7
$ H" W  ?' z/ ]9 v! ^6 O3 E8: C+ l4 U. X3 G/ u& g- P& h
9' e5 Q/ I0 i( a1 P' t* w7 Z# s7 W
10
' W/ f0 @+ n' Y, t, J  ~& h11$ |0 \9 M. c, m1 j
122 O2 b: K( M; b
13: }5 z% ]( t& O2 k3 ?
14  i: v, J+ E; |2 V
15
& L8 e' P% k- n$ s16' O$ ?$ O! e  q5 H
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@% d! i8 e: l0 O6 E$ M  B/ F
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@6 ^0 c: L2 {* _6 P# r
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
' y+ v. |! @3 q9 J9 _# ~Someone could be eavesdropping on you right now (man-in-the- middle attack)!
+ `5 U7 O1 a9 uIt is also possible that the host key has just been changed.
" u6 W/ D" L8 S$ G% |9 B) B* PPlease contact your system administrator.# @% _( F* \: W, o  P+ X( A
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.# h: y$ x7 u7 `! D9 s
Received server key's fingerprint:3 ]) }5 \! f9 x8 O
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
. A" e8 G4 j) ^# V/ sYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
; h. k) i  M6 l- B9 [9 n+ ~2 tAgent forwarding is disabled to avoid attacks by corrupted servers.
9 l: c. J" v$ S7 V0 XAre you sure you want to continue connecting (yes/no)? yes: y* C& B) m6 q" R. b- L
Do you want to change the host key on disk (yes/no)? yes
2 l) P1 [3 O: A6 Z/ ~; {0 C/ OAgent forwarding re-enabled.
6 J: s4 ]+ X% T9 m$ ?; k, ~$ XHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
  P, O% k( ~9 N5 _* a. A) w' qaccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
6 r  p, R5 J, e1 F3、调试SSH客户端会话( O+ ~+ r, i  l9 R; z$ k9 Z
$ o# e: a  t+ V$ V# i: f
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。5 D1 }, ?* T( \8 h. I

, f& W/ F( U" Z( \7 J没有SSH客户端调试信息的例子:
3 F; d& g$ l+ ~. }9 |  v! ]
2 ]* V. N8 W0 e6 S19 y( J8 g; D+ N- `3 {3 R, s
2
2 a& h: Y7 _" E# _( y; E7 llocalhost$ ssh -l jsmith remotehost.example.com7 w3 n. A0 w3 J  |
warning: Connecting to remotehost.example.com failed: No address associated to the name
0 b2 n* |: {8 H! A5 K1 y( o9 l5 V包含ssh调试信息的例子:
; ?: A' A5 M- o( E$ e: L" W
2 F3 g' }% r6 V/ p1
; z" b& [6 o) q2 e2 [5 x; i2 C3 f2
0 j1 ?: l3 z; b( s8 a3
+ l# _( n& s2 x. P6 i, x4. t5 F& h* l1 r# ]+ e  _: P0 e
5
! k* ~- q1 f* R# a67 q  ]' U+ W* O6 H: S6 O
7
* W; O& M7 J( k( u81 C+ W4 r$ f  o: ]
9
* S3 w' h. [* v: ]10
& ^6 o# a9 `: L  X- b$ Flocaclhost$ ssh -v -l jsmith remotehost.example.com" _5 V. t& Z4 i0 F+ T
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
! a& Q4 N! D3 u$ W5 tMetaconfig parsing stopped at line 3., Y0 `+ P4 ~8 c9 R! M/ S. F
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:) h& \. A0 `0 g& w) M
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
( r$ d. P# Y) l/ k7 @! Z* ndebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.: X8 I) ?: w6 O: H& k& I
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')+ u2 ^9 P  I1 [& `
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)7 C4 E# _4 c/ K1 _6 e( G- o
warning: Connecting to remotehost.example.com failed: No address associated to the name/ _& g5 c  S7 p2 W" p
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]; E1 V2 [! [' c# {4 E! {7 O
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:' y7 {- B2 ?2 c6 K0 `
6 D/ d$ d# ?3 ^/ n0 Q5 z
4、用SSH退出符切换SSH会话) a2 e! c+ j5 u+ j3 z# x7 ]
! {& W4 y9 s4 i( n$ t' N
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。" Z9 ]- K$ i1 R' M' K5 f8 M
; ]. b6 A% i* S% ]1 a+ R
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:1 H) L& v& b+ P. H
; p9 V) C* M, X! ^
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:1 U/ t5 \. W5 q3 ^

2 l) A" z7 J+ \- r) Q1.登入远程主机:
/ ]% F, N. `1 m8 O) @, J7 Q% T9 \1 {5 h$ V6 I6 ]9 l
localhost$ ssh -l jsmith remotehost0 s& Y" O5 h9 V
2 A4 U7 \  U2 M2 g
2.已连接远程主机:; }6 ~, v; f. u9 K" h
0 \1 b& `2 I% p: Z9 T8 |
remotehost$
$ t" {* ?4 R4 j! a: t! k  H- G4 F- |7 S1 |: P- B$ K) P; v
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。) F  Q7 q; ^3 l. a& n0 P+ m5 ^0 ?

' Z/ g" \* w2 c/ k; K& }+ Q* p1 U( a当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
0 F2 \8 e0 N* V8 f, ]/ s9 r$ V' E3 v, Q0 Q
remotehost$ ~^Z & e2 l6 d7 ]1 u" I+ Q% j
[1]+ Stopped ssh -l jsmith remotehost 4 X. N( o5 ~& F( _: e- i
localhost$
. K- ~+ ~* z5 [% C9 O& P0 V9 i0 [
5 Y  P$ v+ c5 v1 D3 F7 S. x4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
; F' X6 Y! W+ D7 K' T5 M) s# i  w  Y4 f8 I3 K
localhost$ jobs
# o8 _7 n0 T! M" l/ J. ?/ h[1]+ Stopped ssh -l jsmith remotehost% n3 X- ?% j& H
' ]& t  d4 ]6 T
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
- C! w2 W( g  W9 ^# w: }) i, M- }) `/ \' K* w
localhost$ fg %1
# i7 }  ]9 F) |0 X: Y0 R* Fssh -l jsmith remotehost , X  q( t1 W2 O3 s  n' k- n
remotehost$7 O$ X# r& z. [. t' F* ?
5 u: J3 @6 x1 H! |
5、用SSH退出字符会话,显示信息
, z3 k, {4 z. Y+ S& [3 Z  d' C
" X; f/ [9 M; ^! i( t要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。: \) A0 ^# n0 _: \% r. M

4 `% D3 W9 b6 Y  x/ @3 U* l# @+ p. h& B登录到远程服务器
* w0 ]. c& }& N( c' g- k( w
& i7 q; \. ?4 v/ \: n/ _5 h. Hlocalhost$ ssh -l jsmith remotehost
% n" r  ~3 `! I+ ]7 t/ ^2 Q" d
  B0 i& _; n6 m' K4 e: d+ D1 G如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息; P8 E4 z5 V: V+ y5 ~

3 v; s* @0 S& B14 Z/ P1 W: g  `
2
0 k0 D; `  \, r  G3
$ r: z" O6 Y/ O; {4
1 {4 r. x) j! q53 W, H# _1 z* S1 F" c
6. E) a9 v! d  U, B' X, X, N
7+ K" C5 q6 `# U& N" r1 I: w
86 t: C) f- y; l6 H) I- R" W6 {" O
92 B# N1 e5 q$ L4 _1 Y9 A  I8 h8 P
107 T9 P2 o, h1 t9 h7 L
118 ?4 ~( s+ d5 b$ O
12: K& t+ U! H6 Q+ N' k# [3 |# z
13. a8 i. `, P, a% L3 T2 f) B7 C
14! |. Y3 n  G" e) E- B
15
; I0 H1 i2 Q9 ^' {# V16
8 g( A/ k9 ^1 ?0 z0 F& ]9 ]178 G6 s9 K2 _" O4 A( E4 M6 J
18
2 W/ M8 T9 c- Z5 x2 j19
: _& J. @4 V' w+ q9 O, `20
" }/ A: p7 I5 f5 Q3 ]21
+ N' u$ n* @/ r2 l6 N22
  Z" ?3 {0 u( D* c23% C: G5 N2 x7 P, P' Z! M
24% K5 t9 N6 x9 D
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
4 A  f& K* ?' Tremote host: remotehost! ?' A( t  m( V$ p2 H' j
local host: localhost0 ^- n) M6 p( |( N( o: j5 M
remote version: SSH-1.99-OpenSSH_3.9p1% \1 C& [; H" q6 Y
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
" }; [% u& E2 {1 Y; I" q6 Acompressed bytes in: 1506
# q7 M" r! {; H, L! [0 _uncompressed bytes in: 16221 o. ^  A+ i% D
compressed bytes out: 4997
9 h) K' f3 |( f' x; c" Xuncompressed bytes out: 5118
: V' {) n& E8 f% Y" ^packets in: 15, e1 x8 l# v/ x( v: w# ^5 J9 }
packets out: 24  N* k7 s1 X* U( ~/ `
rekeys: 0
2 u4 o' S0 w6 L% i# a: W$ ~Algorithms:. r% e" g  L! d+ A& Y
Chosen key exchange algorithm: diffie-hellman-group1-sha15 D( @; M0 s, c7 G* a6 j% }% [
Chosen host key algorithm: ssh-dss3 B  Z8 J( A/ O/ @2 }# O3 n
Common host key algorithms: ssh-dss,ssh-rsa' a/ {& v. k$ n' o1 R: B, x
Algorithms client to server:
5 \' [  P2 t, P; m* n8 VCipher: aes128-cbc
* E! i. a+ k" C6 S* |MAC: hmac-sha17 ~- a* L; }  f: K5 x
Compression: zlib
! C8 `5 V5 R4 A& ?8 \% KAlgorithms server to client:
% @$ @+ ~$ o5 q/ q! Y. \5 g& ZCipher: aes128-cbc MAC: hmac-sha1$ p) B+ Q! V) m
Compression: zlib
, L9 X. s" ?8 N: H6 W$ ^localhost$
; U6 v( h: d, x( W* ~" y以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

QQ|手机版|小黑屋|赛格电脑 华强北 电脑城 南山赛格 龙岗电子世界 龙华电脑城 沙井电脑城 松岗电脑城 pc4g.com ( 粤ICP备16039863号 )

GMT+8, 2025-9-19 05:17 , Processed in 0.087814 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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