找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2708|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本' I- U2 q8 a/ A$ c  G7 d

( N. _; g- z2 i! Z7 T2 i有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:# n& e: m9 W, a$ |0 e: x

. y7 I2 U' t7 _# l( G( ~$ ssh -V
4 w- Z2 A2 P5 U) p% `9 U0 n6 t% eOpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
& [6 r  A2 C9 X- B7 u# @) U
. u; T, b2 B8 s下面的例子表明该系统正在使用SSH2:
9 {* c6 o7 k$ `5 _! e$ q/ _) j# Y8 K- g5 I7 |  t! p( L
$ ssh -V 0 {$ h  g' F2 K4 R) N2 e
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu8 c) [3 u" e+ H9 s1 l3 U. `
0 H* b- }( c) W3 Z
2、用SSH登录到远程主机
6 N5 W4 n9 y# ?6 q7 a" S# e* D! \; S( x
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:% _6 i- I  v3 S, `! G8 _

' j/ M- r8 A# g4 g" T13 ~' ~  ^( J. o6 D
2
7 ]9 a& k% F. I7 n3 B2 w3
8 B- ^  ^# j( }4) b3 s% [% ~# ?: y" s
5
% i: y4 W; M, O) |! Y" D( M! `& k67 T) o+ N4 R6 H# O% O0 ~
7
. \" ^8 E- y4 F8 F8* e2 e! L6 H( y! Y3 P9 a2 Z) {
localhost$ ssh -l jsmith remotehost.example.com' o/ p% I5 l; P3 L, Q6 N
Host key not found from database.
$ w* M; D6 n  N( a3 `Key fingerprint:
$ @5 r/ [9 @% D: b8 I6 c0 V4 j+ ^- V5 nxabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum* @$ F, H8 t$ }
You can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
0 `* G3 D% b  R4 [1 p- EAre you sure you want to continue connecting (yes/no)? Yes
+ z: a; w, w2 A& o  `8 fHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,7 z/ G2 A2 d( q
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$
4 L4 `& X$ ^4 W: c因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。
7 A% `% q: `1 \& U! M$ R# a( ~# c2 V2 f( F
1
4 ]& L+ ?3 H/ e; T: ?7 R* d. l, s2# d: w$ ~5 v- q* V& C- R
34 l1 q  E4 p* F0 m: U
localhost$ ssh -l jsmith remotehost.example.com/ r3 f. B6 ]  @" b* r' c$ L
jsmith@remotehost.example.com password:
3 g2 S. |8 W3 r: r# ~$ dremotehost.example.com$9 ?& x5 k6 x% v
由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:* s; m, y9 U/ _+ r8 q/ d9 b

# D" I/ r9 b" G) Lo 系统管理员在远程主机上升级或者重新安装了SSH服务器
. X# f7 z" G+ ?4 F9 Ro 有人在进行一些恶意行为,等等。  S+ R: R6 d/ f0 p1 P5 b, k

* h% i' d  f" [, f在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
6 X( u" N0 [1 B/ w; A( M2 n+ [& t3 |$ |( Q- C8 w& f8 O* {/ a
1, z7 w! x% D. Y9 W9 R
2
9 o8 V/ K& {, g6 E3. u- p) q0 ~- ?) @* i$ @
4! S" K) P9 r) O- r! g
5% L5 `5 h3 [2 P2 m/ ~+ ^7 E
6
& J' x. T& R$ Y; T0 \) ~8 e$ U7. ?6 x# w4 g2 ~
8+ F7 f5 Y0 Q8 a7 B3 B
9
; U6 k$ f. v9 I10& f; ?' u) e+ h  U
114 y  p$ ?# \8 e) h9 @2 ^# u
12
1 U" W4 m& ?  r- b/ ?- V6 R( k13  p7 @3 W9 P0 I
14
: t% B- O! l3 D8 Z* r/ R15
! n9 p$ {  z, i, h  I3 U. K16# w8 M' h+ G3 M, H& V9 N
localhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/ q8 I+ \) i) G2 Y1 F2 b@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
4 ^* H, U5 }- j/ [! nIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
, P: r* r- f; o7 ]  c# pSomeone could be eavesdropping on you right now (man-in-the- middle attack)!* e" Q4 Z8 l! U- q( [- c
It is also possible that the host key has just been changed.  d  m, i3 x3 x# B2 ^( _% b
Please contact your system administrator.
1 P1 @% ~, A5 O$ C+ X( MAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.
2 u" I) i$ v+ L+ j6 F! K; k* \Received server key's fingerprint:; Y5 v8 U6 M6 L' C
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum, `3 g4 V% [) m. v6 \
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.0 n$ p+ X5 q1 f  b% Y
Agent forwarding is disabled to avoid attacks by corrupted servers.
9 C9 f6 ?3 T1 P. ]) M% M+ I" @% TAre you sure you want to continue connecting (yes/no)? yes
$ u  D0 L/ g8 m0 GDo you want to change the host key on disk (yes/no)? yes4 f. l0 f1 v% n
Agent forwarding re-enabled.+ _+ h* s0 ~; d
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,
8 H+ _% o+ d# H! O& G0 ~1 laccepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$! W8 A8 _. U6 F, ]( c" V
3、调试SSH客户端会话' T3 C* L9 f" }5 L
5 L+ |) D+ C" n/ \# R
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。% t. @8 [. A* w$ k: {- v, J

7 f, s7 Y& A* k* X没有SSH客户端调试信息的例子:9 s1 A. e# v- W7 ~4 n) q* U7 o

6 Y6 e3 d  H/ N' R1 f1, u- d0 j* H8 B" e+ j
2
8 H* F$ ~2 W  U1 v" {6 o& y' K( @5 Rlocalhost$ ssh -l jsmith remotehost.example.com
& _$ F0 f. v; i+ p8 {$ ewarning: Connecting to remotehost.example.com failed: No address associated to the name1 r5 L$ a) L. p$ O6 T/ ^
包含ssh调试信息的例子:
4 h8 G/ o9 d# @( x) G( ]8 C, L/ x, S
# S7 l- C* T3 h2 F0 o1
  d: q% X3 ]6 f* u8 B2 w2
# Q9 ?( \- \* q2 ^7 T3
; n' S/ F" c7 m( Z  \9 ?: d4
  y) R5 ~) T7 v, k, K7 _- T5
