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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2366|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本  m$ \8 x- `6 ~% ]
4 m; L0 u' ~5 z5 X
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
: g1 S; \5 B$ P& p0 z. M
  w8 x- S' N3 s0 L1 U" |$ ssh -V
$ z7 t! F. t% o) n8 e7 m; pOpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
8 c8 \& c6 `, [# E0 ?- {% D% V* X3 g* C0 h1 j
下面的例子表明该系统正在使用SSH2:
2 w$ S2 R; f! W" N' P/ S
; u+ p* I3 A, Y* c) R% E$ ssh -V % E/ E% \6 i0 L- q3 [( I. i& N
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
' T0 h6 E4 B4 }4 |
% D/ }! D- I/ U! [! O2、用SSH登录到远程主机
2 a. M; W: O$ h/ |: t
6 J# |2 E& e% [当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:1 T; B0 _' b( J+ t
9 Q' J% \/ V! _
10 {0 @# s# C8 u4 ?& y
2
# @4 v* L4 I: s3
4 U7 Z8 c2 N/ j4( x5 S/ m. i3 J; C. }- i
5
8 s% Y6 }6 }7 r6 H2 j( O, m  o6
4 b7 {1 X. Y" N0 @7! x+ C7 [+ X3 l) c) c
8
% x! y. X2 P$ _, r; x$ Rlocalhost$ ssh -l jsmith remotehost.example.com
1 m( F# h: `, c" G+ F4 yHost key not found from database.
7 C9 g4 D) E1 t! l; O- l& B" u& xKey fingerprint:
- O. c. O: g! a1 v( H' z+ f/ k6 sxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum5 g4 [: e2 M3 v  x8 d5 J5 J
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.$ {7 m% v" Y, n/ {( c- r& j2 H  |
Are you sure you want to continue connecting (yes/no)? Yes3 r4 l- u) I# V( O8 q* h+ @2 a  c
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,7 y( V1 p: t' O5 W( }) N
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
) f. ?8 N7 d, v3 D因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
/ T4 w  h  Y: `5 [( Z$ B) K5 z* w$ w8 _$ P- \
1
- @8 K( |1 ^$ }# ?9 x2
# o6 c" \% u3 x! b$ e3
- C  r8 |. H' _localhost$ ssh -l jsmith remotehost.example.com
; j! ^6 ?6 Z. r8 P9 [0 Ajsmith@remotehost.example.com password:( y* a- E8 S2 A9 z
remotehost.example.com$
8 e$ m2 i5 Z0 m: |6 U由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
7 M1 U* G6 o1 K5 }! g3 c2 x
3 C7 k6 F( y' Lo 系统管理员在远程主机上升级或者重新安装了SSH服务器
% ^- L  ^* j$ w! H- Bo 有人在进行一些恶意行为,等等。  \  X$ X6 F% _
. t. O$ C0 S' J5 t* ^% k
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
1 t0 n  L( c3 m0 t2 {* U( x8 h5 {% X( [6 y0 J+ h
1
/ Y5 y: V5 T$ W4 ]# G2
: x( \' n$ f8 A) F* |3! h5 b- j# o0 H- P' Y9 e
4
; D# q: E3 ?& ]5 ^& K/ x- p) i5
6 x4 y4 d2 y/ ^5 d6/ o3 s" w- Y- U  Q. c( k- Y& l+ s1 Y
70 U. `6 H! F; b' `; J2 y, {
8
7 c1 X/ y1 ?, }/ m9! p, R( W+ X3 |, v
10
7 V; @" E" l* w% i' m- f% b. M11
5 l8 B$ R2 k4 g. L; \* Y12
4 \4 w4 `( j! Z  [4 M; U6 z) c5 p8 G13
0 T6 c# P* i/ \5 h4 d0 e14- o* F) v% r7 A& l) D9 R
15
2 R$ l* S2 v4 `+ o16
* b" Q9 _; ], n/ b' w  \localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@( Z% x" W& H# Z2 c- K! ]
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+ T' O. y) r8 F4 c7 EIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
0 D$ o, @, d- `Someone could be eavesdropping on you right now (man-in-the- middle attack)!* c$ i5 ^! E6 X' a9 z; C2 Z
It is also possible that the host key has just been changed.  U( g' i4 U% L0 S" p" d3 Q
Please contact your system administrator.
# x) a4 `8 E5 X% R5 Q1 ZAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
" d( B8 w: K2 l' W8 `: ZReceived server key's fingerprint:$ I, H" G# h/ O( o
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum  c- u) E9 A* r% E/ @
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.1 u5 Q/ }( Q* K- R2 K. P3 R
Agent forwarding is disabled to avoid attacks by corrupted servers.% I1 e" b( D4 F( Z
Are you sure you want to continue connecting (yes/no)? yes6 q; d/ Q8 _0 g/ x# P% e. D
Do you want to change the host key on disk (yes/no)? yes' ~$ L, A! W: m' V) z* j. N5 Y
Agent forwarding re-enabled.
5 ^& e$ @& b. f5 r! @" hHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
  y: ]" d5 z  |8 m7 \- f* n$ B7 qaccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$7 y2 y& ^' ^2 t! E
3、调试SSH客户端会话2 o0 ], e( A; t) q7 |7 c

