[root@192.168.1.111] ssh-keygen -t rsa [root@192.168.1.111] #以下是生成一个为id_rsa/id_rsa.pub的无密码的公/私钥对 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: [root@192.168.1.111] ls -l ~/.ssh [root@192.168.1.111] #这样我们会看到如下所示的结果集: -rw------- 1 root root 1675 Jul 5 14:01 id_rsa -rw-r--r-- 1 root root 394 Jul 5 14:01 id_rsa.pub |
然后将id_rsa.pub的内容复制到每个机器(也包括本机)的~/.ssh/authorized_keys文件中。如果authorized_keys不存在,则使用touch ~/.ssh/authorized_keys生成一个;如果该文件已经存在,则追加内容进去就OK了,这里我们推荐使用如下命令:
[root@192.168.1.111] cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys [root@192.168.1.111] #因为ssh服务对文件的权限有着非常严格的限制(authorized_keys只能拥有指定用户的写权限)于是需要进行如下处理 [root@192.168.1.111] chmod 644 ~/.ssh/authorized_keys |
经过以上步骤,我们的无密码访问就配置好了,可以通过如下命令进行验证:
[root@192.168.1.111] ssh 192.168.1.112 [root@192.168.1.112] exit [root@192.168.1.111] ssh 192.168.1.113 [root@192.168.1.113] exit |