! u. o1 z5 P1 r- Y; o6
. A# ?5 J) B0 P7
2 ?+ E. X% p' N  w8 M4 I% w8/ O6 d: Y7 F9 y( [3 w% X
9: j9 `& I  X: l/ J' a: |8 F
10: e% p0 M" Q& K6 |
locaclhost$ ssh -v -l jsmith remotehost.example.com
& n; Q. m6 E" V! qdebug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:
. M6 H/ y. r# X0 n- n  @Metaconfig parsing stopped at line 3.
4 [3 a8 ]3 n% }' z( K% qdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:1 B5 @' F! }9 l) r$ L" h
Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
$ D9 C8 f% }' G: Wdebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file./ t' M9 c7 p, j- B+ ?" x* P: [
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
2 g8 z% \3 i4 }3 P9 r1 Bdebug: Connecting to remotehost.example.com, port 22… (SOCKS not used)
. d# Z0 x) l) I" ]' V: a. ~warning: Connecting to remotehost.example.com failed: No address associated to the name
( ^8 |+ y: Q! a# w! M[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]" W+ v# V  \4 a  o2 q
当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
- m! Q/ F& x& F- a+ g& Q: ^8 k6 b, W. ~! R) H; o% M
4、用SSH退出符切换SSH会话) }% U0 \, O% L- j9 h8 y! K
5 E+ P- F7 p7 x. p- T0 v
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。+ O2 D$ g1 s% j: R5 N: K

* x8 e0 p/ F  H+ B) [" o当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:& L0 ~- a: v1 f- d# K
9 g" Y, y( Z+ n: O
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:" J4 l  `+ F& Y3 F0 @% j$ n% l

, V7 Z6 T% p4 n1.登入远程主机:2 A0 P. U5 a+ I0 Z! L' P+ m! v

3 V  o% B: |3 q. p& y. o% H* Nlocalhost$ ssh -l jsmith remotehost
3 L: W" V3 u0 a3 u& I+ h6 v; z0 Q2 Z+ A6 Z) Z3 V- d4 {
2.已连接远程主机:) t, w5 y4 D/ Q( A3 O
: ?3 c. Q( G0 H7 i8 _0 r5 h
remotehost$. Q# Q5 s" m5 Z7 O# `
" D% f, Y/ o0 i6 m# l
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
0 o5 r7 b/ B$ a! I3 z! c5 e- ]' B8 G4 O" l# h; Y4 k
当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
1 H7 F; V; C7 k, v9 ]6 `' P, c" W" `
remotehost$ ~^Z # k" W- K$ U7 Q, ?8 S8 L0 T
[1]+ Stopped ssh -l jsmith remotehost
! d  R2 ^' J# n( @+ l3 \localhost$
# I5 S+ R- c; |# z2 L, ?4 f5 H7 W+ J& X/ i! ~; ~3 L
4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:7 E# x& M/ P% e  t  A5 S
. l9 e6 P1 q- B  h
localhost$ jobs
3 I% ^. i2 P  G$ k[1]+ Stopped ssh -l jsmith remotehost( p5 ^" Y1 V4 |9 n5 Q" P" J" S

; g" U: j/ X7 B% b5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码$ [& o0 `1 p- M6 V! X

# }" [0 n! O+ clocalhost$ fg %1
/ H6 i1 z5 Q; U" n5 n! Fssh -l jsmith remotehost * a. W' ~" P" l2 m; h- h/ ^
remotehost$
) I  K, S+ Y; I  e! s
# t7 r; H: K9 y) B5、用SSH退出字符会话,显示信息8 i. |6 B7 C$ P4 B) q1 e9 }

