因爲工作中經常需要配置服務器之間的ssh互信.在網上找到好多關於ssh互信的配置方法,大多很麻煩,而且配置起來經常不成功.
下面爲介紹一種可以快速建立ssh互信的方法:
實驗背景:
使用的系統爲CentOS 6.0
兩臺服務器:
服務器A :地址192.168.26.134 新建用戶redhat 密碼: redhat ;
服務器B :地址192.168.26.135 新建用戶centos 密碼: centos ;
(出於安全考慮一般不配置root用戶的ssh互信)
配置過程
----------------------------------------------------------------------------
一.配置服務器A:
1.用redhat用戶登錄系統.或者su 到redhat用戶.
2.生成私鑰和公鑰文件
- ssh-keygen -t rsa
(注:這裏不需要事先在家目錄下建立 .ssh 文件夾)
回車後會顯示如下內容: (之後直接回車就可以了)
- Generating public/private rsa key pair.
- Enter file in which to save the key (/home/redhat/.ssh/id_rsa):(回車)
- Created directory '/home/redhat/.ssh'. //自動建立 .ssh文件夾
- Enter passphrase (empty for no passphrase): (回車)
- Enter same passphrase again: (回車)
顯示類似如下內容則表明私,公鑰文件生成成功
3.將公鑰拷貝到服務器B上
- ssh-copy-id -i .ssh/id_rsa.pub [email protected]
出現如下內容:
- The authenticity of host '192.168.26.135 (192.168.26.135)' can't be established.
- RSA key fingerprint is 50:8b:93:c4:98:a9:ca:3d:44:68:9d:71:73:64:53:3e.
- Are you sure you want to continue connecting (yes/no)? (這裏輸入yes)
之後會讓你輸入服務器B上的用戶的密碼
- Warning: Permanently added '192.168.26.135' (RSA) to the list of known hosts.
- [email protected]'s password: (輸入centos用戶的密碼centos)
最後顯示如下內容表示拷貝成功:
4.驗證ssh時是否還需要輸入密碼:
- ssh [email protected] -- 'whoami'
- ssh-copy-id -i .ssh/id_rsa.pub [email protected]
一切正常的話會直接顯示centos用戶的用戶名
---------------------------------------------------------------------------
二.配置服務器B:
配置過程跟配置服務器A相同,這裏簡化描述,只描述步驟與命令.
1.登陸服務器B
2.生成密鑰文件
- ssh-keygen -t rsa
3.拷貝公鑰文件服務器A
- ssh-copy-id -i .ssh/id_rsa.pub [email protected]
4.驗證
- ssh [email protected] -- 'whoami'
--------------------------------------------------------------------------
總結.到此服務器之間的ssh就建立好了.總的來說建立互信只需要執行兩條命令
- ssh-keygen -t rsa
- ssh-copy-id -i .ssh/(公鑰文件,以.pub結尾的文件) USER@(服務器地址)
而不需要建立目錄,修改權限,修改文件.