我们经学会用到ssh、scp、sftp这类命令,但我们不希望交互式输入密码,这样我们就必须在之前做一个准备工作,如下:
假如有两台主机:linuxA/linuxB
第一步:linuxA上生成密匙对,用的是rsa的密钥。使用命令 "ssh-keygen -t rsa"
[root@linuxA]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
e0:f0:3b:d3:0a:3d:da:42:01:6a:61:2f:6c:a0:c6:e7 user1@rh.test.com
生成的过程中提示输入密钥对保存位置,直接回车,接受默认值就行了。接着会提示输入一
个不同于你的password的密码,直接回车,让它空着。当然,也可以输入一个。 Then,密钥对就生成完了。
其中公共密钥保存在 ~/.ssh/id_rsa.pub
私有密钥保存在 ~/.ssh/id_rsa
然后改一下 .ssh 目录的权限,使用命令 "chmod 755 ~/.ssh"
[root@linuxA]$ chmod 755 ~/.ssh
之后把这个密钥对中的公共密钥复制到你要访问的机器上去,并保存为
~/.ssh/authorized_keys.
[root@linuxA]$ scp ~/.ssh/id_rsa.pub root@linuxB:/home/user1/.ssh/authorized_keys
root@linuxB's password:
id_rsa.pub 100% 228 3.2MB/s 00:00
[root@linuxA]$
这样就完成了。之后在LinuxA上使用ssh scp sftp 之类的访问linuxB时,就不用输入密码
了,用在shell script上更是方便。