: H+ z5 h* N1 [1 f( x9 J3 M) a! \要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
0 L+ @( K0 }. ]! a  B- p  ]8 A  [' m$ {3 w# k& t
登录到远程服务器
$ e6 D  p+ T9 ^) u, V# C
& i7 w5 B3 o3 L6 Klocalhost$ ssh -l jsmith remotehost
9 D' ^8 H2 k+ Y& o& _
3 j. E  s1 k8 G  s( x  E+ u如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息
% Y$ Y: _' D) j% Z5 M7 p* d6 }) k: ^5 B  `0 e( `0 ^9 u
18 ?# i/ F, x+ G" T9 \7 T
28 [6 @9 |( U' V, B$ [+ d
3
% w! q# ~8 y5 j( ]8 ]# M4
- l' `6 W0 Q9 P; ]( e) j3 n5
* u0 R' g$ |7 O  ]& D6
, C* H  {# ~4 t0 s. U  u7
( I1 O7 N' I; e2 h3 n' h) G8 }8
& J2 L4 N. Q* g( ~2 S92 a7 g; s; a" i4 Z2 V& ^+ f
100 X, x' d. h% `" N( _* m
11) A7 j4 d. [7 `3 a4 U
12) D6 R& \* c+ Y: W4 D
13+ ~; y' K  X, c' o9 t( n. H
14
4 o  C9 n0 ^5 D* `. P154 ?" g4 s* P0 d( x2 I; L
16
% h: j; w5 D0 x% y- n/ Y" y! V9 l17
/ I/ u' P/ Z% |( ]# G5 |! M18
- c8 }( U: Q: G0 n, _$ ]$ p3 N19
3 V0 c" J0 G; a$ Q2 d8 i& K2 Z% l20
+ s$ y$ P+ o  \0 _212 t# X: i( D  T6 @4 H0 _
22& W8 c5 a- d4 _6 p& O% d: k
23+ U# ]8 H- o0 \
24
/ @( X( |3 b( r& u  Bremotehost$ [注:当你在命令行上输入~s时,它是不可见的.]4 |8 Y6 h) E+ }
remote host: remotehost
% W, v* p! n( Y8 Nlocal host: localhost
+ s& W5 i% e4 f  j9 i/ Gremote version: SSH-1.99-OpenSSH_3.9p1
) W$ m% G, X8 F* \4 b* _local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
% A: ~7 F. o' S- f' k1 ~compressed bytes in: 1506, k% w1 B5 [" a; ?+ M4 |' h
uncompressed bytes in: 16222 [6 t3 P# F" ]7 P! x5 H* r$ A' z( Q
compressed bytes out: 4997) ~* K. n2 P9 V$ Z1 i& B
uncompressed bytes out: 5118
/ F' y& N% s* Epackets in: 15
, |3 l$ c( r) T* h  n  qpackets out: 24
1 o8 `* |2 v7 ]7 V2 h: S( X3 G: urekeys: 0& w* y0 c9 C3 @
Algorithms:' b/ u. Z) @' [4 [: K- g5 k0 T7 D
Chosen key exchange algorithm: diffie-hellman-group1-sha1
; G$ y/ M8 `. x3 G  D7 O& BChosen host key algorithm: ssh-dss3 s8 M! G( {. ~+ Y$ _- f$ ], H
Common host key algorithms: ssh-dss,ssh-rsa
" n" Q4 Z8 |# `2 ~6 ^- K" vAlgorithms client to server:
: I9 T) {, Q& e. G- E) _Cipher: aes128-cbc
( H! @, a* C) E( }2 KMAC: hmac-sha17 {; m6 s, u# x, X9 q4 n. X( d
Compression: zlib4 {& W% s" I- F8 K
Algorithms server to client:4 i: \7 L- m" @# N
Cipher: aes128-cbc MAC: hmac-sha1
/ G9 l) `9 o) SCompression: zlib9 R9 D2 _" _' D" Y
localhost$  ^$ t& e% K9 w4 y7 @
以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-7 11:02 , Processed in 0.114362 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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