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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1864|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本
# e$ f. l: h& @  G2 B1 C' T3 x
# }9 u( J! _9 v! T# l. @; ~* z有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
0 V- @" `. ?" l; Q' D
' `9 L% z5 L6 K+ p3 x. Z+ J% _$ ssh -V $ \4 T/ Z9 W. |* j! u$ p
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003: z8 K) G$ _2 n, v9 Q# y% h

( F  o; u- Z+ f* W( t7 w) e下面的例子表明该系统正在使用SSH2:' e5 p6 O5 Q- R

% Y* e5 a  J" ~  W$ ssh -V
+ I% F; r, H. t3 mssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu$ g: P9 ], M/ N& q7 B& L% n
2 h: Z; h" G9 B9 a; O
2、用SSH登录到远程主机
+ E1 f+ @2 w; C8 Q
( U8 L# Q; C) W) ~当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:9 a# i# Y% ]6 T! s; Y; X

& r0 j+ [0 m7 Q$ p. e" a& ?1, A3 L) M6 X5 p, C$ u
2
: v, \% _7 T! B2 f$ t3 X( Q' O" B3# T1 q; t! S) ?/ k
45 \) ]" D$ }1 e# T8 L
5' x5 a) H+ ~5 C
6
2 h. c6 @0 G6 U7 G, ]" d7
' @$ z& B3 q$ a: H0 p! V8
$ W# n0 Y5 g4 i( g) `% u1 f5 Ulocalhost$ ssh -l jsmith remotehost.example.com' g. B4 I- Y; _( D5 L6 m4 X
Host key not found from database.
8 i+ {/ L- R0 q9 y- QKey fingerprint:8 s* L+ Z6 Q/ U$ h( I
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum* Q2 z! c. s3 }9 H0 `- Q$ W: q8 U
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.+ P1 \% W, h! j, h
Are you sure you want to continue connecting (yes/no)? Yes( G& d; J: V4 r; m
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
# A4 `' U! t5 `( h9 ^' [& Waccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
2 P) H$ b& c* Y  u: L因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
! W) h! n# e) h; r0 i; S( f6 y; r7 z( F
11 ^) V3 _* E4 H0 W8 n
2
7 ]1 R- u' |! b0 ^7 a) O5 z3
% o. Q( t( e4 {2 y" z" ~) N1 {localhost$ ssh -l jsmith remotehost.example.com: p  @# K1 }; @* d
jsmith@remotehost.example.com password:5 q) @8 c" C" T0 o  v6 n" x
remotehost.example.com$' _- D5 u' d) h4 W  U( B
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:6 I; ?: {0 a; W2 y. j$ g% R
6 [9 E7 M. p4 w& ]2 ?
o 系统管理员在远程主机上升级或者重新安装了SSH服务器
1 L8 W0 ^4 F& P4 i) J, to 有人在进行一些恶意行为,等等。
6 [2 |2 l- D/ u& K: J
. H; C8 ^+ J0 d1 k) E9 l+ I在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
* c3 P: b) X* |  v9 p- O/ i2 S2 y  B, ]8 `# k# C9 _9 @
1
" V3 J8 n9 M% v: m4 I7 ]* c2
! M7 a  x3 A' n, R5 H) I3+ V9 s( U1 @7 ?
4
" R. q- h  O7 H8 Z& p! I; ^$ x7 X56 n2 O- _5 N: [; O* F8 x
6
3 W0 m& b1 c4 a: X- ]# B5 j7* x( a8 v0 ?5 `* M
8+ r1 j5 r. W' [' M8 M& G: L
9
3 x$ h6 f5 J, C! L/ F- y10# q8 `7 u% W" K  Z9 t2 j" p
11
3 k& `( o; x7 D; e" K0 a- ^12
/ W% \/ ]5 h' b7 Z* |13( c/ l, |4 m- ~2 ^! @
146 ^3 {0 y9 f0 }, n) ~( Y6 z% U) u0 h8 C
15
; g9 q) k: u8 b( b16( ?& |: h9 i- I* z: y- ^0 O+ M% ~
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@( ?# x( h, ]7 J; E6 s# J
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@( Q0 M/ j  U9 _
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
! A5 O' @  R% x1 H) c0 `Someone could be eavesdropping on you right now (man-in-the- middle attack)!
, Q/ T+ Y* `" c- [8 q1 d3 \It is also possible that the host key has just been changed.
' g8 C3 `" c; qPlease contact your system administrator.
- q) Z3 X% _' ~4 i" W' {! r+ XAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
' ^7 ]0 W! M4 ^Received server key's fingerprint:
# Z+ Y) c9 W! Z& ixabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
* J5 I" A; y+ s3 t6 T' i8 BYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
- C& I) p" T  s% ~" X* G0 u8 y# fAgent forwarding is disabled to avoid attacks by corrupted servers.0 u/ |4 K: W& Z4 Z6 H4 F7 z& j
Are you sure you want to continue connecting (yes/no)? yes
9 m  o* }: ^0 F" {3 ^, q( JDo you want to change the host key on disk (yes/no)? yes
+ c! F/ w2 ~$ |6 D2 J! B" gAgent forwarding re-enabled.+ N# j  J2 p+ w; U/ Q
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
& U2 g5 F" G. h  p4 _accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
& f; q7 I6 Q; @7 \6 O5 I/ Z8 M3、调试SSH客户端会话8 ^) Q6 H* I2 ^! ^7 Q

