找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2703|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本1 E$ W0 p- l. R: f0 r9 b) t5 q) U% ]

2 c4 L5 B  o0 {有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:& I6 F6 Y, {/ [1 v' q
/ ]1 Q  D/ m3 Q+ a$ Y1 ^  M; M
$ ssh -V
, ^/ r% J2 [- t% FOpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
* z5 v/ \( Q: J$ w
- [' |5 ~" O( L4 q; W: O下面的例子表明该系统正在使用SSH2:
+ B7 X; u4 ]" @. [9 G, N8 I
; f) a, F+ F+ f0 F& \6 t5 G+ S" R, y$ ssh -V
6 s% e) X# W7 K; R+ essh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu# i' V3 b- g6 c& x
0 [0 b% M& ?; G; O/ }/ z
2、用SSH登录到远程主机
2 Z1 i$ c5 H2 z) L9 Z% Q. P, ^- k, |0 L. u- Z# w' j6 C( I+ o
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
5 D  Y: K  q1 t8 W6 `  g3 e- E
# H  k' T7 e1 B: R* o" ]16 }- X" W7 G9 Q9 z% @
2
: y( Q: M( u/ L$ h3
- v0 G* J8 i( z9 o" D" W4/ |& m" M) K, P+ \- u" n
5
6 ]# C' i$ }- h: Q; L. `# a2 j6
/ m6 x# p& i1 u$ g9 O5 Q& k7
- n/ c! R6 i3 p" E- ]: ~+ x8
3 f5 ^& ?- i; |localhost$ ssh -l jsmith remotehost.example.com" z) A" Q3 Q% r5 t) L( @
Host key not found from database.; r! {7 L9 @/ J9 z+ l. p
Key fingerprint:
, Q+ e1 k+ Z' u2 Jxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum1 s9 ~" c+ i* W* G% ?) D
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.% U2 q* a: l" w
Are you sure you want to continue connecting (yes/no)? Yes3 `! g5 [" f+ {1 g' z" v" A
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
/ k% [/ D* s- h; v. B0 W, S- P3 saccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
. ~: s  {. ^1 N1 R因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
1 G& s5 _( \, x" H" ?6 q5 o  c) f1 ?+ p3 R! L$ P% g' u& T
19 e5 F5 K: f# a3 x% n
2
/ J  l5 f3 E4 `- m  a! q3
- q. }& N8 X0 T5 Ulocalhost$ ssh -l jsmith remotehost.example.com
; X- f0 t4 N' n0 {) g+ qjsmith@remotehost.example.com password:
; @9 {1 A+ x# \- z8 Z+ ?' I; [remotehost.example.com$! b6 \% M7 f4 K! x5 d7 f
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
  M4 L9 M6 G: Y. [1 P* b) n5 ?% s; E# ]5 A  O% {$ ^" P8 \7 @2 q& ^) w% [
o 系统管理员在远程主机上升级或者重新安装了SSH服务器
/ F$ c9 [( N7 J6 S' D/ V6 @o 有人在进行一些恶意行为,等等。+ _* R4 e2 K! e* R

0 ]5 q3 k$ _: ]2 w在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
) s5 O& U4 u- l% k& e* @. s
7 i5 s+ g( F0 }& K5 P$ W. S1
$ J- b( Q" P3 X, K" ]2
) y$ |6 x3 o4 A7 Q6 [3
, o/ s6 c0 R$ M. s, `40 N* s; q% `1 C% H) r" [5 t  G
5
  k, U  \" a! R: B6
