找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2615|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本
( ^3 V: Z' G) m5 [1 c4 Y* ]: v# i- O' w0 @& c8 Z
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:9 w+ Q5 }3 F$ g" n- I/ ~; P

* Y/ |6 j7 `9 h, }$ ssh -V ' _/ o/ Q* D/ }# j- Q
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
# ?# ^- z! [' T1 `/ G" S+ ]1 |+ I7 n0 G5 |! h' C
下面的例子表明该系统正在使用SSH2:
$ \- h3 [9 {3 v
. Y, V. K  I% S+ K- ?$ ssh -V 5 N% O, ~' \+ R$ q6 W1 B3 F7 a
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
" ^" m- j' ]! i* r+ N( m+ m$ S/ Q) j& G4 c: G9 [) v( ]
2、用SSH登录到远程主机
7 d2 M4 E, ?$ @6 T8 p5 \1 g# J% \) A8 B' [4 p2 o- S& a" A& g
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:8 m7 p3 E3 d& t8 t. G
& [* ?% v+ Z1 k2 @
12 r( ~  S4 M' g- ?1 C
28 \5 v8 s! s/ }; X
3
/ A0 p3 x" h3 ]; }/ Z4
, f2 S! D' U6 Y' a59 F* x) y$ i# v6 U9 Q0 Q" k3 L
6
/ X) c% b; v* y8 }+ i5 j7
5 {0 I3 ~4 _  _  D% h2 B% |) @! Y8
; D+ ]/ r0 f) L2 b$ glocalhost$ ssh -l jsmith remotehost.example.com
6 D3 \  z0 P+ _5 n/ W0 ?9 m* J3 THost key not found from database.
" I( A  I- W$ B1 E" |2 s1 lKey fingerprint:$ Z6 Q) V; w% u. \
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
3 @3 L) }* S5 z% r2 hYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.$ T( B- w! F( U0 @' V( Z0 t& P# n! ]/ {
Are you sure you want to continue connecting (yes/no)? Yes4 y( v0 M4 z: V( b2 u- Z
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
* p9 H7 p  h( d) T9 L* Y' eaccepted by jsmith Mon May 26 2008 16:06:50 -0700 [email protected] password: remotehost.example.com$7 {* y3 s" B# B& e2 w; z  h: d. G
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
5 p. t9 @  d5 P6 h7 D) E8 T3 T
' @3 ^5 S  [( Z) ]& e) E1
- Z  }2 |2 ~) D7 s$ U3 d: }5 m2
6 a; `, n5 ]* q3
% o5 z) F* W& |( ulocalhost$ ssh -l jsmith remotehost.example.com
) h) x7 T; [% a2 r, b: Z* A! D[email protected] password:. D: T0 B, I! f" X
remotehost.example.com$  t" E0 _* x+ @7 G
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
0 I/ K; a0 ~' r
7 y0 I% s. m* u5 ^) bo 系统管理员在远程主机上升级或者重新安装了SSH服务器 ! D) v8 v& R& \# K) Y$ o1 n
o 有人在进行一些恶意行为,等等。
9 V9 Z5 J% l; a4 B
: A3 Y! p) {: D6 @' o4 U在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
  M: B0 Q4 M* ]3 G7 l9 R' e" \) @7 s; L
1$ k6 z" m2 [) Q* j1 X% t& s
24 S+ N' m6 C/ I+ W; x5 E) h
3
* {: |0 \3 c. K  s2 m) H( Y% r4
# ~1 n$ _6 M' S: n" c$ Q' P5
4 X9 \" K. n0 y. y. h# \6" g1 g6 l$ q, q
7
, l5 z( u# K9 K, O7 i8
1 ~4 ?; p8 {# }3 `- e( Y2 h$ Z9+ Q9 g6 I+ t% |
104 F7 b3 w! h5 R3 U! x
11
2 b& @" Y; ~5 ?6 v5 n# _+ C$ {12
6 v6 ~8 e; W$ y8 U13; I. [" n6 T. J! U
14: {" U9 q4 s# r" U$ a
15
+ A) ~" ]: ]- c7 _16
# ]( n# @5 a1 }localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  {( K7 f1 z7 F. v- d@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* `3 [; K2 _9 t/ k! F" N+ P, yIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!) X' N5 {: n1 S+ G8 t( L* S5 x
Someone could be eavesdropping on you right now (man-in-the- middle attack)!1 J- L# y. K% l
It is also possible that the host key has just been changed.7 Z& f& ^2 }5 w9 u2 w
Please contact your system administrator.1 p- s0 R, U/ ^; A
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
3 \, R' n/ m8 D9 j) }) P( J: b7 uReceived server key's fingerprint:' B7 i% ]) m* U, s+ G5 k5 Z
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum- j, i7 p! w; Z8 z  E1 s7 J
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.7 E% J1 _* \' E, C! Q
Agent forwarding is disabled to avoid attacks by corrupted servers.. L& d! D/ L; _4 U7 }
Are you sure you want to continue connecting (yes/no)? yes5 W% U: Q6 ]' t- J
Do you want to change the host key on disk (yes/no)? yes! M" f0 g, z0 o& ?. S. _/ j  {7 q
Agent forwarding re-enabled.. D9 Y  R* b# z6 T+ W7 m
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
! @1 A3 _0 Z% N; h) Faccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$1 |# I3 B- w9 `2 h
3、调试SSH客户端会话" V& K' R: G: c$ J6 I" Q6 Y

