免密码登录是指在登录系统时无需输入密码即可进入,通常通过公钥认证(SSH密钥对)实现。SSH(Secure Shell)是一种加密的网络协议,用于在不安全的网络上安全地传输数据。
在本地机器上生成SSH密钥对:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
这将生成一个公钥(id_rsa.pub
)和一个私钥(id_rsa
)。
将生成的公钥复制到远程服务器的~/.ssh/authorized_keys
文件中:
ssh-copy-id user@remote_host
或者手动复制:
cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
确保SSH客户端配置正确,通常不需要额外配置,但可以在~/.ssh/config
文件中进行自定义:
Host remote_host
User user
IdentityFile ~/.ssh/id_rsa
原因:
~/.ssh/authorized_keys
文件中。解决方法:
~/.ssh/authorized_keys
文件中。chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
原因:
解决方法:
chmod 600 ~/.ssh/id_rsa
原因:
/etc/ssh/sshd_config
)中的设置不正确。解决方法:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
sudo systemctl restart sshd
通过以上步骤,你应该能够成功实现Mac和Linux之间的免密码登录。
领取专属 10元无门槛券
手把手带您无忧上云