码迷,mamicode.com
首页 > 系统相关 > 详细

Linux ssh下实现免密码登录

时间:2019-02-19 01:16:23      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:install   用户   art   远程   ica   tar   sshd_conf   存在   bsp   

1.Linux 生成密钥

   ssh-keygen -t rsa 

    进入“.ssh”会生成以下几个文件 
id_rsa : 生成的私钥文件

id_rsa.pub : 生成的公钥文件 

know_hosts : 已知的主机公钥清单

2.远程免密登录(现在我们就需要把key文件复制到其它几台服务器了,)

常用以下几种方法:

a.通过ssh-copy-id的方式 
 命令: ssh-copy-id (服务器IP)

 常见错误:

      [root@test ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135

      -bash: ssh-copy-id: command not found //提示命令不存在

      解决办法:yum -y install openssh-clients

b.通过scp将内容写到对方的文件中

 命令:scp -p ~/.ssh/id_rsa.pub root@<remote_ip>:/root/.ssh/authorized_keys

也可以分为两步操作:

$ scp ~/.ssh/id_rsa.pub root@<remote_ip>:pub_key //将文件拷贝至远程服务器
$ cat ~/pub_key >>~/.ssh/authorized_keys //将内容追加到authorized_keys文件中, 不过要登录远程服务器来执行这条命令

2.3 通过Ansible实现批量免密

2.3.1 将需要做免密操作的机器hosts添加到/etc/ansible/hosts下:

  [Avoid close]
  192.168.91.132
  192.168.91.133
  192.168.91.134

2.3.2 执行命令进行免密操作

  ansible <groupname> -m authorized_key -a "user=root key=‘{{ lookup(‘file‘,‘/root/.ssh/id_rsa.pub‘) }}‘" -k

2.4 手工复制粘贴的方式

  将本地id_rsa.pub文件的内容拷贝至远程服务器的~/.ssh/authorized_keys文件中

 

最后注意相应文件和文件夹的权限控制:

sudo chmod 700 .ssh
sudo chmod 600 authorized_keys

备注:如果想服务器不用用户密码登录,而是用SSH Key

即做如下操作:ssh-keygen -t rsa -->cat id_rsa.pub> authorized_keys-->修改/etc/ssh/sshd_config文件,  把密码认证关闭, 将认证改为 passwordAuthentication no   重启下sshd。 service sshd restart;

 

Linux ssh下实现免密码登录

标签:install   用户   art   远程   ica   tar   sshd_conf   存在   bsp   

原文地址:https://www.cnblogs.com/ppp1314520818/p/10398666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!