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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1865|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本" `1 v+ g! Y: C7 K9 E. o( T6 Y

3 T. D/ ]. w8 e, S* t1 _. |有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
$ U. ^  e& e* a2 ]
# p) a5 A. }) e$ ssh -V
' \. q/ y+ j% @7 P% b% F, |OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
5 O9 K$ s3 `0 ^! e2 @$ W. p
* v9 f4 S1 S* v0 T. N( V下面的例子表明该系统正在使用SSH2:
$ o" o: E* o4 N/ ]( e: Z
, Q1 W. ~1 b. I1 c- f) D; v) ^) S" @$ ssh -V , F# S- j  W3 o  n+ {9 \2 y
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu; m8 C+ s0 o5 {# V3 m1 x

' h2 |6 `8 e, y2、用SSH登录到远程主机: z" f* Z! T1 ]2 `9 y

% x1 L; [" G; @. O+ i当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
7 `& V1 Z5 _; s/ J! i$ T1 G1 G
. r6 D  C+ |* q2 F7 g1
( i! `+ E; h3 i1 X5 \3 ^+ b8 J2
) v! p8 \4 j5 O3" f" ^) q, E" T" i. R6 w
4
& ~6 L3 r! A4 d* D58 d- S" t. ]; o" j' A
6- C9 O# }& z3 E2 V! k  B
7
1 F  s) n) _8 w+ p' ~7 Q$ z6 ~' X3 U85 f( \5 W0 y5 ?" Q: f( x
localhost$ ssh -l jsmith remotehost.example.com3 b/ R! }+ I8 t9 X0 A  e3 R- t: m' N
Host key not found from database.
7 ^; M/ W5 C( oKey fingerprint:) f$ L8 a  i! K' W" u" a6 Y& @
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
2 v% a. Q" C, b, m; IYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile./ i9 M$ o4 ~& U& ?
Are you sure you want to continue connecting (yes/no)? Yes) L# X$ }3 ?: a6 f
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,5 k3 d5 B4 h2 H& u
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$: L. b0 f% h4 N/ n. `! o& Z
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。6 l* N2 `$ [) E- s- H! ~0 E6 I

  l* h" m8 e3 V( f& T15 ^' p. I) z  C
