ssh 免密码登陆远程主机

Posted on Posted in command
Tips: 本文创建于2015年3月31日,已超过 2 年,内容或图片可能已经失效!

ssh 禁止密码登录,开通秘钥登录

#生成SSH密钥对,-P,后面两个单引号表示空密码,不加敲三次回车,加敲一次(如果需加密,则不加-P)        
ssh-keygen -t rsa -P '' -N '' -f ~/.ssh/id_rsa -C sma.flirt@gmail.com   
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:         
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:        
aa:8b:61:13:38:ad:b5:49:ca:51:45:b9:77:e1:97:e1 root@localhost.localdomain        
The key's randomart image is:        
+--[ RSA 2048]----+        
|    .o.          |        
|    ..   . .     |        
|   .  . . o o    |        
| o.  . . o E     |        
|o.=   . S .      |        
|.*.+   .         |        
|o.*   .          |        
| . + .           |        
|  . o.           |        
+-----------------+        
        
        
#复制公钥到无密码登录的服务器上,22端口改变可以使用下面的命令        
#ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 10022 user@server"        
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.15.241        
        
修改SSH配置文件        
        
#编辑sshd_config文件        
vi /etc/ssh/sshd_config        
#禁用密码验证        
PasswordAuthentication no  #禁止使用基于口令认证的方式登陆      
#启用密钥验证        
RSAAuthentication yes        
PubkeyAuthentication yes  #允许使用基于密钥认证的方式登陆      
#指定公钥数据库文件        
AuthorsizedKeysFile .ssh/authorized_keys        
  
# 取消下面的提示  
The authenticity of host 'hostname (xxxxxxxx)' can't be established.  
RSA key fingerprint is 5e:e0:84:df:1e:f5:30:f3:6f:ec:83:0b:29:0a:8f:94.  
Are you sure you want to continue connecting (yes/no)?  
  
cd .ssh/  
vim config  
StrictHostKeyChecking no  
UserKnownHostsFile /dev/null  
  
chmod 600 *  
/etc/init.d/sshd restart  
» 转载请注明来源:呢喃 » ssh 免密码登陆远程主机

Leave a Reply

Your email address will not be published. Required fields are marked *

15 − three =