找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2705|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本
" t, z+ w" T* o, e) \* q5 s. O  d
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
. e9 g! T& ~! e) o2 C- s4 S! f% ?3 T) d/ g7 O
$ ssh -V
  u6 L" E# Y- B9 x3 b+ U% POpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
8 P2 w+ K/ R0 z9 E8 |
8 i9 \+ ]! B# o0 Q# @' j: _下面的例子表明该系统正在使用SSH2:. y4 q7 D6 ^/ }: S  ^0 _5 U! ?

; X6 ~3 d6 P! x- a0 m" V) R$ ssh -V # L6 ~. L0 ?4 l- k+ G6 a
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
" Y8 T: n2 K/ e: X! k& v  b% f1 l: _6 V+ s9 ?5 s) ~4 k
2、用SSH登录到远程主机- [8 `2 ~& Y- m4 L' d

: X9 u3 a  S) i* i$ I* _当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:% t/ T$ J% u3 l5 }4 ?* y* b
: S0 e3 {, O8 r# m3 d
1
; t+ L  x- Z- l+ P) N' ~" I2
) I( K  O4 I0 P7 }) G. N3( s# Y8 Q) p6 u. d
49 \: E: z' B. _( c0 g
5
0 c3 c6 O5 Q- z6
* c$ i9 U2 y) V; Z! Q% k& g78 i3 @" J9 U: M( E! t' x* _: Y3 c
8; |+ ^5 }2 }! a' c( P
localhost$ ssh -l jsmith remotehost.example.com% h0 f: d% }* f5 [% Z2 e& J
Host key not found from database.. {# V3 [0 F* z
Key fingerprint:2 E; ]1 _) ^0 D: e; r
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum6 m# j+ b9 I! k2 V! E3 i) Y
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.4 d( H0 y2 |- o7 `
Are you sure you want to continue connecting (yes/no)? Yes2 \$ d' A+ g7 B5 Y- T1 F1 N$ A6 o
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
- Q& @0 r. j5 E( g7 b# {$ G1 Caccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$" @, V2 @# p, x7 i4 K3 W4 @/ h$ h
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
5 Q7 D: f* n; U, @* Q. V& |9 [, S" x- f0 z: r& L4 `1 S  O
11 ?' u- f+ u* g( y- a9 [4 x
2
1 h' H* A1 d  O: ^$ L3
6 {1 S) f, y. B; qlocalhost$ ssh -l jsmith remotehost.example.com" k. }8 |* ?/ t% q% C0 P; K1 E6 k
jsmith@remotehost.example.com password:
# N0 h+ c* a7 f2 c# rremotehost.example.com$* Z/ X! ?6 b% q* W* H8 d' l' t
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
( ?" ~+ ?+ F) [) H/ W4 H
9 h; i6 ~# z" r; k, t9 H( Ho 系统管理员在远程主机上升级或者重新安装了SSH服务器 ! r. q( s: j2 [' y4 Y
o 有人在进行一些恶意行为,等等。2 E9 A: C5 e' C/ V

& t& s6 Q: h  L% a# \在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。* v( n( x1 c/ a7 Z7 Y" F- r

2 N. O) }; [$ P+ r3 U* D$ S1/ U1 @6 J/ M( B3 [
2
' n, L& Y5 r1 W3
% p, _' C7 V+ y# e  z4, }) n) }! O% L& J- {
5& h/ v5 Q% a# e' Y8 {$ D
6
) T; ~2 Z+ D; U8 L( R& o; L7
8 n) t- V9 C4 b4 h. Z8
, }& q2 E8 a/ ~6 R  R- t9$ y2 L7 e/ {) O. N" A7 v
10- i4 e' _4 x* c$ X0 c
112 X2 c$ I+ y+ a8 Q% a  e9 J! h6 j
126 ~5 Y  u8 I1 m, g7 |$ a8 }$ }
13
) T5 B  ?, b2 t+ G14
" L8 }5 t( h$ u: |; j# x7 C( h2 G8 L15
4 R/ F! y' P( R9 d  }: v6 Z$ [6 K161 d- J5 H7 n- H/ ?
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/ d) F+ Q/ z) ]& d: a) X@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  z& o1 u7 i( \1 u" L1 _
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!  w+ E+ r, j5 U3 p  U
Someone could be eavesdropping on you right now (man-in-the- middle attack)!
* t. `# @8 g4 b/ b4 n+ h8 CIt is also possible that the host key has just been changed.8 V6 ?' T2 b4 j5 m3 ]. ]9 g1 }( V$ J
Please contact your system administrator.
' K) U! H7 B/ Z/ _& x! YAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.0 q! p# j- S) }, r. d9 ~. y
Received server key's fingerprint:$ ]! Q: ?8 y' h" X8 k
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
9 l, m& n) o& x0 VYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
; v+ K$ S$ |4 XAgent forwarding is disabled to avoid attacks by corrupted servers.  V6 R( P6 c+ F4 l9 }
Are you sure you want to continue connecting (yes/no)? yes5 r  H  S2 A: z2 E! M# z3 x$ G
Do you want to change the host key on disk (yes/no)? yes
! w& P; s1 N( x  ^8 \0 vAgent forwarding re-enabled.
8 |' f4 f! x$ f9 E% N' S/ p0 b# oHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
5 E" [* q0 e5 y8 m9 t& r0 \  [, ]accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
& g* |! O8 V3 D: r3、调试SSH客户端会话
% }& `4 N6 I; W0 E/ I
: B# y  K+ ?, D* a当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
; P& V% d1 j$ P) ~6 Q8 I, v# ?& ]+ v' b7 D/ n7 k: @
没有SSH客户端调试信息的例子:2 |" }) C+ z0 l- s

