Linux|系统管理|WEB开发

关注Linux,系统管理,WEB开发以及开源世界

登陆ssh不需要使用密码

| Comments

每次登陆ssh都需要输入用户名和密码,我想作一个双机的互备。考虑密码隔断时间都需要修改,那我的备份程序也需要修改。因此就想使用不需要密码用ssh登陆机器,这样我就可以使用rsync,scp,rexec等命令来做的远程备份了。

方法如下: 假设A,B两服务器,现在需要在A机上用root登陆B机,而不需要输入密码,那我们可按照下面的步骤来做:

  1. 在A机上生成钥匙对

    A# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:4c:c8:a0:23 mlsx@mlsx

这样,会生成id_rsa,和id_rsa.pub,其中id_rsa是密钥,id_rsa.pub是公钥。

  1. 把在A机生成的d_rsa.pub拷贝到B机上。 scp id_rsa.pub root@B:/tmp

  2. 用root帐号登陆B机,进入其主目录,创建authorized_keys文件,并设置好权限

     B# cd ~/.ssh 
     #cat/tmp/id_rsa.pub >>authorized_keys 
     #chmod 400 authorized_keys 
    
  3. 测试 在A机上转到root帐号,尝试登录B机。看看是不是不要密码.

  4. authorized_keys文件的权限很重要,如果设置为777,那么登录的时候,还是需要提供密码的。

本方法在Fedora Core 2 和RedFlag Desktop 4.1上测试通过。 有什么问题,欢迎交流!

Comments