沒作任何配置前從serverA上SSH登陸到serverB時須要輸入密碼的(若是是第一次登陸,輸入密碼前還會詢問受權yes/no,只管輸入yes就行):centos
[binxin@serverA ~]$ ssh binxin@serverB binxin@serverb's password: Last login: Fri Apr 1 00:35:41 2016 from servera [binxin@serverB ~]$
下面開始免密登錄的配置:dom
[binxin@serverA ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/binxin/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/binxin/.ssh/id_rsa. Your public key has been saved in /home/binxin/.ssh/id_rsa.pub. The key fingerprint is: f2:f1:00:ca:b0:d0:3c:52:ac:9b:b7:0d:7e:62:f3:39 binxin@serverA The key's randomart image is: +--[ RSA 2048]----+ | .. | | +. | |o.= . | |.o = . . | | o. o . S | |o o o + | | o + . . | | * E. | | . =o. | +-----------------+ [binxin@serverA ~]$
查看用戶目錄下的ssh(隱藏的)文件夾,祕鑰對已經生成,公鑰id_rsa.pub,私鑰id_rsassh
[binxin@serverA ~]$ cd .ssh/ [binxin@serverA .ssh]$ ls id_rsa id_rsa.pub known_hosts [binxin@serverA .ssh]$
[binxin@serverA .ssh]$ scp ~/.ssh/id_rsa.pub binxin@serverB:/home/binxin/id_rsa.pub binxin@serverb's password: id_rsa.pub 100% 396 0.4KB/s 00:00 [binxin@serverA .ssh]$
[binxin@serverB ~]$ cd .ssh/ [binxin@serverB .ssh]$ ls [binxin@serverB .ssh]$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys [binxin@serverB .ssh]$ ls authorized_keys [binxin@serverB .ssh]$
[binxin@serverB ~]$ chmod 700 ~/.ssh [binxin@serverB ~]$ chmod 600 ~/.ssh/authorized_keys
[binxin@serverA .ssh]$ ssh binxin@serverB Last login: Fri Apr 1 00:46:54 2016 from servera [binxin@serverB ~]$
配置完authorized_keys一直不生效,極可能是由於.ssh目錄和下面文件的權限問題致使的,由於目錄的權限已經超過了sshd的要求權限。若是但願ssh公鑰生效需知足至少下面兩個條件:.ssh目錄的權限必須是700,.ssh/authorized_keys文件權限必須是600ide