8 d+ Q8 H6 G( Y% s& o# V) C7 d1
! [! }& W+ g; `2 \: f- A  U9 a' w2
4 Q' W" P  t9 F5 b0 Alocalhost$ ssh -l jsmith remotehost.example.com  n6 i+ z0 j0 F, v  `
warning: Connecting to remotehost.example.com failed: No address associated to the name5 L8 n3 q" \  y4 t
包含ssh调试信息的例子:  |, F; {+ R) W/ Q  U

7 N' @* o+ B% Z1 R+ ?/ ~% M! M, }' ^1
+ p  g% J1 `3 w1 X9 F. I$ `2
$ n- K* o0 R7 u8 n3
: i; d2 W8 t8 E  R9 j4 E  J7 B42 b0 }; M& w2 S0 U8 _8 l7 E% L
5; n) c3 e. _. n) l
6+ O' F! R( b7 m2 U3 H1 |* D% Z
7
6 E5 D) T; T' d9 R  Z6 n1 d4 g, m8* ?, v3 P- D4 z- _
97 Y( m6 G3 l' P  x) B' u
10
; A/ m+ h8 s0 e: @- Q# Qlocaclhost$ ssh -v -l jsmith remotehost.example.com
' ]/ S" ]$ s9 P6 ddebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
  I+ @! K1 ~7 G6 a' ?Metaconfig parsing stopped at line 3.
, l, A7 C8 ^9 @) h9 o1 E/ Ddebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
  k8 y: R$ u+ h- N: NSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.) [9 g/ c6 b2 K9 p
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
- H& h5 y6 V7 g3 d( Sdebug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')' M! b1 h7 n1 B3 e
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
0 c- e$ ?; V  h( ~( D6 rwarning: Connecting to remotehost.example.com failed: No address associated to the name
1 m" l$ @" a$ v7 |[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
- H- E# A, e" E5 v6 F当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:4 x4 S! @. B( y3 |, Q! a
4 i9 L4 Y. r6 _% w8 U5 I! P1 s
4、用SSH退出符切换SSH会话
, k/ y3 ?2 p, ]  K  `" v& _
+ }4 c$ j  M" f+ U/ W- f这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。. |+ i- b6 [& {

, h, l% i7 K# H6 O! ?0 D6 u; H当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:' V5 H/ e2 R9 T8 ^# B6 i
7 `0 w$ V0 ^. Z' k4 s
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
  b4 G* q) y" R) G0 K" y5 S  |+ d1 Z" I9 i" D
1.登入远程主机:
5 C; K  G  r$ n* L# M2 `0 d0 O: T; \! A; m! Q2 Z
localhost$ ssh -l jsmith remotehost2 x$ A( Y' V6 s5 S! A

1 Z. W: I7 M! D5 V" u$ F& |2.已连接远程主机:/ j: j2 z( F' a4 Q$ {* Z: d
4 s5 `5 Z" h6 b2 v1 G& n$ W
remotehost$
9 _6 e/ |' Q& r$ K; e
- w0 r7 v! h# `# i+ B0 n# R1 f3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。3 b* q! c0 E- [' D9 |9 w

4 F, r( V" p% E) X当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
, w% {6 y# r& a+ c* b
( U; v( l/ P0 x4 Hremotehost$ ~^Z 9 Q+ ^5 g8 n( w+ i/ a- n6 g/ ]8 p
[1]+ Stopped ssh -l jsmith remotehost
& S2 T* E( u7 klocalhost$
) @  r2 s4 \) A2 e2 C
* U. h7 |* F5 d  b8 y4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:) q: c- x# _( s

$ l3 J" d; T, L" ^8 q3 B4 jlocalhost$ jobs 3 H* A5 ?, m' G" g0 R5 Z
[1]+ Stopped ssh -l jsmith remotehost* A8 S4 X& F6 k! K. n; I
+ g& o  [8 a$ B$ T
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码! W' s+ X+ i* ?

! y- l; k! _* h$ Y6 x) p# ylocalhost$ fg %1
5 F! c  ?& G/ }ssh -l jsmith remotehost
! c' [$ _( P, k  X8 Premotehost$" e( Q9 o% _1 F" |

6 \. c9 e  ~) G& Z/ w) c  m4 n5、用SSH退出字符会话,显示信息) f- z9 J& \. g) l
7 p( y/ X% G6 V) P) E
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
) D9 W1 F2 |$ L# G  ?  n4 b- k) ]. F0 \6 \
登录到远程服务器
( k2 i0 c& F2 K9 J! `& p. K6 o* K( |( k8 Y1 q
localhost$ ssh -l jsmith remotehost9 Q0 r: \) E2 Y- c- r2 L; G
/ E. v$ d/ [8 c
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
' {0 o! U+ t, B" e; j; p7 L( X& z
1& y. Q2 x' g; D  V6 |
2
4 Z$ {' L7 w/ m3: j; Q3 M% V, l1 w" j8 g# G
41 v' W# z" c- O- S
54 k0 T! @8 W/ ~9 F. x: H" X
6
: k5 A  W: x  U' q8 `8 v$ n, G' Q7
5 r. e7 H, l! `) u- a" N4 D5 M8
, d0 N& D" G" r; n6 C6 X9
5 K( Q* W3 h  B& M10) }/ c$ c6 `: x) x/ \  t" x
11! K4 j# ^; z; l' B
12  Z7 ^* w; l# D2 ?7 E5 ]/ O
13
9 R  H0 T1 R: m/ x0 V. T7 H14
1 @1 F6 L4 a9 |* l7 W154 `4 Q- S6 E, M$ t" j. q5 F5 I2 M
16) @* `( Y  Q3 |+ @
17
9 Q7 r+ V# ^. R! V: ?& j# C7 t  y18; o; r9 U  @+ a5 E" D" @& h* j/ Q
19
4 ]" ]6 J0 M& f7 f0 o203 P+ y" F4 ?! A! w7 F: o0 `8 U
21( L5 P4 o9 D2 C
22: Q/ j6 P1 _7 V3 a' M$ x
236 a4 _% `8 m. J/ p* i
24* A7 I, O" f* V7 e7 ~# K
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
6 o" K4 l3 K7 Q2 Z5 z; }$ t2 b; D4 lremote host: remotehost
. [8 Z2 j% ]5 x- _! Rlocal host: localhost9 V/ F' g- j3 g+ A" n
remote version: SSH-1.99-OpenSSH_3.9p1: m' m5 B  s# d0 {/ E( |" y6 E
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)5 x6 u, l. Q! Q3 {0 `0 y# s- }
compressed bytes in: 1506+ a8 d- Z0 {/ l, ]: {% r  S
uncompressed bytes in: 16220 b7 W; `+ A) N) _0 {: m
compressed bytes out: 4997
/ `; ~! M& @# n+ Q6 buncompressed bytes out: 5118
. U0 ]8 A  b& Q3 u6 q7 q, ?2 Epackets in: 15
- n# D0 _! y" c& J$ l; jpackets out: 24
& T) ], Z/ r7 ?# P- Trekeys: 0/ z4 t, M4 H: `1 }! s% S
Algorithms:2 o" ^1 P+ f1 I9 g' D7 C
Chosen key exchange algorithm: diffie-hellman-group1-sha1' @$ l4 h- S& [% l) T& j# Y. x
Chosen host key algorithm: ssh-dss
9 N6 J9 W- E4 c: w# I" |Common host key algorithms: ssh-dss,ssh-rsa
1 u: t# ?0 A( b8 Z) F* T1 EAlgorithms client to server:
/ {/ ^' N+ k- H" i' w0 vCipher: aes128-cbc, t; r9 [; y& p& O2 [+ y5 L9 G
MAC: hmac-sha1) n! t' r1 o" m: |! T0 V0 X4 R
Compression: zlib
$ }+ j- q( e" R' J. [Algorithms server to client:
! o% k/ Q$ o$ y2 `& _" r9 [8 J1 hCipher: aes128-cbc MAC: hmac-sha1
) D, U4 ~5 z$ V& XCompression: zlib+ b3 }9 v( \9 Z
localhost$% y# u/ L. L! e, M2 h
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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