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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2365|回复: 0

Linux下SSH命令使用方法详解

[复制链接]
发表于 2013-1-4 17:07:47 | 显示全部楼层 |阅读模式
1、查看SSH客户端版本
1 K. P% d3 e( M2 K# n# ]* T9 s; g  h. C$ d* v
有的时候需要确认一下SSH客户端及其相应的版本号。使用ssh -V命令可以得到版本号。需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用OpenSSH:4 w0 U- l+ J9 _/ Y$ q$ A. h: p

: ?- {  U  X; \  N& Z$ ssh -V
  Z' O' n- T$ m" |  y- ^OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
1 m. E- w6 G8 Q4 t, _' j0 v
9 F3 J' ~4 x* a1 X# g下面的例子表明该系统正在使用SSH2:8 R) t3 t/ a, [& H9 ~  ^# O( d
& g3 F8 i, N, ^* {, |5 i9 J
$ ssh -V " D& Q6 a. C, U; S) k! j& \
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
$ Y( S  Q6 m. W! n; b* o" k: G$ s! C% g3 W; G
2、用SSH登录到远程主机
7 V4 V% W1 p, h5 L$ G1 d. X! p" F( ?/ T. f) C
当你第一次使用ssh登录远程主机时,会出现没有找到主机密钥的提示信息。输入"yes"后,系统会将远程主机的密钥加入到你的主目录下的 .ssh/hostkeys下,这样你就可以继续操作了。示例如下:
% x/ k& u+ j, y" o* y! V/ D: ~1 J  ^& y1 r6 b7 L
12 S; R, c! T6 b( R1 K/ Y2 C
2
5 t  f4 Q  U7 |3
' e2 g' w3 M1 _5 E4- R6 `- P% j" L; t9 P+ Q" C" W" t% Q
5
9 s# Q/ ?) ?' U' |) U6
2 y0 u4 o% F4 {& `: }- P# o; i7' A8 a+ L6 z4 d3 S7 K/ ^1 j
85 \6 E1 K4 F# @8 Z" ^
localhost$ ssh -l jsmith remotehost.example.com: H" R2 M. ~" D* v5 U$ _
Host key not found from database.! B1 |7 Y, v3 |& i9 R
Key fingerprint:) K. I9 w, U3 I/ Z/ h1 e
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
# V0 L1 q$ X: [, @* o" z2 kYou can get a public key‘s fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.
$ o! y7 k' \& [; X" L4 ^1 n3 DAre you sure you want to continue connecting (yes/no)? Yes
/ B) y1 t2 n6 x& K* e2 I1 \# JHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,: F# }! P" f. q0 b4 |$ `0 H+ I: x. ]
accepted by jsmith Mon May 26 2008 16:06:50 -0700 jsmith@remotehost.example.com password: remotehost.example.com$6 l5 a# P3 E6 e4 J& E' H
因为远程主机的密钥已经加入到ssh客户端的已知主机列表中,当你第二次登陆远程主机时,只需要你输入远程主机的登录密码即可。! w0 M/ j$ M/ G' K
" I3 |) P. W. Y, E: Q, q# N/ h
1- c2 j4 O' ]% Z1 F5 P9 V% ]
2* y$ @4 M% K0 \" F
3
" j* P0 K' S* T8 M: P! slocalhost$ ssh -l jsmith remotehost.example.com7 R: u+ R. T& P& `: B. E
jsmith@remotehost.example.com password:
, E0 {. h; ?8 v; U+ Eremotehost.example.com$
6 i5 h! N  O& `+ T由于各种原因,可能在你第一次登陆远程主机后,该主机的密钥发生改变,你将会看到一些警告信息。出现这种情况,可能有两个原因:
! z5 A8 ]) C& N3 O
+ C" {+ @1 h4 f8 D, jo 系统管理员在远程主机上升级或者重新安装了SSH服务器
3 Q1 k! F0 u: E! ]9 t$ u6 Ro 有人在进行一些恶意行为,等等。
$ J! j6 S$ Y6 N( q
: h. D0 A" \3 N% N; Y5 U在你输入“yes”之前呢,最佳的选择或许是联系你的系统管理员来分析为什么会出现主机验证码改变的信息,核对主机验证码是否正确。
6 v" O" T! l" w& F+ I! d
2 [- i3 J5 m, K' @/ p$ E2 L$ J1
! {4 A, y$ l7 m4 h& X' B% ^. O2
6 B6 s  J( k( Z! ~8 o3% o" M; k- V3 _8 T; z( d
4' c& ]* j4 N; h( ?9 S2 E
5
) |/ E$ h* O" C/ m$ @: q3 S& f6
! V# A3 N( a; X. a3 n5 X* D7% y" |& P! g9 b2 z$ e4 M
8
/ o6 L, y  T2 Y4 g- {9
3 k4 |- m: ]$ q& M9 n. N- b10! [: N5 v4 Q! w% b
11: ?6 h3 b8 X7 \/ v( c
12
5 k- R$ L3 T) s7 U" p  L0 T13
% `. _" Z% H9 _, U* e147 b4 ]  |# I7 R4 i" V# Q
15& h) V8 f3 o/ o( J3 e
16
$ a$ b7 o8 N3 {8 V9 zlocalhost$ ssh -l jsmith remotehost.example.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# A; X/ @' @, i$ f. l@ WARNING: HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6 A9 X0 W# ^$ o2 _6 @  v. c4 NIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!( A) ]8 `' F; ]% B$ ]: C- K! s
Someone could be eavesdropping on you right now (man-in-the- middle attack)!
$ m' T8 g8 f6 ~' }. vIt is also possible that the host key has just been changed.
# P2 p/ u6 N4 d2 B8 kPlease contact your system administrator.
: D& O/ p4 t+ s$ e9 _2 h% s) h6 FAdd correct host key to ―/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub‖ to get rid of this message.9 _7 g4 {* L4 u9 R: X
Received server key's fingerprint:
1 S5 y1 c" B& S8 g) M' B9 _6 ~xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxum5 k2 E  F* ^, M3 _# R
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile.) J" u, [8 W5 c  F% g
Agent forwarding is disabled to avoid attacks by corrupted servers.
$ Z1 M( f$ U" K* G0 v; r- qAre you sure you want to continue connecting (yes/no)? yes
* E8 u& Y, U2 l% t  W! qDo you want to change the host key on disk (yes/no)? yes8 b( F! G% G, b! @
Agent forwarding re-enabled.
: m5 m; V* s' cHost key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com,+ c. _3 {2 b6 d/ X# N/ ^- Y
accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$& v4 R9 ~1 C0 `  R- {* {0 \
3、调试SSH客户端会话9 _: C- g0 h% j% b, F3 ~8 w: a
+ J# ]' X3 ~  V* b
当ssh连接出现问题时,我们需要通过查看调试信息来定位这些错误。一般来讲使用v选项(注意:是小写的v),即可查看调试信息。
! k/ d# m: z4 Y4 f" n; _" f
/ s' w) K& A* C- f& z没有SSH客户端调试信息的例子:
1 h2 Y* E- S1 ~) C' X4 n, `
" l# e# X' c3 T; V: t* a2 t9 V1# _2 O2 K" V# J2 T8 X
2
* i! s) l' n& C7 c& c( Dlocalhost$ ssh -l jsmith remotehost.example.com
3 U1 K3 o% b$ d& V  T4 dwarning: Connecting to remotehost.example.com failed: No address associated to the name$ }" r% p) i# \" c% h+ B
包含ssh调试信息的例子:, }: Q9 c5 s! B! ~: a  Y, e) F
) G# `9 N1 O% ^+ i+ n6 J* ^0 w
1& E8 [& [. f% \" Y. s
2
. A9 \& t  M6 Q3
& h) I& a5 O) c4 B: j3 x4& X# q6 ?3 f) _. Q  y
5
5 a+ \* ?( D& l/ e* E  X6
( E: D( e  M4 F7 L# \7
+ K6 t: H, \$ E5 u8$ t$ h! X2 e$ q. F
9
5 {5 y9 o* C" n$ @& I4 U10) L! s* t4 i# o4 {" k
locaclhost$ ssh -v -l jsmith remotehost.example.com
/ f6 E- |* s& q7 {4 ~9 r& _debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:0 g/ n8 `& b+ ~3 h
Metaconfig parsing stopped at line 3.
+ v7 }) ]5 {/ Bdebug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:
4 J$ D2 k/ [3 W( G( U: `Setting variable 嘠攀爀戀漀猀攀Mode' to FALSE'.
4 w! S5 d7 Y' `% c3 Zdebug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
( K3 ]( m7 x- C  K  edebug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for /home/jsmith/.ssh2/ssh2_config')
5 X7 y3 {8 I  [" Ndebug: Connecting to remotehost.example.com, port 22… (SOCKS not used). c  {! {8 A$ U
warning: Connecting to remotehost.example.com failed: No address associated to the name+ B3 j5 ^& {# f0 f- {( n
[注:很多命令中,v选项对应的英文是 verbose,也就是详细的信息的意思。]
+ T8 R. t- U$ [; D/ V: g当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照第4点的步骤操作即可:
2 b4 [- X8 Q& V: G
( r; w& G& P; A1 K  t, e4、用SSH退出符切换SSH会话) j! ?" ]. w- D
1 {$ f, d2 t9 y8 q) q$ _: ?2 e! H
这个技巧非常实用。尤其是远程登陆到一台主机A,然后从A登陆到B,如果希望在A上做一些操作,还得再开一个终端,很是麻烦。
6 z7 w  N6 D. U
, X, T5 _0 Z- Z% {4 W# K8 b* ]& a" ^7 R当你使用ssh从本机登录到远程主机时,你可能希望切换到本地做一些操作,然后再重新回到远程主机。这个时候,你不需要中断ssh连接,只需要按照如下步骤操作即可:
' _+ w) |( u' @# o+ z9 `2 }2 R- u' w! S6 V! R5 g! S  U. n7 w8 w8 Z4 S
当你已经登录到了远程主机时,你可能想要回到本地主机进行一些操作,然后又继续回到远程主机。在这种情况下,没有必要断开远程主机的会话,你可以用下面的办法来完成:5 g" w' N5 S9 {- Z
5 Z* L) Y5 Z2 t: z% r3 M4 ~9 N( B
1.登入远程主机:" g. H- S) L$ M" y8 B  b

* v  ]  G- X6 F; \0 _% i/ Llocalhost$ ssh -l jsmith remotehost* F6 w5 W7 {8 m
4 C0 ]- n5 R4 j. K! H/ t
2.已连接远程主机:, Y2 K. @8 q, ?
' v6 F; O* c8 y) {3 y9 C
remotehost$/ W; n3 a" y0 ^/ g
3 ?& O! N: ~( q) N& v3 t9 t2 s
3.要临时回到本地主机,输入退出符号:“~”与“Control-Z”组合。
# Y$ r# T$ f8 p8 T: L# l8 Y
- m  X3 z; g$ g% T0 k当你输入“~”你不会立即在屏幕上看到,当你按下<Control-Z>并且按回车之后才一起显示。如下,在远程主机中以此输入“~<Control-Z>”
( v6 }( _! m8 `& w/ m0 `9 P4 P6 g7 H' U7 v4 @
remotehost$ ~^Z , R" W  h0 \* p5 z
[1]+ Stopped ssh -l jsmith remotehost
6 `% O8 S' a; S7 \, {* S+ Blocalhost$
; B5 v' g" l. u
$ m# ]* _! ]& @4 ]+ _* t& P; ]; A7 I4.现在你已经退回到了本地主机,ssh远程客户端会话就在UNIX后台中运行,你可以向下面那样查看它:
) i8 l" j" c3 `4 D, q
) i) m8 Z( ^: e1 v- Z/ H3 ^localhost$ jobs
6 _& M" p3 d" e: v[1]+ Stopped ssh -l jsmith remotehost7 R9 m0 {( r8 U3 o( o

4 D8 D9 }0 N# H& o$ `1 I# S4 r. J5. 你可以将后台运行的ssh会话进程切换到前台,重新回到远程主机,而无需输入密码: O, ]7 T5 g, @( |8 h% _. p3 ]

& m( M% D; M# p- U: E0 o8 V' r! q% n7 S; klocalhost$ fg %1
% A3 x+ b: @! ]3 gssh -l jsmith remotehost
& B$ y  \8 U7 h2 t3 R5 @  y* uremotehost$! ^$ w$ Q9 T- s7 |7 q+ ?
) {2 l; P' w: N; t5 J1 v
5、用SSH退出字符会话,显示信息6 p3 S. d9 w2 g

' O0 `2 M6 d6 _$ o2 w/ @* D2 P要想取得一些关于当前会话有用的信息,可以按以下方式完成。不过这只能在SSH 2 客户端上使用。
, J/ {& z8 f9 n3 {) M! L+ ]( z+ y
7 H: t8 s! O; t' Y6 Q, B7 U登录到远程服务器4 e  r& _7 }7 k6 M# h

( E0 `- e5 e! f0 Clocalhost$ ssh -l jsmith remotehost1 m& ~. P2 a. d# {3 T
4 |: h( C+ @1 U: h* Z" Y6 y9 v
如下所示,在远程服务器上,输入ssh退出字符~并输入s。这样会显示出很多有关当前ssh连接的有用信息4 ~0 q4 j/ u3 d
' _; x) v7 M  P0 k, G& U
1
3 P2 M9 k5 N, `% e  O0 k2( \6 j: H* |9 f/ m1 T
3
" S# n/ A6 C# Q4 f4
9 Q2 m! i0 c. e7 B. [7 V4 n) f7 D5
" F- b/ G8 b9 u! G. Q1 a: I68 W; ~8 p* K3 i  a# ^8 _' V. N0 j8 Y
7
% x9 z' c& [# g3 t5 e0 S) G+ k1 Q2 G7 M8
7 g) _# N0 E2 r# ~) e91 j" N2 B  N: \! R% S' a
10! O( h3 F$ p. D* G1 G
11
$ q8 Q  k6 Z  X' ?12* M6 H: |9 \7 s" U) h# j! G2 M
136 A7 l! J2 Q  z/ H# U. a
140 m  {1 E& h4 i- b
15/ k1 b' k. s' j: E% _: x/ D
16
; f- D8 P7 q  j& H17
. L) k, Z( p8 U, N9 i) G" c18
7 d1 d: W6 v' l1 I' I- [8 P  d19
; f8 M. g7 ~' S  H. M20# ]* p4 a- f' e! A7 l" W
21
0 {2 i/ I% l, N' B, a( r& }7 f+ _22, C' |: l( R' O- s( }
23: K) D% u0 v6 ]2 W4 }
244 u3 _% Y3 a! ^9 \) k
remotehost$ [注:当你在命令行上输入~s时,它是不可见的.]' m1 Y. @7 U  Y5 n
remote host: remotehost
  |( l5 f0 b% V4 H2 A* Qlocal host: localhost. N6 P2 q! ]! f4 y. D
remote version: SSH-1.99-OpenSSH_3.9p1: q+ W8 z( w% X5 y
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
4 Y- x* l! f5 |# U, wcompressed bytes in: 1506
0 S; T" Y, ]$ w* L( cuncompressed bytes in: 1622
) G2 c% i- o3 M9 ~compressed bytes out: 49973 I, z( ?; U( H9 y% o: W. o6 M! ^2 [
uncompressed bytes out: 5118) K( X1 r2 r9 P0 L" `$ Z2 P' }
packets in: 15
8 d8 o) {( v6 \packets out: 24
8 T. [6 c* n8 L; f9 v& s: A: b* Y& @rekeys: 0: r  K% R3 ]5 B
Algorithms:, x- b+ L. u- r: C# Y5 N8 g
Chosen key exchange algorithm: diffie-hellman-group1-sha1" B& x2 n1 D- e! d; J9 I" H$ `
Chosen host key algorithm: ssh-dss) I* T4 E: Y; @% ?( l0 `
Common host key algorithms: ssh-dss,ssh-rsa3 @" @$ S2 J2 K" \, l2 S5 F
Algorithms client to server:; c. S9 U. g% ^* X8 p6 `+ Z" P7 r
Cipher: aes128-cbc: r) q3 ~+ Q$ z/ \0 Y
MAC: hmac-sha1
$ }" w' W) c) {9 y, e7 KCompression: zlib8 u+ R( M: r. S; Y, [7 T
Algorithms server to client:- |7 N% M; A4 u! w/ }3 N
Cipher: aes128-cbc MAC: hmac-sha1
$ v; Z1 A: |* u. CCompression: zlib
) u) j; N6 F$ a0 ?) {) H4 zlocalhost$
6 ^# L) L% Q6 c* }3 _, v以上就是Linux下SSH的详细使用方法,希望能够对你有所帮助
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-20 08:50 , Processed in 0.089108 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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