$ g. V# R2 G7 ^; n5 p$ R当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
8 n5 l& c9 P8 w% Q. B0 T1 q
/ N9 {! A$ N2 O7 U7 U# |/ Q! @没有SSH客户端调试信息的例子:
* Z/ n- w" k/ i; p9 S% r0 z( ]7 X) v, B
1; x3 g: P: o' h# n
2- q" W- p/ y. U4 e4 d; d# a
localhost$ ssh -l jsmith remotehost.example.com
* V  R% [9 n/ n1 \" bwarning: Connecting to remotehost.example.com failed: No address associated to the name. e1 Y6 g# C. c( e7 w
包含ssh调试信息的例子:
$ l7 g/ r( \: z7 F9 B& [& ?3 m" \; {2 v$ w, t6 F$ G4 j
1
& e# D( p$ H! ]1 {2: W; w7 ]* [' q
31 M+ X3 x" k) y: l: o& ]
42 N* H" \( _* {
5  e, t7 C* g2 j1 F- R9 c5 L. ?$ @
6) J+ w- A: R" a& T. ]) G$ y0 P
7! M+ ^( ]" r' Z3 A9 R7 l' b
8; c7 ?# v; G9 B$ K
9# t& R$ G) q& `" |
10
+ ^; B, S& B$ S: glocaclhost$ ssh -v -l jsmith remotehost.example.com8 d# ^$ S* z# w& D" G! f5 U& D
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
+ z/ ~0 ?4 |6 q. |* i3 UMetaconfig parsing stopped at line 3.( s+ \$ o9 b4 A3 T# ]6 ?
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:* L' P# k5 h! v7 h  ]0 {2 y
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
, M. c. o7 q+ }debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
( X0 m$ N  h/ a. H% Ydebug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config'). g! m  b0 I6 e" O& Q
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)! P2 P- O  m3 |. j- I
warning: Connecting to remotehost.example.com failed: No address associated to the name0 `- O( [5 U5 \# {+ T
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]* l2 T0 }  m2 ]" l  H: K- b
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
0 i  R8 v! [4 E: q/ K
& H# c/ k- o. D; j4、用SSH退出符切换SSH会话6 s% D) f6 ]; o0 h# o

  ?$ U6 r4 p3 `8 y这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
  x& M+ ~1 Y, h/ ?% B
- X8 }) U- ^' U* P5 J9 E: D当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
4 T2 q3 I& M( O$ Q' L. O2 }* S! ^* o4 `3 _8 K
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:4 v2 x: T3 v$ A

9 \* \- c# W' V  V. |1.登入远程主机:
$ e0 P3 R0 f' a6 r, S" I1 e
4 v% d4 ]6 X$ p& X% _! F* z. Jlocalhost$ ssh -l jsmith remotehost
2 Q5 K- `' r% ]2 L+ I( G& E3 U5 C1 U' p8 u, r* r4 V  n% V0 B
2.已连接远程主机:( u5 ?8 ]" c' l2 q0 K% t2 F

$ g2 l8 ?$ e' I# r  ~: W% m: A3 Fremotehost$
+ c! j$ t: i# m- J+ u- c( s$ {3 f5 \( A$ W! Q" g0 F: [
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
# b7 D9 B, u8 M  X8 R$ K4 q) A( b
8 n5 A! y( ?/ [2 O; o! K/ m5 O, d当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
5 S% N; V4 f# s7 a8 R& B
6 c- ~* v/ E, s% z: f, Rremotehost$ ~^Z
$ \- H3 D5 f/ t  h  Z" S  _# R9 z[1]+ Stopped ssh -l jsmith remotehost % P5 K# A# d2 i1 d
localhost$# _( s" P3 |+ O5 v

* H" S+ @; {' P- H; D3 c4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
; }* C/ z3 V0 @6 b, o0 h+ C( y; y
, A; t/ i2 R& ]localhost$ jobs
0 X, \& B( H3 y; q* _; q1 e8 d[1]+ Stopped ssh -l jsmith remotehost
* c. C( B8 G% o8 t, @4 K2 F5 Y9 K6 d4 b3 `. I
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
7 t( m3 H" T- n0 \1 N
2 Q- e4 h) G: L  e( alocalhost$ fg %1
. {4 J% B! u# N% M' Qssh -l jsmith remotehost
; h! {5 A3 g. s3 c! uremotehost$
* j) N! y/ o; j. |$ B
8 b+ f- w. ?: L0 T5、用SSH退出字符会话,显示信息0 t8 G8 d, B: d; i7 Y- N' ^1 r+ A4 t
7 b; e+ w0 Q' |) U" \, l& Q% K
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
* g6 _; Z6 ?7 g& n2 f" p. |
2 L1 I, j  A" m% S! I% a% W登录到远程服务器
; {$ \& x$ |3 u. b  ^4 V) `4 m8 [( N+ e, q
localhost$ ssh -l jsmith remotehost
- y$ h4 b3 s! F1 F
! y- j/ L8 c9 z& ?# _' z如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
2 I7 Z6 R3 p  Q( Z. I+ }1 o: i
3 H8 _  S4 Q+ f+ z  f* ^, `% O0 e; |1
$ b# t/ N- E! r  l( }3 y  Q2
& [! O7 I+ A4 r+ X3; n% D. e% p' R# K$ w
4
* W/ L5 K8 [: ~7 [! R" l* Q5
0 p; S# j, N/ z6
/ W% b- R8 r+ t: [6 \  _4 i7" k- A, z7 d( a  N1 p* n( R$ [5 I
8+ W$ H& [  ]7 n! \9 r
9+ _' O( [: R4 b) r1 G0 r2 v6 _
100 |  G6 m8 p& r6 T, x
11
, X) N' ]: Z6 Y' ?; z12
# ~$ x% d6 _% F# w9 n8 r1 Y' P13
; Q) v) I( Q) z# G7 _144 C, ^. B. `* H7 b$ ~
15
* ?+ ^- v* p  ~1 q" M. X  y16
# ]! Y. |; c  e/ I1 F17
5 P: V) h  F4 g# D. n3 _" ]18
; g4 U8 ~. u. K; \, ~% }" D198 Y; L+ v" Y  h6 s" d, Y
205 G: c1 g8 D, h7 p
21
# O3 ]0 }% N5 o$ V1 n22- @1 ?% Y& d* Q  ]& d
23
. z% |8 p. O8 n% y) Y0 t/ ?& ^" \24. t8 }1 g/ B8 Z
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
9 B/ v! V& v# t; m! d  Wremote host: remotehost$ G/ Y: G. I5 C" D" f' j4 F
local host: localhost8 [# a% \, Y9 f! u' b; `, S
remote version: SSH-1.99-OpenSSH_3.9p12 H  }& t2 s3 n1 d# G# I
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
# G4 l4 J  h+ u$ ?  L. @2 q$ ]' c' icompressed bytes in: 15062 Q: U7 @! x! b0 o3 b9 [
uncompressed bytes in: 1622
- z$ V( p* x& r( ~1 }7 V: N1 [. Vcompressed bytes out: 4997; r7 f, z. A$ Y: S
uncompressed bytes out: 5118
( M' B. Y* ]6 Q% |( Jpackets in: 15% x/ L% I% m, N/ L$ a
packets out: 24
: W& t- C) I; I8 X, T' Jrekeys: 0
1 ~5 i+ g( L' _: x6 ~Algorithms:5 N4 u4 _. h+ J
Chosen key exchange algorithm: diffie-hellman-group1-sha1
2 O: d, V. y; JChosen host key algorithm: ssh-dss
; {* _: N0 x! Z2 nCommon host key algorithms: ssh-dss,ssh-rsa
$ ^/ s/ p$ F$ Z: _6 k; d9 C6 JAlgorithms client to server:2 i+ u' n2 e& R4 E6 V
Cipher: aes128-cbc! F; r( ^7 C7 @, [5 Q" M7 P
MAC: hmac-sha19 D" Q7 q* z) L# ]
Compression: zlib
9 t3 o) F" R2 zAlgorithms server to client:% n, i" c* P+ a8 x. d0 t  W& ]
Cipher: aes128-cbc MAC: hmac-sha1
. `+ w! z, U2 l' b7 cCompression: zlib
; Q' m8 x; I( I5 e, ^localhost$
. y+ S' _# k. r; u$ Q2 ^" i0 `6 ]以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-2 03:38 , Processed in 0.082057 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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