" B: r  g7 c# A0 k0 L) D当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。  F  y( m& i$ h" Y! z. n
& U. K0 b1 ?% |& b
没有SSH客户端调试信息的例子:
9 k0 s: n* C- A7 O% ~) a4 M' L' E* G
1' t( ~0 S4 \. X# J- [  G) R/ y
2" @  d5 C0 @+ b6 S0 l( W+ D! \
localhost$ ssh -l jsmith remotehost.example.com
/ M" x6 U3 Y6 }  C+ l% F' _8 Jwarning: Connecting to remotehost.example.com failed: No address associated to the name
' a6 M$ }1 B- a; O包含ssh调试信息的例子:3 B( l/ t9 u6 n' l( R
( O$ M; L: x: r( o5 g
1" K6 ^2 W3 w* T. l
2
  t/ @# f: T$ h% D  O2 s3
7 A4 Y9 y' H3 [- Z$ Z; |4! k- {( R' @; p' ]& `* F
5. D" N; F3 J' C! [  ?$ w' U
6* X. t* `- Y5 l% b& S6 S
7
+ A, g9 ^5 r# Y) r) O' b5 L8: w+ s1 y* `: c4 x, M/ T! }
99 N5 ]2 E  o8 h% o
10
* S) J0 @7 w8 ^) O" {! ulocaclhost$ ssh -v -l jsmith remotehost.example.com
( N' ^" I7 W! Q' O( Edebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
6 m3 ?& m2 m: j) \9 z# JMetaconfig parsing stopped at line 3.
. p+ o3 a: a) Y  g' _2 ~5 H, Jdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
! ]3 F! G! [0 s; ]* L- Y  X. HSetting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
; |- A4 D- X  ]  w" H- \debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.: [* Q% X$ w4 l+ q6 j0 J
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
, [9 ^7 w5 O8 C1 o8 ldebug: Connecting to remotehost.example.com, port 22… (SOCKS not used). D$ S& D! v( M# U$ m) y6 p# I* H
warning: Connecting to remotehost.example.com failed: No address associated to the name" U9 W9 s- T- a) A, k) E- W4 f/ a8 |
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
% y% T7 g, o* Z* d, ^当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
5 ~( L+ l- {/ V/ g/ O. V9 B; U! {1 _. e2 W% x
4、用SSH退出符切换SSH会话' O( j& |" ^" e- C
6 R8 B+ {% \' y2 K9 r* Z
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。' w$ Z+ K) D1 A3 J* m0 [
5 x4 ?7 [6 G6 V6 _& Z* m
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
% G8 W- S+ }8 N" P6 f4 ]' v
5 A; z7 g" _9 W/ c0 H当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
: i) r- E; _  {* p+ A+ `1 g2 q1 s' l1 [# v9 S0 X0 u
1.登入远程主机:
$ P2 y, r! E1 B! p" P
) o5 t; [" M. a0 M2 _; p) V! n3 |localhost$ ssh -l jsmith remotehost
. L2 d/ a: P* D" k. T) p# d( I2 V: F. T* M( R3 x: D5 B$ ?
2.已连接远程主机:
# k- L& H* U8 l/ ^% c, A0 b& N, T  q
remotehost$
8 i8 ]# g  H7 F$ z, b
$ y" t- g2 b+ P2 H1 o& U: |3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
4 |9 ~9 J' ^! `0 R3 ]" q$ d" W* E0 r
0 S; r7 u! u, \% K( b& o. S当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
' O2 n# g2 C1 o: t
4 A% O' }1 f6 x) w: q9 Qremotehost$ ~^Z
2 f+ Q' s% z. C! c& ]+ Y[1]+ Stopped ssh -l jsmith remotehost
5 u* d+ t5 _$ i6 U/ n1 slocalhost$
1 W$ H# D' ]( H2 W9 S1 E4 `+ M, h; y  m
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:" L/ b0 L$ `1 `4 a: e

) e, b- b5 k+ I! V! V# Hlocalhost$ jobs 4 f4 O3 Y' Y5 N7 U. x
[1]+ Stopped ssh -l jsmith remotehost2 H; e. w! Y3 Z- a. n

) y  L# C2 H0 n* ~+ e( n5 C7 x8 {+ n5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
% Y1 T- Q8 K5 a' `$ R
* }2 Q6 B8 u6 g8 Clocalhost$ fg %1 1 j6 L, g1 t, \
ssh -l jsmith remotehost
# m8 D5 ^3 R& i  J9 a1 T$ qremotehost$5 T6 F, y6 K- S0 S

; D9 n% O$ C; z, k9 c5、用SSH退出字符会话,显示信息) k4 L3 q. P! B. Q! z

; N6 R+ C+ {5 d7 R要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。% j; a- I: ^8 j! k' `  d/ G

% Z* _/ V3 m  g% `( Y- _登录到远程服务器
" m* V8 q$ \' T4 d: `
0 B, n/ z; N% l8 c* [localhost$ ssh -l jsmith remotehost
5 B4 s% a  O; x
3 u. H8 m) z  g4 H4 J5 _! A, f如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
) b9 O: V; I# a: j9 B9 ?  D, S/ \: Z- w# i
1
/ B  _: ~# n1 j7 [% O2" t; f' ~! X9 h$ C# V# O
3
  T# F" O6 h+ C# N) }4, J( x  {" O0 M  z" q/ {
5# {3 V. T& p$ \8 D2 G8 ]. ^0 T  o
6
" i0 |+ y) j1 t. F9 u7
9 N5 T2 `3 A1 \$ |; @87 C" e: L9 q8 E/ V
9% Q+ Y; q0 B$ u7 E
10
0 y( H& w2 M$ F: s6 ~11# }. _0 S' V) X) C1 N, v
12" Y' C' p1 x4 M
13, x* j. z# w! W: w, _$ Z
148 Y; \$ M% \% J) u5 K) d/ z6 _
15
# G, S3 F/ \, R169 i% R; M. \# W: B6 Y/ }
17
  q2 S: Y8 Z. L& L# S# a18: T% M. B& N0 {& D
199 d3 \+ ~  F" `5 b: [! i4 F
20
6 v% k5 K9 m$ j6 P& |& j* H21
2 ~% M, {3 L8 ^2 v% Z3 {2 |6 C, Z9 z22
1 t$ o$ L- o5 `+ X1 y$ I/ W" }/ O( @0 B$ G23) c( |! s  A: y5 }+ m# X+ w
24- x+ f! V1 t* P. R
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
+ L+ [3 r! I% @remote host: remotehost) i+ n9 n0 B: a$ x$ ]9 O
local host: localhost
$ V3 _3 m6 s& V6 M/ [remote version: SSH-1.99-OpenSSH_3.9p1
- {, G0 e  T  e* L* _# A( b* c" vlocal version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)4 D( L  g4 b: Y! s! f, z7 z. _! R
compressed bytes in: 1506
6 p# x6 k! |1 |; [+ F! A0 T- b2 C, h5 Buncompressed bytes in: 1622
6 U. H& y  l$ ?3 t- [9 E! ?* ?compressed bytes out: 49971 i; l. K: h2 L/ Y' h
uncompressed bytes out: 51188 r) w* t! m8 ~% r
packets in: 15
9 I( y  u+ M  E. h+ h" v7 o& R) m6 @packets out: 24
* |) W. h/ o* z0 Q; Vrekeys: 0# H6 {4 s. n* P2 ?  c
Algorithms:
) E) f! e& I; z; KChosen key exchange algorithm: diffie-hellman-group1-sha1& U# ?6 w: n5 J; h
Chosen host key algorithm: ssh-dss
4 P3 l' e: N+ X! j+ T# n: j- |Common host key algorithms: ssh-dss,ssh-rsa
+ y$ s% f% `, ]' i# p; \' JAlgorithms client to server:5 z2 V, Z0 d; G0 k
Cipher: aes128-cbc
' ^$ ]  X9 X. U/ rMAC: hmac-sha1. a! t: T( y$ v6 y( u6 s" G
Compression: zlib0 H; b7 z& H% s" q) E/ G
Algorithms server to client:( j0 B% w6 G% R0 c! f% Q; N/ ]
Cipher: aes128-cbc MAC: hmac-sha1. h' u6 Z! C3 F& u5 |$ D
Compression: zlib- r# C% F' K: Y: [, [* Q5 `; t
localhost$
% }4 _; N8 t1 b' ?. P以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-20 14:17 , Processed in 0.088158 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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