找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2667|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本
) ~1 @6 [. |3 [( u' M4 h1 @8 x0 O$ ]+ |, f
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:- k' v. l, \5 U1 c" ^) s- @

# K# a6 c# w) m  [8 [5 R# ?$ S$ ssh -V - f: T( m! i% S: x. J9 ~
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
+ v- |3 O4 E' `, w
3 l+ F  i, o! i' v( U3 f* R/ S下面的例子表明该系统正在使用SSH2:  D" ?& B' M8 E4 N' {% S9 a/ R: T% h

, l5 P% Y) F* i+ g; g$ ssh -V ) x8 _! A% r/ s8 H8 J, K1 M
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu5 b9 j" B1 V2 d0 B3 `6 D

$ P- ^* y, O  w: h2、用SSH登录到远程主机% B; `. Y3 H2 Y
- N7 o* s$ @' F+ }6 S( Z+ y
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
! l* {( o. x  ^9 C' w3 e# L; a1 H% s( w0 j
1
, |3 y! S! ]4 R# g) B+ E# b$ z6 s/ e2 I2: g& D* Z1 K6 ~, d
3& H- A5 x5 ^9 `8 _1 P; J
4+ M6 z: E- m+ i( y/ @
5. \9 C: O! T: g9 s* b+ q# d
6
- {! D4 ^9 ^6 u3 K7
- q  w7 s3 _7 i. y! @0 W8
$ p% q8 J" F" S- Zlocalhost$ ssh -l jsmith remotehost.example.com8 U' s. X1 X8 m+ ~$ k4 Z
Host key not found from database.1 v: z7 F8 x, a. t, R
Key fingerprint:( x  m, A7 j8 s: U$ @2 i$ o5 u1 G
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
: f7 o" t) a  p% `3 e8 `You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile." `* ^4 I; E9 [
Are you sure you want to continue connecting (yes/no)? Yes
# {3 j; O' \; s8 PHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
, j! c, \, O, o, D: A1 ]1 w. Daccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
8 U3 v0 \6 `( Y+ s因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。! z, d( p3 H- \: l3 @

. q9 Z1 p8 k9 D' h" s; D+ `: p1
; x) G. ?; p# N* d, F+ z  e, h  K/ N( \2! E# }7 R+ }) e# V+ t
3
) J: X- M+ G- u1 \+ W- klocalhost$ ssh -l jsmith remotehost.example.com
9 y: B& r2 a# D- \4 F: rjsmith@remotehost.example.com password:
2 l) d- e8 k) K8 R# Fremotehost.example.com$
1 v) e' e' e' L& V; w由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:5 Z' `- O0 q( _. A
  B8 k2 Q  ^! a4 o2 s
o 系统管理员在远程主机上升级或者重新安装了SSH服务器 3 X5 `% V! N: j- W9 J% b
o 有人在进行一些恶意行为,等等。
1 Q8 W8 x, P. `; Q: Y( `+ V% `8 ~3 a& K  j6 K$ d- ^
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
$ ~% g5 c+ Z( w( S" ~7 e0 J% J3 h: O' N/ A, n8 b8 g# d
1+ i! g+ M1 _$ C  \) `
2
. v$ b% t% P; O2 Z1 `3: y0 f5 l# ?7 r6 K& y
4
- h- ~4 g" `6 n% j: \" Q54 b1 I- i; g$ U  L7 K+ s/ E) Z3 c
6/ e$ Y, b; k& C9 C) b: Y' S
7
, [% n$ K( ^/ E8
4 V- {" {, ]. T" [: ^9
; Q+ K: ^' B: I10
8 S, H2 A; T7 f! \! o3 O2 b% D11
+ o: M9 {* I  W; [. A126 ?0 J; o: a; J
13
% x9 l( A7 O/ \6 q$ i" [7 n8 N14
4 L. n$ n+ A/ d" i15
" y! g5 n: b; C. L2 N: }16; z: e! f& p) F) I
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
, X2 L, m- |% P: {5 k@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; C8 h- x0 H$ I0 x4 ?IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
  d  w: W8 @0 ]- I4 p4 ?! O+ [Someone could be eavesdropping on you right now (man-in-the- middle attack)!$ p  P/ T* i$ p+ w/ G* R) f; i
It is also possible that the host key has just been changed.) Z( [' Z0 z2 O0 r& O- w
Please contact your system administrator.& L7 q# f& }! r- `' ]
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.7 V% H$ ~, Z* E: {2 e
Received server key's fingerprint:+ V$ W  T, x# n2 @, ]
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
/ b  y9 U+ F6 ]+ _! sYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.: Y7 E2 h! r$ [; R6 I* d# d1 ^
Agent forwarding is disabled to avoid attacks by corrupted servers.
' f3 c# Q) i3 ]- X3 {, d8 D3 r* hAre you sure you want to continue connecting (yes/no)? yes5 G9 `2 G+ L" N( A. `/ y
Do you want to change the host key on disk (yes/no)? yes* e, |7 J6 Z7 A# E. |# y$ U
Agent forwarding re-enabled.2 u1 ~) G; o' O( Y* t& ^' u, F! O
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
! O) K, b: O' Q4 y+ A& }5 m; [accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$$ x9 s( V3 `& ]  L4 S
3、调试SSH客户端会话
" R3 g$ [' c+ h& @, d* X1 D0 L( [( W) k. v5 w3 `1 u# W
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。* I" p# m/ t  @7 b+ h' B

* |+ {+ f2 d8 q( V0 j) T' D9 r( v没有SSH客户端调试信息的例子:
9 g. v: @- A: d  g; d8 @: a3 t8 p9 r/ E1 f1 w
1
" J$ l# Y* w7 b" r+ q  o2
! D& @2 ?* }5 m  i4 klocalhost$ ssh -l jsmith remotehost.example.com
% m+ Q0 {0 K/ i8 hwarning: Connecting to remotehost.example.com failed: No address associated to the name) E- B' n: k$ R0 X) S& L
包含ssh调试信息的例子:! J; L5 J0 D7 i$ k: s+ r# i

& N4 m% E; `/ z3 \& e1; H3 S3 \" ]5 P! z
2
6 g# A6 K1 a2 i8 [7 O7 S$ p7 {; }3. t# O1 k& n6 E- f3 E
40 ~4 g6 `% a5 i* M5 v
51 C1 S! m% p9 d4 L
6  o5 S8 H5 w9 R" @7 r
7) A$ R8 S  B0 B% ~( Q$ z* V
8# j* C" {; T) e2 f6 L& [, r8 w
9
; \. D$ T: P2 R/ a/ D/ X# v10
! `$ V" u2 {* D$ \# qlocaclhost$ ssh -v -l jsmith remotehost.example.com
8 x6 u9 Z8 z0 u5 Ldebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
- M- W9 O. h; O' H* F: D, PMetaconfig parsing stopped at line 3.1 H; I% y/ N* N  Q+ D& R% `
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:5 r1 E$ V# q% x# J0 P. l6 v
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
* s6 |' T4 U1 }6 d$ adebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
) K  G! c! ?* j8 udebug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')3 K9 j8 ]7 r( J8 r# h' I0 o
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
; b( r& j/ _, ~% P- ^warning: Connecting to remotehost.example.com failed: No address associated to the name, I. K0 a, w" n. L
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]1 T  ^6 y$ ~; r1 ~2 M1 R! ]
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:" U% d' B. A5 Z* Z5 @" X
* z; i+ ~( k# p! S# ]4 K
4、用SSH退出符切换SSH会话
/ o0 `9 j8 ?, o7 J% P5 H9 F* @. t
3 B1 ]. F; g' u这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
4 l/ g# F' C9 w& Q3 C
- I$ `+ c! E! v当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:6 m( ]: N8 K3 i2 W7 O% s. g7 k5 n5 A  A
9 x4 c, j. x& R# z
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
/ y; h, `8 I: ^) Z/ I. b: H: I9 R
: u1 L( m0 J1 Q$ v7 s, ~* C1.登入远程主机:5 p( t4 l7 O5 O( B6 k: Z# ]

. K4 T  Z0 o7 \localhost$ ssh -l jsmith remotehost" f9 E" u3 Z1 A. H3 \
! @3 N5 n( O- X1 k( p/ g4 u9 l
2.已连接远程主机:
: v. Q$ f6 p" ^% u# p0 L; ]5 S2 Y& d& J
remotehost$4 D7 z# d7 p0 e# z; G

' S/ k" ~  K( n5 r3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
: l; }% Q3 ?- j, I* ?, g3 E" x
, T' X5 u/ O7 N: R- l当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”# q9 b. L( u+ u  J' t
6 J% J& n8 w, H7 r) P
remotehost$ ~^Z
, d4 n1 ]  P, H7 [[1]+ Stopped ssh -l jsmith remotehost
4 Y; H! H7 ~% ~5 G1 F; s1 xlocalhost$- D- o7 Z  P# r- _
& \% Y" r/ h) K& S* K- S& ~  i4 {/ [
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
/ Y+ _6 }1 Q9 g* Y+ g+ z& _0 G2 w
localhost$ jobs
8 F9 L+ p6 r( A# @. f  a3 v[1]+ Stopped ssh -l jsmith remotehost$ Y8 w8 T0 a% p6 X& ]9 x
# _' d" V+ B- Y$ Q  P( T3 e
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
7 l! I/ X& o: [# G  f, B2 \/ [5 q% U7 S/ N/ o+ F. i
localhost$ fg %1
: C7 y5 @$ [* Gssh -l jsmith remotehost   W5 t# M1 z9 p* |0 J
remotehost$
) q# F9 h: r; Y; F$ \  X' v; J3 V7 I5 |* X1 C* i
5、用SSH退出字符会话,显示信息
  g- ^% c. J* d/ t: Q2 A
- x5 X1 R4 Q# ^( ^' }, S5 G5 w' m要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。2 @5 m% }1 ]5 u9 ~. W, o: y6 A
& b, a' `7 q  }8 v$ A9 f4 N' A
登录到远程服务器
* R' v! A% }4 U- q0 G$ t' ?, a3 K2 i& ~' {, t, G$ v
localhost$ ssh -l jsmith remotehost" c* `! J. B" W6 v* i; E. i
& V- V! P5 V/ x! r' H, J3 S
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息  d% N1 N9 R* t: r' q' Z
; ?- C8 ?2 T5 f" r1 q( K
1
* Q% y: m& c- k0 C, [8 T6 S  F4 s% U! ]2
8 f+ @! |) {9 @" Z3, U* r) ~+ @+ R& Z, c3 ^
4
5 j; J6 \: h1 M2 V7 I3 a2 F7 O% R# l0 q56 \  v& Z$ X; W% b6 a& L* Q
6
  a+ ^' q) x0 ~1 O3 M7 D7 [- X, }7; x. q5 q% t) C
8
1 a5 c+ E1 H( Z2 _; j9
- ~9 h; y% c, v/ E" ^5 F, o8 p4 ^10
) C* \. g: V. u. s$ o11) c4 u6 \5 A) ?, W1 @) j
12% x2 N3 H: h! n* b- Q
13
4 q  H$ p' Q" T14
, R( c2 ?& }! X# I15
; P) s) K4 f0 d% m; E5 u! I. O16
$ r3 K; G/ Z1 a4 |* V17
- m: m  m/ q, W4 ?  v18
6 e# N( ?' b  W9 }: p2 k19
0 Y& P  B) Q8 K" w: A20
1 v  ~( v6 E( _! ~2 p% M21" L$ P: q- @& ^! H0 `
22" N9 S4 @* F6 Q& {# n8 Y- g4 W
23
3 ]2 H$ q: b$ p* W242 Z9 x( c/ x  k4 V- p
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]6 I6 a: t, P+ [- a4 k+ y2 F
remote host: remotehost
  z3 K( h1 g/ V+ q; ]0 Vlocal host: localhost
' f0 h+ H8 j: P# `; C  [' {remote version: SSH-1.99-OpenSSH_3.9p1
8 Y) w" B4 e0 Alocal version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)  I" ?1 L2 q% L( w8 `) t
compressed bytes in: 1506
2 D; W, [) `$ }) a4 `  tuncompressed bytes in: 1622
3 ^* z6 l! `+ [8 }: o* I9 D4 {/ icompressed bytes out: 4997
) ~* l$ O) M. }1 A1 h) _uncompressed bytes out: 5118, i4 J7 O& |4 ~5 z
packets in: 157 C& E1 }4 F% ^+ g9 `( \6 n) e
packets out: 245 z. _6 r/ `" X* A2 Y
rekeys: 0# p: e1 S. v% l8 Q
Algorithms:! H# q4 i: a  y  S3 g: q1 ~
Chosen key exchange algorithm: diffie-hellman-group1-sha12 A4 Z, L1 Q( E, V
Chosen host key algorithm: ssh-dss
; F/ G- z. X+ U. FCommon host key algorithms: ssh-dss,ssh-rsa
0 |! j; ^7 L( O4 k5 u# nAlgorithms client to server:* ?( O8 w4 Z+ R  h
Cipher: aes128-cbc
2 V* c  Z1 s0 N. r5 w+ H9 f4 t, pMAC: hmac-sha18 P5 V8 O: y9 q
Compression: zlib; J7 J( p; x. R
Algorithms server to client:
2 Y- Z% V5 n& S- C. Y$ _8 ?Cipher: aes128-cbc MAC: hmac-sha1
( [/ B( |" t* K+ NCompression: zlib
- h, _2 H# w; R2 y- y/ ulocalhost$
' m, Q9 p# d7 Y以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-14 14:56 , Processed in 0.122143 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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