假设我们在client.aslibra.com的机器要实现免输入密码登录到server.aslibra.com
那要做什么事情呢?

1 client生成密钥
2 client把公钥上传到server
3 server把公钥加入到自己的authorized_keys文件即可

修改@0508:
谢谢profans的提示,2和3一步就可以完成啦!实验过,正常。
#生成密钥后
ssh-copy-id -i .ssh/id_rsa.pub server.aslibra.com


MAC: ssh-copy-id in Mac OS X
For some reason Macs don’t have this useful script so this will add it.
sudo curl "http://phildawson.co.uk/ssh-copy-id" -o /usr/bin/ssh-copy-id
sudo chmod +x /usr/bin/ssh-copy-id



[root@client ~]# mkdir .ssh/
[root@client ~]# cd .ssh/
[root@client .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
7d:9e:1e:f3:e6:0c:0b:d7:e6:d2:d9:84:5b:c4:f8:4f root@client.aslibra.com
[root@client .ssh]# scp id_rsa.pub server.aslibra.com:~
The authenticity of host 'server.aslibra.com (1.2.3.4)' can't be established.
RSA key fingerprint is 9c:07:78:d6:7d:f0:01:a8:87:32:15:49:a8:1b:c3:0d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server.aslibra.com,1.2.3.4' (RSA) to the list of known hosts.
root@server.aslibra.com's password:
id_rsa.pub    100%  403     0.4KB/s   00:00    
[root@client .ssh]# ssh server.aslibra.com "cat id_rsa.pub >> ~/.ssh/authorized_keys;chmod 600 ~/.ssh/authorized_keys;rm -f id_rsa.pub"
root@server.aslibra.com's password:
[root@client .ssh]# ssh server.aslibra.com
root@server:~#


总结为如下代码:

SERVER=server.aslibra.com

#创建公钥和私钥
cd /root/
mkdir .ssh
cd .ssh/
if [ ! -f id_rsa ]; then ssh-keygen -t rsa ; fi

#传输
scp id_rsa.pub $SERVER:~

#加入到authorized_keys
ssh $SERVER "mkdir .ssh;\
cat id_rsa.pub >> ~/.ssh/authorized_keys;\
chmod 600 ~/.ssh/authorized_keys;\
rm -f id_rsa.pub"


MAC: http://phildawson.tumblr.com/post/484798267/ssh-copy-id-in-mac-os-x


原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags: , , ,
profans
2011/05/07 22:10
#加入到authorized_keys这个可以用一条命令搞定ssh-copy-id $SERVERUsage: /usr/bin/ssh-copy-id [-i [identity_file]] [user@]machine
hqlulu 回复于 2011/05/08 01:33
谢谢你的提醒!这个很好,实验过正常,我已经加入文章内
分页: 1/1 第一页 1 最后页
发表评论
AD
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