' r& s/ i! x! k0 p2 g' u( i当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。; p" j+ K& v+ v5 U% ?

6 ~6 T# k' g* q/ {& d* J3 u没有SSH客户端调试信息的例子:( z3 W$ l$ [+ l9 _2 w8 e* n8 Y# k, R
! R7 Z1 d$ Y5 d) G% _
1
& C; A. J; B6 B+ O3 Y2: y6 M) J$ a+ f2 I$ S2 |! ^9 ~7 |7 m
localhost$ ssh -l jsmith remotehost.example.com, h8 Q; E! q3 B, q# ]: G
warning: Connecting to remotehost.example.com failed: No address associated to the name
* q1 k3 `6 G4 z包含ssh调试信息的例子:, l# t/ n! u: Z0 G+ T
2 T% h# s& n, e! b
1
: h* j, @, w. i$ p' ^& F+ I0 n7 A2
' Z1 H  \& X7 C5 W3# V# P. N/ S& E+ r$ V0 z+ e
46 Z+ |: ~1 P8 N5 K' K& d- g0 \
5
* ~! t* g& S% p6 W5 f$ e$ r2 m6
: C1 E- _. }  I6 C+ u" P1 u7
" H6 j, z7 I7 {& w8, P) ?- K( B2 I* Q
9
# u* M8 a" O! i10
8 g* T& N/ {& a; y: @locaclhost$ ssh -v -l jsmith remotehost.example.com
# x% |: Y( l" l$ |; Mdebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
, g8 c+ V& U( k% J9 LMetaconfig parsing stopped at line 3.0 J# ^$ L' D( F! v
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
. j; t4 e2 i5 C3 _6 C' n& JSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
) d# v! i  }. T. k5 H6 h) Mdebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.  E9 [  n& V) A9 |. D" B& H: |
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')$ I  C7 g' Q. R% ]+ ]
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)( I! ~! g- ^' c8 a! p
warning: Connecting to remotehost.example.com failed: No address associated to the name, B% s" O; ]- D; O1 D
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
3 V  o, S) s& D2 N2 t当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:0 x8 ?1 I; w! e# ?
2 G& d9 {" Q, Z) _/ c5 W# g2 O+ V
4、用SSH退出符切换SSH会话
+ J& d0 I. E, ^3 F2 t3 y0 Q, `' u. j* ]9 B. @% m9 O1 ~0 D/ P
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
! `4 G( e1 f$ a! K+ e$ \1 L3 s, N6 z/ a  g
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:: x# b/ q# z2 M8 K: R- B% Z
5 `! s) N# \# @
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:8 A4 A4 }8 v* V3 l0 J6 o
/ w" I& q3 J: y5 U( q. q
1.登入远程主机:% p/ q' L5 y- x$ ~$ }: U8 J  D

