Ssh,scp自动登陆方法

最近在编写定时python脚本时遇到一个问题。在该脚本中,需要使用scp命令将本地的文件复制到另一台机器中备份。但通常执行scp命令后都需要输入用户密码,这样在定时自动执行的python脚步中就不适用了。
后来在Internet中查找相关资料,有资料[1]介绍在两台机器的两个用户之间建立安全的信任关系后,可实现执行scp命令时不需要输入用户密码。(该方法对ssh也适用)    

   以下为了讨论方便,我们将执行scp命令的机器称为Client,scp命令操作的远端机器称为Server。
以下是在机器Client的root和机器Server的root之间建立安全信任关系的步骤:
1. 在机器Client上root用户执行ssh-keygen命令,生成建立安全信任关系的证书。
[root@Client root]# ssh-keygen -b 1024 -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:
49:9c:8a:8f:bc:19:5e:8c:c0:10:d3:15:60:a3:32:1c root@Client
[root@Client root]#
注意:在程序提示输入passphrase时直接输入回车,表示无证书密码。
上述命令将生成私钥证书id_rsa和公钥证书id_rsa.pub,存放在用户家目录的.ssh子目录中。
2. 将公钥证书id_rsa.pub复制到机器Server的root家目录的.ssh子目录中,同时将文件名更换为authorized_keys。
        [root@Client root]# scp -p .ssh/id_rsa.pub root@192.168.3.206:/root/.ssh/authorized_keys
root@192.168.3.206’s password:          <– 输入机器Server的root用户密码
id_rsa.pub           100% |**************************|   218       00:00
[root@Client root]#
在执行上述命令时,两台机器的root用户之间还未建立安全信任关系,所以还需要输入机器Server的root用户密码。
经过以上2步,就在机器Client的root和机器Server的root之间建立安全信任关系。下面我们看看效果:
[root@Client root]# scp -p text root@192.168.3.206:/root
        text                 100% |**************************|    19       00:00
[root@Client root]#
**********************************************************

Whenever you need to use scp to copy files, it asks for passwords. Same with rsync as it (by default) uses ssh as well. Usually scp and rsync commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying the password is asked every time. I even had the idea of writing an expect script to provide the password. Of course, I didn’t. Instead I browsed for a solution and found it after quite some time. There are already a couple of links out there which talk about it. I am adding to it…

Lets say you want to copy between two hosts host_srcand host_dest. host_srcis the host where you would run the scp,sshorrsyncommand, irrespective of the direction of the file copy!

  1. On host_src, run this command as the user that runs scp/ssh/rsync

    $ ssh-keygen -t rsa

    This will prompt for a passphrase. Just press the enter key. It’ll then generate an identification (private key) and a public key. Do not ever share the private key with anyone! ssh-keygen shows where it saved the public key. This is by default ~/.ssh/id_rsa.pub:

    Your public key has been saved in <your_home_dir>/.ssh/id_rsa.pub

  1. Transfer theid_rsa.pubfile tohost_destby eitherftp, scp,rsyncor any other method.
  1. On host_dest, login as the remote user which you plan to use when you run scp, sshor rsyncon host_src.

  2. Copy the contents of id_rsa.pubto ~/.ssh/authorized_keys

$ cat id_rsa.pub >>~/.ssh/authorized_keys
$ chmod 700 ~/.ssh/authorized_keys

If this file does not exists, then the above command will create it. Make sure you remove permission for others to read this file. If its a public key, why prevent others from reading this file? Probably, the owner of the key has distributed it to a few trusted users and has not placed any additional security measures to check if its really a trusted user.

  1. Note that ssh by default does not allow root to log in. This has to be explicitly enabled on host_dest. This can be done by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin from no to yes. Don’t forget to restart sshd so that it reads the modified config file. Do this only if you want to use the root login.

Well, thats it. Now you can run scp, ssh and rsync on host_srcconnecting to host_destand it won’t prompt for the password. Note that this will still prompt for the password if you are running the commands on host_dest connecting to host_src. You can reverse the steps above (generate the public key on host_destand copy it to host_src) and you have a two way setup ready!

###########################

A为本地主机(即用于控制其他主机的机器) ;
B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;
A和B的系统都是Linux

在A上运行命令:
# ssh-keygen -t rsa (连续三次回车,即在本地生成了公钥和私钥,不设置密码)
# ssh root@192.168.60.110  “mkdir .ssh; chmod 0700 .ssh” (需要输入密码)
# scp ~/.ssh/id_rsa.pub  root@192.168.60.110:.ssh/id_rsa.pub (需要输入密码)

在B上的命令:
# touch /root/.ssh/authorized_keys2 (如果已经存在这个文件, 跳过这条)
# cat /root/.ssh/id_rsa.pub  >> /root/.ssh/authorized_keys2 (将id_rsa.pub的内容追加到 authorized_keys2 中)

回到A机器:
# ssh root@192.168.60.110 (不需要密码, 登录成功)

如果能保护好自己的私钥, 这种方法相对在shell上输入密码, 要安全一些

##############################################
深入一点点:

从表面上简单的理解一下登录的过程,
首 先 ssh-keygen -t rsa 命令生成了一个密钥和一个公钥, 而且密钥可以设置自己的密码,可以把密钥理解成一把钥匙, 公钥理解成这把钥匙对应的锁头,把锁头(公钥)放到想要控制的server上, 锁住server, 只有拥有钥匙(密钥)的人, 才能打开锁头, 进入server并控制,而对于拥有这把钥匙的人, 必需得知道钥匙本身的密码,才能使用这把钥匙 (除非这把钥匙没设置密码), 这样就可以防止钥匙被人配了(私钥被人复制)

当然, 这种例子只是方便理解罢了,
拥有root密码的人当然是不会被锁住的, 而且不一定只有一把锁(公钥), 但如果任何一把锁, 被人用其对应的钥匙(私钥)打开了, server就可以被那个人控制了
所以说, 只要你曾经知道server的root密码, 并将有root身份的公钥放到上面, 就可以用这个公钥对应的私钥”打开” server, 再以root的身分登录, 即使现在root密码已经更改!

如果想控制n个机器, 那就需要n对钥匙(密钥和公钥), ssh-keygen 命令可以随意更改钥匙对的名字, 比如:
[root@wwy .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_192.168.102.12
……

这样私钥和公钥的名字分别就是:
id_rsa_192.168.102.12 和 id_rsa_192.168.102.12.pub
然后将 id_rsa_192.168.102.12.pub 文件的内容, 追加到sever的 ~/.ssh/authorized_keys2 文件中,

最后, 在本地用ssh命令的 -i 参数指定本地密钥, 并登录:
# ssh -i /root/.ssh/id_rsa_192.168.102.12  192.168.102.12

如果密钥设置了密码, 就用密钥的密码登录, 没设密码, 就直接登录进去了

scp也是一样的
如:
scp -i /root/.ssh/id_rsa./xxx  192.168.102.158:/home/wwy/bak

 

发表评论