mac实现ssh的免密远程登录

主要是在mac下利用自带的终端登录虚拟机中的ubuntu系统。用的是virtualbox,虚拟机网咯为NAT模式,设置端口转发,本机的10000对应虚拟机的22端口。22端口是ssh的默认端口。(如果是login远程主机,这些端口的配置可以省略)。  

首先,在编辑/etc/hosts文件,添加虚拟机主机的IP解析,在127.0.0.1后加入需要login的主机hostname。   

运行

ssh-keygen

一路默认。生成一个.ssh目录,包含两个文件id_rsa以及id_rsa.pub。其中id_rsa是私钥,id_rsa.pub是公钥。在~/.ssh/目录下配置文件config。

Host         remotehostname
Hostname     remotehostname
Port 10000 User userThatLoginRemoteHost
chmod 600 ~/.ssh/config

 权限必须设置为600,否则无法正常工作。此时ssh remotehostname,会需要输入密码,以及记录Hostkey。若要免密登录,需要将本机用户的公钥拷贝至remotehost ~/.ssh/authorized_keys。由于mac没有ssh-copy-id,使用scp来进行文件传输。  

scp ~/.ssh/id_rsa.pub user@remotehostname:~/.ssh/id_rsa_tmp.pub

In remotehost:
mv id_rsa_tmp.pub authorized_keys
chmod 600 authorized_keys

编辑本机文件config:

echo "IdentityFile ~/.ssh/id_rsa" >> config

此时要登录远程主机:

ssh remotehost
原文地址:https://www.cnblogs.com/zhang-wen/p/5861543.html