+ P4 o5 s/ ?% x$ T7 t& b7  @& |+ o+ f+ V
8% ]* O- K5 {* C
9$ K$ G) g6 T- I) A# c; V
10
( l1 r" X5 V; p4 r11$ l* r! p6 C. L' S8 P* @% U* E# B
127 j) Z( Q+ W7 p) S4 `/ Z' k
13) E$ z% g& x9 C8 u( A( I( C) m
149 ], v. g3 |" k% o% s6 G1 M# N
15. `) z/ v0 T- ^: m# q% g
16
3 h/ S5 c/ W- U: U' E6 H, Ulocalhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" y$ V0 g9 c) o+ U' o4 a" B
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@$ S; U# K+ s8 Y) h
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!  Z6 z& d3 P. t; f
Someone could be eavesdropping on you right now (man-in-the- middle attack)!: ]8 h! a1 ~3 X( Q5 E1 j
It is also possible that the host key has just been changed.
; g, R% e  V+ [; b. `/ I' PPlease contact your system administrator.
2 j% Y" N5 ~" g# @) z4 P  YAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.3 ]9 m& J( Y  U
Received server key's fingerprint:
/ _; M" @$ I% d9 v* @( N$ dxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
3 V/ u# L% l* TYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
5 q0 j( v2 B# B, kAgent forwarding is disabled to avoid attacks by corrupted servers.
8 i" y5 N$ S& c$ d, BAre you sure you want to continue connecting (yes/no)? yes: K& {" T* T: I7 m* `
Do you want to change the host key on disk (yes/no)? yes
) I+ Q" S6 X" T3 N* s  NAgent forwarding re-enabled.% c* ~" v8 l' N/ Z. T( ?& e
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,8 b$ U* x4 B+ q- o
accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
1 G. e4 {, f1 m. U) |2 B6 ]4 O8 k3、调试SSH客户端会话) D5 w/ e) I  R+ B: Y. }8 w
6 U4 g- a) I3 o/ l" S- D
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
) {1 `3 h! V( q4 N8 m2 D0 L: ^, a+ |& S: ^& [: o, e
没有SSH客户端调试信息的例子:: Q# R& X/ U" Y' Q7 k% P% \
" v' F) x$ F8 @( f6 Z$ h
1; Y$ Y8 J& \' M; w7 r5 i) G* _' N
2
* c2 l* x9 q& j  t7 m$ n; Hlocalhost$ ssh -l jsmith remotehost.example.com
( |( i3 T% p( ^warning: Connecting to remotehost.example.com failed: No address associated to the name
% y) {2 L4 s1 m# X7 A( u5 O- \包含ssh调试信息的例子:
. Z1 A2 s4 H* {+ W1 ]
2 ^. c3 p$ f" e1
8 E( g- n+ ~8 N' e% `; H- ^2
) G% y4 z8 c* h3/ G8 `/ }; H. P- M
4" q# ~7 v4 ^$ o( z; `, C4 o' X
5/ K& B- t0 I7 ~# O- A1 j- A* r2 o
6
# V' k' a, ?/ `, s, H. U1 B7
% m3 Q: `, ^0 z9 D4 v+ E8
/ j; t8 I9 Y6 m! w) \# M9
5 v& L& G& u, R/ }! G10* v! B! @  c3 a. I6 e* h. B; V" ?, `
locaclhost$ ssh -v -l jsmith remotehost.example.com+ f; R3 t7 l: q  P/ U2 t
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
, {' D$ d0 K0 M1 G& j& Z/ @Metaconfig parsing stopped at line 3.% I; y7 U! J' X0 G% u  h( {
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
! d7 v! D* [% _; P- eSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.1 r# L1 u# S" L$ Z
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.% \& m/ v7 R9 E' ~+ E" V' f( m6 n
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')9 q& n0 ?% [# M8 {: H1 a1 ^( w
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)* N7 e& X. A) _
warning: Connecting to remotehost.example.com failed: No address associated to the name0 C0 \. Y# V1 S$ P4 h) P
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
4 [) m6 G5 u4 _; M当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:9 T6 D0 G; _. L9 L  Z2 C& p2 j+ t5 a
6 T! e$ b4 J2 w+ B+ {+ B
4、用SSH退出符切换SSH会话
( k6 g+ M# G8 Y% d1 W
1 K  Y" m" s; v( A5 l这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。# @$ t8 B' U/ L  B8 {4 m6 Y4 `

* Y( M* x1 q0 u- r当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
7 r5 w( K8 [  W3 `, T( a5 a0 H& h. M# M# [) W
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
1 @; M5 Y' W! _- Y. k2 t, [' }3 G8 w2 s
1.登入远程主机:0 |0 I7 E/ A6 p" B8 X

  ~+ H& M4 e" L- Wlocalhost$ ssh -l jsmith remotehost
) U9 R6 C, U5 w% v: E3 x* M1 I. E
2.已连接远程主机:
1 q0 \3 d8 `& X8 d
. g0 [, V; v/ Xremotehost$$ _: B- {& Z: s( x1 D' {

) |: o: R9 E5 q1 E! \$ h6 D3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。" a6 B/ i/ M+ `) z* d. m# q' Z

3 p" c! n- ~1 m# g* _当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”3 b0 U$ v& Y2 R$ ]5 n
7 j! q# w& a- j' j5 F2 ^+ [4 y/ f$ V
remotehost$ ~^Z 5 ~& r! z3 i) L2 k# h. u
[1]+ Stopped ssh -l jsmith remotehost 5 W5 C' q% W! k" T* o
localhost$8 p4 S' Q/ J, X) u& b" [% L

* E% U" |6 `  M4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:* f  U" g% U  s; ?6 ]1 v

" c7 |: o! c* blocalhost$ jobs
. U  f. S* I; ?- q) J[1]+ Stopped ssh -l jsmith remotehost: j* ?+ S% ^4 A* T3 Z& `
: i( U7 i9 Q' N, v4 D2 g! u! K
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
: z* u) A- W, @# C! W
6 V3 Y' V% u7 K2 }# y+ ^1 L: y8 ^localhost$ fg %1
8 Q- A# x  m. _1 gssh -l jsmith remotehost & T/ I, A! F4 f: v* E& w- T! i
remotehost$0 V( t8 D9 F5 |2 o0 F0 _% O1 z
) g% _6 v; D& n9 ?# V$ a* U+ C; i: {
5、用SSH退出字符会话,显示信息5 o0 e1 }% y1 y" b' q
8 @; B5 k: ]5 {( y
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。/ e% f) m* y. A% w; P
- a" Y: V- ^; Y/ v
登录到远程服务器+ s) e) ^' @+ q# i- F

2 ^: R; A( Z# |4 A0 N8 G# q% {localhost$ ssh -l jsmith remotehost. Q: w, }2 L1 d& u
$ h/ t* D* P& I) ^. e9 P/ e* }% t
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息2 `1 K  c& p! u! v. E

  ^& s" r) r3 [1: Z6 }* ?, ]4 I4 A: M
2/ B7 D' N. r1 o& \
3
" T0 l3 L! B/ j0 k- l$ k& r4
' Y3 Z0 |- F7 q# I) {5. Z% I5 _. }1 L0 [8 p6 j
6  L- ]# y! \, V4 f. N& N! b
7
" B- l" I8 _: d8 N4 p7 b: o* J8
& g* T) c! V1 Y/ _+ F, i9& f8 A: I* y8 @  Z; V
100 v( ]) r0 H: R6 T% f
11
- S/ O2 I8 O" n: ]8 W12
% ?% F# q2 j8 @1 b6 F4 B0 C- m13
" F+ f0 E; T/ e0 b( ~+ Q9 U) Z. L14
# b- T: F3 J9 \0 s, @15
0 B1 e7 V) L' v16
1 Z* D7 l+ m, x% c5 N7 ?7 @17( A- P" r3 S  r( v* A! ?
18
3 H9 m4 p2 ?5 n0 I3 P19
. j/ u+ W. u8 n/ z9 u, ]8 s# p204 `* Q3 E% {5 c/ R
21
  [& A0 }2 I( B22
9 t7 t- B2 K6 T' W7 y4 v. R23
: O6 o0 \" D, i2 E24
( G9 e  m5 D3 Eremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
6 W! W8 `$ H7 c3 vremote host: remotehost
9 _2 b: D! f7 K- q0 _local host: localhost
! x4 s0 _7 O# \8 K7 a$ `+ D, Eremote version: SSH-1.99-OpenSSH_3.9p1
/ ^- c8 `- o! k  |local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial). \" F. e$ L- d% N# W- E2 ~0 q+ V
compressed bytes in: 15065 i- y/ L! C% Q5 M
uncompressed bytes in: 1622
9 u% j- S  Y4 z; Qcompressed bytes out: 4997
' f( v2 j! S; {2 }; b1 f6 cuncompressed bytes out: 51186 p1 a  Z, e$ Z( S4 R- D) a! g
packets in: 15
7 T. X$ `! C: E0 opackets out: 24
, f' R* s$ G9 U; w4 ~6 g+ Frekeys: 0
% n' c3 C4 y. X1 f3 B- P, HAlgorithms:$ |3 X% E. o$ ~- O$ n8 S! b
Chosen key exchange algorithm: diffie-hellman-group1-sha1& Z! P- Z; j& I4 s
Chosen host key algorithm: ssh-dss
. Z7 p  v' t& cCommon host key algorithms: ssh-dss,ssh-rsa' o! x. r* N' w' L( b* ?
Algorithms client to server:
/ g4 ~2 k: F# V5 O: hCipher: aes128-cbc
/ x) O, ?/ y! b5 z* H# QMAC: hmac-sha1
$ s5 L8 O7 ~' s0 ?- G7 M; ?) fCompression: zlib
% e1 y$ H; S) N4 y. u5 j( X8 NAlgorithms server to client:; M" F6 X! q: S7 R7 V. j# R" J; e
Cipher: aes128-cbc MAC: hmac-sha14 v8 w4 o, Q, C. {) W
Compression: zlib, c' p* l* g1 B
localhost$0 a5 B! m# F# A3 x- u( w+ Z3 }
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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