Mac ssh登陆远程Linux不需输入密码

本文介绍了如何通过SSH密钥配对,实现从Mac电脑无密码登录远程Linux服务器。首先在Mac上生成公钥和私钥,然后将公钥复制到Linux主机,通常使用ssh-copy-id命令可以便捷完成此过程,从而实现安全的自动化登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步骤如下:

1. 使用ssh-key-gen在本地Mac上生成公钥和密钥

jsmith@local-host$ [Note: You are on local-host here]

jsmith@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host

2.把产生的公钥拷贝到远端host

一般Linux中可以直接使用 ssh-copy-id命令完成:

jsmith@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
{ssh-copy-id会把公钥追加到远程主机的~/.ssh/authorized_keys中}


但是Mac 下使用ssh-copy-id报错: -bash: ssh-copy-id: command not found
解决办法1: 
使用命令代替ssh-copy-id:   cat ~/.ssh/id_rsa.pub | ssh usrname@remotehost "umask 077;  mkdir -p .ssh; cat>>.ssh/authorized_keys"
其中id_rsa.pub是生成的公钥, usrname是远程用户名,remotehost是远程主机ip
解决办法2:
直接使用scp把公钥拷贝到remotehost,然后登陆的remotehost上创建文件~/.ssh/authorized_keys, 并把公钥的内容追加到这个文件中。
jsmith@local-host$ scp ~/.ssh/id_rsa.pub  usrname@remotehost:~/.ssh/
jsmith@local-host$  cat id_rsa.pub>>~/.ssh/authorized_keys

3.再次登陆远程主机
jsmith@local-host$ ssh remotehost
这时,不用输入密码就可以成功登陆了~

参考资料:http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值