找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2668|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本3 m+ O+ F% l; @- p0 p7 x5 \
# w& m+ T% G/ h$ I5 O0 t- R
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:
9 B/ W& ^. f1 P- f( \& R" s: J, p2 R! v* Z: `+ B
$ ssh -V . R+ U% Y6 G# e
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 20038 g0 O! P' G# X' u

+ T. q# F" M1 b" b下面的例子表明该系统正在使用SSH2:) @1 j9 t- ]9 h- v) Q, m9 o
3 p6 Z( j! N8 ~$ B8 p6 `
$ ssh -V
: E8 M7 ]- e+ l. bssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
3 C8 P, N# O' j& a5 c
& }* ?1 F% H4 j7 t1 }& \+ V# i) ]2、用SSH登录到远程主机
) R# F2 B7 x- c3 @" Q7 D' V- J5 f
2 v7 |+ v0 n7 I5 B: j当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
) H8 B+ _6 L* d9 {3 M7 U" n+ W
4 z  Y  H& U: ]: F1
, {$ h" t/ c3 M$ H1 D2  p2 J+ }# s6 I6 u1 l: u
3
5 x1 T+ A( o& {; u. g+ r1 f$ O45 ~4 K8 s' N" z0 n
5
9 ~0 V6 T8 f; T  ?9 Z) a6
; P  b- \& f; r% z7
3 Y5 Z9 h7 [# p  s! R7 j4 e! X8! P6 u/ \- W# W$ E5 i
localhost$ ssh -l jsmith remotehost.example.com
8 ]; j2 m4 @" a' v& w, G! lHost key not found from database.1 _9 W5 t7 D% k
Key fingerprint:6 X5 [% L3 x/ j+ l
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
4 q$ I8 m6 F! T$ p! O8 pYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
, r. f$ ^; D7 m: }, C- B# XAre you sure you want to continue connecting (yes/no)? Yes
+ X  h7 E1 t) X$ L9 V+ S3 F0 UHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
/ d* Z5 @, g: S3 c5 O8 |accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
- P* r8 x# |5 w% `) K因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。( U' i* c5 n5 m& ^4 {; I

( J4 y" H  r( N3 T  D* a& A7 q1
# n5 v, q. O! S' I! V2 r* c1 k2
4 P# j7 z( U0 C0 J3! I2 Y' F( j$ g: t; Z
localhost$ ssh -l jsmith remotehost.example.com# x- ~+ J5 L4 n0 Y9 j& ?) ]
jsmith@remotehost.example.com password:
0 m% `. z3 D9 [3 Z) }remotehost.example.com$. @+ b* A- R9 w2 J( R
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
$ _) W6 h* [$ s9 P2 R7 ~. J9 t( _& R* S& P7 N' N
o 系统管理员在远程主机上升级或者重新安装了SSH服务器
4 x) ~8 _2 p  f$ _2 a/ qo 有人在进行一些恶意行为,等等。
2 U6 E& Y9 x* V4 K4 q! i& H, F, i* c8 P( \- I* s* A; i; F
在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。7 M5 U; C" X. E( j, K3 P% X9 w8 X
% @) b1 a( q& ^) E# F8 @5 ?9 h
1
# i& }5 n! Y/ F  s0 `" v3 s22 y- w( Q2 |' `* H
30 Y; y9 P! b1 _: G( X/ k1 V8 D1 T
48 Z' m8 i* a# M. A1 ^# ^
5
1 e- q5 h6 C. Q1 t; V6
* h  G$ b& |+ p" O' t+ N$ J7
* [4 A9 y9 j6 L# g  Y8
1 Q& O8 R: J5 N0 _0 l3 r9
, g8 ^! e. U7 T+ P0 u6 i, f10, C/ U7 k5 P% I2 b8 `% x  x
11, e! o/ H7 n8 f" q' o& _+ O
12% a4 o& u6 ~" f4 X& S! e+ S
135 V: ?( B) M! m8 Y/ c% G4 m
14
5 v2 F5 R# ]6 r6 l% {15' H$ M6 W8 k0 F; Z
16
9 P# o6 E0 Q1 dlocalhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@6 D$ a' g% i; A* n3 i" N- ]: ?, O' Q
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@! ^; a& [) D8 ]5 a: L
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
* s' g! r8 Q" H' L. iSomeone could be eavesdropping on you right now (man-in-the- middle attack)!
  q# n- I% H2 s. JIt is also possible that the host key has just been changed.: a) @  a* ?) V) H! I( k$ k
Please contact your system administrator.
( y- b/ S# b0 h+ v4 [( ZAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message./ f. c* O. [' g
Received server key's fingerprint:3 o* ]( g1 N( j# Z7 P
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum2 J7 T8 W! o: f2 a, V! `
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.( N: |  g4 a  A9 |/ \* @* S
Agent forwarding is disabled to avoid attacks by corrupted servers.. R5 ]7 [9 t2 @) N: S1 I% {; H
Are you sure you want to continue connecting (yes/no)? yes. `+ z( ~( [2 |
Do you want to change the host key on disk (yes/no)? yes3 h5 E: F' k7 B/ E( |
Agent forwarding re-enabled.
& Z1 u4 ]7 H& ~  o2 HHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
2 u: o: r+ ?  |3 {, jaccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$1 `/ A  Z7 D& o1 F, j. X8 g
3、调试SSH客户端会话
5 K* s: K0 [5 p. w# A" F. U! L+ i5 H$ [, k
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。( \8 o' S3 ^( ~
4 R& R( Y; U9 Y1 e3 z* h
没有SSH客户端调试信息的例子:
7 ^2 r0 Y. |1 ~2 B4 j2 m0 r( L! }; M# Q" g5 t( C
14 Z# e9 h* R4 C" x' i. [4 M& W
2
$ k/ N2 o/ H! ?" }  b( Blocalhost$ ssh -l jsmith remotehost.example.com! f% D. |% Z& j$ h
warning: Connecting to remotehost.example.com failed: No address associated to the name
9 R" w! v4 X9 k" ~3 ?) \包含ssh调试信息的例子:
4 B. T) T1 f( B* S- _5 ]0 O( \$ {8 l: ^5 ^$ O: U. R( I
1
$ H1 F' Z5 l. W  ]) D6 j+ E) V  q2- R& S7 u  k* \
3
; k8 |" s* Z# V  a% a: Z4
; o2 A  f6 c$ s& t' v5! |: l$ M- s" ?0 L8 x7 r
6% A/ I$ l4 f9 T/ @# Z$ X& j
7
) V$ b; [- u- q, ~; P8+ M2 O4 g7 ]0 a- x( I
9  b8 h7 g3 Q& z7 ^9 E1 e4 K9 O. j
10* b+ M5 \( y) I# r
locaclhost$ ssh -v -l jsmith remotehost.example.com7 `- c1 m: I2 D; {1 g; _
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
8 o/ _" b8 A7 i9 Y% `2 FMetaconfig parsing stopped at line 3.9 m. w9 ?/ n8 S4 p# |6 X6 m& Z
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
" G/ I9 U1 Z2 U: V" D% Q1 S- ]Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.& T/ p# I1 ]" l& _$ A! Y
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.' B+ t6 G" m) d6 j8 A( ^5 J7 ?
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')8 o  U4 R: e8 g+ J7 `/ a6 b
debug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
" a! j, c9 C$ X, b! Jwarning: Connecting to remotehost.example.com failed: No address associated to the name1 X; X! y7 G. _5 K% v# d
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
8 ^# s  ^0 W- i- p- U' ^5 q+ z当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
" f. T; E3 a( g) \* N
  }7 ^* m( {$ P$ t- C* f  o4、用SSH退出符切换SSH会话  r: d! _; @, E" H# N9 r. @& [# V8 Y
) n* @5 T$ Z: z% x+ ?
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。5 j2 ^+ }. J- X% `
, b8 t* P  K# ^' y
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
: B' ~" F( a4 R8 ]; A0 e0 E1 s7 \3 q; s$ T
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:7 K% s+ f2 p# K# X2 {6 V

, F/ c6 h& R% M) d1.登入远程主机:
* _9 n% I+ ~) m1 b! s! }! p8 h! L6 w* Q2 @2 ~
localhost$ ssh -l jsmith remotehost' \+ u. c5 ]- c% d, L6 @, X; c# g
; ^& c$ \) p0 D, C
2.已连接远程主机:: ?" d9 @2 J! q5 l

3 E- f& Z% b5 H3 D8 xremotehost$
1 a& W" t. J9 N$ \3 g8 M' m2 ?  j8 I4 P5 @5 C5 G( X# {
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。) B) v2 v# }) c1 w1 l* V$ q5 t

& l+ I3 b3 h4 b' j4 n当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
0 ]& z$ l! ?0 V- w
) B, I, E/ Q, M8 W8 qremotehost$ ~^Z * [( j8 e6 }' ^& l; w
[1]+ Stopped ssh -l jsmith remotehost 1 l5 G) T. ~% Q. t' W- u3 |
localhost$
+ [  Z5 g$ M) L$ s- q$ s, _8 p  U6 X3 [6 e' O$ A3 C. o
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:) @+ z3 g! x: t/ U5 c4 o- v
  I/ `1 u& S  x. s* H! p' f7 w
localhost$ jobs
- i! z' N6 y8 C- _+ ~! X[1]+ Stopped ssh -l jsmith remotehost
9 m7 a- t1 G  I3 B' y5 J' L$ [, E& }& v" U
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码
6 {: ~) x. a3 A8 _+ n1 [' w; b1 N
9 T' |" G9 R. F4 d2 llocalhost$ fg %1
* E# G: |* G& g9 f8 Yssh -l jsmith remotehost . b! J; L' S# S! H& E& t5 a' k; b2 P2 H
remotehost$/ g7 I, ?) R1 Q# X) C! }& ~

3 @* e. G/ f- ~5、用SSH退出字符会话,显示信息, y, _; a5 [5 O0 L) l) ]! J
+ ~3 `/ h6 t% f' v! L
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。1 i! ?0 v& R, g) \. ^; h- R

8 |& s- W6 h( g' A登录到远程服务器7 s0 H" h. F0 P% {

/ l* P5 Y( K4 [+ Flocalhost$ ssh -l jsmith remotehost8 S: H, Y1 s/ u6 W. p7 w! h2 Y

* m. q2 h0 z; l, J* [如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
' m6 I, @2 g. U0 T  d) P% I' ?7 ?$ `  n: R
15 v1 B# q  f0 v# g1 n9 O
2
4 a% y9 T4 H" q2 [4 ?5 k3; f9 l3 N/ w" H* w$ d
42 D; C  A7 s0 R, A7 b- n# ]
5
$ P  i) f8 G& {: y) n6+ R# S8 i/ a6 {1 T' \+ S' r3 A
7! u4 V8 h5 b3 Z' f9 h
8
  q& v" S. O" s9
( o; l: h1 B% W, C10' V, s: F! h  y* Q
11
% ]" w) ?  j* ]/ z1 a, `12
" G$ Y; I8 A6 D! }# G+ Q3 t  G. Y130 \% p6 p, `$ X4 m; z# c/ Z; G
14
3 l. L7 k" i4 P" s: J7 o! P2 x% E15
* r- P1 z6 E2 e3 E0 D16! C1 p4 \% C$ x; I. o
17
9 C7 s4 M9 r/ T  ^: K' K5 w: q0 T18
) K6 Y) G9 ]6 Y, \19
1 U; B( I7 p9 V3 l! L5 [20$ w- |! F! P0 y$ {: B! E" r( g
21
, f; i4 P, |- ^0 X0 Z, T' O$ Z22
6 W* p, i9 E! t% E1 B, |23# d$ A* j/ ]  s( w
24
1 [4 F0 j0 G; X" m8 b4 i! Hremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
0 Q4 I+ {( H+ h  `) ~4 ]remote host: remotehost
* P& B3 ?$ Y/ c1 alocal host: localhost
4 W  q4 a1 N8 p  |+ P1 Yremote version: SSH-1.99-OpenSSH_3.9p1
% K5 n* W. ?0 L  g5 G8 nlocal version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
; r8 F" j1 S$ g6 V, lcompressed bytes in: 1506
0 |0 B0 D* r2 P, }uncompressed bytes in: 1622
( C& E: N8 L6 s0 H, R2 ]3 zcompressed bytes out: 4997
" T5 U, y  [4 }uncompressed bytes out: 51189 P! I) c' U3 Z/ E
packets in: 15' j$ D: [* b% i' R. i# T/ e
packets out: 24
) I1 q) o- m7 }; u# w6 G0 [rekeys: 07 N9 A# x* e, y
Algorithms:' A7 m' m! w- N0 v/ t4 ?* R
Chosen key exchange algorithm: diffie-hellman-group1-sha1
8 z6 C' A& ?+ z1 N, O( ~& NChosen host key algorithm: ssh-dss
7 R0 Y! M* _9 y' L( C$ w3 w! u3 R0 ]/ ?Common host key algorithms: ssh-dss,ssh-rsa: E/ n' T$ g/ c/ f" n) g% \
Algorithms client to server:# ?4 E& b. A9 K, C* x: a7 n( |
Cipher: aes128-cbc2 l( N; M: @) t  u8 E
MAC: hmac-sha1
% k" y, |0 K$ y5 x) E' z/ g& rCompression: zlib
% n0 W/ q! C$ z5 }1 {  \+ k+ _, M, HAlgorithms server to client:
* g5 e& Z3 C% d' O2 DCipher: aes128-cbc MAC: hmac-sha1
4 u3 r, Q* d. U9 ^3 `+ ACompression: zlib
' L5 K" G4 F$ g5 ~localhost$7 {1 u5 {' t4 O9 l( U- I+ W
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-14 14:59 , Processed in 0.093934 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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