原文地址:在这里vim
第一步:在本机中建立秘钥服务器
一、执行命令:ssh
ssh-keygen -t rsa -C "xx@qq.com"
(随便编个字符串,通常用邮箱)编辑器
二、以后一路回车就行啦;会在~(home)目录下中产生.ssh(隐藏)文件夹;
三、里面有两个文件id_rsa(私钥)、id_rsa.pub(公钥)文件
注意事项:
①在liunx环境下,要想复制公钥或是私钥,不要使用vim等编辑器打开文件来复制粘贴;
由于它会产生没必要要的回车。
②应该使用cat把内容打印到终端上再来复制粘贴;.net
第二步:用 ssh-copy-id 把公钥复制到远程主机上日志
ssh-copy-id zhangming@192.168.161.132
用这种方式拷贝使用的端口是Linux默认的22,若是你想指定端口,能够使用:
ssh-copy-id -i /用户名/.ssh/id_rsa.pub '-p 端口号 用户名@106.75.52.44'code
ssh-copy-id -i /root/.ssh/id_rsa.pub '-p 22222 root@106.75.52.44'
这里可能须要等一段时间,反正我是等了挺久的时间,而后显示要你输入密码:
zhangming@106.75.52.44's password:
输入完密码后,显示:blog
Now try logging into the machine, with "ssh '-p 22222 root@106.75.52.44'", and check in:
.ssh/authorized_keysip
to make sure we haven't added extra keys that you weren't expecting.
表示成功了!字符串
第三步: 远程登入
[zhangming@localhost ~]$ ssh zhangming@192.168.161.134 Last login: Mon Oct 10 14:18:54 2016 from 192.168.161.135 ssh zhangming@123.59.44.56 -p 22222
注意
遇到的大坑:配置ssh免密码登陆后,仍提示输入密码
解决方法:
首先咱们就要去查看系统的日志文件
tail /var/log/secure -n 20
发现问题的所在:Authentication refused: bad ownership or modes for file
从字面上能够看出是目录的属主和权限配置不当,查找资料得知:SSH不但愿home目录和~/.ssh目录对组有写权限,经过下面几条命令改下
chmod g-w /home/zhangming chmod 700 /home/zhangming/.ssh chmod 600 /home/zhangming/.ssh/authorized_keys
而后咱们再去登陆,就能不用密码进入了。
有木有很高兴呀!