找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2611|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本' B. E+ o2 \+ f# F; D" m4 Y

/ F, H/ o2 N0 z# k! F1 A0 E3 t有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
% P- q, q+ I" o# r4 u6 b  s, o) \% u' f' u
$ ssh -V
8 s$ _# ~6 c/ W: k/ COpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 20032 G7 O1 G* p. V/ Q4 r) |) y& T

9 ~$ O$ M% d) p6 h0 K下面的例子表明该系统正在使用SSH2:
. T" L6 C% y$ k" T
! x& S: ?$ O3 _& q3 L* A8 M$ ssh -V
# b+ ], J, q- Vssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu/ _0 ?# N' i/ P+ S

# D7 L: V7 w; L7 K9 E% e2、用SSH登录到远程主机6 x8 g* ^- [1 B7 i
4 U2 B. E7 ^) G! Q1 M1 i
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
. N' s( t" O, h3 i' ^' n" O7 [5 u- y7 ?( J) c
11 L3 y- i0 w) G' Z0 z# h) f
2
. f" D* R" B: |7 n& n3
) M: \# O( P& d+ R5 b' I2 s' K4/ p9 M# ~# m6 Z6 {% U3 ~8 O$ h! g
57 b3 x- X3 ~0 {7 o4 w5 ^
6
5 H+ j9 t* |- D77 C/ _1 v. y. N3 V3 X3 @/ m+ `2 Y
8
9 b+ H/ [! ~, _3 ~/ D6 Glocalhost$ ssh -l jsmith remotehost.example.com4 J+ M: {8 M- ?# y/ F' ]  q
Host key not found from database.
$ H& E. H) C' E) L) b9 oKey fingerprint:
: m8 m; Y7 f/ axabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum7 n. n  f/ |9 s. [
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.% B' @$ W4 r# q1 b
Are you sure you want to continue connecting (yes/no)? Yes
7 H! q3 g- d5 x$ [4 E0 R2 XHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
/ F! ], h: p, b# k7 }  U+ T' faccepted by jsmith Mon May 26 2008 16:06:50 -0700 [email protected] password: remotehost.example.com$" r4 U+ X6 S5 q4 P: N, {+ \' d
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。! q' d. u5 S7 ]9 g. `: l3 T
0 G" M0 B! J% d2 k5 z4 {0 I
1' W: M3 a6 l, ]4 p0 E! Z
2
( K/ q1 @8 Y8 h3 s+ g, n3( I' R, i9 [) n9 @+ k% t; j' d
localhost$ ssh -l jsmith remotehost.example.com
- ]/ [) Y5 C3 B8 y5 p2 ^* w6 p[email protected] password:8 J8 z4 v; S" @! Z1 Y, a% O
remotehost.example.com$
2 M5 n, k# Z+ \1 h5 L由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
6 S" q  ^) S% y5 `, X0 ]3 @% @: h2 f5 s9 c
o 系统管理员在远程主机上升级或者重新安装了SSH服务器 # {% w0 K4 M+ S8 K# N* G0 M
o 有人在进行一些恶意行为,等等。+ m; H# F5 x% |! o+ k

2 R, u( e. U( J5 A7 _在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。' V0 s8 f' [9 ?4 A' T: [

3 l+ D0 u$ K. d- ~# w# z& c1 a11 w% ?$ ~* ~: q# o" s# Z, d
22 ]$ d$ t" \! B) d
33 Y6 e5 e( J1 N5 x) @+ |4 n' ^
4
; ?9 d2 d! i) ]% T8 W/ I5+ ^8 r0 y( h/ H8 Y; W0 j5 W3 j$ R
6
) \* _/ I: C2 Z6 L+ L" A  V7 l7" |& n' k: o2 g3 Z+ M2 {
8# W0 d4 c; l- k5 d6 C
9) A5 u; j8 {6 w* t
10
5 H5 T2 B+ `, X5 ]9 V  r113 ~' x9 M; r/ h# g' z; B2 L/ i
12
0 R1 `3 ~+ l! v0 f139 k  F5 T) G; W4 q% t' k3 U
14
) K+ \' ]: ?" J7 e158 M2 n% u, X' F2 h
16% Y- m5 }( m% T- ~5 H) o7 y
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6 ?9 Y% X, S# ]2 x1 z@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- m8 I# V" k# Q0 }5 ~4 l/ hIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
3 L0 B+ Y4 y. I- a) V6 m! O# t0 nSomeone could be eavesdropping on you right now (man-in-the- middle attack)!
9 B3 I8 o7 @) R6 `It is also possible that the host key has just been changed.7 x7 H2 S* Z, L$ P" t
Please contact your system administrator.
6 P' S; t* Y% E6 @# C5 K2 a! xAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
% i  b: o" d2 K3 r2 UReceived server key's fingerprint:
: z! Q$ e" h4 g( T3 X4 ixabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
! N$ w2 ^3 l' o. S* g. XYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
+ b7 W, d+ G* U2 O0 S6 {$ vAgent forwarding is disabled to avoid attacks by corrupted servers.' L5 k$ U" c8 D4 D# \# O- g) Z
Are you sure you want to continue connecting (yes/no)? yes; P  d. o( r' F: ]  R, ~
Do you want to change the host key on disk (yes/no)? yes, ~) w; k( e. Z
Agent forwarding re-enabled.
7 T) @* H* P1 D" EHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
" P0 s. a% S. P: S, U# maccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$+ ~# t- R1 Y6 x; j0 }
3、调试SSH客户端会话- W2 S; H3 Q& a7 i7 n! y, x) g
7 i# [- m5 i5 ^1 ~1 G
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。$ I  I: G6 |8 ~" F8 U

* J: f5 l; ]9 B1 s0 @, {没有SSH客户端调试信息的例子:+ e# \7 s+ N7 v! [+ G( D0 k% |3 A

5 V0 }6 A* ?9 s6 j9 U, D( O1, {8 }" d9 y7 y; J; Y
2& {$ W* J3 H' i- X/ T. Q! S
localhost$ ssh -l jsmith remotehost.example.com
" ~0 A/ x1 @( f% W" a" Ywarning: Connecting to remotehost.example.com failed: No address associated to the name( q5 k* q2 [4 O
包含ssh调试信息的例子:
# A7 x) }2 T! V8 W
/ ?5 G1 ~# b+ Y- h2 O1- C  l, X' i/ s
2
1 C2 d5 E* I/ U: s- O3
$ ^( t1 c# K" i2 a4
; U1 }3 _' S/ M2 d6 I5
0 H0 Y# j, `7 ^+ A3 b6" i: Y+ _+ b& a- M3 O: V
7
- S! t, Y+ C- T1 y8& i% F8 Z1 _- ]( a2 W7 u* w
9) i. t) [2 E1 ]5 m, w: A
10
& z9 b% t# z+ Glocaclhost$ ssh -v -l jsmith remotehost.example.com$ Y5 m& W9 ?# r! m
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
( j1 {0 O, t5 t2 ?1 j! J; W, XMetaconfig parsing stopped at line 3.
# T, t, e0 _7 T5 i, d# w. bdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
9 X& J! `6 Y3 i2 Z7 A# a1 gSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.0 ~! t! P3 ?2 v& Q+ F* N
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.6 [/ [6 y5 L" P; @# j  N
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
. q" H8 u2 b9 o) X' ?) `debug: Connecting to remotehost.example.com, port 22… (SOCKS not used): N+ [3 w. R8 |$ c; h
warning: Connecting to remotehost.example.com failed: No address associated to the name
( k5 |1 U- D# f8 }/ j  F, u2 p[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
6 J& R9 d9 T5 y3 [7 H% _& x当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:, k  z, f9 K* d( i6 b
4 W9 b" C5 P( c. D- l, J" N( r
4、用SSH退出符切换SSH会话
. n2 y* F6 {! T
1 ?" |  S% v6 m3 I, h. G" _这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
8 w0 r2 B4 ?  x- [/ v% H* I
: ]. O& ^& \, A; B! y+ {当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
6 q6 f4 w; B6 A6 C5 u
1 b% B  |$ l3 S$ ~# k3 S当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
2 U: R# g# y' h, w0 f6 Z! U8 l! W# e5 K
1.登入远程主机:5 t0 |3 b. Y5 l$ o& v  Z' Z

$ @$ n  E2 L* x* T/ n0 W7 C- tlocalhost$ ssh -l jsmith remotehost
$ \4 Y/ ?* g0 m, n. ^: n# F
+ ]1 C* k& W6 B2.已连接远程主机:
9 {0 L5 h6 a+ }  G* e8 p# y
' o8 c( \6 J, k+ U& b6 Rremotehost$
1 v# H/ M1 B( q. x+ H. B) |9 k
9 q8 Z7 t  r, q) T5 I, }3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
/ {' x$ R( N) x# e! T% K9 O- Q8 @+ N/ z" `9 J6 l6 m& f) }- J5 A  s
当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
- J4 Y3 p; `5 {4 n/ w9 N& I) D" o7 w
remotehost$ ~^Z $ o; ^! Z! x6 P: z6 I! I% B
[1]+ Stopped ssh -l jsmith remotehost ( z6 v+ f* ^9 o) k
localhost$  P+ v8 C9 W& m; ]0 b

9 l5 r: _; X9 N% P" I4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:" Y. k  w, n1 L

+ {  M4 t8 l& Tlocalhost$ jobs
) ?3 w* S, Q" N* x- [6 R[1]+ Stopped ssh -l jsmith remotehost+ |. w0 m' g- A  x, S

  ^$ W, N& k( C3 W" p4 y5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码0 z$ e! Z( s, I& J* F8 H, v

) i  _6 G  ~- A) `: [. q$ d; l# q, wlocalhost$ fg %1 - Q4 d, {' }- u: v. L( {4 ?
ssh -l jsmith remotehost
6 q/ z+ @2 z- G: hremotehost$
8 {, i( x1 t2 t3 e# d/ e! g+ Q
2 n9 h! k; Q6 I: e/ \$ W5、用SSH退出字符会话,显示信息- V% d# |1 r4 I; c" f

) v4 `; L/ q5 Y8 U$ o( B: W要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。( e/ B2 t* g& O7 z, J  p& N

- b3 A/ Y6 t/ Y% S登录到远程服务器& w: |$ h, p( |: j
1 ~- W# b- T' B& ]5 Z, m
localhost$ ssh -l jsmith remotehost
& N9 G( V9 M9 C( M
, ~# `' h9 ~. s6 Q* `如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息( C& e4 L2 p' Y0 ~9 H1 R7 N8 n0 A4 H
) u" D0 r" `6 v
1
7 t2 |) N$ G: h" N& x9 j2 b2
. R: x7 u' ]& K- o: W7 S36 C5 R( L7 a3 R
47 _( m! A; {" X7 Z* L
5
9 N$ d+ i) f6 [7 q& P3 ~6% L! C. d" \, U7 G1 I
7
9 z  I( u5 u5 T8
1 @4 }+ {, j0 b8 `" o6 O9
/ q! u) k& c7 f( n) a# y10
9 h( A4 r/ A0 F7 b112 u8 B" O+ M6 u. Y% N
128 h$ D0 C% [# X" }2 s& d
13
+ m1 @& M$ d$ w4 {% `0 N14
8 ~" @" M9 z1 Z  ^& |: |155 G4 _3 t/ S, b7 s2 F' i4 G
16) S" v$ _- g) J2 ^' X# ^
17- g6 X  T1 s/ Y/ J$ Z& z
18; x  Y$ B" B) |3 K! R& K9 ~7 V
19
8 v0 i; g8 l; ?, T6 g20
5 D/ B5 ?+ k8 A( p+ q4 Z21. Q- p0 G# A8 k( F
22
3 `1 f9 ^. d' v1 B23# J4 }) {$ ?8 s& j7 o
24
3 p  g  T0 ^7 `- y2 H1 gremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
$ H' U( U# T' Eremote host: remotehost, y& h8 S: M- `; f! L0 k
local host: localhost& ]5 q6 o+ V' W4 y, {+ V% r+ x* p
remote version: SSH-1.99-OpenSSH_3.9p1- b# ], z7 m* D5 t" p) m
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)' g0 ?0 m. e, A3 p7 d
compressed bytes in: 15069 f% d+ R5 O! y7 Y2 }0 w1 [
uncompressed bytes in: 1622
7 q" t3 q$ r  @compressed bytes out: 4997
- m  I  q3 d! u, K8 |6 cuncompressed bytes out: 5118
- G3 [+ f7 A0 H; Ipackets in: 15$ S# i, E$ \- m5 o) s
packets out: 24
3 f" F6 w) Q+ l- T& krekeys: 0
; |  @1 [0 A. I9 |1 P9 XAlgorithms:
: M- e7 A! E1 I* cChosen key exchange algorithm: diffie-hellman-group1-sha1) R' K( x" u, y2 P
Chosen host key algorithm: ssh-dss
8 n' [. H) J3 F6 A2 pCommon host key algorithms: ssh-dss,ssh-rsa, g8 j: c; t, N2 V8 M
Algorithms client to server:
, p2 _$ M% `  S. eCipher: aes128-cbc
& w$ k. Q) w0 W, S0 \) QMAC: hmac-sha1+ A; x4 r  L, K7 L
Compression: zlib
" s" X! p. U8 @4 X# `Algorithms server to client:
: e  ]5 @! z$ y  SCipher: aes128-cbc MAC: hmac-sha1
  `0 T+ j) F8 }Compression: zlib' r# ?4 V9 d9 N  t( t' Y3 `# j$ L
localhost$* o# I) Y% p' A) z
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-4 23:24 , Processed in 0.080986 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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