2
6 w2 @$ C0 ^3 Y2 Y# U3
0 T7 j2 y! ]4 n. ?localhost$ ssh -l jsmith remotehost.example.com
' U& w" Y; c" {, @5 c$ H6 F& wjsmith@remotehost.example.com password:$ J+ I$ S+ V  `' P
remotehost.example.com$
# `8 E- d9 P4 S- R由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
* R# `- x: @3 e% w9 X, L! I
( Q$ I, M6 P9 E6 e6 W$ B: ko 系统管理员在远程主机上升级或者重新安装了SSH服务器 * [# v7 z0 @* j5 ?/ i* Q
o 有人在进行一些恶意行为,等等。
3 `+ x3 @/ E( [& S
0 h; x" a) J1 \* @在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
; [+ i: p1 d2 e  i$ Y% K5 y+ A
3 [$ P- d8 v, Y1
$ b: j: j5 i1 x2 b) `- U2
: f  n0 A' Q; o' r% T6 D3" j, P5 e' \( s! y, p6 L
49 f6 ?. W* H- A5 a- \& D* t
5
. s1 p2 Q( H. }60 ~8 Z& R4 g7 |( }$ y
7
- Z1 V& Y# Q) B% T* L0 h3 b0 _80 S( K6 A) E" v  J# T9 |
9
- p4 c  Q# G" y0 @, v/ o$ B: C5 z# k10- ?2 }; G, d3 T0 p7 x; a# |  ?# n
11
' d2 Q0 h- o8 I3 |/ d$ z12' N. a6 T  j% S: k, h* L! ^, s
13  Z* s$ ~5 a& |: T4 f4 B. l
146 w% [) v. B, B4 Q- x
15% [' _9 Z4 _* K9 N; X; T  i
16: `9 x. X( j' M
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2 A4 k- \2 r/ E# S, i2 s@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
) L" n9 h# U% c; yIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
) L8 ^( L; A3 x* H/ \1 n0 ^Someone could be eavesdropping on you right now (man-in-the- middle attack)!3 \6 L3 C5 q3 u. _/ e
It is also possible that the host key has just been changed.) n! R/ T! h! V* M; d* L  g% L
Please contact your system administrator.
2 K2 A1 Z8 T( {! vAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.5 l/ M* x: W2 v% j
Received server key's fingerprint:
' S  ^- D! ]1 e0 M' D6 L# Xxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum3 v# S) I4 A$ }; K
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile./ a4 E% S5 x: `+ g/ U, v9 \; d: L
Agent forwarding is disabled to avoid attacks by corrupted servers.
& X* O1 h0 n! V- ~' f/ ZAre you sure you want to continue connecting (yes/no)? yes4 a: G" C0 L5 @$ T$ `
Do you want to change the host key on disk (yes/no)? yes
3 f; W# e8 |) hAgent forwarding re-enabled.
+ V1 M4 v) P% O! s+ E  \4 PHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,; Z. d( w3 ^# Q) K% t
accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$7 q, w# E9 A- b8 `) I, W
3、调试SSH客户端会话
- D7 H- u0 [2 i2 ], x; Q5 ?* B1 f  h
. N+ F6 v/ T7 H6 t6 p0 n& u当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。4 M+ k7 J9 f7 }* I

$ u' c7 o' H9 k. E没有SSH客户端调试信息的例子:
0 O* k  Z9 A" z! S8 q# h8 U1 P, [% }& {/ G8 G& P
1, {1 J4 U& H4 o8 s5 ~/ C& {1 l: v9 T  k
2
) |& }4 A/ c9 [/ X2 W- ]localhost$ ssh -l jsmith remotehost.example.com
! P3 {$ b: {9 ~/ M+ \; o' Dwarning: Connecting to remotehost.example.com failed: No address associated to the name! f7 V' t6 l5 z% i/ h* I
包含ssh调试信息的例子:
$ U0 P& l% B3 O3 Z7 T( q" n. Q4 S' ?& R8 H; K4 U( j
1
4 i; D. L3 K1 g6 q9 E2
" F  t3 |: J: D0 a; N# b3
8 H9 ?& D! {+ Q: _" D' b4( y% A2 P. L% j) c3 l. w
5
8 Q; M7 V+ H% m; `& o6
  v7 |% v/ h5 B, N. T  q) {  P71 Y8 z, z) j8 S
8
* e2 E/ a. ?# E( w9$ r' @- a3 j$ @; A$ N  K& i  O
10& C9 j( W4 V0 c! u- W, u$ k4 F
locaclhost$ ssh -v -l jsmith remotehost.example.com
$ M: x3 Q8 k5 _+ `/ Mdebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
, N$ m' Q+ P' i8 Q, aMetaconfig parsing stopped at line 3.
; X; l8 ^7 K1 B" N( @  [3 cdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
1 m( H6 t) e0 K" t$ T* [& KSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
* f+ |6 N3 _6 _) t5 o& I0 y# mdebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.7 P* @. v) g7 u! R& E' L) @: g
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
0 W" J$ M: F+ f- W  w# Odebug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
9 p( ^, d+ N9 D$ U( rwarning: Connecting to remotehost.example.com failed: No address associated to the name( j! v# e0 E! k8 X0 P/ ^& _( n
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]( ?# O+ ~2 [" _
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
: ?/ |4 P3 N3 |! T  M- @" ], ]3 Z- a* \; z1 I4 O( [( z
4、用SSH退出符切换SSH会话
$ _+ B1 o4 U9 [6 T1 L% F3 |
6 w0 K: t. G' I6 M% A  e这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
! Z# K0 m2 M5 X) {7 E8 r4 y+ @- |6 q" E2 S$ c. a
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
9 R3 f, z; U, W7 H
  U/ m% [; I/ k当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
4 S+ w1 t7 p# I" B( p/ N- ^2 t
" L. _/ r) q; D1.登入远程主机:
6 E& m6 I6 w4 k' K; t( N8 y% a% L! \
localhost$ ssh -l jsmith remotehost/ G6 C0 |& y- d  p' x3 }! p

. B6 E0 e4 ^1 N/ u2.已连接远程主机:8 i+ }+ T& _0 \: @5 S
& X! n$ t+ F! ?( R6 u% f, A! b
remotehost$
6 J9 e# N# k1 J$ P
1 f  t2 i: N- C- Q% Z$ u% Z3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
* g* l8 i7 C2 N+ N* q: r; p; t8 [2 h! O# j1 h3 ~* L# H" M* H; D
当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”) n) Y; V1 O& m8 o3 x! f

* |5 V. f) T. l1 Bremotehost$ ~^Z : S( A; G! ]( j- _- ]! P
[1]+ Stopped ssh -l jsmith remotehost
6 V8 f  V5 j- @* @/ e: f. `& Plocalhost$
) f. r: B. B8 `7 H0 r5 C! V7 z. J
  B2 x# V. ?1 b2 c+ |; b4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
. M7 [& v! g. E$ I* f* @2 {& ?) r- P! _8 x6 m4 t
localhost$ jobs 9 x+ j) D; B3 i, R% f  C
[1]+ Stopped ssh -l jsmith remotehost
: a  e1 u2 B$ Z5 s2 }% O' \, I( c( l. H4 q7 V$ J
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
# W4 M9 j" Y" Y2 z3 ~6 ]+ [- m; d& a  D
localhost$ fg %1 ; x2 V# N5 f% C% p# j
ssh -l jsmith remotehost
% m% m5 e' N& M9 b, b0 P% _- lremotehost$# {  `! ?. J6 N5 J9 P. F" W
  R3 j$ `  c0 V( _- F
5、用SSH退出字符会话,显示信息' r% L, \4 A0 f0 ^5 v
6 d! f; \6 J2 R! T/ E' }+ a6 {$ c
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。% A+ J% t5 V5 t1 A

2 ]+ P! l. R3 r% Y登录到远程服务器
2 N2 P4 A3 c* I: v
. ^( X* o& Q7 C* Vlocalhost$ ssh -l jsmith remotehost5 e5 G; e( t3 z4 W/ x
9 j* A$ m4 k1 U0 w9 }
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息( B. W* l' h4 |) B+ S

8 G8 E( B$ @8 v' d3 x/ e# y$ i1& ^& q: Q$ a8 h9 s7 S
2  R) p6 y2 h4 u. }4 k
3
# r6 Z7 `' U; _2 e5 l* p7 G4
) y( }0 l- Z! q% L$ N& i  {57 V  Z/ p$ U% c0 F
6
+ y( K3 `! x/ o0 z7$ n: {0 `# p. i, s% [0 i
8
' X0 {. S' a, }( n, W3 E! x/ R; h9& s; u5 O; y- p9 U1 c! e# M
10
2 p, {1 g: |: ?/ q" b9 U) d112 j( f7 H, n# g1 e1 X
12
2 l8 x) ]6 g$ h, _( b" A13
2 I+ d4 ~$ w3 u3 e14! v/ N5 n$ N( @; Q% c) F
15
9 V& f9 E7 G8 P, k% l$ K16
2 o# ]. [, n+ P" L5 r17
# `4 D* Z/ U. z# b18! i3 H4 W$ j9 O0 T
19$ D5 ~- n! }5 _$ E# b
207 g! D4 W( ~! l, w7 x
21
, b9 l6 H: f% ?2 N22; {- T1 Y' ?9 G. o( Y, O
23- Q' j" V- U& D. f
24
; N5 |: M, n( B. ]+ m9 Rremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]% E+ q" p" y7 K, D6 q# Z4 U) w1 F
remote host: remotehost* A( E/ i9 \5 [9 B. f7 j
local host: localhost
/ V" B7 e/ F: Premote version: SSH-1.99-OpenSSH_3.9p10 _- B; w: |6 A- c* N  U2 f
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)) ~' S% v& I+ t5 J
compressed bytes in: 1506
# @% m8 D; B6 n# Q0 P% P* N2 O# \' Funcompressed bytes in: 1622
1 V) `0 p& ]( g. @6 J: B6 Mcompressed bytes out: 4997
$ O$ E6 U9 v9 L! U) D5 Huncompressed bytes out: 5118
% `" D6 p2 g9 s2 z5 Q( Hpackets in: 15% X: K; Q( }2 [+ \
packets out: 24# K6 @5 V$ |3 H0 C4 o
rekeys: 0
8 |  V" G9 z; Y* a6 F' b0 PAlgorithms:6 s* |2 m( _* ]9 w# F
Chosen key exchange algorithm: diffie-hellman-group1-sha13 F. e% |: |7 Y+ T1 ]3 o# x5 W
Chosen host key algorithm: ssh-dss
# [8 V: ^7 i2 n0 V; b4 FCommon host key algorithms: ssh-dss,ssh-rsa  m/ ~% D% B6 {" K8 E  L9 q! G
Algorithms client to server:  h  A1 a2 `& J4 I& `) q7 c) h) G
Cipher: aes128-cbc& `* G# \& z1 u9 X  m) r5 Z
MAC: hmac-sha1! P; ^0 Y5 U# R: ~4 u, S
Compression: zlib+ d) X: ?( P7 C. N% c$ H3 V, H7 f
Algorithms server to client:
5 d: ?) D( t" N. M- a( `Cipher: aes128-cbc MAC: hmac-sha1
. d- N1 }: H1 v! H: }' B. }2 G+ MCompression: zlib
2 ~% x3 s; P# m# D7 J0 f" zlocalhost$
0 F! Y- I7 ?1 |以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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