工作中一般需要一台运维机来管理多台主机,就需要运维机和主机间的无密码登录,直接ssh就可以连接成功,否则每次都需要输入密码很麻烦。其实实现这种无密码登录很简单,主机间进行秘钥认证就可以了!
使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机。
ssh-keygen 创建公钥和密钥。 ssh-copy-id 把本地主机的公钥复制到远程主机的authorized_keys文件上。 ssh-copy-id 也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh/authorized_keys设置合适的权限 。 注意权限 chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
步骤1: 用 ssh-keygen 在本地主机上创建公钥和密钥
ssh-keygen -t rsa # 然后一直按 [Enter] 键 Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key] Enter passphrase (empty for no passphrase): [Press Enter key] Enter same passphrase again: [Pess Enter key] Your identification has been saved in /home/jsmith/.ssh/id_rsa. Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. The key fingerprint is: 33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 ll /root/.ssh/ # 在用户的家目录$home/.ssh/ 路径下就会有2个文件生成,id_rsa私钥,id_rsa.pub公钥 -rw------- 1 root root 1671 1月 19 14:59 id_rsa -rw-r--r-- 1 root root 401 1月 19 14:59 id_rsa.pub
步骤2: 用 ssh-copy-id 把公钥复制到远程主机上
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.10.2 # 将自己的id_rsa.pub导入到远程主机10.10.10.2的/root/.ssh/authorized_key 里面 ligh@remote-host‘s password: Now try logging into the machine, with ―ssh ?remote-host‘‖, and check in: .ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting. [注: ssh-copy-id 把密钥追加到远程主机的 .ssh/authorized_key 上.]
步骤3: 直接登录远程主机
ssh 10.10.10.2 # 就可以直接登录,不需要输入密码 Last login: Sun Nov 16 17:22:33 2008 from 10.10.10.3 [注: SSH 不会询问密码.] root@10.10.10.2: # 此时已经登录到远程主机上
转载于:https://blog.51cto.com/liumissyou/1600814