找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2706|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本' f4 Q5 C+ M/ b

& b4 z& ]4 ?1 T; y, T有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:; U9 q- x5 c/ s
+ F6 N8 p) g5 n
$ ssh -V
, [5 n' X! O9 J) U' `/ o: ~9 ?OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
: o2 Y% [$ ^( l/ [$ t: h
& V$ B' E$ X3 n: _* z7 L+ p' `下面的例子表明该系统正在使用SSH2:
6 S. P  C' U, a* B+ \3 {$ M
) L8 g- P7 T; q" G; A$ B$ ssh -V " T, h3 D( y* x. d7 G  b8 L
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu# U5 W. W- u' ~% T( ]

7 ~: [0 Y% I) v1 N. y& X2、用SSH登录到远程主机2 _, {% M) C- {8 L$ {
  {2 t' u) x% V
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:9 z+ Z( T' U# ?1 z8 x

% O8 n  N2 ~8 ~$ F1 Z1
, m& T; Q' m* G" E5 ]& x2
$ V) m9 B4 L8 O3
; t0 g# `3 E' H: |# q7 `/ U% L6 d4
6 R, O. L7 l8 m5: K. }0 ]: S* X, [/ g% x
6
: e2 h$ S& K  I8 ^$ H* o( Y77 i: _$ ?4 L4 r' D+ I' z5 l
8
0 Y' n- i! i  t( m& N6 slocalhost$ ssh -l jsmith remotehost.example.com
; T6 q6 p0 Z+ n3 \7 M% uHost key not found from database.' g4 ]# C# F- x) E. B6 X
Key fingerprint:
4 G% L, W0 y+ j+ {& |, zxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum# X) x$ M$ j7 \3 V' Q
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
# \! Y' K6 @: BAre you sure you want to continue connecting (yes/no)? Yes# x2 m, d# {; t; J+ I" z
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
. v5 o9 n& j* s# P5 aaccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
) H0 P( L( C% M* o) A' s因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。) Y8 B+ |4 T* R- Z. T* L
2 F, I; ]3 s3 L2 O8 V
1
$ F  z( g7 h" [, N% F* u4 q8 C2& U! r( f1 j( o9 j
3
  ^& `  G) h4 b/ I+ ]9 flocalhost$ ssh -l jsmith remotehost.example.com- r$ Z3 A5 C- q+ ^) O; O
jsmith@remotehost.example.com password:
2 n0 G" _3 F: Z4 qremotehost.example.com$) h7 J5 \0 d; y
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
$ V) c) S. D# t! j4 P4 _! N- j, \. P; L: n8 X* m
o 系统管理员在远程主机上升级或者重新安装了SSH服务器
! N4 f& N" s9 f5 _$ _3 Lo 有人在进行一些恶意行为,等等。
' t! [3 _" b, V/ g; i0 U2 a  M" d- z" T' @
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。$ y$ l8 Q* H0 R' @( [

3 L: W' W; V' d; }  K5 |1
7 s" L" a+ X$ S, k# ~$ \, l/ i2, a, ^% M! [9 t8 w: S. J# u, G
3/ h3 a" X3 y! J6 }+ n, J
4
0 w3 v; m9 _! `8 k/ f& m  r& s2 K+ ~/ \5
: g+ a. J0 `# P% v  ~7 q$ z) P6' K* L% b/ U  |  S( X5 T" P) x
73 Q1 `" T' r- |5 c% ^/ E4 W  c
8
- ]6 q, a2 z- N, q9 Z' `4 a$ y9" i( j3 _; _& A; A
100 E2 j2 i" p6 v$ z: T
11
! ]; ^& U1 f8 e5 K- M0 s12
4 o+ i/ t7 c- \  `13( n& |- f5 K" ]& d- @
14
' ?* H& L/ {6 v15+ Y$ k5 f! J: W+ }6 v* g3 p
16
- ?- _' h1 R. Blocalhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' `3 ]2 F8 s! X  j3 X0 z
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1 f$ `+ ^+ l! y6 J1 GIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
, p- z& v6 g. {6 D& Y' }Someone could be eavesdropping on you right now (man-in-the- middle attack)!
; f  n5 p- F/ n% P  p' YIt is also possible that the host key has just been changed.6 C* G3 Z  K  j
Please contact your system administrator.7 v, u- @; K% r6 r7 `# b
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.9 o. X, G; u" B/ e8 V  l) g
Received server key's fingerprint:
' v8 A! q2 j- m( [+ Fxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum  F; j; I9 G$ T& e, h( ^( A
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
( }" T  x* Q* dAgent forwarding is disabled to avoid attacks by corrupted servers.
, F! q( j' [7 B# QAre you sure you want to continue connecting (yes/no)? yes
! d& d" i3 X2 F* Z3 NDo you want to change the host key on disk (yes/no)? yes
3 ~. U$ i' d, N: f& [0 j9 P: n' ]Agent forwarding re-enabled.
6 [# `" }/ k6 y! {Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,; E$ q7 i9 j+ B- o/ Y0 f
accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
% r+ c- S' @! C' s3、调试SSH客户端会话% a% d3 B7 @& F; j- k1 ?
& L# r  q1 T  }. j
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。& W: {3 I! e1 \( b$ [
3 N' c5 t7 B& i4 t6 {0 J7 I; |8 Q; K
没有SSH客户端调试信息的例子:$ a3 o, Z" a9 @: k* G. G- V) b
( c/ G  p. Q0 P$ h6 k6 ^
1! W) s1 e. u1 ?0 L
22 [4 g6 Y, T* x- b' Z
localhost$ ssh -l jsmith remotehost.example.com3 _# m6 q1 {5 o3 A: ?7 n* h4 a  t
warning: Connecting to remotehost.example.com failed: No address associated to the name. ?" Y' ~& q$ J8 U1 R
包含ssh调试信息的例子:# S5 j& g, K( o( u  w
# V! ?8 t! r- L; k6 {/ [6 u
1$ K- G0 M" I1 T4 z& E
27 v3 c- C* [0 g3 X0 Q. M# R; d/ E
3
' R  `; ?. [3 U+ f8 D% b: ~! ]4
9 f/ _2 Y7 t1 y: I5# c& W; V/ ], L" P
6" b& W4 N8 b# U% \- V; S7 I
75 r' ]2 ~: L2 X) T
82 a' e( P4 \. T: H/ U2 K5 C: d9 b
9
. c9 {9 ?5 V$ M" i) H6 e% z4 V10
4 T. v! j1 E  X- V, l5 Ilocaclhost$ ssh -v -l jsmith remotehost.example.com  ~7 a; a4 [4 q; r0 O5 A2 j
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:# ?6 H, w! I; \, k0 [$ \8 s- B' E
Metaconfig parsing stopped at line 3.
6 d, f, O/ Q& j0 U; T! s$ Vdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
* v7 i' {7 i, z8 cSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
0 I4 X1 q+ T0 Q- i) A8 H+ s; _- E8 J$ Odebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.. O2 q8 x3 E3 m# d+ f
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
- Q( Y$ ?( ^, z$ I- \debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
1 a9 C" `9 e( n% {warning: Connecting to remotehost.example.com failed: No address associated to the name
. G0 M: s: @  O4 z7 w/ C& m/ Q[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]( t6 g4 e6 [0 p+ P
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:' M6 n2 P, W2 ]/ ]: _4 g. e
8 s6 D7 ~' J3 w, k5 y% X! Y
4、用SSH退出符切换SSH会话
, I! b+ m0 o' \2 x% t5 y! Y$ S" s$ h  Y1 \# P* N
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
/ @, I$ |% X: Z/ r" ~# u6 I% J5 l
( `1 y5 |0 y; z7 B当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:6 k3 w2 p! `4 g" s  l) P# n

  x) v/ N  [  e  }( b当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:( E6 F2 \( \8 z& ~* e
6 G! J9 z+ P$ ^3 M# W
1.登入远程主机:
7 K' s& B! `" ]3 n+ U% |
, S! Q  L+ D5 x$ K# r- r7 Ulocalhost$ ssh -l jsmith remotehost
% Q" ]" W, x0 A8 L, J5 u
" n1 j4 G! Q" Z, x5 ]: v, _4 p0 M2.已连接远程主机:
4 J/ _# R7 F; ]1 y; P: z% M. e) X! X9 s
remotehost$0 `  |: \( a, ^! [! i& n6 J

: q2 B8 I; a  W( {" M3 h, f& X" z) w3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。0 Q, }5 M; R. f  N! K

, x$ \* r5 y# L9 _. B3 F0 F1 ~+ Y当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”+ V. D6 S( T8 b4 f3 @1 d3 v: y
7 _) G$ z$ o8 J) u3 t/ {/ [4 g
remotehost$ ~^Z
1 E  ]  S& ]1 A5 O' h' i: C4 z[1]+ Stopped ssh -l jsmith remotehost
1 w: ]& x8 ?' \3 X; P6 z0 S' b1 O' Ilocalhost$
0 ^8 B% g0 O" P( I5 d& D: _6 B, S# U+ \0 U. g' K1 y- [) O
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:" c8 _. v3 b0 j2 K) Q! E
5 r  f9 s/ |7 J0 i6 v- \. p' N
localhost$ jobs
5 U, \7 {! F- s6 a+ ~[1]+ Stopped ssh -l jsmith remotehost
: E5 Y! T2 V! u) J
  X) N9 z9 @/ Y5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
2 e7 @- V; A+ D. O8 Q; x( p  Y* L4 t1 E2 ]. d3 |4 ]) K& J# V* T% `
localhost$ fg %1
6 G0 ]' E" P5 z0 v, U: jssh -l jsmith remotehost ' N$ n" Y; [# b3 G( J
remotehost$( j& |2 M3 ?4 w

5 T- ^+ t8 p$ @5、用SSH退出字符会话,显示信息" h1 l: w. u" ^3 L& Y5 p' |
& N$ e4 m* C, k9 F
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
% h- Y7 N, ^9 }
( w7 O# L& v/ S登录到远程服务器
. Y" Y& a3 ?8 P
* B  R& z/ [7 l6 T' w# glocalhost$ ssh -l jsmith remotehost, m& ^& g7 r" {0 o* p: ]
2 k+ x  a* H! a0 a. s1 i8 ~
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息8 J5 }6 {& @& L7 }

$ {/ l& w- e$ z2 @" Y/ p1
! v0 Z7 h% o3 S  S, l; g2
0 i+ d$ D6 p6 @3% q1 _0 q2 w( d0 C- R  ?' k
4
/ @* p5 H! u  k1 y# ~# K9 l5
' v( y8 }% b' F9 Z* G6
: T/ ?8 ~  H5 H1 h# Z) O/ _7
3 m8 ?9 w4 S* y8* |# |1 L, G' O$ q1 H- `7 S
9
) t( @2 E6 {' O; L" c! r! W& P10
# l, i7 x4 g( V11
/ ~+ E4 D; ]: m4 f+ m# u( T12
! }6 w+ X! ^# _135 t1 O" ^6 o! ~
14
$ K+ \' I2 P' \) x; W8 |159 c4 M$ K: L& ]( U( y
164 Y. g' u; N( `1 E2 e
176 `* z  b) s$ [6 g; H
18
' F3 Q4 B. |2 s% z# y$ p& u196 Q! G3 R6 I+ F) G4 H1 `. e
204 J1 i  s  a9 y
21+ }2 q' L4 M6 `) c
22
9 Y: ^4 m. e7 ^9 x+ h( K% F23
! K7 z; Y* m# r' I: |% d24$ A% v' g. I+ \, m$ W2 S
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
: J" u- _( F$ s$ y5 ~( Y, G" t5 ^remote host: remotehost
$ B# S4 B& {) D2 D% d/ ^/ Nlocal host: localhost" @$ R. p0 v3 }# F/ u& O) c# R
remote version: SSH-1.99-OpenSSH_3.9p1
. w+ C/ N% T, llocal version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)- a" y+ }8 F* G2 J7 Q" Z
compressed bytes in: 1506
1 Q* M( ]; @; z- S; x" Q! n8 Wuncompressed bytes in: 1622: j& G+ C0 c6 W" Q2 V: }
compressed bytes out: 4997. {. W- |- K3 {0 {
uncompressed bytes out: 5118
. U! G& d! T0 S0 f$ u1 A3 C4 {packets in: 15
8 c. j4 K5 }& d% Spackets out: 24
2 d8 u. T% g. x. d. o& J: u0 ?rekeys: 0
  M1 E0 _2 L# J1 R1 zAlgorithms:5 X; ]$ Q/ S$ f
Chosen key exchange algorithm: diffie-hellman-group1-sha1
2 |2 w1 r$ i2 W7 f! yChosen host key algorithm: ssh-dss
0 _7 B  s* K0 ]' G# _; ]& G, oCommon host key algorithms: ssh-dss,ssh-rsa
: g) W+ ~& d! f% x" MAlgorithms client to server:" E$ L* m: `9 M
Cipher: aes128-cbc
$ N1 w% i4 @( \$ ?MAC: hmac-sha1
( p4 u5 T4 i# y0 p7 [Compression: zlib
" w& @+ @. ?5 QAlgorithms server to client:
; _2 y5 r( }- r2 ~, V, VCipher: aes128-cbc MAC: hmac-sha1
7 y, e1 Y# l9 W! Q+ d* t2 xCompression: zlib4 F0 V" P: C) z& ?7 V
localhost$( k* P$ G2 |$ z# o% m* p
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-7 08:44 , Processed in 0.092774 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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