|
|
初来此地, 收获不少, 也想做点贡献. 我发现好像很多人
不用ssh, 所以写点东西算是抛块砖头.
本文以redhat9做例子, 但大多数例子也适合其它版本的
GNU/Linux和UNIX.
为什么要用ssh? telnet或rsh不是挺好? 首先当然是安全上
的考虑. telnet和rsh用明码传递密码, 非常不安全. 除了安
全的因素, ssh还有许多telnet和rsh所没有的功能, 像打通
防火墙, 压缩后再传以提高速度, 等等. 我想不出有不用ssh
的理由. 我每天都在用ssh, 但至少有三年没用过telnet了.
RH9里带有openssh. 你需要以下rpm包: openssh-clients,
openssh, openssh-server. 另外推荐把openssh-askpass和
openssh-askpass-gnome也装上. 你可以用
"rpm -qa | grep ssh"来查看你是否安装了这些包.
在第一次使用ssh前, 你还应该核对一些东西. 如果你要从
A机ssh登录到B机, 你要让B允许ssh进来. 你可以运行
"redhat-config-securitylevel", 然后选"Customize",
并且"Allow incoming SSH". 另外,你还要确保B机上的
sshd在运行. 如果没有,运行
"/sbin/chkconfig --level 2345 sshd on", 然后,
"/etc/rc.d/init.d/sshd start". 当然这些都需要root的
身份。
如果都好了,我们来第一个例子。假设你要登录到
remote.foo.net, 你在那上面的用户名是q, 你可以,
"ssh -l q remote.foo.net". 接着你可能会看到,
The authenticity of host 'remote.foo.net (1.2.3.4)' can't be established.
RSA key fingerprint is ed:61:e2:ce:40:00:ca:06:93:e8:a4:c2:3a:37:1e:9c.
Are you sure you want to continue connecting (yes/no)?
这时你需要输入yes (这一步以后不会出现). 然后
你会看到,
Warning: Permanently added 'remote.foo.net (1.2.3.4)' (RSA) to the list of known hosts.
q@remote.foo.net's password:
你就输入你的密码后回车。现在你应该已经登录上了。
你也可以用"ssh q@remote.foo.net"来代替
“ssh -l q remote.foo.net". 如果你在本地机上
的用户名和远程的一样, 你可以省略用户名,只要
"ssh remote.foo.net"就行了。又如果你的本地机是
local.foo.net, 你可以更简单,"ssh remote"就行.
如果你在本地用的是windows, 你可以试试putty.
有时你需要在不同机子间传递文件,你可以用"scp".
比如说你需要将本地的一个文件myfile拷到远程机
的某个目录下,你就,
"scp myfile q@remote.foo.net:/home/q/mydir/"
或"scp myfile q@remote.foo.net:~/mydir/". 这样
你在远程上就会有个文件: ~/mydir/myfile. 再举一
个例子, 你想把remote.foo.net上的~/mydir/myfile
拷到本地并改名叫mylocalfile. 你可以,
"scp q@remote.foo.net:~/mydir/myfile mylocalfile".
在用scp时, 你还当然还可以用*?等通配符. 另外"scp -r"
也很有用, 可以拷贝整个目录.
在用ssh和scp时, 你还可以加上"-C". 这样机子间的通
信就是压缩了再进行, 变快了. 如果你的网速很慢, 效
果很大.
有时在使用ssh时, 你会遇到类似下面警告,
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for xxx has changed,
and the key for the according IP address xxx
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
8a:32:d1:f3:5a:81:27:9b:fd:02:13:dc:da:c5:d0:7d.
Please contact your system administrator.
Add correct host key in /home/xxx/.ssh/known_hosts to get rid of this message.Offending key in /home/xxx/.ssh/known_hosts:4
RSA host key for xxx has changed and you have requested strict checking.
Host key verification failed.
这一般是因为远程机重装了的缘故. 你只要改一下
本地的~/.ssh/known_hosts就行了. 把那个文件里与那个远
程机有关的删掉就行了.
ssh还有一个很有用的功能. 例如,
"ssh remote.foo.net w" 可以显示有哪些用户在那台机子
上. 当然w也可以是其它命令. 这一招下面可以用上.
用过一阵ssh后, 你可能厌倦了反复地敲password. 下
面介绍"钥匙", 可以让你偷些懒, 而且更安全. 你首
先要生成钥匙. "ssh-keygen -t rsa"可以给你生成
钥匙. 在生成过程中会要你"Enter passphrase", 你就
输入一个. 你可以用一句话像, "I am using -*% ssh.".
最好不要用空的passphrase. 然后你会发现在~/.ssh下有
两个新文件: id_rsa和id_rsa.pub. 你把id_rsa.pub加入
到remote.foo.net的/home/q/.ssh/authorized_keys的文
件末尾. (假设q是你的用户名.) 你当然有你的办法轻易
做到这一点. 你也可以学了ssh就用上.
"cat id_rsa.pub | ssh remote.foo.net 'cat >> ~/.ssh/authorized_keys'"
就行了. (别连"也输入!) 现在再试一下"ssh remote.foo.net",
你会发现你还需要敲密码, 而且是更长的passphrase. 不过
如果你刚才输入的passphrase是空的, 你现在就不用敲, 自
动进去了. 这样当然方便, 但显然不安全! 这就是为什么我
刚刚不推荐用空的passphrase. 你别着急. 有更好的办法:
ssh-agent. 下面一段我是从redhat的手册那儿"偷"来的.
如果你用GNOME,
Select Main Menu Button (on the Panel) => Preferences
=> More Preferences => Sessions, and click on the
Startup Programs tab. Click Add and enter /usr/bin/ssh-add
in the Startup Command text area. Set it a priority to a
number higher than any existing commands to ensure that it
is executed last. A good priority number for ssh-add is 70
or higher. The higher the priority number, the lower the
priority. If you have other programs listed, this one should
have the lowest priority. Click Close to exit the program.
Log out and then log back into GNOME; in other words, restart X.
After GNOME is started, a dialog box will appear prompting you
for your passphrase(s). Enter the passphrase requested. If you
have both DSA and RSA key pairs configured, you will be prompted
for both. From this point on, you should not be prompted for a
password by ssh, scp, or sftp.
大意是说, 选择主菜单=>首选=>更多首选=>sessions. 点击
"StartupPrograms". 点击"加"并输入/usr/bin/ssh-add".
将优先级设为70. 然后就退出再登录. 以后每次登录时你会
被问passphrase, 你就输入你的passphrase. 下面ssh和scp时
你就不需要密码了. 如果你不用GNOME, 你就自己想办法吧.
有时候你需要登录的机子在防火墙后面. 假设防火墙是
firewall.foo.net, 而你想登录的remote.foo.net在防
火墙后, 无法直接进入. 你可以用ssh打条通道. 先运行
"ssh -L 9022:remote.foo.net:22 firewall.foo.net".
然后, "ssh -p 9022 localhost", 你就进remote了.
注意, 如果你的用户名在个机子上不一样, 你需要用
"yourname@remote..."等等. 同样道理, 你也可以为
其它如SMTP等打开通道. 比如,
"ssh -L 9025:smtp.foo.net:25 firewall.foo.net"后,
你的localhost:9025就通到smtp.foo.net:25了.
如果你要等录veryverylonglonglongname.foo.net, 敲
那么长的名字实在太麻烦了. 有个办法可以帮你. 你建
立个如下内容文件: ~/.ssh/config.
Host shortname
HostName veryverylonglonglongname.foo.net
这样你"ssh shortname"就行了. 这个文件还可以有很
多用处.以上一段的防火墙做例子. 你可以建立如下文件:
~/.ssh/config.
Host remote
HostName 127.0.0.1
Port 9022
Host firewall
HostName firewall.foo.net
LocalForward 9022 remote.foo.net:22
这样你就可以用"ssh firewall"来打通道, 以后用
"ssh remote"来等录remote.foo.net, 用"scp remote"
来拷文件了.
ssh还有许许多多功能, 不可能一一介绍, 我也没
有这个能力. 大家一起学吧! |
|