4 m2 d/ j2 r5 h8 f( hlocalhost$ ssh -l jsmith remotehost! D: ~4 W: l6 v

( ?6 n: Z3 [9 |: }2 |9 A& H2.已连接远程主机:
) G, \) n  Q) u$ h; X6 P5 {' \  e/ `6 k8 ~$ m+ m! f6 I: v( }* ?
remotehost$1 `  d# o, q( D) }+ R
8 \4 I8 S! v8 n0 m1 z9 T. W1 `9 _
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。0 m) e( {* v1 D, x6 l

4 \3 K8 a! l& I/ u( o& H当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”0 y0 z5 z$ U6 i, }
+ d/ C2 c8 c3 h* u( z9 _
remotehost$ ~^Z
9 C* f* N8 P6 T# b[1]+ Stopped ssh -l jsmith remotehost
5 I1 N. ^/ G( U0 s4 A$ a& H3 [localhost$* ]$ e( N) Z$ C( b$ i& W* f
) w1 p3 m8 P1 U9 U2 Q
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:8 c& {% w3 B: F: Q# u0 H) |% P6 J
# J& J3 S, h: l% P8 \8 s- ^
localhost$ jobs ! C9 b% w$ |6 d  {# t# Q$ Y9 S
[1]+ Stopped ssh -l jsmith remotehost
# K. T! ~( p$ f! \7 M' n* c6 a1 l
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
# P' _- n  A- d# X* d5 k( X; g1 K' g- X' c2 A3 w: P2 a% y4 D8 M4 V
localhost$ fg %1
4 v9 L! \8 I' }" N- f" K; d# gssh -l jsmith remotehost
4 v) h; N7 r0 x5 Rremotehost$
2 g9 z7 B7 B' k* i8 y" [- w3 ]$ j& T. H- F% ^+ ]# u1 Z6 u
5、用SSH退出字符会话,显示信息9 v& x0 T" V1 L: B& P, R, r; T
/ M1 e' K: \0 r6 D/ P
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
, |* N1 x# e1 g$ b8 e* ?8 K! A6 w7 o  G: F& e
登录到远程服务器& x' V, {" s5 e2 i0 `+ [

* {9 {: s" S, r  Q2 a9 \  \) W2 dlocalhost$ ssh -l jsmith remotehost0 V& f1 g8 o) A& @0 X1 s% X
$ y4 b. k% T; m% r' {" ]
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
5 F# |3 @* H; s9 A6 G  [/ k$ H# R
19 X1 K7 L2 ]  c# a5 f" L+ U
2
4 J$ Q" Z/ P: w5 c6 a' [1 c3
3 U4 l7 V; D% l5 ], \; D( S4  w" O& F" x4 u8 ?" a
5
0 M2 ]( m% J* E' _% ?5 s6
2 k9 i( n6 C* x2 I+ q5 f76 J8 c2 f. ?: I2 Q9 f  \) i
8
& J. j, ~9 i$ j# x9' T4 x4 P% s# p
10
) E( _4 c$ \/ M1 {0 A) G11
6 S# J( i+ `+ J' E. f8 r2 }128 D! z8 k% h( a  p
13
2 \! d+ r% C) G; j! C% t  Y: B14
# ~( ^# I. s$ J; Q: u1 U15
6 R* c  u5 G- S0 Y16
- x( |" [" Y6 V- m17) c# T5 p7 q" b) q7 Y- K$ e- g+ |  p
18
$ a# S; k6 i$ D4 v19; A5 f/ k$ P/ l0 H
20; m) A2 m% ]1 Y* X  o/ m* x
21
2 D0 y. H) |5 W) h+ @7 V1 N) z22# V- [) K) }& N: a; y% ]/ [
23" Y( g+ H& ?! D6 I
24
. i" S  X! H4 V+ F; u8 _1 premotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
) `! J: A+ E" S: Wremote host: remotehost
8 {% U, A0 p! K; {) G4 C  o# G4 Tlocal host: localhost, V9 ?7 D! m3 b; k, X) q# ~5 u) m
remote version: SSH-1.99-OpenSSH_3.9p1# ]5 _) o, |4 P# }
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)5 C! N  _+ Z! b/ i5 N3 i
compressed bytes in: 1506% G- }7 M2 o9 a6 @3 Z# e' T$ h
uncompressed bytes in: 16229 o9 W5 ?$ Y6 v# D% o; Z0 ~- _: U. _
compressed bytes out: 4997
% h& W1 G6 o" G. b/ U' `/ y3 v9 funcompressed bytes out: 5118+ t( Y! [* F% w: Y
packets in: 15  O2 X0 X) i; I. R8 x
packets out: 24
7 l. L0 T$ e5 w; }) `* j8 F# m6 ~rekeys: 0- |" O- G# ?* f. Y  M& b7 A9 R
Algorithms:
" x+ k4 m" v0 f8 i' r& NChosen key exchange algorithm: diffie-hellman-group1-sha1
/ s! J3 m$ {/ i# o( ZChosen host key algorithm: ssh-dss+ n6 a! n5 p7 I" o. G6 n
Common host key algorithms: ssh-dss,ssh-rsa
2 N  M. ~+ N& a/ e5 W1 mAlgorithms client to server:: W0 }# [/ w+ j1 p
Cipher: aes128-cbc8 D. e3 j) j2 S7 M9 l( r1 X4 O& N$ V
MAC: hmac-sha1
% i4 d2 h' A3 a( [0 b1 ]* W) q, qCompression: zlib
% ?! @' W: w1 Y4 v* GAlgorithms server to client:& x9 V; Q7 c5 @! o
Cipher: aes128-cbc MAC: hmac-sha1& E8 p5 F4 o% P8 m) b: \
Compression: zlib3 g6 f' G4 ~* ]- u. T
localhost$
6 c/ e6 W+ J5 v2 ^! W以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-19 19:23 , Processed in 0.158080 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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