ssh免密码登录服务器

步骤

  1. 本机生成公钥 & 私钥
> ssh-keygen -t rsa # 参数-t指定生成的密匙类型,其实默认类型也是 rsa,也就是说可以什么都不加
  1. 拷贝公钥到远端主机
> ssh-copy-id user@host # 方法1
> ssh user@host 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub # 方法2

注意 & 建议

  1. 用户权限不足,拷贝公钥到服务器将出现错误!
  2. 检查/etc/ssh/sshd_config文件是否如下设置
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
  1. 若修改了配置文件,需重启sshd服务
service sshd restart // centos 6
systemctl restart sshd.service // centos 7
  1. 服务器~/.ssh目录权限需设为700,文件.ssh/authorized_keys权限需设为600,如下:
> chmod 700 ~/.ssh
> chmod 600 ~/.ssh/authorized_keys

参考

http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html
http://www.ruanyifeng.com/blog/2011/12/ssh_port_forwarding.html

你可能感兴趣的:(ssh免密码登录服务器)