1、查看SSH客户端版本
( e2 z" u! E3 m4 o: x
* ~! x3 ^: y+ J7 Q& ^8 s有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:0 u" n. v1 B% G+ j" A5 ]
; n5 q. z7 J& S8 O9 i( F( p0 q$ ssh -V
8 h5 D/ i6 C6 _8 D8 yOpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 20036 q/ f# e/ Z$ a3 Z/ Y' `
- M3 g' d* g! |下面的例子表明该系统正在使用SSH2:
) M( K: w1 d% P$ g, O. ], w6 C4 p0 P
$ ssh -V ! o" G* D) W, V9 f( v0 _, ?
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
1 Z' i! s; o7 P# v* c4 W+ N
: D" d7 |& y6 F% M, |2、用SSH登录到远程主机' K* ?# C$ p( d) n2 u& {2 `9 X
: p: x& f: }3 X/ B; n当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
4 B% |! E5 p2 j/ v! q5 |0 M
8 |% N4 q8 v9 n$ N9 L4 }) G1
1 _ |9 j3 K7 J- Q2
' D2 g! I# x, i* Z. w6 B f3+ M$ D7 ~- C' m! ^+ w3 F
4
& C" Z u) h. h, m5& A# ~# [* T' G7 p( m2 y
6 A5 Z l, S0 i% A
7
. b, F, [: P4 l8# z# Z" o6 l% r/ `: b* _
localhost$ ssh -l jsmith remotehost.example.com Q$ i3 @6 N: ]0 I% _5 P$ C
Host key not found from database.
7 P' E- ~4 ^, c9 h4 T; E) `/ `; SKey fingerprint:0 ?& I- R9 g1 z" ]
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
/ b# o }* s4 d& `- L3 U4 A! S/ S, iYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.+ q+ ]/ Z- e- v9 a8 V' \& Z7 D: q
Are you sure you want to continue connecting (yes/no)? Yes
, J. {/ `. @( ` L% [# J" vHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
4 S2 x! R6 c% l; D9 taccepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$- f; o: {2 |5 W# |1 S$ d
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
. I' @& [% ]9 ?4 a3 e0 o% J! M4 T# ~5 V, I& B
1' h# G4 `; b' C) T8 q, q# M% Y1 D
2
0 |8 {8 S3 C% r0 f5 ~6 O, Y3# B6 q* F& _) r" U# Z c& @
localhost$ ssh -l jsmith remotehost.example.com, n _, v* @0 a, E1 D" R9 Q V
jsmith@remotehost.example.com password:
0 N5 I. y* c$ T. [3 h* K# bremotehost.example.com$
$ M! l7 H2 f$ h由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:' l$ B2 M: b2 C/ A! c7 K- Z+ q
0 [: y& V: q X- Ko 系统管理员在远程主机上升级或者重新安装了SSH服务器 - U o1 i' D# J& P5 G: R) z# `2 Q
o 有人在进行一些恶意行为,等等。
% T+ y, x& U, o/ ]8 w
% n. L" g! N( s在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。6 }1 B- x" B0 b7 V) E
2 R/ P8 `: u$ t2 L. a' c" L9 ]1
- m3 H% Y, E3 G; D' k& N2
1 T# w" N; l* X+ O5 ^- A3
4 X0 C! p, A. l: E4 G4
- o, h$ E2 F' L) _9 j5
0 C0 {; L$ e0 k0 Q" _, a& V6" G. R' E2 J$ S- I/ b# O- _$ S
7
/ F1 E8 g. t7 \3 g& E8. ~ |2 C1 F% }, b# W* y
9
, V: I5 c, Q! a8 k10
, N/ R" _* c+ k; y, ~# F* ]% K11& [6 D9 i6 N9 w
12, g6 J! X( |6 n) d( P$ o* C6 F$ r
131 o/ Z* F8 H( E& s" k& N
14
: l. q, w( b* v7 G15
& ]8 ]7 z# U9 B. C4 `" {16
* ?" }3 t H# e1 W7 l: ]localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5 B7 O1 j; j5 P# O# C@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/ ~ Q; B7 u* o, C; y$ x
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
- z" }! C* D* O$ b' ~/ R9 H1 C; RSomeone could be eavesdropping on you right now (man-in-the- middle attack)!
2 I! h* M1 J/ n6 X- c e, S gIt is also possible that the host key has just been changed.4 K4 K: A2 C: ?
Please contact your system administrator.1 S. `0 C& R9 n8 y
Add correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.3 n8 z. B9 J$ Q5 {' s8 }3 c
Received server key's fingerprint:" ]6 l. s6 T! V* d4 R* {
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum
+ |' e. |8 m( @5 ~/ q# q7 AYou can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
7 \8 R& Z e7 o( tAgent forwarding is disabled to avoid attacks by corrupted servers.
2 ]7 U& v/ W( C2 n. @7 iAre you sure you want to continue connecting (yes/no)? yes
6 W) H( @) H4 L9 }5 D4 ~Do you want to change the host key on disk (yes/no)? yes
+ @7 ^# s! V5 s; O" l o$ ZAgent forwarding re-enabled.
R; `5 A4 [7 PHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
' |$ [ ]" ]! B8 Caccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
( ~/ E- v: O+ d5 W- y3、调试SSH客户端会话
3 U" p9 N! Q' K" B2 u) ?7 |# {
l! s' c5 Q I# J9 c8 X当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
; V# A( L0 W1 Q7 Q; ]3 ]9 O
& j: S( P6 V5 B没有SSH客户端调试信息的例子:6 N" M2 e) p3 @. V
, I# N0 s% S) J! q5 _1# o) C3 S# x9 `3 L$ j
2
! {" `3 E; a, M) v5 D6 R/ Ylocalhost$ ssh -l jsmith remotehost.example.com1 v8 n$ T/ i6 N* G& `% W4 u1 D4 J
warning: Connecting to remotehost.example.com failed: No address associated to the name
2 |. y0 t2 _4 K, B& ^5 Z+ z包含ssh调试信息的例子:
- b5 w. R3 Z; }" R( W" [
6 W$ A1 l% W* x- y* V6 X1" \% n X3 Q( h: I" P) M
2; a2 D& Q, T9 h
3' a9 j) }3 G6 Y5 v; i3 A( ? ?) h" i, g
4
: C; w/ m q0 s, m. J5
% U$ }6 v; o0 y- J, \" s6
, S+ O+ e1 K% g7 Q7
/ E! Y5 G2 Q v83 f) x! i% w5 a3 t+ N \
9& X- ?4 n) u4 @0 P: {( N/ y
10
% i% J% o x. o; t/ K+ m2 l9 Plocaclhost$ ssh -v -l jsmith remotehost.example.com
2 d" S) q& y: o" ^" Pdebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
9 F0 d! Y6 R5 U5 }7 V% FMetaconfig parsing stopped at line 3." t+ L$ N7 a% m( R0 c' f; a$ Z+ A
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:1 @' K/ f" M. M1 W* S. Y ^7 R
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.. z7 c$ f7 v8 e
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
0 M0 q6 L5 P: v1 o7 p7 v' rdebug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
0 s% t$ \5 a* p% @$ W; U: ]; ydebug: Connecting to remotehost.example.com, port 22… (SOCKS not used)0 L9 W0 \2 C% |, m8 y2 a
warning: Connecting to remotehost.example.com failed: No address associated to the name9 c) z# P$ E4 t# b4 U" O" i
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
7 o/ n! ]; |5 J2 P2 ^% M当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
" X2 r/ `4 e- g2 J% C ~* ]8 L2 W# Z
4、用SSH退出符切换SSH会话
% K0 O2 H ?- T* F# j' T2 H* q2 s1 o/ `/ b5 S; G
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
. Y- O) D- c8 M8 i6 o% |5 ]6 L2 ^, V3 w& F
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
4 _5 X4 c3 j# j; K3 t" `& n& G
% n( i. T3 c. T- C$ q/ {当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:
' Y- \* W+ x- ]1 |, b; M2 R" m. M2 p- s2 X% U! v5 P
1.登入远程主机:
! A! ~* Y0 Y( D2 I4 b8 A+ G
+ X; y* j! y0 n# k# u8 slocalhost$ ssh -l jsmith remotehost
7 b+ ]# ]+ G- [! j2 Y
4 Z! e/ Z+ ~! P7 y( z/ {2.已连接远程主机:
6 A w, [+ x; W2 M/ R; p* U: j- c2 i* M* q/ H
remotehost$; j S9 s- |- V3 M1 w3 }5 m/ {
! {6 u7 A- S U
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。& ]& A/ _- p# Z/ K" I& d4 }
; E7 {/ v; h0 }6 E% x* l当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”/ M, N9 l+ p9 g- w
, f7 V, `# f5 l {& h! u+ `
remotehost$ ~^Z
; V W# C: l0 S' p[1]+ Stopped ssh -l jsmith remotehost
1 W+ b& Y; e8 s( Llocalhost$
; Y% J1 f% [) L( M- k& X1 P* d2 _/ \% j3 w3 d% A
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:9 s, J4 T7 L1 o+ ~# H" p" C* B2 O
7 S6 z' l6 s: K1 y7 k: `9 alocalhost$ jobs t* w4 E4 D( ]% v8 L
[1]+ Stopped ssh -l jsmith remotehost
3 y' r1 a6 u( Z/ |: K) h! _ E, e) n
5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码6 ?# J3 ]9 ~- M1 u) E ?
# b4 p2 V+ A# m* z
localhost$ fg %1
) X4 E) v" |9 R- }$ k! F3 {ssh -l jsmith remotehost
3 ^% z3 V1 J+ h4 D; t; ?remotehost$3 W @; h" d/ b" Q
$ t( S& R5 H& o, q. D5、用SSH退出字符会话,显示信息$ ?! n+ B# m: J3 m' \/ [
$ i6 e0 L2 t c
要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。' \6 a. A/ P( g
- W# w: ^8 G/ g登录到远程服务器% z& F6 E$ `9 s% H% w f8 x
3 A3 T: ?0 }& v9 s. r
localhost$ ssh -l jsmith remotehost
4 T2 |& ?4 v2 }6 n' h! I( @% X6 A o2 q, t3 u$ a
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息2 r8 v) F& n. t8 L
" p9 O* N7 [, i. `3 v1
& p8 B' C% b' V( ]. a+ V2
4 U2 l+ m, h' x5 c; f; n39 _7 W3 g$ U9 E: D% b7 m9 L
45 e1 ~$ H D; Q
5
4 K( _9 v$ S: k: R' b5 k5 D61 j% r, V& X) I0 H& _# V
7
. y. C5 Z w1 x0 Z8
- S, O. J3 d* d9% [% t& Y7 u$ H% Q1 n8 d2 ^) a
103 w/ U5 T0 c! W& t3 g
11, V" q ?, j+ g n, ~5 [
12
# B& @- U0 l) M/ v9 K" J13
) U. K) ] `3 w( a14! G/ w% S. s. s ]9 o
15
2 C- @, Q/ x7 E3 m3 N: ^3 d7 [161 Y7 B8 z( G- y0 V/ W& J* K2 c
17 T$ M4 |" _1 a& ]. ~
18
2 \1 r$ D- k) z1 f9 J* Z19
$ T# e" ^0 \( C) W' e/ L0 X209 `3 b& m5 l: M5 [) d! b' j5 f- H
21
2 A1 V% }1 ~% q5 Q, ~& o224 g! n, m7 }! m4 k2 ]& e
23% j7 P7 ]' e/ {9 Z; R
24& M8 Y( ~& m) k
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]
3 _3 b1 Z( g0 T$ ]3 m( y3 z1 Aremote host: remotehost+ I5 |1 u/ W' T5 R8 _
local host: localhost% g3 h* F) @- ?" V2 m6 D
remote version: SSH-1.99-OpenSSH_3.9p10 e3 w# N H0 K
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial): i, D+ C6 r+ G7 v' C: `
compressed bytes in: 1506
+ {. u+ ?4 c$ h" G p% Suncompressed bytes in: 1622" F2 y, ^+ |) h- N2 ^2 o* [" |+ x6 x, l
compressed bytes out: 4997) W$ [* h7 K2 Y; R
uncompressed bytes out: 5118* q/ b E% a5 Z2 o# E
packets in: 15, f$ i' ?! F; q# q( U
packets out: 24% _1 D$ ^4 N) D" _6 C3 I
rekeys: 0, w- i' Z- @4 y! x( s, g8 Q3 s7 ^0 P
Algorithms:: e# k1 Q+ H5 i2 x: Y+ u
Chosen key exchange algorithm: diffie-hellman-group1-sha1
- O( G P; y$ K( K# b; XChosen host key algorithm: ssh-dss$ V/ f' C; w5 v2 I9 z
Common host key algorithms: ssh-dss,ssh-rsa
# U. t# x8 T0 |Algorithms client to server:! }/ d3 t+ a7 U
Cipher: aes128-cbc) c+ o- b+ H: d9 H+ m- { X
MAC: hmac-sha1
]) j. O7 p+ O& R$ i7 FCompression: zlib5 ^3 Z/ ?4 @9 R) ^
Algorithms server to client:
3 W R/ r* ?; D, Q, b' h+ @9 ?Cipher: aes128-cbc MAC: hmac-sha12 p( V3 f( k/ b) W5 o
Compression: zlib. ~8 D' o& h+ Y6 d% ^/ {
localhost$
4 z! A2 u I3 k q以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助 |